#! /bin/sh

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

# Some variables to make maintaining this script easier
OGRE_VERSION="1.6.4.dfsg1"
OGRE_TARBALL="ogre-v1-6-4.tar.bz2"
OGRE_TARBALL_CHECKSUM="7770e6a97d834d77a52d2af7e19817b8"

USAGE="\n\
This script is used to generate the orig tarball used in building\n\
Debian packages for ogre-$OGRE_VERSION.\n\
Usage: ogre-get-orig-source [OPTION]\n\
\n\
 -h, --help                 Display this help message.\n\
 --keep-upstream-tarball    Keep the upstream source tarball.\n\
 --keep-orig-dir            Keep the generated orig directory.\n"

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

if [ ! -f $OGRE_TARBALL ] ; then
# Download the tarball
wget http://downloads.sourceforge.net/ogre/$OGRE_TARBALL
fi

# Verify the checksum
COMPUTED_CHECKSUM=`md5sum $OGRE_TARBALL | cut -d ' ' -f 1`

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

# Prepare the ogre orig tarball
if [ ! -d ogre ]; then
	echo "Extracting $OGRE_TARBALL"
	tar -xjf $OGRE_TARBALL
else
	echo "Already found extracted ogre directory. Please remove 
or move ogre and ogre-$OGRE_VERSION directories and also ogre_$OGRE_VERSION.orig.tar.gz."
	exit 1
fi
if [ ! -d ogre-$OGRE_VERSION ]; then
	echo "Renaming extracted ogre directory to ogre-$OGRE_VERSION"
	mv ogre ogre-$OGRE_VERSION
else
	echo "Already found ogre-$OGRE_VERSION directory. Please remove or move 
ogre and ogre-$OGRE_VERSION directories and also ogre_$OGRE_VERSION.orig.tar.gz."
	exit 1
fi

# Remove non-distributable samples
echo "Removing all non-distributable demos."
rm -r ogre-$OGRE_VERSION/Samples/BezierPatch
rm -r ogre-$OGRE_VERSION/Samples/BSP
rm -r ogre-$OGRE_VERSION/Samples/CameraTrack
rm -r ogre-$OGRE_VERSION/Samples/CelShading
rm -r ogre-$OGRE_VERSION/Samples/Compositor
rm -r ogre-$OGRE_VERSION/Samples/CubeMapping
rm -r ogre-$OGRE_VERSION/Samples/DeferredShading
rm -r ogre-$OGRE_VERSION/Samples/Dot3Bump
rm -r ogre-$OGRE_VERSION/Samples/DynTex
rm -r ogre-$OGRE_VERSION/Samples/EnvMapping
rm -r ogre-$OGRE_VERSION/Samples/FacialAnimation
rm -r ogre-$OGRE_VERSION/Samples/Fresnel
rm -r ogre-$OGRE_VERSION/Samples/Grass
rm -r ogre-$OGRE_VERSION/Samples/Gui
rm -r ogre-$OGRE_VERSION/Samples/Instancing
rm -r ogre-$OGRE_VERSION/Samples/Lighting
rm -r ogre-$OGRE_VERSION/Samples/Media
rm -r ogre-$OGRE_VERSION/Samples/OceanDemo
rm -r ogre-$OGRE_VERSION/Samples/ParticleFX
rm -r ogre-$OGRE_VERSION/Samples/RenderToTexture
rm -r ogre-$OGRE_VERSION/Samples/Shadows
rm -r ogre-$OGRE_VERSION/Samples/SkeletalAnimation
rm -r ogre-$OGRE_VERSION/Samples/SkyBox
rm -r ogre-$OGRE_VERSION/Samples/SkyDome
rm -r ogre-$OGRE_VERSION/Samples/SkyPlane
rm -r ogre-$OGRE_VERSION/Samples/Smoke
rm -r ogre-$OGRE_VERSION/Samples/Terrain
rm -r ogre-$OGRE_VERSION/Samples/TextureFX
rm -r ogre-$OGRE_VERSION/Samples/Transpacency
rm -r ogre-$OGRE_VERSION/Samples/VolumeTex
rm -r ogre-$OGRE_VERSION/Samples/Water
rm -r ogre-$OGRE_VERSION/Samples/Isosurf
rm -r ogre-$OGRE_VERSION/Samples/ParticleGS
rm -r ogre-$OGRE_VERSION/Samples/PCZTestApp
rm -r ogre-$OGRE_VERSION/Samples/Common/bin
rm -r ogre-$OGRE_VERSION/Samples/Common/include
rm -r ogre-$OGRE_VERSION/Samples/Common/setup
rm -r ogre-$OGRE_VERSION/Samples/ReadMe.html

# Remove non-free autogenerated documentation
rm -rf ogre-$OGRE_VERSION/Docs/manual/
rm -rf ogre-$OGRE_VERSION/Docs/api/

# Remove autom4te.cache directories
echo "Removing generated directories from autotools."
rm -rf ogre-$OGRE_VERSION/autom4te.cache
rm -rf ogre-$OGRE_VERSION/ReferenceApplication/autom4te.cache

# Create the tarball
if [ ! -f ogre_$OGRE_VERSION.orig.tar.gz ]; then
    echo "Creating orig tarball."
    tar -czf ogre_$OGRE_VERSION.orig.tar.gz ogre-$OGRE_VERSION/
    if [ -z $KEEP_ORIG_DIR ]; then
        echo "Removing extracted directory."
        rm -rf ogre-$OGRE_VERSION/
    fi
    if [ -z $KEEP_UPSTREAM_TARBALL ]; then
        echo "Removing upstream tarball"
        rm $OGRE_TARBALL
    fi
else
    echo "Already found orig tarball. Please remove or move ogrenew 
and ogre-$OGRE_VERSION directories and also ogre_$OGRE_VERSION.orig.tar.gz."
    exit 1
fi
