#!/bin/bash
###############################################################################
#
# AstroMenace Startup Script
#
# Copyright (C) 2004-2006  Andreas Schneider <gladiac@liflg.org>
#                          Steffen Pankratz  <kratz00@liflg.org>
#                          Ronny Dressler    <kingron@liflg.org>
# Copyright (C) 2007  Michael Kurinnoy, Viewizard
#
# This script is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
############################################################################### 



# The game binary
GAME_BINARY="AstroMenace"
GAME_BINARY_32="AstroMenace"
GAME_BINARY_64="AstroMenace64"

# Subdirectory
SUBDIR="."

# Library directory
LIBDIR="lib32"
LIBDIR_32="lib32"
LIBDIR_64="lib64"

# Additional commandline options
CMD_ARGS=""




###############################################################################
## DO NOT EDIT BELOW THIS LINE
############################################################################### 




# Set correct binary and lib path name, accordingly to arch
case $(uname -m) in
	x86_64 | amd64) 
		GAME_BINARY=$GAME_BINARY_64 LIBDIR=$LIBDIR_64 
	;;
	i?86 | i86*) 
		GAME_BINARY=$GAME_BINARY_32 LIBDIR=$LIBDIR_32
	;;	
	*) 
		echo "Unknown architecture. By default game binary set to <"$GAME_BINARY">, lib dir set to <"$LIBDIR">"
esac



# Do not allow dublicate
if ps -A | grep -e "$GAME_BINARY$" > /dev/null; then
        exit 
fi





SCRIPT="$0"
COUNT=0
while [ -L "${SCRIPT}" ]
do
	SCRIPT=$(readlink ${SCRIPT})
	COUNT=$(expr ${COUNT} + 1)
	if [ ${COUNT} -gt 100 ]
	then
		echo "Too many symbolic links"
		exit 1
	fi
done
GAMEDIR=$(dirname ${SCRIPT})

cd ${GAMEDIR}
cd ${SUBDIR}



# check LD_LIBRARY_PATH
if test ! "$LD_LIBRARY_PATH"; then
	export LD_LIBRARY_PATH="/lib:/usr/lib:"$(cat /etc/ld.so.conf | tr '\n' ':')
fi

# export game library directory
test -n "${LIBDIR}" && export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GAMEDIR}/${LIBDIR}"


# start the game
./${GAME_BINARY} ${CMD_ARGS} "$@"
EXITCODE="$?"


exit ${EXITCODE}
