#!/bin/bash
set -e

# this script was badly mangled from the original DS install script... 
# don't blame me if it eats your computer ;) 
# I haven't done much bash scripting.

#
# If you're looking at this script to see what to do, type
# 	./edynn-install --help
# to find out the parameters you can use.
#
# The Docking Station web site is at
# 	http://ds.creatures.net
#
# Otherwise, you must be looking at this script to see how
# it works.  If you have improvements, please send patches 
# or suggestions to francis.irving@creaturelabs.com.

# Some distributions have this set to a higher security value,
# which would mean users can't read the files that root generates.
umask 022

DEFAULT_INSTALL_DEST=/usr/local/games/edynn
DEFAULT_DS_MAIN=/usr/local/games/dockingstation
DEFAULT_BIN_DEST=/usr/local/bin
EXECUTE_NAME=edynn
BUILD_FILE=BuildNumber.txt

REMOTE_BUILD_FILE=BuildNumber.txt
BUILD_DESC=Latest

# 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 awk/ls magic portable?
    if [ -L "$fullpath" ]; then
        fullpath="`ls -l "$fullpath" | awk '{print $11}'`"
    fi
    dirname $fullpath
}

# If launched from a symlink, we know our destination path
[ -L "$0" ] && INSTALL_DEST=`FindPath $0`
[ -L "$0" ] && BIN_DEST=`dirname $0`
# Use default values if the user hasn't set them
[ -z "$INSTALL_DEST" ] && INSTALL_DEST=$DEFAULT_INSTALL_DEST
[ -z "$BIN_DEST" ] && BIN_DEST=$DEFAULT_BIN_DEST
[ -z "$DS_MAIN" ] && DS_MAIN=$DEFAULT_DS_MAIN

# Check install and bin dest don't have trailing slash
INSTALL_DEST=`echo "$INSTALL_DEST" | sed "s/\/\$//"`
BIN_DEST=`echo "$BIN_DEST" | sed "s/\/\$//"`

# Play hunt Creatures 3
if [ -z "$C3_MAIN" -o ! -d "$C3_MAIN" ]
then
	# Look for a symlink executable
	if type "creatures3" 2>&1 >/dev/null
	then
		C3_BIN=`type -p creatures3`
		C3_MAIN=`FindPath "$C3_BIN"`
	fi
fi
if [ -z "$C3_MAIN" -o ! -d "$C3_MAIN" ]	
then
	# Guess it is in the obvious place
	C3_MAIN=/usr/local/games/creatures3
fi

# Recursive checks
if [ -z "$RECURSIVE" ] 
then
	# Put a blank line (if we haven't run ourself)
	trap echo EXIT
else
	# If root, remove old signal
	rm -f /tmp/EDYNN_installer_su_ok
fi

# Are we installing off CD or tarball?
if [ -z "$CD_PATH" -a -e "`FindPath $0`/cdtastic" ]
then
	CD_PATH="`FindPath $0`"
	# Make absolute
	if ! echo "$CD_PATH" | egrep "^/" >/dev/null
	then
		CD_PATH="`pwd`/$CD_PATH"
	fi
	# Strip slash
	CD_PATH=`echo "$CD_PATH" | sed "s/\/\$//"`
fi

# Put a blank line when script ends
trap interrupted SIGINT

# Remember what we are, so we can relaunch ourself
SELF=$0
SELF_PARAMETERS=$@
BASH_SWITCH=""

function interrupted()
{
	echo 
	echo "Script interrupted!"
	echo
	echo "You can resume the installation by running the script"
	echo "again, or by typing 'edynn' if the installation"
	echo "has got far enough."

	exit 1
}

# Cope with Slackware, which doesn't have mktemp
function make_temp_file()
{
	if type mktemp 2>/dev/null >/dev/null
	then
		TEMP_FILE=`mktemp /tmp/$BUILD_FILE_GLOBAL.XXXXXX`
	else
		TEMP_FILE=/tmp/$BUILD_FILE.$$
		touch $TEMP_FILE
	fi
}


