#!/bin/sh
#

#
# starting script for Simutrans
#

# PAKVERSION has to be empty for manual selection
PAKVERSION=`basename $0 | sed s/simutrans099//g`
GAMEDIR="/usr/share/games/simutrans099"
SIMUTRANS_DIR="$HOME/.simutrans"
SIMUEXE="simutrans"
# Add mandantory flags below
#MAND_FLAGS="-nomidi -nosound" # FOR 99.08
MAND_FLAGS=" "

if [ x$1 = "x-beta" ] ; then
	SIMUEXE="simutrans-beta"
fi

if [ ! -d $GAMEDIR ] ; then
	echo "Could not find game directory $GAMEDIR"
	exit 1
fi

if [ ! -d $GAMEDIR/pak$PAKVERSION ] ; then
	echo "Could not find valid PAK-set"
	exit 1
fi

if [ ! -d $SIMUTRANS_DIR ] ; then
	if [ -d $HOME/simutrans ] ; then
		echo "Moving old simutrans-userdir to new $SIMUTRANS_DIR"
		mv $HOME/simutrans $SIMUTRANS_DIR
		if [ $? != 0 ] ; then
			echo "Failed to move $HOME/simutrans to $SIMUTRANS_DIR"
			exit 1
		fi
	else
		echo "Creating Simutrans directory $SIMUTRANS_DIR"
		mkdir $SIMUTRANS_DIR
		if [ $? != 0 ] ; then
			echo "Failed to create directory $SIMUTRANS_DIR"
			exit 1
		fi
	fi
fi

# simutab.conf

if [ ! -f $SIMUTRANS_DIR/simuconf.tab ] ; then
	touch $SIMUTRANS_DIR/simuconf.tab && echo "Created empty $SIMUTRANS_DIR/simuconf.tab"	
else
	echo "Leaving your $SIMUTRANS_DIR/simuconf.tab untouched!"
fi

# simworld.conf

if [ ! -f $SIMUTRANS_DIR/simworld.cfg ] ; then
	cp $GAMEDIR/simworld.cfg.DONTUSE $SIMUTRANS_DIR/simworld.cfg
fi



if [ ! -d $SIMUTRANS_DIR/save ] ; then
	echo "Creating save-game directory $SIMUTRANS_DIR/save"
	mkdir $SIMUTRANS_DIR/save
	if [ $? != 0 ] ; then
		echo "Failed to create save-game directory $SIMUTRANS_DIR/save"
		exit 1
	fi
fi


# game has to be started in install-dir!
echo "Using Simutrans with pak version: $PAKVERSION"
$GAMEDIR/$SIMUEXE $MAND_FLAGS -objects pak$PAKVERSION/ $*
#$GAMEDIR/$SIMUEXE $MAND_FLAGS $*

exit $!

