#!/bin/sh

make_dist_file=`pwd`
make_dist_file="$make_dist_file/make_dist.out"

if [ -z $1 ]; then
    echo $0 email_version
    exit;
fi

VERSION="$1"

printf "\rSetting up configure scripts and version info..."
echo "$VERSION" > VERSION
autoheader
autoconf > /dev/null 2>&1
cd dlib
autoheader
autoconf
cd ..

mkdir -p "email-${VERSION}/dlib"
cmd=`git archive master | tar -x -C email-${VERSION}`

printf "\rDoing a git...            "
$cmd > /dev/null

# Get dlib
cd dlib
git archive master | tar -x -C ../email-${VERSION}/dlib > /dev/null
cd ../email-${VERSION}

# Fix VERSION and configure.ac script in release dir
printf "\rParsing files and creating ./configure...      "
if [ ! -f configure.in ]; then
	echo
	echo "Something is wrong... configure.in does not exist out of the repository email."
	exit 1
fi
autoheader
autoconf > /dev/null 2>&1
cd dlib
if [ ! -f configure.in ]; then
	echo
	echo "Something is wrong... configure.in does not exist out of the repository for dlib."
	exit 1
fi
autoheader
autoconf
cd ..

# Tar and gzip distrobution file
cd ..
if [ ! -d dist ]; then
	mkdir dist
fi
printf "\rTaring and gziping up email-$VERSION...        "
tar -cf dist/email-"$VERSION".tar email-"$VERSION"
cd dist
gzip -c email-${VERSION}.tar > email-${VERSION}.tar.gz
bzip2 email-${VERSION}.tar
cd ..
rm -rf email-${VERSION}

printf "\rDistribution file is at dist/email-$VERSION.tar.gz\n"
