#! /bin/sh
# Copyright (c) 2000-2002 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Burchard Steinbild, 1996-97
#         Bernhard Kaindl <bk@suse.de>, 1999
#         Rüdiger Oertel <ro@suse.de>, 2000
#
# This module generates profile, csh.cshrc and csh.login
# in /etc/SuSEconfig.

# check if we are started as root
# only one of UID and USER must be set correctly
if test "$UID" != 0 -a "$USER" != root; then
    echo "You must be root to start $0."
    exit 1
fi

r=$ROOT

test -f $r/lib/YaST/SuSEconfig.functions || {
    echo "ERROR - can not find $r/lib/YaST/SuSEconfig.functions!!"
    echo "This should not happen.  Exit..."
    exit 1
}
. $r/lib/YaST/SuSEconfig.functions
# this variable might be set in the environment but cleared
# in the config recently
unset CUPS_SERVER
#
for i in $r/etc/sysconfig/language $r/etc/sysconfig/windowmanager $r/etc/sysconfig/suseconfig \
	 $r/etc/sysconfig/mail $r/etc/sysconfig/proxy ; do
  test -f $i || {
    echo "No $i found."
    exit 1
  }
  . $i
done

if test -n "$ENABLE_SUSECONFIG" -a "$ENABLE_SUSECONFIG" = "no" ; then
    echo "SuSEconfig is disabled in $r/etc/sysconfig/suseconfig."
    echo "Exit..."
    exit 0
fi

#
# Now setup /etc/SuSEconfig stuff
#
mkdir -p $r/etc/SuSEconfig
echo_warning /etc/SuSEconfig/profile \
        "# Change variables below /etc/sysconfig instead." \
        > $r/etc/SuSEconfig/profile.SuSEconfig
echo_warning /etc/SuSEconfig/csh.login \
        "# Change variables below /etc/sysconfig instead." \
        > $r/etc/SuSEconfig/csh.login.SuSEconfig
echo_warning /etc/SuSEconfig/csh.cshrc \
        "# Change variables below /etc/sysconfig instead." \
        > $r/etc/SuSEconfig/csh.cshrc.SuSEconfig

if test "$CWD_IN_ROOT_PATH" = "yes" ; then
    echo "test \"\$UID\" -lt 100 && PATH=\"\$PATH:.\"" \
        >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "if ( \"\$uid\" < \"100\" ) set path=( \$path . )" \
        >> $r/etc/SuSEconfig/csh.login.SuSEconfig
fi
if test "$CWD_IN_USER_PATH" = "yes" ; then
    echo "test \"\$UID\" -ge 100 && PATH=\"\$PATH:.\"" \
        >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "if ( \"\$uid\" >= \"100\" ) set path=( \$path . )" \
        >> $r/etc/SuSEconfig/csh.login.SuSEconfig
fi

# start of i18n support
echo '# start of i18n support'       >> $r/etc/SuSEconfig/profile.SuSEconfig
echo '# start of i18n support'       >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig

echo 'if test -z "$SSH_SENDS_LOCALE" ; then' >> $r/etc/SuSEconfig/profile.SuSEconfig
echo 'if (! ${?SSH_SENDS_LOCALE} ) then'    >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig

locale_vars="\
LANG         \
LC_ALL       \
LC_MESSAGES  \
LC_CTYPE     \
LC_COLLATE   \
LC_TIME      \
LC_NUMERIC   \
LC_PAPER     \
LC_MONETARY"

if test "$ROOT_USES_LANG" = yes ; then
    message="\
#
# /etc/sysconfig/language: ROOT_USES_LANG = yes:
#
# All users get all the locale variables from /etc/sysconfig/language:
#"

    # sh und ksh:
    echo "$message"                  >> $r/etc/SuSEconfig/profile.SuSEconfig
    for var in $locale_vars
    do
        if eval test "\$RC_$var"; then
            eval echo "$var=\\\"\$RC_$var\\\""
            echo "export $var"
        else
            echo "unset $var"
        fi
    done                             >> $r/etc/SuSEconfig/profile.SuSEconfig

    # csh:
    echo "$message"                  >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    for var in $locale_vars
    do
        if eval test "\$RC_$var"; then
            eval echo "setenv $var \\\"\$RC_$var\\\""
        else
            echo "unsetenv $var"
        fi
    done                             >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig

