#!/bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/webmail-install-locale - install additional locale
#
# Copyright (c) 2001-2012 The Eisfair Team, team(at)eisfair(dot)org
#
# Creation:    2005-06-26 jed
# Last Update: $Id$
#
# Usage: webmail-install-locale [-debug]
#           - debug - show only content of gz-archive
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# read eislib etc.
. /var/install/include/eislib
. /var/install/include/check-eisfair-version
. /var/install/include/jedlib

#exec 2>./webmail-install-locale-trace-$$.log
#set -x

#----------------------------------------------------------------------------
# get group
# input: $1 - username
# return: 0 - ok
#         1 - not found
#----------------------------------------------------------------------------
get_group ()
{
    u_name=$1
    ret=1

    line=`grep "^$u_name:" /etc/passwd`

    if [ "$line" != "" ]
    then
        _ifs="$IFS"
        IFS=:
        set -- $line
        gid_search=$4

        not_found=1
        while read line
        do
            set -- $line
            group_tmp="$1"
            gid_tmp="$3"

            if [ $gid_search -eq $gid_tmp ]
            then
                echo $group_tmp
                not_found=0
                break
            fi
        done </etc/group

        IFS="$_ifs"

        if [ $not_found -eq 0 ]
        then
            ret=0
        else
            echo "$gid_search"
        fi
    else
        mecho -error "User '$u_name' doesn't exist!"
    fi

    return $ret
}

#----------------------------------------------------------------------------
# check if locale infrastructure exists
# input: $1 - locale name
# return: 0 - ok
#         1 - not found
#----------------------------------------------------------------------------
check_locale_infra ()
{
    l_name=$1
    ret=1

    # check if e.g. entry 'pl_PL.' exists in $locale_alias_path/locale.alias
    # and also directory $locale_file_path/pl_PL exist.

    grep -v '^#' /usr/share/locale/locale.alias | grep -q "${l_name}."

    if [ $? -eq 0 -a -d $locale_file_path/$l_name ]
    then
        ret=0
    fi

    return $ret
}

#----------------------------------------------------------------------------
# create list of all locales
#----------------------------------------------------------------------------
create_all_locale_list ()
{
    for FNAME in $webmail_path/$all_locale_mask
    do
        for CCODE in `tar -tzf "$FNAME"|grep "locale/"|cut -d/ -f2|sort|uniq`
        do
            code=`echo $CCODE|tr 'A-Z' 'a-z'`

            echo "$code:$CCODE:$FNAME"
        done
    done | sort > $tmpfile
}

#----------------------------------------------------------------------------
# create list of locales
#----------------------------------------------------------------------------
create_locale_list ()
{
    for FNAME in $webmail_path/$locale_mask
    do
        disp_code=`basename "$FNAME"|cut -c1-5`
        inst=''
        code=`echo $disp_code|tr 'A-Z' 'a-z'`

        echo "$inst:$code:$disp_code:$FNAME"
    done | sort > $tmpfile
}

#----------------------------------------------------------------------------
# print list of locales
#----------------------------------------------------------------------------
print_locale_list ()
{
    # print header
    clrhome
    mecho -info "List of found locale packages in $webmail_path"
    mecho
    techo begin 2 15 15
    techo row "" -info Code -info Infrastructure

    tty=`tty`
    row=4

    while read line
    do
        _ifs="$IFS"
        IFS=:
        set -- $line
        disp_code="$3"
        IFS="$_ifs"

        if check_locale_infra $disp_code
        then
            infra_check='ok'
        else
            infra_check='-warn missing'
        fi

        # output data
        techo row "" $disp_code $infra_check

        row=`expr $row + 1`

        if [ "$act_pmode" = 'tty' ]
        then
            if [ $row -eq 21 ]
            then
                mecho
                mecho
                anykey <$tty

                # print header
                clrhome
                mecho -info "List of found locale packages in $webmail_path" <$tty
                mecho
                techo row "" -info Code -info Infrastructure
                row=4
            fi
        fi
    done < $tmpfile

    techo end
    mecho
}

#----------------------------------------------------------------------------
# print header
# input : $1 - country code
#----------------------------------------------------------------------------
print_header ()
{
    c_code=$1

    # get document root
    eval wm_root_path=${testroot}'$WEBMAIL_'${_wminst}'_DOCUMENT_ROOT'

    clrhome
    mecho -info "Install locale"
    mecho
    mecho -n "Path:   "
    mecho -warn "$wm_root_path"
    mecho -n "Locale: "
    mecho -warn "$c_code"
    mecho
}

#----------------------------------------------------------------------------
# print infrastructure error
# input : $1 - country code
#----------------------------------------------------------------------------
print_locale_infra_error ()
{
    c_code=$1

    mecho
    mecho -n "  "
    mecho -error "Locale infrastructure error"
    mecho
    mecho "  The locale infrastructure files in directory '$locale_file_path' for the"
    mecho "  locale '$c_code' couldn't be found or the required locale entry in the"
    mecho "  file '$locale_alias_path/locale.alias is missing."
    mecho
    mecho "  Please fix these errors before you install additional webmail locales."
    mecho
}

