#!/bin/bash
#
# Run this script in C-Locale, or some tools will fail.
export LC_ALL=C
# /boot should be the first directory.  This increases the probability, that
# lilo stuff lies before 1024 cyl.
mkdir -p boot
#
# make sure, tmp directories do exist
#
for i in tmp var/tmp ; do
    test -d $i || {
        if test -L $i ; then
            echo "Error! $i is a dangling symlink."
            echo
            ls -l $i
            echo
            echo "To avoid big problems this link has to be deleted and a directory"
            echo "will be created.  Remember to fix it after installation."
            echo
            rm -f $i
        elif test -f $i ; then
            echo "$i is a file.  This makes no sense.  Moving it to $i.save."
            mv -v $i $i.save
        fi
        mkdir -p $i
    }
done
#
# now create a var/adm/fillup-templates/passwd.aaa_base.
# If etc/passwd does not exist, copy
# var/adm/fillup-templates/passwd.aaa_base to etc/passwd.
# deleted db2 groups and users were uids 46,47,48 and gids 46,47,48
#
mkdir -p etc
touch etc/mtab
mkdir -p var/adm/fillup-templates
echo "root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/bash
daemon:x:2:2:Daemon:/sbin:/bin/bash
lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash
mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false
news:x:9:13:News system:/etc/news:/bin/bash
uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash
games:x:12:100:Games account:/var/games:/bin/bash
man:x:13:62:Manual pages viewer:/var/cache/man:/bin/bash
wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false
ftp:x:40:49:FTP account:/srv/ftp:/bin/bash
nobody:x:65534:65533:nobody:/var/lib/nobody:/bin/bash" \
 > var/adm/fillup-templates/passwd.aaa_base

echo "root:x:0:
bin:x:1:daemon
daemon:x:2:
sys:x:3:
tty:x:5:
disk:x:6:
lp:x:7:
www:x:8:
kmem:x:9:
wheel:x:10:
mail:x:12:
news:x:13:
uucp:x:14:
shadow:x:15:
dialout:x:16:
audio:x:17:
floppy:x:19:
cdrom:x:20:
console:x:21:
utmp:x:22:
public:x:32:
video:x:33:
games:x:40:
xok:x:41:
trusted:x:42:
modem:x:43:
ftp:x:49:
man:x:62:
users:x:100:
nobody:x:65533:
nogroup:x:65534:nobody" > var/adm/fillup-templates/group.aaa_base

rm -f var/adm/fillup-templates/shadow.aaa_base
while read LINE ; do
  case $LINE in
    root*)
	echo "root::$(($(date '+%s')/86400))::::::" \
		>> var/adm/fillup-templates/shadow.aaa_base
	;;
    *)
	echo "${LINE%%%%:*}:*:$(($(date '+%s')/86400))::::::" \
		>> var/adm/fillup-templates/shadow.aaa_base
	;;
  esac
done < var/adm/fillup-templates/passwd.aaa_base

# etc/passwd update
if test -e etc/passwd ; then
  sed " 
	# re-do these users on update, their home-dir was changed
	 /^news:\|^uucp:\|^man:\|^ftp:.*:.usr.local.ftp:/d

	# change group of wwwrun: nogroup -> www
        s|^wwwrun:x:30:65534:|wwwrun:x:30:8:|

	# change login shell of wwwrun
	s|^\(wwwrun:x:30:8:.*:\)/bin/bash|\1/bin/false|
  " < etc/passwd > etc/passwd.t
  cat etc/passwd.t > etc/passwd
  rm -f etc/passwd.t
fi
if test -e etc/shadow ; then
    # nobody had sp_lstchg set to 0 in 8.0 and older
    sed "/^nobody:[^:]*:0:/d" < etc/shadow > etc/shadow.t
    cat etc/shadow.t > etc/shadow
    rm -f etc/shadow.t
fi

# Rename game -> games, wwwadmin -> www
for i in etc/group ; do
  if test -e $i ; then
    sed -e "s|^game:|games:|" -e "s|^wwwadmin:|www:|" $i > $i.t
    cat $i.t > $i
    rm -f $i.t
  fi
done

