#!/bin/sh

# Copyright (c) 1995 Sun Microsystems, Inc.
# All rights reserved.
# 
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in
# all copies of this software.
# 
# IN NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
# OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF SUN
# MICROSYSTEMS, INC. HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# SUN MICROSYSTEMS, INC. SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THE SOFTWARE PROVIDED
# HEREUNDER IS ON AN "AS IS" BASIS, AND SUN MICROSYSTEMS, INC. HAS NO
# OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
# MODIFICATIONS.

PATH=/usr/bin:$PATH

if [ -f $HOME/.doomarenarc ] ; then
    DAPARENT=`/bin/grep DAPARENT $HOME/.doomarenarc | /bin/tail -1 | \
	/bin/awk '{print $3}'`
    export DAPARENT
fi
if [ -z "$DAPARENT" ] ; then
    DAPARENT=__DAPARENT__
    export DAPARENT
fi

if [ "$1" = -local ] ; then
    DAHOME=$DAPARENT
    shift
else
    if [ -f $HOME/.doomarenarc ] ; then
	DAHOME=`/bin/grep DAHOME $HOME/.doomarenarc | /bin/tail -1 | \
	    /bin/awk '{print $3}'`
    fi
    if [ -z "$DAHOME" ] ; then
        DAHOME=/tmp/doomarena
    fi
fi
export DAHOME

SelectInstallDir() {
    DEFAULT=`/bin/dirname $DAHOME`
    echo "Please specify the directory where Doom Arena should be installed."
    echo "The directory must be writable, must be on the local file system,"
    echo "and must have 40MB of space available."
    echo " "

    while [ 1 ] ; do
	echo "Enter directory [$DEFAULT] "\\c
	read DIR
	if [ $? -ne 0 ] ; then
	    exit
	fi
	if [ -z "$DIR" ] ; then
	    DIR=$DEFAULT/doomarena
	else
	    DIR=$DIR/doomarena
	fi

	if [ -f $DIR/arenasum ] ; then
	    DAHOME=$DIR
	    echo "set DAHOME $DAHOME" >> $HOME/.doomarenarc
	    break
	fi

	/bin/mkdir -p $DIR
	if [ $? -ne 0 ] ; then
	   continue
	fi

	/bin/touch $DIR/permission
	if [ $? -ne 0 ] ; then
	   /bin/rm -f $DIR/permission
	   continue
	fi
	/bin/rm -f $DIR/permission

	SPACE=`/bin/df -bl $DIR`
	if [ $? -ne 0 ] ; then
	   continue
	fi

	SPACE=`echo $SPACE | /bin/tail -1 | /bin/awk '{ print $4 }'`

	if [ $SPACE -lt 40000 ] ; then
	    echo "${SPACE}k available in $DIR.  Need at least 40000k"
	    continue
	fi

	while [ 1 ] ; do
	    echo "Confirm installation in $DIR [y/n] "\\c
	    read CONFIRM
	    if [ $? -ne 0 ] ; then
		exit
	    fi
	    if [ -z "$CONFIRM" ] ; then
	        continue
	    elif [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ] && \
		 [ "$CONFIRM" != "n" ] && [ "$CONFIRM" != "N" ] ; then
		continue
	    else
		break
	    fi
	done
	if [ "$CONFIRM" = "y" ] || [ "$CONFIRM" = "Y" ] ; then
	    DAHOME=$DIR
	    echo "set DAHOME $DAHOME" >> $HOME/.doomarenarc
	    break
	fi
    done    
}

UpdateFiles() {
    sum=`/bin/diff $DAHOME/arenasum $DAPARENT/arenasum`
    if [ "$sum" != "" ] ; then
	echo Updating Doom Arena files...
        FILE=NONE
        STATE=NONE
        for token in $sum ; do
            char=`echo $token | /bin/cut -c1`
            if [ $char = "<" ] ; then
                STATE=REMOVE
            elif [ $char = ">" ] ; then
                STATE=COPY
            elif [ $char = "-" ] ; then
                STATE=NONE
            elif [ $char -lt 1 ] && [ "$char" != "0" ] ; then
                FILE=$token
		if [ $STATE = REMOVE ] ; then
                    echo Remove $FILE
                    /bin/rm -f $DAHOME/$FILE
                elif [ $STATE = COPY ] ; then
                    echo Create $FILE
		    DIR=`/bin/dirname $DAHOME/$FILE`
		    if [ ! -d $DIR ] ; then
			/bin/mkdir -p $DIR
		    fi
		    if [ ! -h $DAPARENT/$FILE ] ; then
		        /bin/cp $DAPARENT/$FILE $DAHOME/$FILE
		    else
		        symlink=`/bin/ls -l $DAPARENT/$FILE | \
			  /bin/awk '{ print $11 }'`
			/bin/ln -s $symlink $DAHOME/$FILE
		    fi
                else
		    echo BAD STATE
		fi
            fi
        done

	/bin/rm -f $DAHOME/arenasum
	/bin/cp $DAPARENT/arenasum $DAHOME/arenasum
    fi
}

if [ ! -f $DAHOME/arenasum ] ; then
    SelectInstallDir $DAHOME
    cd $DAHOME

    if [ ! -f $DAHOME/arenasum ] ; then
	echo Installing Doom Arena in $DAHOME
	echo Installing Tcl/Tk and audio files
	installfiles=`/bin/ls $DAPARENT | /bin/egrep -v 'SCCS|^arenacache|^cleanup|^doom.*wad|^incoming|^server|^src|^wads|^zip'`
	(cd $DAPARENT ; /bin/tar cf - $installfiles) | /bin/tar xf -
	/bin/mkdir -p $DAHOME/wads
	chmod 777 wads
    else
	echo Doom Arena is already installed in $DAHOME
	UpdateFiles
    fi
else
    UpdateFiles
fi

if [ ! -f $DAHOME/doom.wad ] ; then
    if [ -f $DAPARENT/doom.wad ] ; then
	echo Copying doom.wad
	/bin/cp $DAPARENT/doom.wad $DAHOME
    else
	if [ ! -f $DAHOME/doom1.wad ] ; then
	    echo Copying doom1.wad
	    /bin/cp $DAPARENT/doom1.wad $DAHOME
	fi
    fi
fi

if [ ! -f $DAHOME/doom2.wad ] && [ -f $DAPARENT/doom2.wad ] ; then
    echo Copying doom2.wad
    /bin/cp $DAPARENT/doom2.wad $DAHOME
fi

echo Launching Doom Arena
cd $DAHOME

TCL_LIBRARY=$DAHOME/lib/tcl
export TCL_LIBRARY
TK_LIBRARY=$DAHOME/lib/tk
export TK_LIBRARY
LD_LIBRARY_PATH=$DAHOME/lib
export LD_LIBRARY_PATH

exec $DAHOME/bin/wish -file bin/doomarena.tcl "$@"
