#!/bin/sh

# emergency divertion

clear
. /etc/info
. /etc/color
. /etc/path.conf

echo -e "${BRIGHT}$GREEN"
cat <<END

  WELCOME TO MULINUX v`cat /etc/version`, `date`

END
echo -e "${NORMAL}"

# function

in_ram()
{
df / | rgrep /dev/ram > /dev/null
}

startup_is_1722()
{
SplitArgs `cat /proc/cmdline`
[ "$f1722" ] && return 0
return 1
}

low_mem()
{
set -- `cat /proc/meminfo`
MB4=4194304
MEM=$8
[ "$MEM" -le "$MB4" ]
}

usr_mounted()
{
[ -d /usr/bin ]
}


fast()
{
in_ram || return 1
low_mem && MSG="RAM is to low to load USR" && return 1
usr_mounted && MSG="USR is mounted in /usr" && return 0
umount /startup 2>/dev/null

UNCOMPRESS="bzip2 -ds"
SOURCE=/dev/fd0H${USR_MEDIA}

if [ "$USR_OFFSET" -eq 0 ] ; then
read -p "Put the USR floppy-disk in the drive and hit -ENTER- " enter
T="$(/bin/file $SOURCE)" 

case $T in
*gzip*)
	UNCOMPRESS="gzip -dc"
	;;
esac

fi


echo -n "loading USR ... "
eval fastdd $SOURCE ${USR_OFFSET} | $UNCOMPRESS > /dev/ram1
echo "done."
echo -n "Mounting /usr ..."
mkdir -p /usr
mount /dev/ram1 /usr 
sleep 1
echo "  done."

echo -n "Creating /tmp ram partition ..."
mkfs.ext2 -q /dev/ram2 2048 > /dev/null 2>&1
mkdir -p /tmp
mount /dev/ram2 /tmp
chmod ugo+rwx /tmp
sleep 1
echo " done."
MSG="USR is mounted on /usr."
}


rustic_auth()
{
cat <<END
The 'root' password is required. If you are lucky, a simple ENTER will works.
END
while [ 1 ]; do
[ "`which stty`" ] && stty -echo
read -p "password: " password
[ "`which stty`" ] && stty echo
/sbin/chkpasswd "root" "$password" && break
done
}


# Main

echo -e "$BRIGHT Fast Boot facility $NORMAL"
fast


cat <<END

   -----------------------------------------------------------
   Maintenance Mode.
   $MSG
   "loadkeys it|us|uk ..." for keymaps, "help" for help.
   Please, remember: you are root, the superuser.
   Typing 'exit', the standard boot sequence restarts.
   -----------------------------------------------------------

END

echo -e "${BRIGHT} System Ready${NORMAL}, but (relatively) refractory."
echo
rustic_auth
/bin/-ash

(
umount /tmp
) 2>/dev/null

echo -e "$BRIGHT Continuing ... $NORMAL"
sleep 1
