#!/bin/sh

expand_mes() {
    source /usr/lib/setup/SeTconfig.mes
}

T_PX="`cat /tmp/SeTT_PX`"
ROOT_DEVICE="`cat /tmp/SeTrootdev`"
if [ ! -d $T_PX/bin -a ! -d $T_PX/etc ]; then # if there no Linux here, exit
    exit
fi
( cd $T_PX ; chmod 755 ./ )
( cd $T_PX ; chmod 755 ./var )

if [ -d $T_PX/usr/src/linux ]; then
    chmod 755 $T_PX/usr/src/linux
fi

if [ ! -d $T_PX/proc ]; then
    mkdir $T_PX/proc
    chown root.root $T_PX/proc
fi

if [ ! -d $T_PX/sys ]; then
    mkdir $T_PX/sys
    chown root.root $T_PX/sys
fi

chmod 1777 $T_PX/tmp

if [ ! -d $T_PX/var/spool/mail ]; then
    mkdir -p $T_PX/var/spool/mail
    chmod 755 $T_PX/var/spool
    chown root.mail $T_PX/var/spool/mail
    chmod 775 $T_PX/var/spool/mail
fi 

mount --bind /dev $T_PX/dev
mount --bind /proc $T_PX/proc
mount --bind /sys $T_PX/sys
mount --bind /tmp $T_PX/tmp
# chmod a+rw $T_PX/dev/null

expand_mes
dialog --title "system configurations" --yesno "$config_mes" 14 74 
if [ $? = 1 -o $? = 255 ]; then
    exit
fi

# Post installation and setup scripts added by packages.
if [ -d $T_PX/var/log/setup ]; then
    for INSTALL_SCRIPTS in $T_PX/var/log/setup/setup.*
    do
	SCRIPT=`basename $INSTALL_SCRIPTS`
	# Here, we call each script in /var/log/setup. Two arguments are provided:
	# 1 -- the target prefix (normally /, but /mnt from the bootdisk)
	# 2 -- the name of the root device.
	( cd $T_PX ; sh var/log/setup/$SCRIPT $T_PX $ROOT_DEVICE )
	if echo $SCRIPT | grep onlyonce 1> /dev/null 2> /dev/null; then # only run after first install
	    if [ ! -d $T_PX/var/log/setup/install ]; then
		mkdir $T_PX/var/log/setup/install
	    fi
	    mv $INSTALL_SCRIPTS $T_PX/var/log/setup/install
	fi
    done
fi

# Load keyboard map (if any) when booting
CONSOLE="$T_PX/etc/sysconfig/console"

if [ -r /tmp/keyset ]; then
    if [ ! -d $T_PX/etc/sysconfig ]; then
	mkdir -p $T_PX/etc/sysconfig
    fi
    
    chk1=`grep ctrl /tmp/keyset`
    chk2=`grep k2esc /tmp/keyset`

    if grep 106 /tmp/keyset 1> /dev/null 2>/dev/null ; then
	if [ "$chk1.x" != ".x" ]; then
	    echo 'KEYMAP="jp106 ctrl"' > $T_PX/etc/sysconfig/console
	else
	    echo 'KEYMAP="jp106"' > $T_PX/etc/sysconfig/console
	fi
    else
	if [ "$chk1.x" != ".x" ]; then
	    if [ "$chk2.x" != ".x" ]; then
		echo 'KEYMAP="defkeymap ctrl k2esc-d"' > $T_PX/etc/sysconfig/console
	    else
		echo 'KEYMAP="defkeymap ctrl"' > $T_PX/etc/sysconfig/console
	    fi
	fi
    fi

fi

dialog --title "Final configurations" --msgbox \
"$final_mes" 8 74 

if [ -f $T_PX/etc/rc.d/rc.once ]; then
  chroot $T_PX /etc/rc.d/rc.once
  mv $T_PX/etc/rc.d/rc.once $T_PX/tmp
fi

if [ -f $T_PX/etc/rc.d/rc.initpkg ]; then
  touch $T_PX/Run_Once
  chroot $T_PX /etc/rc.d/rc.initpkg
  rm $T_PX/Run_Once
fi

# . /usr/lib/setup/SeTbootdsk