else
    if test "$ROOT_USES_LANG" = ctype ; then
    root_message="\
    #
    # /etc/sysconfig/language: ROOT_USES_LANG = ctype:
    #
    # root uses only the LC_CTYPE. We unset all effective locale variables
    # but LC_CTYPE and set LANG to POSIX.
    # This is necessary for non iso-8859-1 roots to be able to type all
    # the characters in X Windows.
    #"
    else
    root_message="\
    #
    # /etc/sysconfig/language: ROOT_USES_LANG != yes|ctype:
    #
    # root doesn't use locales. We unset all effective locale variables
    # and set LANG to POSIX.
    #"
    fi

    user_message="\
    #
    # All other users get all the locale variables from /etc/sysconfig/language:
    #"

    # sh und ksh:
    echo 'if test "$UID" = 0 ; then' >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "$root_message"             >> $r/etc/SuSEconfig/profile.SuSEconfig
    for var in $locale_vars
    do
	if test "$ROOT_USES_LANG" = ctype -a "$var" = LC_CTYPE ; then
            if eval test "\$RC_$var"; then
               eval echo "'    '$var=\\\"\$RC_LC_CTYPE\\\""
            else
               eval echo "'    '$var=\\\"\$RC_LANG\\\""
            fi
            echo "    export LC_CTYPE"
        else
            echo "    unset $var"
        fi
    done                             >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo 'else'                      >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "$user_message"             >> $r/etc/SuSEconfig/profile.SuSEconfig
    for var in $locale_vars
    do
        if eval test "\$RC_$var"; then
            eval echo "'    '$var=\\\"\$RC_$var\\\""
            echo "    export $var"
        else
            echo "    unset $var"
        fi
    done                             >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo 'fi'                        >> $r/etc/SuSEconfig/profile.SuSEconfig

    # csh:
    echo 'if ( "$uid" == "0" ) then' >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    echo "$root_message"             >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    for var in $locale_vars
    do
	if test "$ROOT_USES_LANG" = ctype -a "$var" = LC_CTYPE ; then
            if eval test "\$RC_$var"; then
               eval echo "'    'setenv $var \\\"\$RC_CTYPE\\\""
            else
               eval echo "'    'setenv $var \\\"\$RC_LANG\\\""
            fi
        else
            echo "    unsetenv $var"
        fi
    done                             >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    echo 'else'                      >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    echo "$user_message"             >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    for var in $locale_vars
    do
        if eval test "\$RC_$var"; then
            eval echo "'    'setenv $var \\\"\$RC_$var\\\""
        else
            echo "    unsetenv $var"
        fi
    done                             >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    echo 'endif'                     >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
fi

echo '# end of i18n support'         >> $r/etc/SuSEconfig/profile.SuSEconfig
echo '# end of i18n support'         >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig

echo 'fi'                            >> $r/etc/SuSEconfig/profile.SuSEconfig
echo 'endif'                         >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
# end of i18n support



if test -n "$FROM_HEADER" ; then
    echo "FROM_HEADER=\"$FROM_HEADER\"" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export FROM_HEADER" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv FROM_HEADER \"$FROM_HEADER\"" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
fi
if test -n "$SCANNER_TYPE" ; then
    echo "SCANNER_TYPE=$SCANNER_TYPE" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export SCANNER_TYPE" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv SCANNER_TYPE $SCANNER_TYPE" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
fi
if test -n "$DOC_HOST" ; then
    echo "SUSE_DOC_HOST=$DOC_HOST" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export SUSE_DOC_HOST" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv SUSE_DOC_HOST $DOC_HOST" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
fi
if test -d $r/usr/lib/qt3 ; then
    echo "QTDIR=/usr/lib/qt3" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export QTDIR" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv QTDIR /usr/lib/qt3" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
else
  if test -d $r/usr/lib/qt2 ; then
    echo "QTDIR=/usr/lib/qt2" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export QTDIR" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv QTDIR /usr/lib/qt2" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
  fi
fi
if test -d $r/usr/X11R6/lib/xad ; then
    echo "XADIDIR=/usr/X11R6/lib/xad" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export XADIDIR" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv XADIDIR /usr/X11R6/lib/xad" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
