#! /bin/sh
# Copyright (c) 1996-2002 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Burchard Steinbild, 1996-97
#         Florian La Roche, 1996
# i18n support added by Aleksey Smirnov <smi@logic.ru>
#  & Aleksey Novodvorsky <aen@logic.ru> from IPLabs Linux Team, Moscow.
# Attention! The shell must be restarted before localisation takes effect!
#
# Rewrote i18n support to use RC_LANG and RC_LC_* sysconfig variables
# Bernhard Kaindl <bk@suse.de>, 1999
#
# Change ROOT_LOGIN_REMOTE support to edit PAM config files.
# Thorsten Kukuk <kukuk@suse.de>, 1999
#
# Move PAM configuration into SuSEconfig.pam
# Thorsten Kukuk <kukuk@suse.de>, 2000
#
# For feedback, please use http://www.suse.de/feedback

echo "Starting SuSEconfig, the SuSE Configuration Tool..."
umask 022

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

# The environment variable ROOT indicates the root of the system to be
# managed by SuSEconfig when that root is not '/'
r=$ROOT


FASTRUN=false
export FASTRUN
CHECK_NEWPACKAGE=true
export CHECK_NEWPACKAGE
FORCE_REPLACE=false
export FORCE_REPLACE
VERBOSE=false
export VERBOSE
DO_RESTARTS=true
export DO_RESTARTS

while test $# -gt 0; do
  case $1 in
    *-verbose)
      VERBOSE=true
      echo Running in verbose mode.
    ;;
    *-quick)
      FASTRUN=true
      echo Running in quick mode.
    ;;
    *-nonewpackage)
      CHECK_NEWPACKAGE=false
      echo Skipping new package checks.
    ;;
    *-force)
      FORCE_REPLACE=true
    ;;
    *-norestarts)
      DO_RESTARTS=false
    ;;
    *-nomodule)
      ONLY_MODULE="none"
      shift
    ;;
    *-module)
      FASTRUN=true
      ONLY_MODULE=$2
      echo Running module $ONLY_MODULE only
      shift
    ;;
    *)
      echo Unknown parameter $1.
      echo Usage: SuSEconfig "[--quick|--nonewpackage|--force|--verbose] [--module name]"
      exit 1
    ;;
  esac
  shift
done

test "$FASTRUN" = false -a "$CHECK_NEWPACKAGE" = true && \
    echo Running in full featured mode.

#
# where can SuSEconfig store data in running system?
#
LIBDIR=$r/var/adm/SuSEconfig
MD5DIR=$LIBDIR/md5
export MD5DIR
IS_RUNNING=$r/var/lock/SuSEconfig-is-running

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

echo "Reading $r/etc/sysconfig and updating the system..."

for i in $r/etc/sysconfig/suseconfig ; do
  test -f $i && . $i
done

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

# set a new PATH
if test "$r" != ""; then
        PATH=$PATH:$r/bin:$r/usr/bin:$r/sbin:$r/usr/sbin
        export PATH
fi

#
# first of all test if we have space.  If not abort.
#
my_test_for_space $r/etc || exit 1


#
# delete very old lock files
#

if test -x /usr/bin/find -a -x /usr/bin/xargs ; then
    /usr/bin/find $IS_RUNNING -cmin +1000 2> /dev/null \
    | /usr/bin/xargs --no-run-if-empty rm -f
fi

#
# check locking
#

if test -e $IS_RUNNING ; then
    echo -n "Another SuSEconfig seems to run.  Waiting up to 60 seconds"
    for i in 1 2 3 4 5 6 7 8 9 0 ; do
        for j in 1 2 3 4 5 6 ; do
            echo -n .
            sleep 1
            test -e $IS_RUNNING || break
        done
        test -e $IS_RUNNING || break
    done
    if test -e $IS_RUNNING ; then
        echo
        echo
        echo "Giving up..."
        echo
        echo "ps a | grep SuSEconfig"
        ps a | grep SuSEconfig
        echo
        echo "If You think, there is no other SuSEconfig running, please call"
        echo
        echo "    rm $IS_RUNNING "
        echo
        echo "Then you can start SuSEconfig again."
        echo
        exit 1
    fi
    echo
fi

echo $$ > $IS_RUNNING

trap "rm -f $IS_RUNNING ; exit 1" 1 2 15

#
# the following stuff should be done in running system...
#
# first things, that have to be done in 'normal' systems as well as in
# 'demo mode' system.

if test -z "$r" -o "$FASTRUN" = "true" ; then
    SUB_SCRIPTS=""
    if test "$ONLY_MODULE" = "none" ; then
       echo skipping modules
       # do nothing, only keep FASTRUN off
    elif test -n "$ONLY_MODULE" ; then
        if test -f /sbin/conf.d/SuSEconfig.$ONLY_MODULE ; then
            SUB_SCRIPTS=/sbin/conf.d/SuSEconfig.$ONLY_MODULE
        else
            echo Module $ONLY_MODULE does not exist
        fi
    elif test "$FASTRUN" = "true" ; then
	# to be deleted ... all other scripts do not exist any more
        SUB_SCRIPTS="$r/sbin/conf.d/SuSEconfig.sendmail"
    else
        SUB_SCRIPTS="/sbin/conf.d/SuSEconfig.*"
    fi
    #
    # Now start the package skripts
    #
    for SKRIPT in $SUB_SCRIPTS ; do
        test -f $SKRIPT || continue
        case "$SKRIPT" in
          *~|*.swp|*.bak|*.sav|*.save|*.rpmsave|*.rpmorig|*.rpmnew|*.orig)
            continue
            ;;
          *)
            echo Executing $SKRIPT...
            bash $SKRIPT
            ;;
        esac
    done
fi


#
# Now do things, only allowed in running 'normal' system.
#
if test -z "$r" -a ! -e $r/usr/lib/YaST/.DemoMode ; then
    #
    # we have only one package with a /usr/bin/vi symlink, but several vi
    # packages. If it does not exist, create it
    #
    if test ! -e /usr/bin/vi ; then
        rm -f /usr/bin/vi
        test -e /usr/bin/xvi && ln -s xvi /usr/bin/vi
        test -e /usr/bin/nvi && ln -s nvi /usr/bin/vi
        test -e /usr/bin/elvis && ln -s elvis /usr/bin/vi
        test -e /usr/bin/vim && ln -s vim /usr/bin/vi
    fi

    if test "$FASTRUN" != "true" ; then
	/sbin/update-modules.dep
    fi
fi

#
# we have finished and can remove our lock file.
#
rm -f $IS_RUNNING

echo "Finished."
exit 0
