#!/bin/sh
#
#	This script file is used to install the encrypted compressed
#	tar files for selected Games platforms.
#
#	It is run by the user to decrypt, uncompress, untar the system
#	files.  The default location is specified by the environment variable
#	ALIAS_LOCATION (defaulting to /usr/aw/alias) or that specified on the
#	command line. 
#
usage()
{
	echo "Usage: InstallGame <-hdeaftsk>"
	echo ""
	echo "where: -h          prints this usage info"
	echo "       -e          assume files are not encrypted"
	echo "       -d          decrypt the library file only"
	echo "       -a dir      alias directory location"
	echo "       -f dir      install from this directory"
	echo "       -t dir      install to this directory"
	echo "       -s System   install this System"
	echo ""
	echo "Default with no options on command line is to use the"
	echo "\$ALIAS_LOCATION/ODS/Games/support directory as the source"
	echo "directory and to install back into the ODS/Games directory." 
	echo "Use absolute paths for the from and to directories"
    exit
}

doit()
{
	cd $outDir

    if [ -r $installFrom/$SUBSYSTEM ]
    then

		if [ $encrypted = 1 ]
		then
        	echo "Unpacking game library $SUBSYSTEM"
        	echo " "

        	$glibProgram -P $SUBSYSTEM < $installFrom/$SUBSYSTEM > tmp.tar.Z
			if [ $? != 0 ]
			then
				echo "Error:check your Encryption key entered"
				exit
			fi

		else
			echo "Using game library $SUBSYSTEM as decrypted previously"
			echo ""
			cp $installFrom/$SUBSYSTEM tmp.tar.Z
		fi

		if [ $decryptOnly = 1 ]
		then
			echo "Creating $SUBSYSTEM as decrypted file"
			mv tmp.tar.Z $SUBSYSTEM
		else
        	/usr/bsd/uncompress tmp.tar.Z
			if [ $? != 0 ]
			then
		   		echo "Check that the encryption option specified is correct"
           		echo "for the given game library file"
           		exit
			fi

			if [ -d source ]
			then
        		cd source
			else
				mkdir source
				cd source
			fi

        	/sbin/tar xvoBf ../tmp.tar
			if [ $? != 0 ]
			then
				echo "Error untaring the file, perhaps the encryption option"
				echo "is in the wrong state. Check and try again"
				/bin/rm ../tmp.tar
				exit
			fi

       		/bin/rm ../tmp.tar
		fi

#       for Sega will be 2 files SE and SE.1
#       so lets check and unpack the 2nd one at the same time

        if [ "$SUBSYSTEM" = "SE" ]
        then
        	if [ -r $installFrom/SE.1 ]
            then
                cd $outDir

				if [ $encrypted = 1 ]
				then
                	$glibProgram -P $SUBSYSTEM < $installFrom/SE.1 > tmp.tar.Z
				else
					cp $installFrom/SE.1 tmp.tar.Z
				fi

				if [ $decryptOnly = 1 ]
				then
					mv tmp.tar.Z SE.1
				else
                	/usr/bsd/uncompress tmp.tar.Z
		        	if [ $? != 0 ]
        			then
           				echo "Error uncompressing file, perhaps the encryption option"
           				echo "is in the wrong state. Check and try again"
           				exit
        			fi

                	cd source
                	/sbin/tar xvof ../tmp.tar
        			if [ $? != 0 ]
        			then
            			echo "Error untaring file, perhaps the encryption option"
            			echo "is in the wrong state. Check and try again"
            			/bin/rm ../tmp.tar
            			exit
        			fi

               		/bin/rm ../tmp.tar
				fi
            fi

			if [ $decryptOnly = 1 ]
			then
				return
			fi

            if [ ! -d "Sega" ]
            then
                mkdir Sega
            fi
            cd Sega
        fi
#
#		if we are only decrypting the file then just return
#
		if [ $decryptOnly = 1 ]
		then
			return
		fi
#
#		else need to continue with the unpacking
#
        if [ "$SUBSYSTEM" = "SO" ]
        then
            if [ ! -d "Playstation" ]
            then
                mkdir Playstation
            fi
            cd Playstation
        fi
#
#
        if [ "$SUBSYSTEM" = "N" ]
        then
            if [ ! -d "Nintendo" ]
            then
                mkdir Nintendo
            fi
            cd Nintendo
        fi

        if [ -d bin ]
        then
			chmod a+x bin/*
            cp -r bin ../../
        fi

        if [ -d plugins ]
        then
            cp -r plugins ../../
        fi

        if [ -d Dtplugins ]
        then
            cp -r Dtplugins ../../

			if [ "$SUBSYSTEM" = "SO" ]
        	then
				if [ -d ../../lib ]
				then
					cp Dtplugins/SonyPSX ../../lib
				else
					mkdir ../../lib
					cp Dtplugins/SonyPSX ../../lib
				fi
			fi

        fi

		if [ -d pst ]
		then
			cp -r pst ../..
		fi

        echo "Finished unpacking $SUBSYSTEM"
        echo " "
    else
        echo "Game library '$SUBSYSTEM' does not exist."
        echo "Please check that you have specified a valid code"
        echo "for a game library, and that the spelling/case is correct."
        echo " "
    fi

}


alias_loc="${ALIAS_LOCATION:-/usr/aw/alias}"
#if test ! "$ALIAS_LOCATION"
#then
#  alias_loc=/usr/aw/alias
#else
#  alias_loc="$ALIAS_LOCATION"
#fi

installFrom=""
installTo=""
installSys=""
encrypted=1
decryptOnly=0

while getopts heda:f:t:s: arg
do
    case $arg in
    h)    usage ;;
	e)	  encrypted=0 ;;
	d)    decryptOnly=1 ;;
	a)    alias_loc="$OPTARG";;
    f)    installFrom="$OPTARG";;
    t)    installTo="$OPTARG" ;;
    s)    installSys="$OPTARG" ;;
    \?)
        echo "invalid InstallGame option -$arg" ;;
    esac
done

echo "========================================================"
echo "Alias | Wavefront Game Export Tool 2.0 library installer"
echo "========================================================"
echo ""

echo "Enter your ALIAS_LOCATION (hit enter to use $alias_loc)"
read NEWPATH
if test -n "$NEWPATH"
then
	alias_loc="$NEWPATH"
fi

if test ! "$installFrom"
then
	installFrom="$alias_loc/ODS/Games/support"
fi

if [ -r "$installFrom" ]
then
    cd $installFrom
    glibProgram=$alias_loc/bin/install_glib
	if [ ! -x $glibProgram ]
	then
		echo "Can not find $glibProgram to run"
		exit
	fi
else
    echo "Please enter a complete path to your Alias/Wavefront"
    echo "software environment eg. /usr/aw/alias"
    echo " "
    echo "Enter path:"
    read AWPATH

    alias_loc=${AWPATH}
    installFrom="$alias_loc/ODS/Games/support"
    if [ -r $installFrom ]
    then
        cd $installFrom
        glibProgram=$alias_loc/bin/install_glib
	    if [ ! -x $glibProgram ]
    	then
    	    echo "Can not find $glibProgram to run"
    	    exit
    	fi
    else
        echo "Could not find $installFrom software environment."
        exit -1
    fi
fi

if test ! "$installTo"
then
	installTo="$alias_loc/ODS/Games"
fi

#  See if there is a special place to output the files to:

if [ -w $installTo ]
then
    outDir=$installTo
else
    echo "Can not write to $installTo."
	echo "Check directory permissions or run as root"
    exit
fi


#
# Let's see if the user gave everthing on the command line
# if not then continue with the prompting
#

if test "$installSys"
then
    if [ -r "$installFrom/$installSys" ]
    then
        SUBSYSTEM="$installSys"
		if [ $encrypted = 1 ]
		then
        	if test "$installKey"
        	then
            	ENCRYPT="$installKey"
            	doit
            	exit
        	fi
		else
			doit
			exit
		fi
    fi
fi

#
#	Now Loop around asking for which systems to install
#

while ( /bin/true )
    do

	if [ $encrypted = 1 ]
	then
	    echo " "
    	echo "The following games libraries are enabled by your encryption:"
    	echo " "

    	echo "Code    Description"
    	echo "---------------------------------"
    	$glibProgram -v
        if [ $? != 0 ]
        then
            echo "Error:check your Encryption key installation"
            exit
        fi

	else
		echo " "
		echo "Which of the game libraries would you like to install:"
		echo " "
        echo "Code    Description"
        echo "---------------------------------"
		echo "N       Nintendo"
		echo "SO      Sony Playstation"
		echo "SE      Sega Saturn"
	fi

    echo " "
    echo "Which game libraries would you like to install?"
    echo "Enter the codes for the libraries separated by spaces"
    echo "or enter \"none\" or \"exit\" to exit the installer:"

    read RESPONSE

    echo " " 

    for SUBSYSTEM in $RESPONSE
        do
        if [ "$SUBSYSTEM" = "none" ]
           then
           exit 0
        fi

        if [ "$SUBSYSTEM" = "exit" ]
           then
           exit 0
        fi

		doit

    done
done