fi
if test -d $r/usr/lib/dvgt_help ; then
    echo "DV_IMMED_HELP=/usr/lib/dvgt_help" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export DV_IMMED_HELP" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv DV_IMMED_HELP /usr/lib/dvgt_help" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
fi
if test -d $r/usr/lib/rasmol ; then
    echo "RASMOLPATH=/usr/lib/rasmol" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export RASMOLPATH" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv RASMOLPATH /usr/lib/rasmol" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
fi
if test -d $r/usr/lib/cint ; then
    echo "CINTSYSDIR=/usr/lib/cint" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export CINTSYSDIR" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv CINTSYSDIR /usr/lib/cint" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
fi
if test -d $r/usr/lib/maxwell ; then
    echo "MAXHOME=/usr/lib/maxwell" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "export MAXHOME" >> $r/etc/SuSEconfig/profile.SuSEconfig
    echo "setenv MAXHOME /usr/lib/maxwell" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
fi
if test "$PROXY_ENABLED" != "no"; then
    if test -n "$HTTP_PROXY" ; then
	echo "http_proxy=$HTTP_PROXY" >> $r/etc/SuSEconfig/profile.SuSEconfig
	echo "export http_proxy" >> $r/etc/SuSEconfig/profile.SuSEconfig
	echo "setenv http_proxy $HTTP_PROXY" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    fi
    if test -n "$FTP_PROXY" ; then
	echo "ftp_proxy=$FTP_PROXY" >> $r/etc/SuSEconfig/profile.SuSEconfig
	echo "export ftp_proxy" >> $r/etc/SuSEconfig/profile.SuSEconfig
	echo "setenv ftp_proxy $FTP_PROXY" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    fi
    if test -n "$GOPHER_PROXY" ; then
	echo "gopher_proxy=$GOPHER_PROXY" >> $r/etc/SuSEconfig/profile.SuSEconfig
	echo "export gopher_proxy" >> $r/etc/SuSEconfig/profile.SuSEconfig
	echo "setenv gopher_proxy $GOPHER_PROXY" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    fi
    if test -n "$NO_PROXY" ; then
	echo "no_proxy='$NO_PROXY'" >> $r/etc/SuSEconfig/profile.SuSEconfig
	echo "export no_proxy" >> $r/etc/SuSEconfig/profile.SuSEconfig
	echo "setenv no_proxy '$NO_PROXY'" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig
    fi
fi

SAVEPATH=$PATH
PATH=$PATH:$r/usr/X11R6/bin:$r/opt/gnome/bin:$r/usr/openwin/bin
DEFAULT_WM=`basename $DEFAULT_WM`
WINDOWMANAGER=`type -p $DEFAULT_WM`
PATH=$SAVEPATH
unset SAVEPATH
test -z "$WINDOWMANAGER" && WINDOWMANAGER=/usr/X11R6/bin/kde
echo "test -z \"\$WINDOWMANAGER\" && WINDOWMANAGER=$WINDOWMANAGER" >> $r/etc/SuSEconfig/profile.SuSEconfig
echo "export WINDOWMANAGER" >> $r/etc/SuSEconfig/profile.SuSEconfig
echo "if ( ! \${?WINDOWMANAGER} ) setenv WINDOWMANAGER $WINDOWMANAGER" >> $r/etc/SuSEconfig/csh.cshrc.SuSEconfig

test -n "$CONSOLE_MAGIC" && {
        echo "# The following sequence is needed for most non-west-european languages" >> $r/etc/SuSEconfig/profile.SuSEconfig
        echo "test \"\$TERM\" = \"linux\" -a -t && echo -en \"\\033"$CONSOLE_MAGIC\"\
 >> $r/etc/SuSEconfig/profile.SuSEconfig
        echo "# The following sequence is needed for most non-west-european languages" >> $r/etc/SuSEconfig/csh.login.SuSEconfig
        echo "test \"\$TERM\" = \"linux\" -a -t && echo -n \"\\033"$CONSOLE_MAGIC\" >> $r/etc/SuSEconfig/csh.login.SuSEconfig
}

check_md5_and_move $r/etc/SuSEconfig/profile
check_md5_and_move $r/etc/SuSEconfig/csh.login
check_md5_and_move $r/etc/SuSEconfig/csh.cshrc

