File: game-data-packager-shared

package info (click to toggle)
game-data-packager 30
  • links: PTS, VCS
  • area: contrib
  • in suites: wheezy
  • size: 292 kB
  • sloc: sh: 117; makefile: 69
file content (277 lines) | stat: -rw-r--r-- 7,017 bytes parent folder | download
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
supported() {
	echo "the following games are supported:"
	echo
	printf "\t    name\tdescription\n"
	printf "\t    ----\t-----------\n"

	find $SUPPORTED -type f | grep -v '\.svn' | grep -v 'swp$' | sort |
	while read file; do
		. $file
		printf "\t%8s\t%s\n" "$SHORTNAME" "$LONGNAME"
	done
}
options() {
	echo "game-data-packager arguments:"
	echo "        -i            install the generated package"
	echo "        -n            do not install the generated package (requires -d, default)"
	echo "        -d OUTDIR     write the generated .deb(s) to OUTDIR"
}

usage() {
	echo "usage:"
	printf "\tgame-data-packager [game-data-packager-args] game [game-args]\n"
	echo
	options
	echo
	supported
	echo
	echo "run game-data-packager [game] to see game-specific arguments."
	echo
}

set +u
if [ -n "$DEBUG" ]; then
debug() {
		echo "DEBUG: $*" >&2
}
else
debug() { :; }
fi
set -u

warn() {
	echo "WARNING: $*" >&2
}

