#!/bin/bash
# vim:set sw=4 ts=4:
#
# $Id: prepare_alice,v 2.23 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005           SUSE GmbH, Nuernberg, Germany
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Author: Pieter Hollants, Fabian Herschel
#
##############################################################################
#
# prepare_alice
# Implants ALICEs installation path into the systems first bootup 
# (so make_all can be called then)
#
# Sections: --
# Tags:     --

# Where does YaST the installation media resp. the ALICE scripts?
alice_mount="/var/adm/mount"
# Where does YaST mount the installation target?
target_mount="/mnt"
if [ "$1" = "-auto" ]
then
   target_mount=$(pwd)
else
   target_mount="/"
fi

export alice_mount target_mount
if [ -e $target_mount/etc/rc.config ]
then
    . $target_mount/etc/rc.config
    AL_RC_CONFIG=yes
else
    AL_RC_CONFIG=no
fi
if [ -e $target_mount/etc/rc.config.d/alice.rc.config ]
then
. $target_mount/etc/rc.config.d/alice.rc.config
fi
#
# maybe a mini config in the initrd of the floppy disc?
# this sould set variables like ALICE_CONFIG,
# SET_ROOT_PWD, ROOT_START_PWD and ALICE_CONFIG_URL
#
if [ -e /alrc.cfg ]
then
   . /alrc.cfg
fi
#
# Do we run in auto installation mode?
#
if [ "$ALICE_CONFIG" != "yes" ]
then
   #
   # nothing to do in this case
   #
   exit
fi
#
# Try to set a default root password (encrypted)
#
if [ "$SET_ROOT_PWD" = "yes" ]
then
   epwd="8lcNGAWj7Bx9I"
   if [ -n "$ROOT_START_PWD" ]
   then
       epwd=$ROOT_START_PWD
   fi
   cp $target_mount/etc/shadow $target_mount/etc/shadow.old
   $target_mount/usr/bin/awk -F: '
      $1 == "root" && $2 == ""  { printf "%s:%s:%s:%s:%s:%s:%s:%s:%s\n",$1,epwd,$3,$4,$5,$6,$7,$8,$9 }
      $1 == "root" && $2 != ""  { print $0 }     # never change root passwd, if it is already set
      $1 != "root" { print $0 }' epwd=$epwd $target_mount/etc/shadow.old > $target_mount/etc/shadow
fi
#
# Now let us set the alice logger for syslog
#
cp $target_mount/etc/syslog.conf $target_mount/etc/syslog.conf.orig.$$
(
if [ -n "$ALICE_LOGGER_FACILITY" ]  # i.e. local0
then
   if [ -n "$ALICE_LOGGER_ACTION_ALL" ]
   then
      for action in $ALICE_LOGGER_ACTION_ALL
      do
         echo "$ALICE_LOGGER_FACILITY.* $action"
      done
   fi
   if [ -n "$ALICE_LOGGER_ACTION_ERR" ]
   then
      for action in $ALICE_LOGGER_ACTION_ALL
      do
         echo "$ALICE_LOGGER_FACILITY.err $action"
      done    
   fi
   if [ -n "$ALICE_LOGGER_ACTION_WARN" ]
   then
      for action in $ALICE_LOGGER_ACTION_ALL
      do
         echo "$ALICE_LOGGER_FACILITY.warn $action"
      done    
   fi
fi
) >> $target_mount/etc/syslog.conf 
#
# Searching the correct directory for startup (boot) programs
#
# for SuSE >= 7.1
#
if [ -d ${target_mount}/etc/init.d ]
then
    init_dir=/etc/init.d
else
#
# for SuSE < 7.1
#
    if [ -d ${target_mount}/sbin/init.d ]
    then
        init_dir=/sbin/init.d
    else
#
# some other stuff?
#
        if [ -d ${target_mount}/etc/rc.d ]
        then
            init_dir=/etc/rc.d
        fi
    fi
fi

#
# if no route is set, but a DEF_GW is needed create the route
#
route=`grep default ${target_mount}/etc/route.conf 2>/dev/null`
if [ -z "$route" ]; then
	if [ "$DEF_GW" ]; then
		echo "default $DEF_GW" >> ${target_mount}/etc/route.conf
	fi
fi

#
# Create basic alice on target system
#
#############################################################################
############ START  ALICE ###################################################
#############################################################################
#
cat <<EOF >${target_mount}/${init_dir}/alice 
#! /bin/sh
#
# Copyright (c) 1999 SuSE Rhein-Main AG, Eschborn, Germany.  All rights reserved.
#
# Authors: Pieter Hollants <hollants@suse.de>
#          Fabian Herschel <fabian.herschel@suse.de>
#          Juergen Henge-Ernst <hernst@suse.de>
#
# /${init_dir}/alice
### BEGIN INIT INFO
# Provides:          alice
# Required-Start:    \$network portmap route
# Required-Stop:
# Default-Start:     2 3 5
# Default-Stop:
# Description:       Runs the alice system config scripts
### END INIT INFO
#
. /etc/rc.config
if [ -f /etc/rc.config.d/alice.rc.config ]
then
	. /etc/rc.config.d/alice.rc.config
fi
if [ ! -e /var/state/alice_run ]; then
  	echo "Skipping ALICE post-installation scripts (/var/state/alice_run found)."
	echo -e	"\$rc_done"
  	exit
fi
case \$1 in
	start ) # do everything after the esac ;-)
			;;
	stop  ) # nothing to do for stop
			exit
			;;
	*	  ) # not known
			echo "usage: \$0 start|stop"
			exit 1
			;;
esac
#
# Tell the world were running
#
echo "Running ALICE post-installation scripts..."
/etc/rc.d/portmap start
if [ -z "\$ALICE_REMOUNT_WAITS" ]
then
	ALICE_REMOUNT_WAITS="10 10 60 60"
fi
for i in \$ALICE_REMOUNT_WAITS
do
	mount $(df -P ${alice_mount} 2>/dev/null | ${target_mount}/usr/bin/awk '$1 !~ /Filesystem/ {print $1}') ${alice_mount} && break
	echo "retry mount after \$i sec"
	sleep \$i
done
	

export ALICE_HOME=${alice_mount}/alice

# 1st remove the file because if a make_reboot is called, init is faster and the
# file can't be deleted
rm /var/state/alice_run
/usr/lib/alice/bin/make_all --fqhn $MY_FQHN

umount ${alice_mount}
echo "ALICE post-installation scripts have finished" 
echo -e "\$rc_done" 
EOF

#############################################################################
############# END   ALICE ##############################################
#############################################################################
#
# Set permissions for alice
#
${target_mount}/bin/chmod a+x ${target_mount}/${init_dir}/alice
#
# Link alice into target systems bootup sequence
#
if [ -x ${target_mount}/sbin/insserv ]; then
  ${target_mount}/sbin/insserv ${target_mount}/${init_dir}/alice
else
  ln -s /${init_dir}/alice \
	${target_mount}/${init_dir}/rc2.d/S09alice
  # also create a link for runlevel 3 (if the installation reboots
  # (cause of kernel change) beginning with 7.1 the runlevel is 3
  # and not 2 like before)
  ln -s /${init_dir}/alice \
	${target_mount}/${init_dir}/rc3.d/S09alice
fi
mkdir -p ${target_mount}/var/state
touch ${target_mount}/var/state/alice_run
#
# I know, the following is durty, but the installation should not hold,
# only because the package lxuser is selected
#
if [ -f ${target_mount}/var/adm/setup/setup.lxuser ]
then
  rm  ${target_mount}/var/adm/setup/setup.lxuser
fi
