#! /bin/sh

# This script is used to generate the alien-arena orig tarball that can be
# distributed through Debian.

ALIEN_ARENA_TARBALL="alienarena2008-linux20080227.zip"
ALIEN_ARENA_VERSION="7.0"
CORRECT_CHECKSUM="5fcb17a65e220b1aad5ec25f97b35932"

USAGE="\
alien-arena orig tarball download script\n\
This script will generate an orig tarball that's distrubeted through Debian.\n\
Usage: alien-arena-get-orig-source [OPTION]\n\
\n\
 -h, --help                 Display this text\n\
--keep-upstream             Don't delete the upstream source tarball\n\
--keep-orig-dir             Don't delete the orig directory\n"

while [ "$#" -gt "0" ]
do
    case "$1" in
        -h|--help)
            echo -e "${USAGE}"
            exit 1
            ;;
        --keep-upstream-tarball)
            KEEP_UPSTREAM_TARBALL=1
            shift
            ;;
        --keep-orig-dir)
            KEEP_ORIG_DIR=1
            shift
            ;;
    esac
done

if [ ! -f $ALIEN_ARENA_TARBALL ] ; then
	# Download upstream zip file
	wget -c http://icculus.org/alienarena/Files/$ALIEN_ARENA_TARBALL
	echo "Downloaded upstream zip file."
fi

# Verify the checksum
echo -n "Verifying MD5 checksum..."
COMPUTED_CHECKSUM=`md5sum $ALIEN_ARENA_TARBALL | cut -d ' ' -f 1`
echo "done."

if [ $CORRECT_CHECKSUM != $COMPUTED_CHECKSUM ] ; then
	echo "Checksum verification failed. Checksum was $COMPUTED_CHECKSUM
Expected checksum $CORRECT_CHECKSUM"
	exit 1
else
	echo "Checksum verified. Checksum is $COMPUTED_CHECKSUM."
fi

# Prepare the alien-arena orig tarball
if [ ! -d alienarena2008 ]; then
	echo -n "Extracting $ALIEN_ARENA_TARBALL..."
	unzip -qq $ALIEN_ARENA_TARBALL
	echo "done."
else
	echo "Already found extracted alienarena2008 directory. Please remove
or move alienarena2008 and alien-arena-$ALIEN_ARENA_VERSION directories and also alien-arena_$ALIEN_ARENA_VERSION.orig.tar.gz."
	exit 1
fi
if [ ! -d alien-arena-$ALIEN_ARENA_VERSION ]; then
	echo "Renaming extracted alienarena2008 directory to alien-arena-$ALIEN_ARENA_VERSION"
	mv alienarena2008 alien-arena-$ALIEN_ARENA_VERSION
else
	echo "Already found alien-arena-$ALIEN_ARENA_VERSION directory. Please remove
or move alienarena2008 and alien-arena-$ALIEN_ARENA_VERSION directories and also alien-arena_$ALIEN_ARENA_VERSION.orig.tar.gz."
	exit 1
fi

# Remove all precompiled binaries
echo "Removing all precompiled binaries"
rm alien-arena-$ALIEN_ARENA_VERSION/crded
rm alien-arena-$ALIEN_ARENA_VERSION/crx
for REMOVE_DLL in `find alien-arena-$ALIEN_ARENA_VERSION -name *.dll`; do
	rm -r "$REMOVE_DLL"
done
for REMOVE_SO in `find alien-arena-$ALIEN_ARENA_VERSION -name *.so`; do
	rm -r "$REMOVE_SO"
done
for REMOVE_EXE in `find alien-arena-$ALIEN_ARENA_VERSION -name *.exe`; do
	rm -r "$REMOVE_EXE"
done
for REMOVE_LIB in `find alien-arena-$ALIEN_ARENA_VERSION -name *.lib`; do
	rm -r "$REMOVE_LIB"
done

# Removing other non-distributable components
echo "Removing non-distributable components"
for REMOVE_ICO in `find alien-arena-$ALIEN_ARENA_VERSION -name *.ico`; do
	rm -r "$REMOVE_ICO"
done
for REMOVE_BAT in `find alien-arena-$ALIEN_ARENA_VERSION -name *.bat`; do
	rm -r "$REMOVE_BAT"
done
for REMOVE_DSP in `find alien-arena-$ALIEN_ARENA_VERSION -name *.dsp`; do
	rm -r "$REMOVE_DSP"
done
for REMOVE_DSW in `find alien-arena-$ALIEN_ARENA_VERSION -name *.dsw`; do
	rm -r "$REMOVE_DSW"
done

# Remove components distributed through alien-arena package
echo -n "Removing components distributed through alien-arena-data package..."
rm -r alien-arena-$ALIEN_ARENA_VERSION/arena
rm -r alien-arena-$ALIEN_ARENA_VERSION/botinfo
rm -r alien-arena-$ALIEN_ARENA_VERSION/data1
echo "done."

# Create the tarball
if [ ! -f alien-arena_$ALIEN_ARENA_VERSION.orig.tar.gz ]; then
	echo "Creating orig tarball."
	tar -czf alien-arena_$ALIEN_ARENA_VERSION.orig.tar.gz alien-arena-$ALIEN_ARENA_VERSION/
	if [ ! -n "$KEEP_ORIG_DIR" ]; then
		echo -n "Removing orig directory..."
		rm -rf alien-arena-$ALIEN_ARENA_VERSION/
		echo "done."
	fi
    if [ ! -n "$KEEP_UPSTREAM_TARBALL" ]; then
        echo -n "Removing upstream tarball..."
        rm $ALIEN_ARENA_TARBALL
        echo "done."
    fi
else
	echo "Already found orig tarball. Please remove or move
alien-arena_$ALIEN_ARENA_VERSION.orig.tar.gz."
	exit 1
fi
