#!/bin/sh
RETVAL=1
if [ `whoami` != "root" ] 
then 
	until [ $RETVAL -eq 0 ]
	do

	echo -n "Enter roots password:"
	su root -c ./install 2>/dev/null
	RETVAL=$?
	echo ""
	if [ $RETVAL -ne 1 ] 
	then
		exit
	fi
	echo "Please enter the RIGHT root password."
	done
fi
echo -en "\033[2J"
echo -en "\033[1;5r\033[HQuestions            0/6\033[3r\033[3H"
echo ""
echo "So, you want to install DOSemu do you?"
echo "Well, ya gotta answer a couple of questions for me then!"
echo ""
BLAH=0
until [ $BLAH -eq 1 ]
do
	echo "What user is this going to be installed for?"
	echo -n "[bbs] "
	read INSTALL_USER
	if [ ! $INSTALL_USER ]
	then
		INSTALL_USER="bbs"
	fi
	USERPATH=`cat /etc/passwd|sed "s/:[^:]*:[^:]*:[^:]*:[^:]*//"|grep -x "$INSTALL_USER:.*"|sed "s/[^:]*://"|sed "s/:.*//"`
        if [ -f "$USERPATH/mystic/mystic" ] && [ -x "$USERPATH/mystic/mystic" ]
        then
                MYSTIC_PATH="$USERPATH/mystic"
        fi
        if [ -f "$USERPATH/mystic" ] && [ -x "$USERPATH/mystic" ]
        then
                MYSTIC_PATH="$USERPATH"
        fi
        until [ -f "$MYSTIC_PATH/mystic" ] && [ -x "$MYSTIC_PATH/mystic" ]
        do
                echo "What is the path to your mystic software?"
                echo -n "[$USERPATH/mystic] "
                read MYSTIC_PATH
                if [ ! $MYSTIC_PATH ]
                then
                	MYSTIC_PATH="$USERPATH/mystic"
        	fi
         done
	echo "First off, where would you like your doors to be installed to?"
	echo -n "[$USERPATH/doors] "
	read DOOR_PATH
	if [ ! $DOOR_PATH ]
	then
		DOOR_PATH="$USERPATH/doors"
	fi
	DOSEMU_PATH=`pwd`
	until [ -e "$DOSEMU_PATH/dosemu-1.0.1.tgz" ]
	do
		echo "What is the path to your dosemu-1.0.1.tgz?"
		echo -n "[`pwd`] "
		read DOSEMU_PATH
		if [ ! $DOSEMU_PATH ]
		then
			DOSEMU_PATH=`pwd`
		fi
	done
	DOSEMU_PATH="$DOSEMU_PATH/dosemu-1.0.1.tgz"
	PATCH_PATH=`pwd`
	until [ -e "$PATCH_PATH/patch-1.0.1-missing-new-vmodem.gz" ]
	do
		echo "What is the path to your patch-1.0.1-missing-new-vmodem.gz?"
		echo -n "[`pwd`] "
		read PATCH_PATH
		if [ ! $PATCH_PATH ]
		then
			PATCH_PATH="`pwd`"
		fi
	done
	PATCH_PATH="$PATCH_PATH/patch-1.0.1-missing-new-vmodem.gz"
	echo "What is the path to your temporary directory?"
	echo -n "[/tmp] "
	read TMP_PATH
	if [ ! $TMP_PATH ]
	then
		TMP_PATH="/tmp"
	fi
	echo
#	echo -en "\033[2J\033[H"
	echo "Ok, I got from you the following:"
	echo "  User:           $INSTALL_USER"
	echo "  Mystic Path:    $MYSTIC_PATH"
	echo "  Door Path:      $DOOR_PATH"
	echo "  DosEMU Tarball: $DOSEMU_PATH"
	echo "  DosEMU Patch:   $PATCH_PATH"
	echo "  Path to Tmp:    $TMP_PATH"
	echo ""
	BLAH=0
	until [ $BLAH -ne 0 ]
	do
		echo "Is this correct?"
		echo -n "[y/n]"
		read CORRECT
		if [ $CORRECT ]
		then
			if [ $CORRECT = "y" ]
			then
				BLAH=1
			fi
			if [ $CORRECT = "n" ]
			then
				BLAH=2
			fi
		fi
	done
done

#echo -en "\033[2J\033[H"
echo "OK, GOOD!  Proceeding with install"
pushd .
cd $TMP_PATH
echo -en "\033[s\033[1;2r\033[HExtracting           1\033[3r\033[u"
tar xvfz $DOSEMU_PATH
cd dosemu-1.0.1
echo -en "\033[s\033[1;2r\033[HApplying Patch       2\033[3r\033[u"
gzip -dc $PATCH_PATH|patch -p1
echo -en "\033[s\033[1;2r\033[HRunning Configure    3\033[3r\033[u"
./configure
echo -en "\033[s\033[1;2r\033[HRunning Make         4\033[3r\033[u"
make
echo -en "\033[s\033[1;2r\033[HRunning Make Install 5\033[3r\033[u"
make install
popd
echo -en "\033[s\033[1;2r\033[HCopying files        6\033[3r\033[u"
echo "Copying global.conf"
cp "global.conf" /var/lib/dosemu/
echo "Copying dosemu.conf"
cp "dosemu.conf" /etc
echo "Copying dosemu.user"
cp "dosemu.users" /etc

if [ -e "hdd.c" ] 
then
	echo "Copying hdd.c"
	cp hdd.c /var/lib/dosemu
fi
if [ ! -d "$USERPATH/bin" ]
then
	echo "Creating $USERPATH/bin"
	mkdir -p "$USERPATH/bin"
	chown $INSTALL_USER $USERPATH/bin
fi

echo "Fixing disclaimer in dosemu"
mkdir -p $USERPATH/.dosemu
touch $USERPATH/.dosemu/disclaimer
chown "$INSTALL_USER" -R $USERPATH/.dosemu

echo "Creating door_script to match profile"
echo "#!/bin/sh">$USERPATH/bin/door_script
echo "printf \"Loading \$2....\"">>$USERPATH/bin/door_script
echo "todos < $MYSTIC_PATH/temp\$1/DOOR.SYS > $MYSTIC_PATH/temp$1/DOOR.SY1">>$USERPATH/bin/door_script
echo "mv $MYSTIC_PATH/temp\$1/DOOR.SY1 $MYSTIC_PATH/temp\$1/DOOR.SYS">>$USERPATH/bin/door_script
echo "dos -u virtual -I 'keystroke \"\r\"' \"NODE=\$1\" \"DOOR=\$2\" 2>/dev/null  >/dev/null">>$USERPATH/bin/door_script
chmod 555 $USERPATH/bin/door_script
ln -s $USERPATH/bin/door_script $USERPATH/door_script
chown "$INSTALL_USER" "$USERPATH/bin/door_script"
if [ -e "todos" ]
then
	cp todos /usr/bin
fi
echo -e "\033[s\033[r\033\033[u"
echo ""
echo "There!  That should be it, for doing steps inside Reapern66's"
echo "Howto.  If you have experienced ANY problems with this not working properly"
echo "please don't hesitate to e-mail me @ invisi+install@mn.mediaone.net"
echo "						- Thanks"
echo ""
echo  -e "\033[1000H"
