#!/bin/sh
#----------------------------------------------------------------------------
# /var/install/config.d/environment-update.sh - update or generate new environment configuration
#
# Creation:     2007-10-16  hbfl
# Last Update:  $Id: environment-update.sh 33483 2013-04-15 21:42:37Z hbfl $
#
# Copyright (c) 2001-2013 the eisfair team, team(at)eisfair(dot)org
#
# 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.
#----------------------------------------------------------------------------

# set package name
package_name='environment'

# include configib
. /var/install/include/configlib

# ---------------------------------------------------------------------------
# set the default values for configuration
# ---------------------------------------------------------------------------
EDITOR='joe'
PAGER='less'

MAX_BACKUP_CONFIG='10'
SCROLL='no'

HTTP_PROXY=''
HTTP_PROXY_USER=''
HTTP_PROXY_PASSWD=''

LC_CTYPE='de_DE.UTF-8'

# ---------------------------------------------------------------------------
# read old configuration
# ---------------------------------------------------------------------------
rename_old_variables()
{
    # read old values
    if [ -f /etc/config.d/${package_name} ]
    then
        /var/install/bin/backup-file --quiet ${package_name}
        . /etc/config.d/${package_name}
    fi
}
# ---------------------------------------------------------------------------
# write default files
# ---------------------------------------------------------------------------
make_default_file()
{
    internal_conf_file=${1}

    {
    # -----------------------------------------------------------------------
    printgpl --conf ${package_name} "2003-07-20" "fm"
    # -----------------------------------------------------------------------

    # -----------------------------------------------------------------------
    printgroup "Setup menu configuration"
    # -----------------------------------------------------------------------

    printvar "EDITOR"            "joe (alternative: vi)"
    printvar "PAGER"             "less (alternative: more)"
    echo
    printvar "MAX_BACKUP_CONFIG" "maximum number of package config files"
    printvar ""                  "to backup up in /etc/backup.d"
    printvar "SCROLL"            "no:  no-scroll - show pages if lists are too long"
    printvar ""                  "yes: always show entire lists - you need to scroll manually"

    echo
    printvar "HTTP_PROXY"        "http proxy for package installer"
    printvar ""                  "(empty to disable)"
    printvar ""                  "server:port"
    printvar "HTTP_PROXY_USER"   "http proxy username (emtpy to disable auth)"
    printvar "HTTP_PROXY_PASSWD" "http proxy password"

    # -----------------------------------------------------------------------
    printgroup "Localisation"
    # -----------------------------------------------------------------------

    printvar "LC_CTYPE"          "locale setting (show help for correct setting)"

    # -----------------------------------------------------------------------
    printend
    # -----------------------------------------------------------------------

    } > ${internal_conf_file}
    # Set rights
    chmod 0600 ${internal_conf_file}
    chown root ${internal_conf_file}

}
# ---------------------------------------------------------------------------
# write config files
# ---------------------------------------------------------------------------
make_config_file()
{
    internal_conf_file=${1}

    if ! grep -q ^SCROLL ${internal_conf_file}
    then

        config_entry=`grep "^HTTP_PROXY=" ${internal_conf_file}`

sed -e "/^${config_entry}/i\\
\  \\
SCROLL='no'                            # no:  no-scroll - show pages if lists \\
\                                       #      are too long \\
\                                       # yes: always show entire lists - you \\
\                                       #      need to scroll manually \\
\  " ${internal_conf_file} >${internal_conf_file}$$

        mv ${internal_conf_file}$$ ${internal_conf_file}

        # Set rights
        chmod 0600 ${internal_conf_file}
        chown root ${internal_conf_file}

    fi
}
# ---------------------------------------------------------------------------
# create check.d file
# ---------------------------------------------------------------------------
make_check_file()
{
printgpl --check ${package_name} "2005-03-04" "max" >/etc/check.d/${package_name}
cat >>/etc/check.d/${package_name} <<EOF
# Variable             OPT_VARIABLE      VARIABLE_N     VALUE
EDITOR                 -                 -              NOBLANK
PAGER                  -                 -              NOBLANK

MAX_BACKUP_CONFIG      -                 -              NUMERIC
SCROLL                 -                 -              YESNO

HTTP_PROXY             -                 -              ENVIRONMENT_PROXY
HTTP_PROXY_USER        -                 -              ENOBLANK
HTTP_PROXY_PASSWD      -                 -              ENOBLANK

LC_CTYPE               -                 -              ENVIRONMENT_LC_CTYPE_CUI
EOF

     # Set rights for check file
    chmod 0600 /etc/check.d/${package_name}
    chown root /etc/check.d/${package_name}


#printgpl --check_exp ${package_name} "2006-01-11" "max" #>/etc/check.d/${package_name}.exp
#cat >>/etc/check.d/${package_name}.exp <<EOF
#ENVIRONMENT_PROXY  = '()|((http://)?((RE:FQDN)|(RE:IPADDR)):(RE:PORT))(/)?'
#                   : 'Invalid proxy entry, use server:port'
#ENVIRONMENT_LC_CTYPE_CUI = '  '
#                         : '  '
#EOF
#
#     # Set rights for check file
#    chmod 0600 /etc/check.d/${package_name}.exp
#    chown root /etc/check.d/${package_name}.exp
}
# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
# write default config file
make_default_file /etc/default.d/${package_name}

# update from old version
rename_old_variables

# write new config file
make_config_file /etc/config.d/${package_name}

# write check.d file
make_check_file

exit 0
# ---------------------------------------------------------------------------
# end
# ---------------------------------------------------------------------------
