#!/bin/sh

# Install <lang> and <lang>-desktop task packages for all configured locales.
#
# schweer, 2018-12-21

set -e

. /usr/share/debian-edu-config/testsuite-lib.sh

install_pkg1() {
	if ! deb_installed "$pkg1" ; then
		apt-get install -yq "$pkg1" || true
	fi
}
install_pkg2() {
	if ! deb_installed "$pkg2" ; then
		apt-get install -yq "$pkg2" || true
	fi
}
install_pkg3() {
	if ! deb_installed "$pkg3" ; then
		apt-get install -yq "$pkg3" || true
	fi
}
install_pkg123() {
	install_pkg1
	install_pkg2
	if dpkg -l education-desktop-kde 1>/dev/null ; then
		install_pkg3
	fi
}

langsfull="$(locale -a | grep utf8 | cut -d'.' -f1)"
for lang in $langsfull ; do
	baselang="${lang%_*}"
	# <lang>, <lang>-desktop, <lang>-kde-desktop and <lang>-gnome-desktop tasks; rather weird
	# due to a lot of cases. The code might be smarter.
	# FIXME: Data and cases were taken from the /usr/share/tasksel/debian-tasks.desc file and
	# should be kept in sync.
	case "$lang" in
		en_GB) # only desktop task (kde not always).
		pkg1=task-british-desktop
		pkg2=task-british-kde-desktop
		install_pkg1
		install_pkg2
		;;
		en_ZA)
		pkg1=task-south-african-english-desktop
		install_pkg1
		;;
		pt_BR) # lang and desktop tasks
		pkg1=task-brazilian-portuguese
		pkg2=task-brazilian-portuguese-desktop
		pkg3=task-brazilian-portuguese-kde-desktop
		install_pkg123
		;;
		ca_ES)
		pkg1=task-catalan
		pkg2=task-catalan-desktop
		pkg3=task-catalan-kde-desktop
		install_pkg123
		;;
		zh_CN|zh_SG)
		pkg1=task-chinese-s
		pkg2=task-chinese-s-desktop
		pkg3=task-chinese-s-kde-desktop
		install_pkg123
		;;
		zh_TW|zh_HK)
		pkg1=task-chinese-t
		pkg2=task-chinese-t-desktop
		pkg3=task-chinese-t-kde-desktop
		install_pkg123
		;;
		pt_PT|pt_AO|pt_CV|pt_TL|pt_GW|pt_MZ|pt_ST)
		pkg1=task-portuguese
		pkg2=task-portuguese-desktop
		pkg3=task-portuguese-kde-desktop
		install_pkg123
		;;
	esac
	case "$baselang" in
		en) # only lang task (en); all other ones: only desktop task (kde not always available).
		pkg1=task-english
		install_pkg1
		;;
		sq)
		pkg1=task-albanian-desktop
		install_pkg1
		;;
		ka)
		pkg1=task-georgian-desktop
		install_pkg1
		;;
		dz)
		pkg1=
		pkg2=
		install_pkg1
		install_pkg2
		apt-get install -y task-dzongkha-desktop || true
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		apt-get -yq install task-dzongkha-kde-desktop || true
		fi
		;;
		id)
		pkg1=task-indonesian-desktop
		pkg2=task-indonesian-kde-desktop
		install_pkg1
		install_pkg2
		;;
		ne)
		pkg1=task-nepali-desktop
		pkg2=task-nepali-kde-desktop
		install_pkg1
		install_pkg2
		;;
		si)
		pkg1=task-sinhala-desktop
		pkg2=task-sinhala-kde-desktop
		install_pkg1
		install_pkg2
		;;
		kn)
		pkg1=task-kannada-desktop
		pkg2=task-kannada-kde-desktop
		install_pkg1
		install_pkg2
		;;
		vi)
		pkg1=task-vietnamese-desktop
		pkg2=task-vietnamese-kde-desktop
		install_pkg1
		install_pkg2
		;;
		ug)
		pkg1=task-uyghur-desktop
		pkg2=task-uyghur-kde-desktop
		install_pkg1
		install_pkg2
		;;
		xh)
		pkg1=task-xhosa-desktop
		pkg2=task-xhosa-kde-desktop
		install_pkg1
		install_pkg2
		;;
	esac
	# all other cases (lang and desktop tasks; kde not always, gnome sometimes available).
	duo="
	am:amharic
	ar:arabic
	ast:asturian
	eu:basque
	be:belarusian
	bn:bengali
	bs:bosnian
	bg:bulgarian
	hr:croatian
	be:cyrillic
	bg:cyrillic
	kz:cyrillic
	mk:cyrillic
	mn:cyrillic
	ru:cyrillic
	sr:cyrillic
	cs:czech
	da:danish
	nl:dutch
	eo:esperanto
	et:estonian
	fi:finnish
	fr:french
	gl:galician
	de:german
	el:greek
	gu:gujarati
	he:hebrew
	hi:hindi
	hu:hungarian
	is:icelandic
	ga:irish
	it:italian
	ja:japanese
	kk:kazakh
	km:khmer
	ko:korean
	ku:kurdish
	lv:latvian
	lt:lithuanian
	mk:macedonian
	ml:malayalam
	mr:marathi
	se:northern-sami
	no:norwegian
	nb:norwegian
	nn:norwegian
	fa:persian
	pl:polish
	pa:punjabi
	ro:romanian
	ru:russian
	sr:serbian
	sk:slovak
	sl:slovenian
	es:spanish
	sv:swedish
	tl:tagalog
	ta:tamil
	te:telugu
	th:thai
	tr:turkish
	uk:ukrainian
	cy:welsh"
	for l in $duo ; do
		lbase="$(echo "$l" | cut -d':' -f1)"
		lname="$(echo "$l" | cut -d':' -f2)"
		if [ "$baselang" = "$lbase" ] ; then
			pkg1=task-$lname
			pkg2=task-$lname-desktop
			install_pkg1
			install_pkg2
			if dpkg -l education-desktop-kde 1>/dev/null ; then
				case "$baselang" in
					ast|mr|se|ti|ta|cy) # kde desktop task not available
					;;
					*)
					pkg1=task-$lname-kde-desktop
					install_pkg1
					;;
				esac
			fi
			if dpkg -l education-desktop-gnome 1>/dev/null ; then
				case "$baselang" in
					he|ja|ko|ml|ta|te|th)
					pkg1=task-$lname-gnome-desktop
					install_pkg1
					;;
					*) # gnome desktop task not available
					;;
				esac
			fi
		fi
		done
done
# finally the stock desktop tasks
for d in cinnamon gnome kde mate lxde lxqt xfce ; do
	if dpkg -l education-desktop-$d 1>/dev/null ; then
	apt-get -yq install task-$d-desktop || true
	fi
done