# $1 - Path to write to
function check_writeable()
{
	if [ ! -d "$1" ]
	then
		echo "Directory $1 doesn't exist, or is a file."
		echo "You could try making it and running the script again."
		exit 1
	fi
	if [ ! -w "$1" ]
	then
		echo "You do not have write access to $1"
		if [ "$DEFAULT_INSTALL_DEST" = "$INSTALL_DEST" ]
		then
			echo
			echo "(You can install to your home directory by setting"
			echo "INSTALL_DEST to a path before running this script."
			echo "e.g. INSTALL_DEST=~/DockingStation BIN_DEST=~/bin $0)"
			echo
		fi
		echo "Rerunning installation script as root"

		# Terrible hack to pass info back from SU.  We just
		# need the return code of the script run as SU, but
		# SU doesn't return it.
		if ! su --shell=/bin/bash -c "RECURSIVE=true CD_PATH=\"$CD_PATH\" sh $BASH_SWITCH \"$SELF\" $SELF_PARAMETERS"
		then
			# Try again with a vanilla version of su
			echo "Failed to run \"su\" with --shell.  This could be because"
			echo "you got the password wrong, or it could be because su"
			echo "does not support the --shell option on your distribution."
			echo
			echo "Trying again without --shell.  If you are using a "
			echo "shell other than bash as your standard shell then "
			echo "you may have problems.  If so, either upgrade to "
			echo "a more recent version of \"su\" or change root's shell"
			echo "to be bash."
			echo 
			if ! su -c "RECURSIVE=true CD_PATH=\"$CD_PATH\" sh $BASH_SWITCH \"$SELF\" $SELF_PARAMETERS"
			then
				echo
				echo "Failed to launch \"su\" for some reason.  Try running \"su\" "
				echo "first to become root before starting the installation."
				exit 1
			fi
	   	fi
		
		if [ -e /tmp/EDYNN_installer_su_ok ]
		then
			# Run self as normal user to carry on with installation
			echo "Rerunning self as normal user"
			env RECURSIVE= CD_PATH="$CD_PATH" sh $BASH_SWITCH "$SELF" $SELF_PARAMETERS
			exit $?
		else
			exit 1
		fi
	fi
	
}

function update()
{
	echo
	echo " Edynn InstallBlast"
	echo "-------------------"
	echo
	
	[ ! -z "$CD_PATH" ] && echo -e "Installing off filesystem $CD_PATH\n"

	if [ -e "$INSTALL_DEST" ]
	then
		echo "Checking for updates..."
		echo 
	fi
	update_port
	make_bin_link
#	echo
#	update_global
	echo

#	if [ ! -z "$CD_PATH" -a -z "$RECURSIVE" ]
#	then
#		echo -ne "Rerunning to check for "
#		echo -ne '\033]0;\w\007\033[32mnetwork updates\033[0m'
#		echo
#		cd $INSTALL_DEST
#		env RECURSIVE= CD_PATH= sh $BASH_SWITCH dstation-install $SELF_PARAMETERS
#		exit $?
#	fi
}


