#!/bin/sh

# install script for UniWorks release 2.0

dfile=/usr/tmp/uw$$

# tape must be nrmt
tape=/dev/nrmt0

cdbdir=/usr

cat /dev/null > $dfile
while [ -f $dfile ]
do
echo 
echo "This script installs UniWorks release 2.0 and cdbx from tape."
echo
echo "Warning: Do not install this cdbx revision if your system"
echo "is running Unix Release 4.1 or earlier."
echo
echo "Unix Release 5.0 systems should install this revision of cdbx."
echo
echo -n "Do you wish to install cdbx now ? [ynq] : "
read ANS
ANS=`echo $ANS | awk '{print $1}'`
if [ $ANS ]
then
    case $ANS in
	Yes | yes | Y | y)
	;;

	X | x | Q | q)
	echo Exiting.
	exit
	;;

	No | no | N | n)
	cdbdir=""
	;;

	*)
	echo Invalid Input
	continue;
	;;

    esac
fi

dest=/usr/uw

echo -n "Do you wish to install UniWorks now ? [ynq] : "
read ANS
ANS=`echo $ANS | awk '{print $1}'`
if [ $ANS ]
then
    case $ANS in
	Yes | yes | Y | y)
	;;

	X | x | Q | q)
	echo Exiting.
	exit
	;;

	No | no | N | n)
	if [ $cdbdir ]
	then
	    dest=""
	    rm -f $dfile
	else
	    echo Exiting.
	    exit 
	fi
	;;

	*)
	echo Invalid Input
	continue;
	;;

    esac
fi

if [ $dest ]
then
echo
echo "Warning: Do not install UniWorks 2.0 on top of an existing"
echo "version of UniWorks 1.5."
echo
echo The usual location for UniWorks is the directory /usr/uw.
echo -n "Do you wish to install UniWorks in /usr/uw ? [ynq] : "
read ANS
ANS=`echo $ANS | awk '{print $1}'`

if [ $ANS ]
then
    case $ANS in
	Yes | yes | Y | y)
	;;

	X | x | Q | q)
	echo Exiting.
	exit
	;;

	No | no | N | n)
	echo -n "Where do you wish to install UniWorks? : "
	read ANS
	ANS=`echo $ANS | awk '{print $1}'`
	if [ $ANS ]
	then
	    dest=$ANS
	else
	    continue
	fi
	;;

	*)
	echo Invalid Input
	continue;
	;;

    esac
fi

echo
echo Installing UniWorks directories in $dest
echo -n "Is this correct? [ynq] : "

read ANS
ANS=`echo $ANS | awk '{print $1}'`

if [ $ANS ]
then
    case $ANS in
	Yes | yes | Y | y)
	rm -f $dfile
	;;
	X | x | Q | q)
	echo Exiting.
	exit
	;;
	N | n | No | no)
	;;
	*)
	echo Invalid Input
	;;
    esac
else
    rm -f $dfile
fi

fi

done

# be sure directory exists or can be created

if [ $dest ]
then

if [ ! -d $dest ]
then
    mkdir $dest
fi

if [ ! -d $dest ]
then
    echo Could not create $dest
    exit
fi
fi
    
if [ $cdbdir ]
then
    echo INSTALLING cdbx
    echo Rewinding tape
    mt -f $tape rew
    mt -f $tape fsf 1
    echo Extracting cdbx directories...
    cd $cdbdir
    tar xpf $tape
else
    echo Skipping cdbx directories...
fi

if [ $dest ]
then
echo
echo INSTALLING UniWorks
echo Rewinding tape...
mt -f $tape rew
mt -f $tape fsf 2
cd $dest
echo Extracting UniWorks directories...
tar xpf $tape
rm -f $dfile

# change the suffixes of the config/<target> files to correct name
echo Cleaning up...
if [ -f $dest/config/is20/Makefile.vrtx ]
then
    suffix=vrtx
elif [ -f $dest/config/is20/Makefile.psos ]
then
    suffix=psos
elif [ -f $dest/config/is20/Makefile.wind ]
then
    suffix=wind
else
    suffix=
    echo File consistency error, Makefile has wrong name.
fi

if [ $suffix ]
then
    for dname in is20 is25 is30 liberator
    do
    cd $dest/config/$dname
    mv Makefile.$suffix Makefile
    mv UniWorks.$suffix UniWorks
    mv UniWorks.$suffix.sym UniWorks.sym
    mv usrConfig.$suffix.o usrConfig.o
    done

    for dname in dy4 hkv2f ms100 mv133 mv147 mz7122 sun3e frc21 iv24 mv131 mv135 mz7120 mz8115 ubar
    do
    cd $dest/unsupported/config/$dname
    mv Makefile.$suffix Makefile
    mv UniWorks.$suffix UniWorks
    mv UniWorks.$suffix.sym UniWorks.sym
    mv usrConfig.$suffix.o usrConfig.o
    done
fi

fi

echo Rewinding tape...
mt -f $tape rew
echo Done.
