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 "        -n            not do not install the generated package (requires -d)"
	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
}

verify_md5sum() {
	FILE=$1
	GOODSUM=$2
	SUM=`md5sum $FILE|cut -d' ' -f1`
	if [ "$SUM" != "$GOODSUM" ]; then
		echo "error: $FILE's md5 checksum is unknown." >&2
		echo "perhaps it is corrupted?" >&2
		echo "quitting." >&2
		exit 1
	fi
}

verify_directory() {
	DIR=$1
	if [ ! -d "$DIR" ]; then
		echo "error: $DIR is not a directory. Quitting." >&2
		exit 1
	fi
}

verify_file() {
	FILE=$1
	if [ ! -f "$FILE" ]; then
		echo "error: $FILE is not a file. Quitting." >&2
		exit 1
	fi
}

die() { 
    if [ $# -lt 2 ]; then
        RET=2
    else
        RET=$2
    fi
	echo $0: $1 >&2
	exit $RET
}

# 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
		slipstream_file "$1" "$RELPATH"
		shift
	done

	slipstream_instsize
	slipstream_repack "$DEB"
	slipstream_cleanup

	cd "$OLDWD"
}

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="$1"
	dpkg-deb -e "$DEB" "./DEBIAN"
	dpkg-deb -x "$DEB" "./slipstream.unpacked"
}

slipstream_file() {
	FILE="$1"
	RELPATH="$2"

	cp -p "$FILE" "./slipstream.unpacked/$RELPATH"
	chmod 644 "./slipstream.unpacked/$RELPATH"

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

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="$1"     # the .deb file we are going to mangle

	# repack
	mv DEBIAN slipstream.unpacked
	# XXX: capture the output of dpkg-deb and hide it
	fakeroot dpkg-deb -b slipstream.unpacked "$DEB" | ( \
		grep -v "^dpkg-deb: building package \`[a-z-]\+' in \`$DEB'."\
		|| true)

}

slipstream_cleanup() {
	rm -rf ./slipstream.unpacked
}

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

install_deb() {
	DEB="$1"
	sudo dpkg -i "$DEB"
}

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
