#!/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
}
# (gui/ExecTerm)
#-------------------------------------------------------------------------------
# GFSBashLib Copyright (C) Eskild Hustvedt 2005
# LocateDir ExecTerm version 0.1Test1
#
# 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:
# "ExecTerm (0.1Test1) from GFSBashLib (C) Eskild Hustvedt"
#
# Runs a terminal with the command supplied. Ex:
# Exec_Term bash
#
# Returns 0 on success
# Returns 1 on failure from terminal
# Returns 2 on no terminal detected
ExecTerm ()
{
	local TERMINAL=""
	local XTERMS="rxvt xvt xterm dtterm eterm Eterm kvt konsole aterm"
	local COMMAND="$*"
	for a in $XTERMS; do
		if type $a &>/dev/null; then
			local TERMINAL="$a"
			break
		fi
	done
	if [ "$TERMINAL" !=  "" ]; then
		$TERMINAL -e $COMMAND
		if [ "$?" == "0" ]; then
			return 0
		else
			return 1
		fi
	else
		return 2
	fi
}
# (gui/xmsg)
#-------------------------------------------------------------------------------
# GFSBashLib Copyright (C) Eskild Hustvedt 2005
# LocateDir xmsg version 0.1Test1
#
# 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:
# "xmsg (0.1Test1) from GFSBashLib (C) Eskild Hustvedt"
#
# Display a "ok" box with the text supplied to xmsg_ok, ex:
# xmsg_ok "Bla bla bla bla"
#
# Returns 0 on success
# Returns 2 when no DISPLAY variable is st
# Returns 3 on no suitable program detected
xmsg_ok ()
{
	if [ "$DISPLAY" == "" ]; then
		echo "No \$DISPLAY variable set, can't continue."
		return 2
	fi
	MESSAGE="$*"
	if [ "$MESSAGE" == "" ]; then MESSAGE="Error: No message supplied to $FUNCNAME (GFSBashLib).
This is a bug in the program you are using,
please report it to the author.";fi
	# Only allow kdialog as primary when we are _in_ KDE.
	# Using it as default in a non-KDE environment is way too slow
	# but still a useful fallback if all else fails.
	if [[ -n "$KDE_FULL_SESSION" ]]; then
		local XPROGS="kdialog zenity Xdialog gxmessage gmessage xmessage"
	else
		local XPROGS="zenity Xdialog gxmessage gmessage xmessage kdialog"
	fi
	for a in $XPROGS;do
		if type $a &>/dev/null; then
			local PROG="$a"
			break
		fi
	done
	case $PROG in
		gxmessage|xmessage|gmessage ) $PROG "$MESSAGE";;
		Xdialog ) $PROG --wrap --msgbox "$MESSAGE" 0 0;;
		kdialog ) $PROG --msgbox "$MESSAGE" 0 0 2>/dev/null;;
		zenity ) $PROG --info --text "$MESSAGE";;
		* ) echo "No suitable program detected, can't continue.";return 3;;
	esac
}
# Display a file selection box with the directory supplied as the first
# argument and an optional text. Ex:
# xmsg_fsel $HOME Select the file
#
# Returns 0 and echos a filename on success
# Returns 1 and echos a lowercase "none" on no file selected
# Returns 2 when no DISPLAY variable is set
# Returns 3 on no suitable program detected
xmsg_fsel ()
{
	if [ "$DISPLAY" == "" ]; then
		echo "No \$DISPLAY variable set, can't continue."
		return 2
	fi
	DIR="$1";shift 1;TITLE="$*"
	if [[ -n "$KDE_FULL_SESSION" ]]; then
		local XPROGS="kdialog zenity Xdialog"
	else
		local XPROGS="zenity Xdialog kdialog"
	fi
	if [ "$DIR" == "" ]; then DIR="$PWD";fi
	if [ "$TITLE" == "" ]; then TITLE="GFSBashLib file selection dialog";fi
	for a in $XPROGS;do
		if type $a &>/dev/null; then
			local PROG="$a"
			break
		fi
	done
	case $PROG in
		zenity ) if [ "$TITLE" == "" ]; then
			FILE="`$PROG --filename="$DIR"/ --file-selection`"
		else 
			FILE="`$PROG --title="$TITLE" --filename="$DIR"/ --file-selection`"
		fi ;;
		Xdialog ) if [ "$TITLE" == "" ]; then
			FILE="`$PROG --stdout --fselect $DIR 0 0`"
		else
			FILE="`$PROG --stdout --title "$TITLE" --fselect $DIR 0 0`"
		fi;;
		kdialog ) if [ "$TITLE" == "" ]; then
			FILE="`$PROG --getopenfilename $DIR 2>/dev/null`"
		else
			FILE="`$PROG --title "$TITLE" --getopenfilename $DIR 2>/dev/null`"
		fi;;
		* ) echo "No suitable program detected, can't continue"; return 3;;
	esac
	if [ "$FILE" == "" ]; then
		echo "none"
		return 1
	fi
	echo "$FILE"
	return 0
}
# (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 ""
# (gui/maingui)
if [ ! -e $LPFDIR/loki_patch-fix ]; then
	echo "ERROR: $LPFDIR/loki_patch-fix did not exist!"
	exit 1
fi
FILE="`xmsg_fsel $HOME "Select the patch file"`"
case $? in
	1 | 2 ) exit 0;;
	3 ) xmsg_ok "No suitable program to use for selecting the file
was found.
Please use the commandline interface";exit 1;;
esac
if [ "$1" == "--force" ] || [ "$1" == "-f" ]; then OPTS="--force";fi
ExecTerm "$LPFDIR/loki_patch-fix $OPTS --endread $FILE"
if [ "$?" == "2" ]; then
	xmsg_ok "No useable terminal detected"
fi