## die(string,retcode)
##      end the program, complaining with string on stderr
##      and returning retcode if supplied, 2 if not.
die() { 
    if [ $# -lt 2 ]; then
        RET=2
    else
        RET=$2
    fi
	echo $0: $1 >&2
	exit $RET
}

## verify_md5sum(file,sum)
##      calculates the md5sum of file and compares it to sum.
##      if the sum doesn't match, complains on stderr and causes the program
##      to exit.
verify_md5sum() {
	FILE=$1
	GOODSUM=$2
	SUM=`md5sum $FILE|cut -d' ' -f1`
	[ "$SUM" = "$GOODSUM" ] || die "$FILE's md5 checksum is unknown"
}

## verify_md5sum_alternatives(file,sums)
##      calculates the md5sum of file and compares it to sums (comma-separated).
##      if the sum doesn't match any of them, complains on stderr and causes
##      the program to exit.
verify_md5sum_alternatives() {
	FILE=$1
	GOODSUMS=$2
	SUM=`md5sum $FILE|cut -d' ' -f1`
	case ",$GOODSUMS," in
		(*,$SUM,*)
			;;
		(*)
			die "$FILE's md5 checksum $SUM is unknown"
			;;
	esac
}

## verify_directory(dir)
##      ensures dir is a directory, or complains on stderr
##      and causes the program to exit.
verify_directory() {
	DIR=$1
	[ -d "$DIR" ] || die "$DIR is not a directory" 
}

## verify_file(file)
##      ensures file is a file, or complains on stderr
##      and causes the program to exit.
verify_file() {
	FILE=$1
	[ -f "$FILE" ] || die "$FILE is not a file"
}

## slipstream(deb,relpath,file1,file2...)
##      insert file1,file2... into the deb, under the
##      path 'relpath', relative to the package root, e.g.:
##          slipstream(deb, 'usr/share/doc', 'README', 'copyright')
##              => /usr/share/doc/README
##              => /usr/share/doc/copyright
##      prerequisites:
##        * $WORKDIR must be defined to a directory within which
##          slipstream can do it's work (somewhere writeable)
# TODO: this assumes every file is going to go in the same RELPATH. hmm.
slipstream() {
	DEB="$1"     # the .deb file we are going to mangle
	RELPATH="$2" # relative path in the unpacked .deb
	shift 2

	OLDWD=`pwd`
	cd "$WORKDIR"

	slipstream_permcheck "$DEB"
	slipstream_unpack "$DEB"

	while [ "$#" -gt 0 ]; do
		file="$1"
		destpath="$RELPATH"
		if [ "x${RELPATH%/}" != "x${RELPATH}" ]; then
			# RELPATH ends with /; append the basename of the
			# file (e.g. pak0.pk3)
			destpath="${RELPATH}${file##*/}"
		fi
		slipstream_file "$file" "$destpath"
		shift
	done

	slipstream_instsize
	slipstream_repack "$DEB"
	slipstream_cleanup

	cd "$OLDWD"
}

## slipstream_permcheck(deb)
##      ensures that the file deb can be written to and
##      that the current working directory is writeable
slipstream_permcheck() {
	DEB="$1"

	# ensure we can write to $DEB
	if [ ! -w "$DEB" ]; then
		ls -l "$DEB"
		die "wrong permissions on $DEB (I can't write to it)"
	fi

	# ensure we can write to the workdir
	if [ ! -w . ]; then
		die "cannot write to $PWD"
	fi
}

## slipstream_unpack(deb)
##      unpacks the deb file into "./slipstream_unpacked"
##      and the control data into "./DEBIAN"
slipstream_unpack() {
	DEB="$1"
	dpkg-deb -e "$DEB" "./DEBIAN"
	dpkg-deb -x "$DEB" "./slipstream.unpacked"
}

## slipstream_file(file,destpath)
##      copies the file into "./slipstream_unpacked/$destpath",
##      calculates the files md5sum and adds it to the md5sums
##      file in the control area.
slipstream_file() {
	file="$1"
	destpath="$2"

	cp -p "$file" "./slipstream.unpacked/$destpath"
	chmod 644 "./slipstream.unpacked/$destpath"

	# add a line to md5sums
	cd slipstream.unpacked
	md5sum "$destpath" >> "../DEBIAN/md5sums"
	cd ..
}

##  slipstream_instsize
##      calculates the installed size of the deb, (based on
##      the contents of the ./slipstream_unpacked directory)
##      and writes the result to the control file in the
##      control area.
slipstream_instsize() {
	# figure out the new installed-size
	INSTSIZE=`du -sk ./slipstream.unpacked | cut -f1`
	sed -i  "s/^Installed-Size.*/Installed-Size: $INSTSIZE/" \
		"./DEBIAN/control"
}

## slipstream_repack(deb)
##      writes a new debian package over deb, packing
##      the files from ./slipstream_unpacked inside,
##      using the control area in ./DEBIAN
slipstream_repack() {
	DEB="$1"     # the .deb file we are going to mangle

	# repack
	mv DEBIAN slipstream.unpacked
	# XXX: store output in a temporary file, then cat the file if
	# dpkg-deb fails for some reason. (this is all to hide a non-
	# suppressable "building package foo in ..." message)
	fakeroot dpkg-deb -b slipstream.unpacked "$DEB" >/dev/null

}

## slipstream_cleanup()
##      removes the ./slipstream_unpacked directory.
slipstream_cleanup() {
	rm -rf ./slipstream.unpacked
}

# stuff relating to installing the generated packages ########################

## install_deb(deb)
##      uses sudo and dpkg to install the supplied .deb file
# TODO: configurable priviledge escalation method (not hardcoded sudo)
# TODO: configurable package installation method (not hardcoded dpkg)
install_deb() {
	DEB="$1"
    echo "using su(1) to obtain root privileges and install the package"
	su -c "dpkg -i \"$DEB\""
}

## unravel(path)
##      convert 'path' from relative to absolute
##      if it does not begin with a slash.
unravel() {
	FILE="$1"
	if echo "$FILE" | grep ^/ >/dev/null; then
		:
	else
		# assume a relative path
		FILE="$PWD/$FILE"
	fi

	echo $FILE
}

if [ -f ./debian/changelog ]; then
    GAME_PACKAGE_VERSION=`dpkg-parsechangelog -ldebian/changelog --format \
        rfc822 | grep Version | cut -d' ' -f2`
else
    GAME_PACKAGE_VERSION=`dpkg-query --showformat='${Version}\n' \
        --show game-data-packager`
fi

gdp_unzip() {
    zipfile="$1"
    shift
    if which 7za >/dev/null; then
        debug "using 7za"
        7za e -y "$zipfile" "$@" >/dev/null
    elif which 7z >/dev/null; then
        debug "using 7z"
        7z e -y "$zipfile" "$@" >/dev/null
    else
        debug "using unzip"
        unzip -qqo "$zipfile" "$@"
    fi
}

## ifind(base,file)
##      case-insensitive search for file in base
ifind() {
  base="$1"
  file="$2"
  find "$base" -iwholename "$base/$file"
}