function update_port()
{
	# Find current port build number
	if [ -e "$INSTALL_DEST/$BUILD_FILE" ]
	then
		CURRENT_BUILD=`cat $INSTALL_DEST/$BUILD_FILE | tr -d "\r"`
	else
		CURRENT_BUILD=""
	fi

	# Get latest global build number from server
	make_temp_file
	cp $CD_PATH/$REMOTE_BUILD_FILE $TEMP_FILE
#	cp $REMOTE_BUILD_FILE $TEMP_FILE
	LATEST_BUILD=`cat $TEMP_FILE | tr -d "\r"`
	rm -f $TEMP_FILE
	echo Current build: $CURRENT_BUILD
	echo $BUILD_DESC build: $LATEST_BUILD

	if [ "$CURRENT_BUILD" = "$LATEST_BUILD" ] 
	then
		echo Nothing to update
		if [ $MAKE_LINKS ]
		then
		#don't update, but make links
			check_writeable "$INSTALL_DEST"
			cd "$INSTALL_DEST"
			get_ds_stuff
		fi
		return
	fi
	echo Updating to latest version

#	OUR_SUM=`md5sum -b "$SELF"`

	# Make the directory, and check we can write to it
	check_writeable `dirname "$INSTALL_DEST"`

	# We are root now, and the script won't be relaunched
	# before the directory is made, so if it is not there
	# we do a license prompt.
#	[ ! -e "$INSTALL_DEST" ] && show_license

	# Make the directory, and check it is writeable
	mkdir -p "$INSTALL_DEST"
	check_writeable "$INSTALL_DEST"
	cd "$INSTALL_DEST"

	# Mark that our build is in an intermediate state
	echo > $INSTALL_DEST/$BUILD_FILE

	echo Updating to latest version
#	download_file_list "ports/$LATEST_BUILD_PORT/" file_list_$SYS.txt .
#just copy the whole thing over
	cp -r $CD_PATH/* $INSTALL_DEST
#now get DS files
	get_ds_stuff
#if we're updating, copy new journal files
#really we should do this for every user, but eh.
	if [ -d "$HOME/.edynn/Journal" ]; then
		cp $INSTALL_DEST/Journal/* $HOME/.edynn/Journal/
	fi
#convert
	echo Converting images to local format
	./imageconvert Images/* Backgrounds/*
	echo $LATEST_BUILD > $INSTALL_DEST/$BUILD_FILE
	echo Finished update

}

function get_ds_stuff() 
{
	if [ ! -d "$DS_MAIN" ]
	#no ds?? tell user.
	then
		echo "Meep! DS Doesn't seem to be installed."
		echo If it is, try setting DS_MAIN to the path it was installed to.
		echo "Edynn needs certain files from DS, or it won't work."
		exit 1
	fi
#hardlink the engine files
	echo Linking to DS engine files...
	for i in imageconvert lc2e lc2e-netbabel.so lc2elib.so libSDL-1.2.so.0 libSDLStretch.so libSDL_mixer-1.2.so.0 libstdc++-libc6.1-2.so.3; do
		[ -e "$INSTALL_DEST/$i" ] || ln "$DS_MAIN/$i" $INSTALL_DEST
	done

}

function make_bin_link()
{
	# If an existing symbolic link, check it goes to the same place
	if [ -L "$BIN_DEST/$EXECUTE_NAME" ]
	then
        LINK_NAME="`ls -l "$BIN_DEST/$EXECUTE_NAME" | awk '{print $11}'`"
		if [ "$LINK_NAME" != "$INSTALL_DEST/edynn-install" ]
		then
			echo
			echo "File $BIN_DEST/$EXECUTE_NAME is already a symbolic"
			echo "link to a different place.  The installer is trying"
			echo "to make a link to $INSTALL_DEST/edynn-install."
			echo "The current link is:"
			echo
			ls -l $BIN_DEST/$EXECUTE_NAME
			echo
			echo "Either delete this link, or install the link to"
			echo "another directory by setting BIN_DEST to a path"
			echo "before running this script."
			echo
			echo "e.g. INSTALL_DEST=~/DockingStation BIN_DEST=~/bin $0"
			echo
			exit 1
		fi

		# Link is fine, so return
		return
	fi

	# If a file is in the way, complain
	if [ -e "$BIN_DEST/$EXECUTE_NAME" ]
	then
		echo
		echo "File $BIN_DEST/$EXECUTE_NAME is in the way."
		ls -l $BIN_DEST/$EXECUTE_NAME
		echo
		echo "(Install the link to another directory by setting"
		echo "BIN_DEST to a path before running this script."
		echo "e.g. INSTALL_DEST=~/DockingStation BIN_DEST=~/bin $0)"
		echo
		exit 1
	fi

	# Nothing there, so make our link
	echo
	echo Making \'edynn\' executable link...
	check_writeable "$BIN_DEST"
	ln -s "$INSTALL_DEST/edynn-install" "$BIN_DEST/$EXECUTE_NAME"
}

function uninstall()
{
	echo
	echo "Edynn uninstaller"
	echo "---------------------------"

	if [ ! -e "$INSTALL_DEST" ]
	then
		echo
		echo Edynn is not installed at $INSTALL_DEST
		exit 1
	fi

	check_writeable `dirname "$INSTALL_DEST"`
	check_writeable "$INSTALL_DEST"
	check_writeable "$BIN_DEST"

	# Mark that our build is in an intermediate state
	echo > $INSTALL_DEST/$BUILD_FILE

	cd "$INSTALL_DEST"
	#let's just destroy everything...
	rm -rf *
#	rm -f wget-log
#	uninstall_file_list file_list.txt .
#	rm -f "$BUILD_FILE_GLOBAL"
#	uninstall_file_list file_list_$SYS.txt .
#	rm -f "$BUILD_FILE_PORT"
	cd ~
	rmdir --ignore-fail-on-non-empty "$INSTALL_DEST"

	if [ -L "$BIN_DEST/$EXECUTE_NAME" ]
	then
		echo "Removing symbolic link $BIN_DEST/$EXECUTE_NAME"
		rm "$BIN_DEST/$EXECUTE_NAME"
	fi	

	echo Finished uninstallation. You may want to remove ~/.edynn
}

function user_launch()
{
	if [ -z "$REALLY_RUN_AS_ROOT" ]
	then
		if whoami | egrep "^root$" > /dev/null
		then
			# Break out of being root, by recursing
			if [ ! -z "$RECURSIVE" ] 
			then
				# Search above for explanation of this file
				touch /tmp/dstation_installer_su_ok
				exit 0
			fi
			echo "WARNING: You are trying to run the game as root."
			echo "This is probably not advisable.  Log in as a normal"
			echo "user and type 'dockingstation' to try again, or"
			echo "set the environment variable REALLY_RUN_AS_ROOT"
			echo "if you really want to."
			echo
			exit 1
		fi
	fi

	EDYNN_HOME=$HOME/.edynn
	EDYNN_MAIN=$INSTALL_DEST

	# Make sure all the directories are there
	if [ ! -d "$EDYNN_HOME" ]; then
		echo "Making home directory $EDYNN_HOME"

		# Make all directories
		mkdir "$EDYNN_HOME"
		cd "$EDYNN_HOME"
		for X in Backgrounds Body\ Data Bootstrap Catalogue Creature\ Galleries Genetics Images Journal My\ Agents My\ Creatures My\ Worlds Overlay\ Data Sounds Users; do
			mkdir -p "$X"
		done

		#copy over edynn's journal files
		cp $EDYNN_MAIN/Journal/* $EDYNN_HOME/Journal/
	fi

	cd "$EDYNN_HOME"

# See if Creatures 3 is there
	if [ -d "$C3_MAIN" ]
	then
		echo "Creatures 3 found at $C3_MAIN"
		C3_INSTALLED=true
	else
		echo ""
		echo "Creatures 3 is not installed (you can buy it, or you can"
		echo "buy Creatures Internet Edition, which has Docking Station"
		echo "and the Magma Norns bundled with it, see the web site"
		echo "http://ds.creatures.net for more info)"
		echo ""
		C3_INSTALLED=
	fi
	# See if we need to update machine.cfg
	# (in case C3 has been installed or uninstalled)
	if [ -e "machine.cfg" ]
	then
		# Check if current config file refers to C3
		if grep "$C3_MAIN" machine.cfg >/dev/null
		then
			if [ -z "$C3_INSTALLED" ]
			then
				echo "Creatures 3 has been removed since last launch"
				rm machine.cfg
			fi
		else
			if [ ! -z "$C3_INSTALLED" ]
			then
				echo "Creatures 3 has been installed since last launch"
				rm machine.cfg
			fi
		fi
	fi

#check for DS
	if [ -d $DS_MAIN ]
	then
		echo "DS found at $DS_MAIN"
		DS_INSTALLED=true
	else
		echo "DS was not found. I hope you're not in the middle of installing Edynn..."
		DS_INSTALLED=
	fi
	#see if config needs changing for DS
	if [ -e "machine.cfg" ]
	then
		if grep "$DS_MAIN" machine.cfg >/dev/null
		then
		#can find ds in machine.cfg
			if [ -z "$DS_INSTALLED" ]
			then
				echo "DS removed since last launch"
				rm machine.cfg
			fi
		else
			if [ ! -z "$DS_INSTALLED" ]
			then
				echo "DS installed since last launch"
				rm machine.cfg
			fi
		fi
	fi

# Check for obsolete machine.cfg
	if [ -e "machine.cfg" ]
	then
		if ! grep "Main Auxiliary" machine.cfg >/dev/null
		then
			echo "Out of date machine.cfg - updating"
			rm machine.cfg
		fi
	fi

	# Create config file pointing to correct directories
	if [ ! -e "machine.cfg" ]; then
		echo "Pointing to directories"
		mkdir -p "Everything Dummy"
		echo "\"Game Name\" \"Edynn\"" > machine.cfg
		cat >>machine.cfg <<END
"Backgrounds Directory" "$EDYNN_MAIN/Backgrounds/"
"Body Data Directory" "$EDYNN_MAIN/Body Data/"
"Bootstrap Directory" "$EDYNN_MAIN/Bootstrap/"
"Catalogue Directory" "$EDYNN_MAIN/Catalogue/"
"Creature Database Directory" "$EDYNN_MAIN/Creature Galleries/"
"Exported Creatures Directory" "$EDYNN_MAIN/My Creatures/"
"Genetics Directory" "$EDYNN_MAIN/Genetics/"
"Images Directory" "$EDYNN_MAIN/Images/"
"Journal Directory" "$EDYNN_MAIN/Journal/"
"Main Directory" "$EDYNN_MAIN/"
"Overlay Data Directory" "$EDYNN_MAIN/Overlay Data/"
"Resource Files Directory" "$EDYNN_MAIN/My Agents/"
"Sounds Directory" "$EDYNN_MAIN/Sounds/"
"Users Directory" "$EDYNN_MAIN/Users/"
"Worlds Directory" "$EDYNN_MAIN/My Worlds/"
END
	#duplicate entries to override DS (ugh)
		cat >>machine.cfg <<END
"Auxiliary 3 Backgrounds Directory" "$EDYNN_MAIN/Backgrounds/"
"Auxiliary 3 Body Data Directory" "$EDYNN_MAIN/Body Data/"
"Auxiliary 3 Bootstrap Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 3 Catalogue Directory" "$EDYNN_MAIN/Catalogue/"
"Auxiliary 3 Creature Database Directory" "$EDYNN_MAIN/Creature Galleries/"
"Auxiliary 3 Exported Creatures Directory" "$EDYNN_MAIN/My Creatures/"
"Auxiliary 3 Genetics Directory" "$EDYNN_MAIN/Genetics/"
"Auxiliary 3 Images Directory" "$EDYNN_MAIN/Images/"
"Auxiliary 3 Journal Directory" "$EDYNN_MAIN/Journal/"
"Auxiliary 3 Main Directory" "$EDYNN_MAIN/"
"Auxiliary 3 Overlay Data Directory" "$EDYNN_MAIN/Overlay Data/"
"Auxiliary 3 Resource Files Directory" "$EDYNN_MAIN/My Agents/"
"Auxiliary 3 Sounds Directory" "$EDYNN_MAIN/Sounds/"
"Auxiliary 3 Users Directory" "$EDYNN_MAIN/Users/"
"Auxiliary 3 Worlds Directory" "$EDYNN_MAIN/My Worlds/"
END
		cat >>machine.cfg <<END
"Auxiliary 4 Backgrounds Directory" "$EDYNN_HOME/Backgrounds/"
"Auxiliary 4 Body Data Directory" "$EDYNN_HOME/Body Data/"
"Auxiliary 4 Bootstrap Directory" "$EDYNN_HOME/Bootstrap/"
"Auxiliary 4 Catalogue Directory" "$EDYNN_HOME/Catalogue/"
"Auxiliary 4 Creature Database Directory" "$EDYNN_HOME/Creature Galleries/"
"Auxiliary 4 Exported Creatures Directory" "$EDYNN_HOME/My Creatures/"
"Auxiliary 4 Genetics Directory" "$EDYNN_HOME/Genetics/"
"Auxiliary 4 Images Directory" "$EDYNN_HOME/Images/"
"Auxiliary 4 Journal Directory" "$EDYNN_HOME/Journal/"
"Auxiliary 4 Main Directory" "$EDYNN_HOME/"
"Auxiliary 4 Overlay Data Directory" "$EDYNN_HOME/Overlay Data/"
"Auxiliary 4 Resource Files Directory" "$EDYNN_HOME/My Agents/"
"Auxiliary 4 Sounds Directory" "$EDYNN_HOME/Sounds/"
"Auxiliary 4 Users Directory" "$EDYNN_HOME/Users/"
"Auxiliary 4 Worlds Directory" "$EDYNN_HOME/My Worlds/"
"Main Auxiliary" 4
END
		if [ ! -z "$C3_INSTALLED" ]
		then
			echo "Creatures 3 is installed, connecting to it"
			cat >>machine.cfg <<END
"Auxiliary 1 Backgrounds Directory" "$C3_MAIN/Backgrounds/"
"Auxiliary 1 Body Data Directory" "$C3_MAIN/Body Data/"
"Auxiliary 1 Bootstrap Directory" "$C3_MAIN/Bootstrap/"
"Auxiliary 1 Catalogue Directory" "$C3_MAIN/Catalogue/"
"Auxiliary 1 Creature Database Directory" "$C3_MAIN/Creature Galleries/"
"Auxiliary 1 Exported Creatures Directory" "$C3_MAIN/My Creatures/"
"Auxiliary 1 Genetics Directory" "$C3_MAIN/Genetics/"
"Auxiliary 1 Images Directory" "$C3_MAIN/Images/"
"Auxiliary 1 Journal Directory" "$C3_MAIN/Journal/"
"Auxiliary 1 Main Directory" "$C3_MAIN/"
"Auxiliary 1 Overlay Data Directory" "$C3_MAIN/Overlay Data/"
"Auxiliary 1 Resource Files Directory" "$C3_MAIN/My Agents/"
"Auxiliary 1 Sounds Directory" "$C3_MAIN/Sounds/"
"Auxiliary 1 Users Directory" "$C3_MAIN/Users/"
"Auxiliary 1 Worlds Directory" "$C3_MAIN/My Worlds/"
END
		else
			echo "Creatures 3 is not installed"
			# To keep auxiliary directory numbers consistent,
			# we have to point Aux 1 to dummy paths when C3
			# isn't present. C3 _has_ to be Aux 1, as CAOS
			# code in the DS World Switcher refers to it as that.
			cat >>machine.cfg <<END
"Auxiliary 1 Backgrounds Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Body Data Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Bootstrap Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Catalogue Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Creature Database Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Exported Creatures Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Genetics Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Images Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Journal Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Main Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Overlay Data Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Resource Files Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Sounds Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Users Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 1 Worlds Directory" "$EDYNN_HOME/Everything Dummy"
END
		fi
	#check for DS
	#thanks to auxiliary bugs, need to use dummy for bootstrap
		if [ ! -z "$DS_INSTALLED" ]
		then
			echo "DS installed, connecting to it"
			cat >>machine.cfg <<END
"Auxiliary 2 Backgrounds Directory" "$DS_MAIN/Backgrounds/"
"Auxiliary 2 Body Data Directory" "$DS_MAIN/Body Data/"
"Auxiliary 2 Bootstrap Directory" "$EDYNN_HOME/Everything Dummy"
"Auxiliary 2 Catalogue Directory" "$DS_MAIN/Catalogue/"
"Auxiliary 2 Creature Database Directory" "$DS_MAIN/Creature Galleries/"
"Auxiliary 2 Exported Creatures Directory" "$DS_MAIN/My Creatures/"
"Auxiliary 2 Genetics Directory" "$DS_MAIN/Genetics/"
"Auxiliary 2 Images Directory" "$DS_MAIN/Images/"
"Auxiliary 2 Journal Directory" "$DS_MAIN/Journal/"
"Auxiliary 2 Main Directory" "$DS_MAIN/"
"Auxiliary 2 Overlay Data Directory" "$DS_MAIN/Overlay Data/"
"Auxiliary 2 Resource Files Directory" "$DS_MAIN/My Agents/"
"Auxiliary 2 Sounds Directory" "$DS_MAIN/Sounds/"
"Auxiliary 2 Users Directory" "$DS_MAIN/Users/"
"Auxiliary 2 Worlds Directory" "$DS_MAIN/My Worlds/"
END
		fi
	fi

# Language config removed- edynn only has english
	if [ ! -e "language.cfg" ]; then
		echo "Language en-GB" > language.cfg
		echo "LanguageCLibrary english-uk" >> language.cfg
	fi

	# User config file
	if [ ! -e "user.cfg" ]; then
		cat "$EDYNN_MAIN/user.cfg" | tr -d "\r" | sed s/EDYNN_/EDYNN_/ > user.cfg
		echo "Icon \"$EDYNN_MAIN/edynn.bmp\"" >> user.cfg
	fi

	# World switcher bootstrap needs to be in main directory
	if [ ! -e "$EDYNN_HOME/Bootstrap/000 Switcher" ]; then
		ln -s "$EDYNN_MAIN/Bootstrap/000 Switcher" $EDYNN_HOME/Bootstrap
	fi
	if [ ! -e "$EDYNN_HOME/lc2e-netbabel.so" ]; then
		ln -s "$EDYNN_MAIN/lc2e-netbabel.so" $EDYNN_HOME/lc2e-netbabel.so
	fi

	# Tell them to type "dockingstation" next time
	if echo "$SELF" | grep edynn-install >/dev/null
	then
		echo
		echo -n "You can type "
		echo -ne '\033]0;\w\007\033[32medynn\033[0m'
		echo " to run the game from"
		echo "now on.  The original edynn-install file can be"
		echo "deleted, as dockingstation will also check for updates."
		echo
	fi

	# Launch the game
	echo "Welcome to Edynn!"
	cd "$EDYNN_HOME"
	export LD_LIBRARY_PATH="$EDYNN_MAIN:$LD_LIBRARY_PATH"
	"$EDYNN_MAIN"/lc2e #--autokill
}

# Check command line parameteres
for PARAM in $@
do
	if [ "$PARAM" = "uninstall" ]
	then
		uninstall
		exit 0
	elif [ "$PARAM" = "lang" ]
	then
		echo "Forcing language selection for $USER"
		rm -f ~/.edynn/language.cfg
	elif [ "$PARAM" = "cleanse" ] 
	then
		echo "Forcing check of installed files"
		check_writeable "$INSTALL_DEST"
		echo > $INSTALL_DEST/$BUILD_FILE_GLOBAL
		echo > $INSTALL_DEST/$BUILD_FILE_PORT
	elif [ "$PARAM" = "nocheck" ]
	then
		NO_CHECK=true
	elif [ "$PARAM" = "nolaunch" ]
	then
		NO_LAUNCH=true
	elif [ "$PARAM" = "link" ]
	then
		MAKE_LINKS=true
	elif [ "$PARAM" != "" ]
	then
		echo
		echo "Edynn InstallBlast"
		echo "----------------------------"
		echo
		echo To download and run the game, simply run this script in an
		echo X terminal with no parameters.  You will be prompted for 
		echo the root password if necessary, so run the script as an
		echo ordinary user.
		echo 	
		echo When the download is complete the game will automatically
		echo launch.  Next time, type \'edynn\' to check for
		echo updates and launch the game.
		echo
		echo Parameters are:
		echo "  link - Links to DS files if they're not already linked"
		echo "(useful for a cvs install)"
		echo "  lang - Change the language that you play the game in"
		echo "  cleanse - Forces a rescan of files to fix broken builds"
		echo "  nocheck - Do not automatically check for game updates"
		echo "  nolaunch - Do not launch the game when finished"
		echo "  uninstall - Uninstalls the game"
		echo
		echo Environment variables are:
		echo "  INSTALL_DEST - Directory to install game in"
		echo "  BIN_DEST - Directory to install executable link in"
		echo "  C3_MAIN - Where Creatures 3 is (use if autodetect fails)"
		echo "  DS_MAIN - Directory to get the DS engine n'stuff from"
		echo 
		echo Note: After setting INSTALL_DEST and BIN_DEST for the initial
		echo "install you shouldn\'t need to set them again.  You should"
		echo launch the game by typing \$BIN_DEST/dockingstation or putting
		echo \$BIN_DEST on the path and typing dockingstation.  The script
		echo will find INSTALL_DEST automatically.
		echo 
		echo "Blaa!"
		exit 1
	fi
done

[ -z "$NO_CHECK" ] && update
[ -z "$NO_LAUNCH" ] && user_launch

exit 0

