#!/bin/sh
# 2006 - SabayonLinux - Fabio Erculiani
# Gentoo System Language configurator

# ERROR CODES:
# 1 - not enough parameters
# 2 - root access required
# 3 - locale not available
# 4 - too many parameters
# 5 - $1 is not system nor kde nor mozilla nor openoffice

# We edit /etc/env.d/02locale and even /etc/profile.env we don't need to do: env-update

# parameters test
if [ "$#" -lt "1" ] || [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
    if [ "$#" -lt "1" ]; then
        echo "ERROR: not enough parameters"
    fi
    echo "language-setup <valid locale> <system,kde,mozilla,openoffice>"
    exit 1
fi

if [ "$#" -gt "2" ]; then
    echo "ERROR: too many parameters"
    echo "language-setup <valid locale> <system,kde,mozilla,openoffice>"
    exit 4
elif [ -n "$2" ] && [ "$2" != "system" ] && [ "$2" != "kde" ] && [ "$2" != "mozilla" ] && [ "$2" != "openoffice" ]; then
    echo "ERROR: wrong parameters"
    echo "language-setup <valid locale> <system,kde,mozilla,openoffice>"
    exit 5
fi

# root test
if [ "$UID" != "0" ]; then
    echo "ERROR: you need to run this as root"
    exit 2
fi

# Variables and constants
syslocale=$1

if [ -n "$2" ]; then
    desktop_env=$2
fi

# locale test
locale_exists=$(/usr/bin/locale -a | grep $syslocale)
if [ -z "$locale_exists" ]; then
    echo "ERROR: locale not available, please check your glibc installation"
    exit 3
fi

utf_syslocale=$syslocale
utf_syslocale_exists=$(/usr/bin/locale -a | grep $syslocale".utf8")
if [ -n "$utf_syslocale_exists" ]; then
    utf_syslocale=$syslocale".utf8"
fi

# functions

configure_system_locale() {

    if [ -e "$ROOT/etc/env.d/02locale" ]; then
        sed -i '/LANG=/ s/LANG=.*/LANG="'$utf_syslocale'"/' $ROOT/etc/env.d/02locale
        sed -i '/LANGUAGE=/ s/LANGUAGE=.*/LANGUAGE="'$utf_syslocale'"/' $ROOT/etc/env.d/02locale
        sed -i '/LC_ALL=/ s/LC_ALL=.*/LC_ALL="'$utf_syslocale'"/' $ROOT/etc/env.d/02locale
        sed -i "s/LANG=.*/LANG='$utf_syslocale'/" $ROOT/etc/profile.env
        sed -i "s/LANGUAGE=.*/LANGUAGE='$utf_syslocale'/" $ROOT/etc/profile.env
        sed -i "s/LC_ALL=.*/LC_ALL='$utf_syslocale'/" $ROOT/etc/profile.env
    else
        echo 'LANG="'$utf_syslocale'"' >> $ROOT/etc/env.d/02locale
        echo 'LANGUAGE="'$utf_syslocale'"' >> $ROOT/etc/env.d/02locale
        echo 'LC_ALL="'$utf_syslocale'"' >> $ROOT/etc/env.d/02locale
        echo "LANG='$utf_syslocale'" >> $ROOT/etc/profile.env
        echo "LANGUAGE='$utf_syslocale'" >> $ROOT/etc/profile.env
        echo "LC_ALL='$utf_syslocale'" >> $ROOT/etc/profile.env
    fi

}

configure_kde_locale() {

    # Configure KDM
    if [ -e "$ROOT/usr/kde/3.5/share/config/kdm/kdmrc" ]; then
        sed -i '/Language=/ s/Language=.*/Language='$syslocale'/' $ROOT/usr/kde/3.5/share/config/kdm/kdmrc
    fi

    # KDE 3.x.x
    # Search for all kdeglobals in /home
    kdeglobals_files=$(find $ROOT/home -name kdeglobals)
    kdelocale=$(echo $syslocale | cut -d"_" -f 1 | cut -d"-" -f 1)
    if [ -n "$kdeglobals_files" ]; then
        for file in $kdeglobals_files
        do
	    sed -i '/Language=/ s/Language=.*/Language='$kdelocale'/' $file
        done
    fi
    # Search for all kdeglobals in /root
    kdeglobals_files=$(find $ROOT/root -name kdeglobals)
    kdelocale=$(echo $syslocale | cut -d"_" -f 1 | cut -d"-" -f 1)
    if [ -n "$kdeglobals_files" ]; then
        for file in $kdeglobals_files
        do
	    sed -i '/Language=/ s/Language=.*/Language='$kdelocale'/' $file
        done
    fi
    # Search for all kdeglobals in /etc/skel
    kdeglobals_files=$(find $ROOT/etc/skel -name kdeglobals)
    if [ -n "$kdeglobals_files" ]; then
        for file in $kdeglobals_files
        do
	    sed -i '/Language=/ s/Language=.*/Language='$kdelocale'/' $file
        done
    fi

}

configure_mozilla_locale() {

    moz_syslocale=$(echo $syslocale | sed 's/_/-/' | cut -d"-" -f 1)

    # Mozilla 1.7.x, Seamonkey 1.x, Firefox 1.x and 2.x supported - users
    moz_lang_files=$(if [ -d "$ROOT/home" ]; then grep -r 'pref("general.useragent.locale' $ROOT/home/*/.mozilla | cut -d: -f1; fi)" "$(if [ -d "$ROOT/root/.mozilla" ]; then grep -r 'pref("general.useragent.locale' $ROOT/root/.mozilla | cut -d: -f1; fi)" "$(if [ -d "$ROOT/etc/skel/.mozilla" ]; then grep -r 'pref("general.useragent.locale' $ROOT/etc/skel/.mozilla/* | cut -d: -f1; fi)" "$(if [ -d "$ROOT/usr/lib/seamonkey/defaults" ]; then grep -r 'pref("general.useragent.locale' $ROOT/usr/lib/seamonkey/defaults/* | cut -d: -f1; fi)" "$(if [ -d "$ROOT/usr/lib/mozilla-thunderbird/defaults" ]; then grep -r 'pref("general.useragent.locale' $ROOT/usr/lib/mozilla-thunderbird/defaults/* | cut -d: -f1; fi)" "$(if [ -d "$ROOT/usr/lib/mozilla-firefox/defaults" ]; then grep -r 'pref("general.useragent.locale' $ROOT/usr/lib/mozilla-firefox/defaults/* | cut -d: -f1; fi)" "$(if [ -d "$ROOT/usr/lib/mozilla/defaults" ]; then grep -r 'pref("general.useragent.locale' $ROOT/usr/lib/mozilla/defaults/* | cut -d: -f1; fi)" "$(if [ -d "$ROOT/opt/mozilla/defaults" ]; then grep -r 'pref("general.useragent.locale' $ROOT/opt/mozilla/defaults/* | cut -d: -f1; fi)" "$(if [ -d "$ROOT/opt/thunderbird/defaults" ]; then grep -r 'pref("general.useragent.locale' $ROOT/opt/thunderbird/defaults/* | cut -d: -f1; fi)" "$(if [ -d "$ROOT/opt/firefox/defaults" ]; then grep -r 'pref("general.useragent.locale' $ROOT/opt/firefox/defaults/* | cut -d: -f1; fi)
    if [ -n "$moz_lang_files" ]; then
        for pref in $moz_lang_files
        do
	    sed -i '/general.useragent.locale/ s/"general.useragent.locale", ".*"/"general.useragent.locale", "'$moz_syslocale'"/' $pref
        done
    fi

}

openoffice_locale_handler() {

    # $1 directory
    ooo_locale_files=$(find $ROOT$1 -name Setup.xcu)
    if [ -n "$ooo_locale_files" ]; then
        for ooo_lang in $ooo_locale_files
        do

	    ooo_lang_start="0"

	    for line in `cat $ooo_lang`
            do
                #<prop oor:name="ooLocale" oor:type="xs:string">
                # <value>it</value>
                #</prop>
                # $locale_ooo --> locale to set
                start_ooLocale_exists=$(echo $line | grep 'oor:name="ooLocale"')
                if [ -n "$start_ooLocale_exists" ] && [ "$ooo_lang_start" == "0" ]; then
                    ooo_lang_start="1"
                elif [ -z "$start_ooLocale_exists" ] && [ "$ooo_lang_start" == "1" ]; then
                    sed -i 's/<value>.*<\/value>/<value>'$locale_ooo'<\/value>/' $ooo_lang
                    ooo_lang_start="0"
                fi
            done

        done
    fi
    ooo_locale_files=$(find $ROOT$1 -name Linguistic.xcu)
    if [ -n "$ooo_locale_files" ]; then
        for ooo_lang in $ooo_locale_files
        do
            sed -i 's/<value>.*<\/value>/<value>'$locale_ooo_extended'<\/value>/g' $ooo_lang
        done
    fi

}

configure_openoffice_locale() {

    # Set OpenOffice 2.0 language
    # actually, we do not check which OOo langpacks exist on the system
    locale_ooo=$(echo $syslocale | sed 's/_/-/' | cut -d'-' -f 1 )
    locale_ooo_extended=$(echo $syslocale | sed 's/_/-/')

    # /home directory
    openoffice_locale_handler /home
    # /root directory
    openoffice_locale_handler /root
    # /etc/skel directory
    openoffice_locale_handler /etc/skel

}

# code

if [ -z "$desktop_env" ]; then

    # Configure system locale (/etc/env.d/02locale)
    echo -en "Setting up GNOME and system locale..."
    configure_system_locale
    echo -e "\t OK"

    # Configure KDE
    echo -en "Setting up KDE locale..."
    configure_kde_locale
    echo -e "\t\t OK"

    # Configure Firefox
    echo -en "Setting up Mozilla products locale..."
    configure_mozilla_locale
    echo -e "\t OK"

    # Configure OpenOffice
    echo -en "Setting up OpenOffice locale..."
    configure_openoffice_locale
    echo -e "\t\t OK"

else

    case $desktop_env in
        system)
            # Configure system locale (/etc/env.d/02locale)
            echo -en "Setting up GNOME and system locale..."
            configure_system_locale
            echo -e "\t OK"
        ;;
        kde)
            # Configure KDE
            echo -en "Setting up KDE locale..."
            configure_kde_locale
            echo -e "\t\t OK"
        ;;
        mozilla)
            # Configure Firefox
            echo -en "Setting up Mozilla products locale..."
            configure_mozilla_locale
            echo -e "\t OK"
        ;;
       openoffice)
            # Configure OpenOffice
            echo -en "Setting up OpenOffice locale..."
            configure_openoffice_locale
            echo -e "\t\t OK"
        ;;
    esac

fi

# Finalize the config
if [ -z "$ROOT" ]; then
    source /etc/profile
else
    echo source /etc/profile | chroot $ROOT
fi



