#!/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
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:
lock:x:54:
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


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

#
# 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/after.local \
		/etc/init.d/before.local \
		/etc/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, 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
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, 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
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
echo "#! /bin/sh
#
# Copyright (c) 2010 SuSE LINUX Products GmbH, Germany.  All rights reserved.
#
# Author: Werner Fink, 2010
#
# /etc/init.d/before.local
#
# script with local commands to be executed from init before executing
# any script of a runlevel. 
#
# Here you should add things, that should happen directly before entering
# a runlevel. Common environment variables for this are:
#  RUNLEVEL  -- The current system runlevel.
#  PREVLEVEL -- The previous runlevel (useful after a runlevel switch).
#
" > /etc/init.d/before.local.new
test -e /etc/init.d/before.local || mv /etc/init.d/before.local.new /etc/init.d/before.local
rm -f /etc/init.d/before.local.new
chmod 744 /etc/init.d/before.local
echo "#! /bin/sh
#
# Copyright (c) 2010 SuSE LINUX Products GmbH, Germany.  All rights reserved.
#
# Author: Werner Fink, 2010
#
# /etc/init.d/after.local
#
# script with local commands to be executed from init after all scripts
# of a runlevel have been executed.
#
# Here you should add things, that should happen directly after
# runlevel has been reached.  Common environment
# variables for this are:
#  RUNLEVEL  -- The current system runlevel.
#  PREVLEVEL -- The previous runlevel (useful after a runlevel switch).
#
" > /etc/init.d/after.local.new
test -e /etc/init.d/after.local || mv /etc/init.d/after.local.new /etc/init.d/after.local
rm -f /etc/init.d/after.local.new
chmod 744 /etc/init.d/after.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

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