#!/bin/sh

# this script is for Linux!

# 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
}

if [ "${DREDMOR_DATA_PATH}" = "" ]; then
    DREDMOR_DATA_PATH="`FindPath $0`"
fi

echo "Dredmor: Installed in '$DREDMOR_DATA_PATH'."
cd "$DREDMOR_DATA_PATH"

if [ "${DREDMOR_ARCH}" = "" ]; then
    DREDMOR_ARCH=`uname -m`
fi

if [ "${DREDMOR_ARCH}" = "x86_64" ]; then
    echo "Dredmor: Using amd64 version."
    LD_LIBRARY_PATH=./amd64:$LD_LIBRARY_PATH exec ./Dredmor-amd64 "$@"
else
    echo "Dredmor: Using x86 version."
    LD_LIBRARY_PATH=./x86:$LD_LIBRARY_PATH exec ./Dredmor-x86 "$@"
fi