#----------------------------------------------------------------------------
# print list of document roots
#----------------------------------------------------------------------------
print_docrootlist ()
{
    # print header
    clrhome
    mecho -info "List document root(s)"
    mecho
    techo begin 2 5 40 
    techo row "" -info "Nbr" -info "Document root"

    tty=`tty`
    row=4

    idx=1
    while [ $idx -le $WEBMAIL_N ]
    do
        eval docroot='$WEBMAIL_'${idx}_'DOCUMENT_ROOT'

        # output data
        techo row "" $idx $docroot

        row=`expr $row + 1`

        if [ "$act_pmode" = 'tty' ]
        then
            if [ $row -eq 21 ]
            then
                mecho
                mecho
                anykey <$tty

                # print header
                clrhome

                mecho -info "List document root(s)" <$tty
                mecho
                techo row "" -info "Nbr" -info "Document root"
                row=4
            fi
        fi

        idx=`expr $idx + 1`
    done

    techo end
    mecho
}

#----------------------------------------------------------------------------
# select webmail instance
#----------------------------------------------------------------------------
select_webmail_instance ()
{
    # extract document root
    grep -E "^START_WEBMAIL|^WEBMAIL_N|^WEBMAIL_._DOCUMENT_ROOT" ${webmailfile} > ${tmpfile2}

    . ${tmpfile2}

    if [ $WEBMAIL_N -gt 1 ]
    then
        # more than one document root have been defined
        print_docrootlist

        dr_exit=0
        until [ $dr_exit -eq 1 ]
        do
            /var/install/bin/ask "Please select webmail instance (1-$WEBMAIL_N), (q)uit:" '' '+' > /tmp/ask.$$
            rc=$?
            instance=`cat /tmp/ask.$$`
            rm -f /tmp/ask.$$
            if [ $rc = 255 ]
            then
                rm $tmpfile
                exit 1
            fi

            case $instance in
                q ) # quit
                    dr_exit=1
                    ;;

                * ) # doc root
                    if is_numeric $instance && [ "$instance" -ge 1 -a "$instance" -le $WEBMAIL_N ]
                    then
                        _wminst=$instance
                        dr_exit=1
                    fi
                    ;;
            esac
        done
    else
        # less or equal one document root has been defined
        _wminst=$WEBMAIL_N
    fi
}

#----------------------------------------------------------------------------
# install locale
#----------------------------------------------------------------------------
install_locale ()
{
    # get document root
    eval wm_root_path=${testroot}'$WEBMAIL_'${_wminst}'_DOCUMENT_ROOT'

    clrhome
    mecho -info "Install locale"
    mecho
    mecho -n "Path:   "
    mecho -warn "$wm_root_path"
    mecho

    ccode=''
    u_exit=0
    until [ $u_exit -eq 1 ]
    do
        if [ "$ccode" = "" -o "$ccode" = "l" ]
        then
            # read uname
            /var/install/bin/ask "Please enter locale (code) to install, (l)ist or (q)uit:" '' '+' > /tmp/ask.$$
            rc=$?
            ccode=`cat /tmp/ask.$$|tr 'A-Z' 'a-z'`
            rm -f /tmp/ask.$$
            if [ $rc = 255 ]
            then
                rm $tmpfile
                exit 1
            fi
        fi

        case $ccode in
            l ) # list locales
                print_locale_list
                ;;

            q ) # quit
                u_exit=1
                ;;

            * ) # process locale
                cut -d: -f2 $tmpfile | grep -q "$ccode"

                if [ $? -eq 0 ]
                then
                    display_ccode=`grep ":$ccode:" $tmpfile|cut -d: -f3`
                    print_header $display_ccode

                    if [ "$debug" != "-debug" ]
                    then
                        if check_locale_infra $display_ccode
                        then
                            /var/install/bin/ask "Install locale" 'n' > /tmp/ask.$$
                        else
                            print_locale_infra_error $display_ccode
                            echo 'no' > /tmp/ask.$$
                            anykey
                        fi
                    else
                        /var/install/bin/ask "Debug mode - list content of locale archive" 'n' > /tmp/ask.$$
                    fi
                    rc=$?
                    yesno=`cat /tmp/ask.$$`
                    rm -f /tmp/ask.$$
                    if [ $rc = 255 ]
                    then
                        rm $tmpfile
                        exit 1
                    fi

                    if [ "$yesno" = "yes" ]
                    then
                        locale_file=`grep ":$ccode:" $tmpfile | cut -d: -f4`

                        if [ "$debug" != "-debug" ]
                        then
                            if [ $all_locale -eq 0 ]
                            then
                                # create deinstall file
                                tar -tzf "$locale_file" | sort -r > $webmail_path/${display_ccode}-filelist-${_wminst}.txt

                                # extract files
                                tar -C "$wm_root_path/" -T $webmail_path/${display_ccode}-filelist-${_wminst}.txt -xzf "$locale_file"
                            else
                                # all_locale - create deinstall file
                                tar -tzf "$locale_file"|grep -E "${display_ccode}|^install|^version|.locales$"|sort -r > $webmail_path/${display_ccode}-filelist-${_wminst}.txt

                                # extract files
                                tar -C "$wm_root_path/" -T $webmail_path/${display_ccode}-filelist-${_wminst}.txt -xzf "$locale_file"
                            fi

                            # change file ownership
                            for FNAME in `cat $webmail_path/${display_ccode}-filelist-${_wminst}.txt` help images locale
                            do
                                chown -R $webmail_user  "$wm_root_path/$FNAME"
                                chgrp -R $webmail_group "$wm_root_path/$FNAME"

                                chmod 444 "$wm_root_path/$FNAME"
                            done

                            # change directory ownership
                            for FNAME in help images locale
                            do
                                find $wm_root_path/$FNAME -type d -exec chmod 755 {} \;
                            done
                        else
                            mecho
                            if [ $all_locale -eq 0 ]
                            then
                                # list files
                                tar -tzf "$locale_file"
                            else
                                # all_locale - list files
                                tar -tzf "$locale_file"|grep -E "${display_ccode}|^install|^version|.locales$"
                            fi
                            mecho
                        fi

                        mecho "The '$display_ccode' locale has successfully been installed!"
                    fi

                    u_exit=1
                else
                    if [ "$display_ccode" = "" ]
                    then
                        display_ccode="$ccode"
                    fi

                    mecho -warn "Locale '$display_ccode' doesn't exist!"
                    ccode=''
                    anykey
                fi
                ;;
        esac
    done
}

