#!/bin/sh

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

set -e
langsfull="$(locale -a | grep utf8 | cut -d'.' -f1)"
for lang in $(echo $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).
		apt-get install -y task-british-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-british-kde-desktop
		fi
		;;
	    en_ZA)
		apt-get install -y task-south-african-english-desktop
		;;
	    pt_BR) # lang and desktop tasks
		apt-get install -y task-brazilian-portuguese task-brazilian-portuguese-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-brazilian-portuguese-kde-desktop
		fi
		;;
	    ca_ES)
		apt-get install -y task-catalan task-catalan-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-catalan-kde-desktop
		fi
		;;
	    zh_CN|zh_SG)
		apt-get install -y task-chinese-s task-chinese-s-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-chinese-s-kde-desktop
		fi
		;;
	    zh_TW|zh_HK)
		apt-get install -y task-chinese-t task-chinese-t-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-chinese-t-kde-desktop
		fi
		;;
	    pt_PT|pt_AO|pt_CV|pt_TL|pt_GW|pt_MZ|pt_ST)
		apt-get install -y task-portuguese task-portuguese-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-portuguese-kde-desktop
		fi
		;;
    esac
    case "$baselang" in
	    en) # only lang task (en); all other ones: only desktop task (kde not always available).
		apt-get install -y task-english
		;;
	    sq)
		apt-get install -y task-albanian-desktop
		;;
	    ka)
		apt-get install -y task-georgian-desktop
		;;
	    dz)
		apt-get install -y task-dzongkha-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-dzongkha-kde-desktop
		fi
		;;
	    id)
		apt-get install -y task-indonesian-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-indonesian-kde-desktop
		fi
		;;
	    ne)
		apt-get install -y task-nepali-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-nepali-kde-desktop
		fi
		;;
	    si)
		apt-get install -y task-sinhala-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-sinhala-kde-desktop
		fi
		;;
	    kn)
		apt-get install -y task-kannada-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-kannada-kde-desktop
		fi
		;;
	    vi)
		apt-get install -y task-vietnamese-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-vietnamese-kde-desktop
		fi
		;;
	    ug)
		apt-get install -y task-uyghur-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-uyghur-kde-desktop
		fi
		;;
	    xh)
		apt-get install -y task-xhosa-desktop
		if dpkg -l education-desktop-kde 1>/dev/null ; then
		    apt-get -yq install task-xhosa-kde-desktop
		fi
		;;
    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 $(echo $duo) ; do
        lbase="$(echo $l | cut -d':' -f1)"
        lname="$(echo $l | cut -d':' -f2)"
        if [ "$baselang" = "$lbase" ] ; then
            apt-get install -y task-$lname task-$lname-desktop
            if dpkg -l education-desktop-kde 1>/dev/null ; then
                case "$baselang" in
                    ast|mr|se|ti|ta|cy) # kde desktop task not available
                    ;;
                    *)
                        apt-get -yq install task-$lname-kde-desktop
                    ;;
                esac
            fi
            if dpkg -l education-desktop-gnome 1>/dev/null ; then
                case "$baselang" in
                    he|ja|ko|ml|ta|te|th)
                        apt-get -yq install task-$lname-gnome-desktop
                    ;;
                    *) # 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
	fi
done
