#!/bin/sh
#
# This is the install script
#
# 19.03.1994 by Andre Florath (florath@jupiter.stochastik.rwth-aachen.de)
#

# Here are specified all subdirs that are from global interest
POSSUBDIRS="bin man info m68k-cbm-netbsd"

# Watch out for the dirs that are realy existent.
SUBDIRS=""
for DIR in $POSSUBDIRS;
do
	if [ -d $DIR ];
	then
		SUBDIRS="$SUBDIRS $DIR"
	fi
done

# Watch out for the target directory
if [ -z $1 ];
then 
	TARGETDIR=./..
else
	TARGETDIR=$1
fi


# Change to the main directory, if we are not already there.
LOCPWD=`pwd`
if [ "install" = `basename $LOCPWD` ];
then 
	cd ..
	LOCPWD=`pwd`
fi

PROGNAME=`basename $LOCPWD`

# The targetdir must exists!
if [ ! -d $TARGETDIR ];
then
	echo "ERROR: the target directory $TARGETDIR do not exists."
	exit
fi

echo
echo "********************************************************************"
echo "  Installing the program"
echo "        $PROGNAME"
echo "  in the directory"
echo "        $LOCPWD/$TARGETDIR"
echo "********************************************************************"

if [ -e ./install/Uninstall ];
then
	echo
	echo "ERROR: The file ./install/Uninstall already exists."
	exit
fi

echo
echo "Testing if there are files with the same name this package will use."

# Look, if there are already files with the same name we will use.
for DIR in $SUBDIRS;
do
	FILES=`find $DIR -type f -print`
	for FILE in $FILES;
	do
		if [ -e $TARGETDIR/$FILE ];
		then
			echo "ERROR: The file $TARGETDIR/$FILE already exists."
			echo "       Please remove it first."
			exit
		fi
	done
done

echo "No files exist - continuing the installation"
echo

cat >./install/Uninstall <<EOF
#!/bin/sh
#
# This file was automaticaly created to remove all files
# for the $PROGNAME from the system
#
EOF

# Install the files
for DIR in $SUBDIRS;
do

	if [ ! -d $TARGETDIR/$DIR ];
	then
		mkdir $TARGETDIR/$DIR
	fi		

	FILES=`find $DIR -type f -print`
	for FILE in $FILES;
	do
		echo "Installing '$FILE'"
		ln -s $LOCPWD/$FILE $TARGETDIR/$FILE
		echo >>./install/Uninstall "rm -f $LOCPWD/$TARGETDIR/$FILE"
	done
done
	
if [ -d ./info ];
then
	echo
	echo "Add a line for $PROGNAME to the file $TARGETDIR/info/dir"
	echo "to use the on line documentation in info format."
	echo

	echo >>./install/Uninstall
	echo >>./install/Uninstall "echo \"Remove the line for $PROGNAME from the $TARGETDIR/info/dir\""
	echo >>./install/Uninstall
fi

echo >>./install/Uninstall "rm -f ./install/Uninstall.old"
echo >>./install/Uninstall "mv ./install/Uninstall ./install/Uninstall.old"

chmod u+x install/Uninstall