#============================================================================
# main
#============================================================================

#testroot=/tmp/wm                                      # only for testing
 testroot=""

act_pmode=`get_printmode`

base=`basename $0`

# set platform specific parameters
case ${EISFAIR_SYSTEM} in
    eisfair-1)
        # eisfair-1
        webmail_path=${testroot}/var/webmail
        ;;
    *)
        # default to eisfair-2/eisxen-1
        webmail_path=${testroot}/data/packages/webmail
        ;;
esac

### set path names ###
locale_file_path=$testroot/usr/lib/locale
locale_alias_path=$testroot/usr/share/locale

### set file names ####
webmailfile=/etc/config.d/webmail
tmpfile="/tmp/$base-$$"
tmpfile2=/tmp/$base-$$
all_locale_mask='all_locales-*.tar.gz'
locale_mask='??_??-1.4.1[0-9]-*.tar.gz'

webmail_user='wwwrun'
webmail_group=`get_group $webmail_user`

if [ "$1" = "-debug" ]
then
    debug='-debug'
    shift
fi

all_locale=0

# look for all_locales file
ls $webmail_path/all_locales-*.tar.gz > /dev/null 2> /dev/null

if [ $? -eq 0 ]
then
    # all_locales file found
    create_all_locale_list
    all_locale=1
    install_locale
else
    # look for separate locale files
    # da_DK-1.4.5rc1-20050616.tar.gz
    ls $webmail_path/$locale_mask > /dev/null 2> /dev/null

    if [ $? -eq 0 ]
    then
        select_webmail_instance

        if [ "$START_WEBMAIL" = "yes" -a ${_wminst} -ne 0 ]
        then
            # one or more webmail instance have been defined
            create_locale_list
            install_locale
        else
            # no webmail instance defined
            mecho -warn "The webmail package has not been activated or no webmail"
            mecho -warn "instance has been defined in the configuration file."
            mecho
            mecho -info "Please set START_WEBMAIL='yes' and WEBMAIL_N to a value greater than zero."
        fi
    else
        # no files found
        mecho -warn "No locale file has been found in $webmail_path!"
        mecho
        mecho -info "Follow these steps to install additional locales:"
        mecho
        mecho -info "1. Download new locale file in gz-format and save it"
        mecho -info "   in the directory ${webmail_path}."
        mecho
        mecho -info "   Example: download file 'da_DK-1.4.6rc1-20050616.tar.gz'"
        mecho -info "            or 'all_locales-1.4.6rc1-20050616.tar.gz' from"
        mecho -info "            http://www.squirrelmail.org/download.php"
        mecho
        mecho -info "2. Rerun the function 'Install additional locale(s)' from"
        mecho -info "   the menu and install the desired locale."
    fi
fi

rm -f ${tmpfile}
rm -f ${tmpfile2}

anykey

#============================================================================
# end
#============================================================================