summaryrefslogtreecommitdiff
path: root/includes/generic-includes.sh
blob: 45375017764db1d64639f12659054abae86ae09d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright 2009 Nick White
#
# 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.
#
# See <http://www.gnu.org/licenses/> for a copy of the GNU General
# Public License.

### default variables ###

[ -z "$TMPDIR" ] && export TMPDIR="/tmp/iepatch"

[ -z "$TARGETDIR" ] && export TARGETDIR="$PWD/$SHORTGAMENAME"

[ -z "$LANGUAGE" ] && export LANGUAGE="English"

### functions ###

function die
{
	echo "Install can not continue, cleaning up partial install"
	rm -rf "$TARGETDIR"

	echo ""
	echo "The install failed; sorry."
	echo "Please make sure you have the correct CDs for the game"
	echo "and enough free disk space, and try again."

	exit 1
}

function diequietly
{
	exit 1
}

function setuptmp
{
	rm -rf $TMPDIR
	mkdir -p $TMPDIR || die
}

function cleanuptmp
{
	rm -rf $TMPDIR
}

function checkforbin
{
	# usage: checkforbin binnames...
	retstatus=0

	while [ "$1" != "" ]; do
		binname=$1

		which "$binname" &> /dev/null
		if [ $? -ne 0 ]; then
			echo "WARNING: This script needs $binname, which doesn't appear to be installed." 1>&2
			retstatus=1
		fi

		shift
	done

	return $retstatus
}

function setperms
{
	# usage setperms targetdir

	if ! [ -d "$1" ]; then
		return 1
	fi

	find "$1" -type d -exec chmod 755 '{}' \;
	find "$1" -type f -exec chmod 644 '{}' \;
	find "$1" -type f -iname '*.exe' -exec chmod 755 '{}' \;
	find "$1" -type f -iname '*.ini' -exec chmod 664 '{}' \;
	chgrp -R games "$1"
	if [ -d "$1"/save ]; then
		chmod -R g+w "$1"/save
	fi
	if [ -d "$1"/mpsave ]; then
		chmod -R g+w "$1"/mpsave
	fi
}

function usage
{
	echo "Usage:" $0 "[-i installdir] [-p patchdir] [-c cdmount] [-l language]"
	echo -e "  -i installdir is the directory to install to."
	echo -e "     default:" $TARGETDIR
	echo -e "  -c cdmount is the mount location of the cd drive used."
	echo -e "     default:" $CDMOUNT
	echo -e "  -p patchdir is an optional directory containing patch files."
	echo -e "     default:" $PATCHDIR
	echo -e "  -l language can be one of English, Spanish, French, German, Italian   or Language Independant"
	echo -e "     default:" $LANGUAGE
	# TODO: Add option to show and select all known BG1 versions
}

function parseargs
{
	while getopts ":i:p:c:l:" options; do
		case $options in
			i ) export TARGETDIR="$OPTARG";;
			p ) export PATCHDIR="$OPTARG";;
			c ) export CDMOUNT="${OPTARG%\/}";; # ensure no trailing slash
			l ) export LANGUAGE="$OPTARG";;
			\? )	usage
				exit 1;;
			h ) usage
				exit 1;;
		esac
	done
}

function copylower
{
	#usage: copylower source destination
	if [ -d "$1" ]; then
		for filename in $(find "$1" -type f); do
			lowerpath="$(echo $filename|gawk -F "$1" '{print $2}'|tr A-Z a-z)"
			cp -f "$filename" "$2/$lowerpath" || die
		done
	elif [ -f "$1" ]; then
		cp -f "$(echo $1|tr A-Z a-z)" "$2"
	fi
}