summaryrefslogtreecommitdiff
path: root/includes/generic-includes.sh
blob: 9f3a0db3f176ef6faa786934c60562b76969e28c (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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 3
# 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" ] && [ -e "$SHORTGAMENAME" ] && export TARGETDIR="$PWD/$SHORTGAMENAME"

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

[ -z "$SKIP_DETECTION" ] && export SKIP_DETECTION=0

### functions ###

function die
{
	echo "Install can not continue"

	if [ "$alwaysdiesafely" != "1" ]; then
		echo "Cleaning up partial install"
		rm -rf "$TARGETDIR"
	fi

	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 diesoftly
{
	# a non-destructive die

	echo " ! The install failed; sorry." 1>&2

	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" &> /dev/null
	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] [-n]"
	echo -e "  -i installdir is the directory to install to."
	echo -e "     default: ${TARGETDIR} (changes per game)"
	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}"
	echo -e "  -n skip CD autodetection"
	echo -e "     default: off"
}

function parseargs
{
	# usage: parseargs args...

	while getopts ":i:p:c:l:n" 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";;
			n ) export SKIP_DETECTION=1;;
			\? )	usage
				exit 1;;
			h ) usage
				exit 1;;
		esac
	done
}

function copylower
{
	# usage: copylower source destination
	mkdir -p "$2"

	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
		lowername="$(basename $1|tr A-Z a-z)"
		cp -f "$1" "${2}/${lowername}"
	fi
}

function setlower
{
    # usage: setlower target
    if [ "$1" ]
    then
        cd "$1"
        for each_file in `find ./ -iname "*"`
        do
            lower="`echo "$each_file" | tr "[:upper:]" "[:lower:]"`"

            if [ "$each_file" != "$lower" ]
            then
                mv "$each_file" "$lower"
            fi
        done
    fi
}

function move_and_remove
{
    # usage: move_and_remove source destination
    # copies the files in the source directory to the destination.
    # then removes the source entirely.
    if [ "$1" != "$2" ]
    then
        mkdir -p "$2" || die
        cp -R "$1/"* "$2" || die
        rm -r "$1"
    fi
}

function teardown
{
    # usage: teardown target
    if [ "$1" ]
    then
        cleanuptmp
        setperms "$1"
        setlower "$1"
    fi
}