#!/bin/sh
#
# Unreal Technology updater startup script
#  script last modified: October 11th, 2002 by Ryan C. Gordon.

PRODUCT=ut2003
AUTOPATCH=$HOME/.ut2003/System/AutoPatch.txt

# 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 if not already set.
if [ "${UPDATER_PATH}" = "" ]; then
    UPDATER_PATH="`FindPath $0`"
fi

# Let's boogie!
if [ -x "${UPDATER_PATH}/loki_update" ]; then
	LD_LIBRARY_PATH=.:${UPDATER_PATH}:${LD_LIBRARY_PATH}
	export LD_LIBRARY_PATH

	cd "${UPDATER_PATH}/"

    if [ -f $AUTOPATCH ]; then
		for feh in `xargs < $AUTOPATCH`
        do
            echo "updating from $feh ..."
            ./loki_update --noselfcheck $PRODUCT --update_url "$feh"
            if [ $? == 0 ]; then
                rm -f $AUTOPATCH
                exit 0
            fi
        done
	else
		exec "./loki_update" --noselfcheck $PRODUCT $*
	fi
else
    echo "Couldn't run autoupdater (loki_update). Is UPDATER_PATH set?"
    exit 1
fi

exit 0

# end of update ...

