#!/bin/sh
#
#  Startup script
#

RUN_UPDATER=false

# Function to find the real directory a program resides in.
# Feb. 17, 2000 - Sam Lantinga, Loki Entertainment Software
FindPath()
{
    fullpath="`echo $1 | grep /`"
    if [ "$fullpath" = "" ]; then
        oIFS="$IFS"
        IFS=:
        for path in $PATH
        do if [ -x "$path/$1" ]; then
               if [ "$path" = "" ]; then
                   path="."
               fi
               fullpath="$path/$1"
               break
           fi
        done
        IFS="$oIFS"
    fi
    if [ "$fullpath" = "" ]; then
        fullpath="$1"
    fi

    # Is the sed/ls magic portable?
    if [ -L "$fullpath" ]; then
        #fullpath="`ls -l "$fullpath" | awk '{print $11}'`"
        fullpath=`ls -l "$fullpath" |sed -e 's/.* -> //' |sed -e 's/\*//'`
    fi
    dirname $fullpath
}

# Set the home path if not already set.
if [ "${GAME_HOME_PATH}" = "" ]; then
    GAME_HOME_PATH="`FindPath $0`"
fi

# first run the updater if it is there

if [ "$RUN_UPDATER" = "true" ] && [ ! -e "${GAME_HOME_PATH}/noupdate" ] && [ -x "${GAME_HOME_PATH}/update" ]
then
    ${GAME_HOME_PATH}/update --auto 
fi

LD_LIBRARY_PATH=.:${GAME_HOME_PATH}/lib:${LD_LIBRARY_PATH}

export LD_LIBRARY_PATH

EXENAME=netpanzer.bin

# Let's boogie!
if [ -x "${GAME_HOME_PATH}/$EXENAME" ]
then
	cd "${GAME_HOME_PATH}/"
	exec "./$EXENAME" $*
fi
echo "Couldn't run $EXENAME. Is GAME_HOME_PATH correct?"
echo "GAME_HOME_PATH: " $GAME_HOME_PATH
exit 1
