#!/bin/bash
# loki_patch-fix
# Copyright (C) 2005 Eskild Hustvedt
# http://sourceforge.net/projects/goldenfiles
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# loki_patch is also licensed under the GNU GPL and here distributed in
# binary form. You can download the source code from
# http://www.icculus.org/loki_setup/
#
# (src/LocateDir)
#-------------------------------------------------------------------------------
# GFSBashLib Copyright (C) Eskild Hustvedt 2005
# LocateDir version 0.1.1
#
# GFSBashLib is licensed under the GNU GPL
# See the included LICENSE file for more information
# ------------------------------------------------------------------------------
#
# When you use this function in a program please keep the above notice
# or include the following one if you don't want it to take so much space:
# "LocateDir function (0.1.1) from GFSBashLib (C) Eskild Hustvedt"
#
# ---
# This file requires dirname and sed to function properly
# 
# Inspired by the FindPath function by Sam Lantinga, Loki Entertainment Software
# - Feb. 17, 2000
#
# It will locate the directory that the argument specified REALLY resides in
# --
LocateDir ()
{
	local TemPath="$*"
	local MyPath="`echo $TemPath |grep /`"
	local XPATH="$PATH:."
	# Find first instance
	if [ "$MyPath" == "" ]; then
		local IFS=":"
		for a in $XPATH; do
			if [ -x $a/$TemPath ]; then
				local MyPath="$a/$TemPath"
				break
			fi
		done
		# Didn't we find it?
		if [ "$MyPath" == "" ]; then
			# Nope just try TemPath then...
			local MyPath="$TemPath"
		fi
	fi
	# Loop checking if the found path is a link
	while [ -L $MyPath ]; do
		local MyPath="`ls -l "$MyPath" |sed -e 's/.* -> //' |sed -e 's/\*//'`"
	done
	# Let dirname find the path
	local DIR="`dirname $MyPath`"
	# If the path is . return $PWD instead of . else return $DIR
	if [ "$DIR" == "." ]; then echo "$PWD" ;else echo "$DIR";fi
}
# (src/cleanup)
# Function to clean up our temporary files and if any extra option is supplied
#  exit with that return value.
cleanup ()
{
	if [ "$EXTRACTTO" != "" ]; then
		if [ -e $EXTRACTTO ]; then
			cd $HOME
			rm -rf $EXTRACTTO || echo "ERROR REMOVING TEMPORARY DIRECTORY ($EXTRACTTO)!"
		fi
	fi
	if [ "$1" != "" ]; then
		if [ "$ENDREAD" == "1" ]; then
			read -n 1 -p "Press enter to quit."
		fi
		exit $1
	fi
}
# (src/PerformFix)
FixFile ()
{
	TARGETNAME="`basename $TARGET`"
	echo -n "Verifying $TARGETNAME..."
	FILETEST="`sh $TARGET --help 2>&1 |grep -- '--target NewDirectory Extract in NewDirectory'`"
	if [ "$FILETEST" == "" ]; then
		echo "failed"
		if [ "$FORCE" == "1" ]; then
			echo "But you forced me to continue anyway"
		else
			fatal_error "$TARGETNAME does not seem to be a loki_patch file.
If you are sure you want to try to apply the fix to this file supply the --force
option to `basename $0`"
		fi
	else
		echo "ok"
	fi
	echo -n "Extracting $TARGETNAME..."
	EXTRACTTO="/tmp/`basename $0`-$RANDOM.$RANDOM.$RANDOM-`whoami`"
	sh $TARGET --keep --target $EXTRACTTO &>/dev/null
	if [ ! -e $EXTRACTTO ]; then
		echo "failed"
		fatal_error "The script did not seem to extract correctly!
Are you sure it is a loki_patch file?
Unable to continue"
	fi
	echo "done"
	echo -n "Patching $TARGETNAME..."
	cd $EXTRACTTO || fatal_error "cd to $EXTRACTTO failed!"
	if [ ! -e ./bin/Linux/x86/loki_patch ]; then
		fatal_error "The file bin/Linux/x86/loki_patch was not found within the archive!
Unable to apply fix."
	fi
	echo -n "."
	rm -f ./bin/Linux/x86/loki_patch
	echo -n "."
	if type loki_patch &>/dev/null; then
		PATCH="`which loki_patch`"
	else
		if [ ! -e "$LPFDIR/fixedpatch" ]; then
			fatal_error "The fixed loki_patch file
$LPFDIR/fixedpatch does not exist! Unable to continue without this file!"
		fi
		PATCH="$LPFDIR/fixedpatch"
	fi
	echo -n "."
	cp "$PATCH" ./bin/Linux/x86/loki_patch
	chmod +x ./bin/Linux/x86/loki_patch
	echo "done"
	case $PERFORMACTION in
		RUN | * ) echo "Starting updated patcher..."
		sh update.sh ;;
	esac
}
# (src/errors)
# Function to display a fatal error and exit
fatal_error ()
{
	echo "Fatal error: $*"
	cleanup 1
}
# (src/help)
display_help ()
{
	echo "Usage: `basename $0` [FILE.RUN]"
	echo " or  : `basename $0` [OPTIONS] [FILE.RUN]"
	echo ""
	echo "Valid options:"
	echo "-h,  --help		Display this help screen"
	echo "-f,  --force		Force the fix"
	echo "--          		Arguments passed after -- will be passed on to the"
	echo "           		update.sh patch script."
	echo ""
	echo "If you only want to install a patch run: `basename $0` file.run"
}
# (src/ver_info)
VER="0.1"
LPTVER="1.0.2"
TARGET=""
LPFDIR="`LocateDir $0`"
EXTRACTTO=""
PERFORMACTION=""
FORCE="0"
echo ""
echo "loki_patch-fix version $VER with Loki Patch Tools version $LPTVER"
echo "loki_patch-fix comes with ABSOLUTELY NO WARRANTY"
echo ""
# (src/loki_patch-fix)
# Trap interrupt 2 (CTRL+C)
trap 'cleanup 0' 2
if [ "$1" == "" ]; then
	display_help
	cleanup 0
fi
while [ "$#" -gt "0" ]; do
	case $1 in
		--help | -h ) display_help; exit 0;;
		--force | -f ) FORCE=1;;
		--endread ) ENDREAD=1;;
		-- ) shift 1; OPTIONS="$*";break;;
		* ) if [ -e $1 ]; then TARGET="$1"; else
		fatal_error "$1 does not exist and is not a valid argument for `basename $0`";fi;;
	esac
	shift 1
done
if [ "$TARGET" == "" ]; then
	echo "The file to fix was not supplied!"
	echo ""
	display_help
	exit 1
fi
FixFile
cleanup 0
