#!/bin/sh

prog=`basename $0`

echo "Instalation program for xemp & tcemp"
echo

echo -n "Do you want me to solve all problems automatically [y]: "
read ans

if test X${ans} = X -o X${ans} = Xy -o X${ans} = XY -o X${ans} = Xyes
then
	echo "Auto mode enabled"
	flags=a
	auto=1
else
	echo "Auto mode disabled"
	flags=
	auto=0
fi

echo
echo "Current directory: "`pwd`
echo -n "Do you want xemp to be installed in this directory [y]: "
read ans

if test X${ans} = X -o X${ans} = Xy -o X${ans} = XY -o X${ans} = Xyes
then
	echo "Installing in this directory"
	idir=
else
	echo -n "Dir: "
	read ans
	idir=`echo $ans | sed -e "s-~-${HOME}-"`
	echo "Installing in: "${idir}

	if test ! -d ${idir}
	then
		echo "Trying to make this directory ... "
		echo "$ mkdir ${idir}"
		mkdir ${idir}
		if test ! $? = 0
		then
			echo "mkdir failed."
			echo "${prog} aborted"
			exit
		fi
	fi
fi

echo
echo "Some machines (like ultrix) can not handle complicated makefiles."
echo "If this is the case, you should only make one program at a time."
echo
echo "Do you want to make:"
echo "1: xemp"
echo "2: tcemp"
echo "3: both tcemp & xemp"

cont=1
while test ${cont} = 1
do
	echo
	echo -n "make [3]: "
	read ans

	case X${ans} in

		X1)
			make_xemp=1
			make_tcemp=0
			flags=X${flags}
			echo "making xemp"
			cont=0
			;;
		
		X2)
			make_xemp=0
			make_tcemp=1
			flags=T${flags}
			echo "making tcemp"
			cont=0
			;;
		
		X|X3)
			make_xemp=1
			make_tcemp=1
			echo "making both xemp & tcemp"
			cont=0
			;;
		
		*)
			echo "Illegal input."
	esac
done
echo

echo "$ cd src/setup"
cd src/setup

if test ! $? -eq 0
then
	echo "cd failed"
	echo "${prog} aborted"
	exit
fi

echo "$ make"
make

if test ! $? -eq 0
then
	echo "make failed"
	echo "${prog} aborted"
	exit
fi

echo "$ cd ../.."
cd ../..

echo "$ setup -Vvps${flags}"
setup -Vps${flags}

ls -l makefile

if test ${make_xemp} = 1
then
	if test ${make_tcemp} = 0
	then
		echo "$ make proto.h"
		make proto.h
		[ ! $? = 0 ] && exit
		echo "$ make"
		make
		[ ! $? = 0 ] && exit
		echo "xemp ready"
		ls -l xemp
	else
		echo "$ make x_proto.h"
		make x_proto.h
		[ ! $? = 0 ] && exit
		echo "$ make xemp"
		make xemp
		[ ! $? = 0 ] && exit

	fi

fi

if test ${make_tcemp} = 1
then
	if test ${make_xemp} = 1
	then
		echo
		echo "when you want to compile tcemp, you need to throw"
		echo "away all object files that are created by xemp"
		echo
		echo -n "Delete all object files [y]: "
		read ans
		if test X${ans} = X -o X${ans} = Xy -o X${ans} = XY
		then
			echo "$ make cleanup"
			make cleanup
		else
			echo "Sorry, but I am unable to make tcemp now."
			echo "Bye"
			exit
		fi

		echo "$ make tc_proto.h"
		make tc_proto.h
		[ ! $? = 0 ] && exit
		echo "$ make tcemp"
		make tcemp
		[ ! $? = 0 ] && exit
	else
		echo "$ make proto.h"
		make proto.h
		[ ! $? = 0 ] && exit
		echo "$ make"
		make
		[ ! $? = 0 ] && exit
	fi
fi

if test -f ${HOME}/.xemprc
then
	echo "You already got an old .xemprc"
	echo "If this .xemprc belongs to a version earlier than 4.02 you will"
	echo "have to make a LOT of changes to get it working again"
	echo
	echo -n "Delete the old one [n]: "
	read ans
	if test X${ans} = Xyes -o X${ans} = Xy -o X${ans} = XY
	then
		echo "$ rm ${HOME}/.xemprc"
		rm ${HOME}/.xemprc
		echo "$ cp .xemprc.def ${HOME}/.xemprc"
		cp .xemprc.def ${HOME}/.xemprc
		echo "$ chmod 600 ${HOME}/.xemprc"
		chmod 600 ${HOME}/.xemprc
	fi
else
	echo "$ cp .xemprc.def ${HOME}/.xemprc"
	cp .xemprc.def ${HOME}/.xemprc
	echo "$ chmod -r ${HOME}/.xemprc"
	chmod +rw ${HOME}/.xemprc
fi

echo
echo "The bin directory contains a small program to enter the data"
echo "of a current game into your .xemprc (called newgame)"

quest="Do you want to add a new game"
def="y"
cont=1
while test ${cont} = 1
do
	echo
	echo -n "${quest} [${def}]: "
	read ans
	[ X${ans} = X ] && ans=${def}

	if test ${ans} = y -o ${ans} = Y -o ${ans} = yes
	then
		bin/newgame
		quest="Do you want to add another game"
		def="n"
	else
		cont=0
	fi
done

if test ${make_tcemp} = 1
then
	echo
	echo "Now trycap has to be made. This helps you selecting the"
	echo "right capabilities for your different terminals"
	echo
	echo "$ cd src/trycap"
	cd src/trycap
	echo "$ make"
	make
	echo "$ make cleanup"
	make cleanup
	echo "$ cd ../.."
	cd ../..
	echo "$ trycap"
	trycap

	echo
	echo "You should run trycap on every terminal from which you want"
	echo "to play tcemp"
	echo
	echo -n "more: "
	read ans
fi

if test ! X${idir} = X
then
	thisdir=`pwd`
	echo
	echo "Now I am going to install some directories (using tar)"
	echo "(mv cannot cross devices)"
	echo
	for dir in bin progs info
	do
		echo "$ tar -cf /tmp/xemp.${dir}.t ./${dir}"
		tar -cf /tmp/xemp.${dir}.t ./${dir}
		echo "$ cd ${idir}"
		cd ${idir}
		echo "$ tar -xf /tmp/xemp.${dir}.t"
		tar -xf /tmp/xemp.${dir}.t
		echo "$ cd ${thisdir}"
		cd ${thisdir}
		echo "$ rm /tmp/xemp.${dir}.t"
		rm /tmp/xemp.${dir}.t
	done

	echo 
	echo Copying executables
	echo

	if test ${make_xemp} -eq 1
	then
		echo "$ cp xemp ${idir}/bin"
		cp xemp ${idir}/bin
	fi

	if test ${make_tcemp} -eq 1
	then
		echo "$ cp tcemp ${idir}/bin"
		cp tcemp ${idir}/bin

		echo "$ cp trycap ${idir}/bin"
		cp trycap ${idir}/bin

		echo "$ cp setup ${idir}/bin"
		cp setup ${idir}/bin
	fi
fi
