#!/bin/sh

export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
export HOME="/root"

cd /dev ; rm -f fd
ln -sf /proc/kcore      core
ln -sf /proc/self/fd    fd
ln -sf fd/0             stdin
ln -sf fd/1             stdout
ln -sf fd/2             stderr
cd /
mkdir -p /mnt/{source,target}

#echo "Running ldconfig ..."
#ldconfig

echo "
T2 SDE installer (2nd stage) ...

The 2nd stage is a small Linux distribution, loaded into your system's memory.
It has everything needed to install T2 Linux, restore an old installation,
perform some administrative or backup tasks."

for x in /etc/setup-*.sh /setup/setup.sh ; do
   if [ -f "$x" ] ; then
      echo ; echo "Running $x ..." ; sh $x
      echo "Setup script $x finished."
   fi
done

ttydevs="console= `cat /proc/cmdline`"; ttydevs=${ttydevs##*console=}; ttydevs=${ttydevs%% *}
ttydevs="${ttydevs:-tty1 tty2 tty3 tty4 tty5 tty6}"
while true; do
	echo -n "
If you use a serial terminal, enter the names of terminal devices to use,
for example 'ttyS0' or 'ttyUSB0' for the first serial port or just 'console',
just hit enter otherwise. (default: $ttydevs): "
	read _ttydevs
	if [ "$_ttydevs" ]; then # check
		for x in $_ttydevs; do
			[ ! -c /dev/$x ] &&
				echo "$x is not a char-device." && continue 2
		done
		ttydevs="$_ttydevs"
	fi
	break
done

echo "
If you don't know what to do now, it's time to read the T2 documentation.
Simply type 'install' now, if you want to perform a normal installation of T2 "
if type -p dialog > /dev/null ; then
	echo "(or type 'install -text' if you prefer non-dialog based menus)."
else
	echo "(only the text interface is available)."
fi

for x in $ttydevs ; do
   ( ( while : ; do agetty -i 38400 -J $x -n -l /sbin/login-shell ; done ) & )
done

exec < /dev/null > /dev/null 2>&1
while : ; do sleep 1 ; done