for file in passwd group ; do
 if test -f etc/$file ; then
  # like fillup, but : is the only separator
  rm -f etc/$file.add
  sort -k 1,1 -t: -u etc/$file var/adm/fillup-templates/$file.aaa_base \
  | sort -k 1,1 -t: etc/$file - | uniq -u > etc/$file.add
  cat etc/$file.add >> etc/$file
  rm -f etc/$file.add
  # fix permissions if this script is called with strange umask
  chmod 644 etc/$file
 else
  cat var/adm/fillup-templates/$file.aaa_base > etc/$file
 fi
done

if test -d sbin/init.d -a ! -L sbin/init.d ; then
   if test -L etc/init.d ; then
     rm -f etc/init.d
   fi
fi
#
# we have several local files, that changed over the time.  Check the
# existing one, if they contain real data.  If not, delete them.
#
for LOCALFILE in root/bin/cron.daily.local \
		etc/init.d/boot.local \
		etc/init.d/halt.local \
		sbin/init.d/boot.local \
		sbin/init.d/halt.local \
		usr/sbin/usradd.local \
		usr/sbin/usrdel.local \
		usr/sbin/userdel.local ; do
    test -f $LOCALFILE || continue
    LOCALFILE_CONTAINS_DATA=false
    while read LINE ; do
        case "$LINE" in
          "#"*)
          ;;
          "echo "*">"*)
            LOCALFILE_CONTAINS_DATA=true
          ;;
          "echo "*)
          ;;
          ". /etc/rc.config")
          ;;
          "exit "*)
          ;;
          "")
          ;;
          *)
            LOCALFILE_CONTAINS_DATA=true
          ;;
        esac
    done < $LOCALFILE
    test "$LOCALFILE_CONTAINS_DATA" = false && rm -f $LOCALFILE
done
mkdir -p etc/init.d
echo "#! /bin/sh
#
# Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany.  All rights reserved.
#
# Author: Werner Fink <werner@suse.de>, 1996
#         Burchard Steinbild, 1996
#
# /etc/init.d/boot.local
#
# script with local commands to be executed from init on system startup
#
# Here you should add things, that should happen directly after booting
# before we're going to the first run level.
#
" > etc/init.d/boot.local.new
if [ ! -e etc/init.d/boot.local ] ; then
  if [ -e sbin/init.d/boot.local ] ; then
    mv sbin/init.d/boot.local etc/init.d/boot.local
  fi
fi
test -e etc/init.d/boot.local || mv etc/init.d/boot.local.new etc/init.d/boot.local
rm -f etc/init.d/boot.local.new
chmod 744 etc/init.d/boot.local
echo "#! /bin/sh
#
# Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany.  All rights reserved.
#
# Author: Werner Fink <werner@suse.de>, 1998
#         Burchard Steinbild, 1998
#
# /etc/init.d/halt.local
#
# script with local commands to be executed from init on system shutdown
#
# Here you should add things, that should happen directly before shuting
# down.
#
" > etc/init.d/halt.local.new
if [ ! -e etc/init.d/halt.local ] ; then
  if [ -e sbin/init.d/halt.local ] ; then
    mv sbin/init.d/halt.local etc/init.d/halt.local
  fi
fi
test -e etc/init.d/halt.local || mv etc/init.d/halt.local.new etc/init.d/halt.local
rm -f etc/init.d/halt.local.new
chmod 744 etc/init.d/halt.local
mkdir -p etc
for LINK in usr/X11R6/lib/X11 var/X11R6/lib/fonts usr/lib/mgetty+sendfax \
    usr/man/cat? usr/X11R6/man/cat? usr/openwin/man/cat? usr/lib/news ; do
    if test -L $LINK ; then
        echo "Found forbidden/oldish Link: $LINK ...deleting"
        rm -f $LINK
    fi
done
for DIR in etc/rc.d usr/X11 ; do
    if test -d $DIR -a ! -L $DIR ; then
        echo -n "Found oldish directory /$DIR.  "
        if test -d $DIR.old ; then
            echo /$DIR.old already exist. Can not move /$DIR to it.
        else
            echo Moving it to /$DIR.old
            mv $DIR $DIR.old
        fi
    fi
done

if test -f /root/.gnupg/secring.gpg ; then
  cp -a /root/.gnupg/secring.gpg /root/.gnupg/secring.gpg.aaa_save
fi