#!/bin/sh

# self-extracting setup builder 
# version 1.0
# Wim Kerkhoff <wim@merilus.com>

# this is little shell script builds a self-extracting executable for xfmail
# run it from the xfmail/ source directory
# modify variables below as necessary.
# it should leave a xfmail-1.5.5.sh in $TEMPDIR

#################
# NOTE: by default this script will run 'make clean' on the source tree you specified. You may not want that to happen... then comment the 'make clean' line out below.
#################

# program version... up with every release
VERSION=1.5.5

# name of the program... xfmail or this or that or ying or yang or foo or bar
# this is also the name of the directory your program is in
NAME=xfmail

# a name for the self-extracting exe...
EXE=$NAME-$VERSION.sh

# temporary directory used when creating the self-extracting file..
TEMPDIR=/tmp/$NAME.builddir

# Shouldn't need to change anything below here....
#####################################################

# some utils
TAR=`which tar`
GZIP=`which gzip`
UUENCODE=`which uuencode`

# Detect the command line arguments
# and do some error checking

if ! [ -r "$1" ] 
then
	echo "Please pass an argument of the local source directory you want to use"
	echo "as the input for the self-extracting file..."
	ERR=1
else
	# locate source directory
	INPUTDIR=$1
fi

if ! [ -x "$TAR" ]
then
	echo "Could not find an executable tar"
	ERR=1
fi

if ! [ -x "$GZIP" ]
then
	echo "Could not find an executable gzip"
	ERR=1
fi

if ! [ -x "$UUENCODE" ]
then
	echo "Could not find an executable uuencode"
	ERR=1
fi

if [ "$ERR" = "1" ]
then
	echo "Exitting..."
	exit
fi

# code starts here

rm -rf $TEMPDIR > /dev/null
mkdir $TEMPDIR
cd $INPUTDIR
cd ..
echo "$TAR cvvf $TEMPDIR/$NAME-$VERSION.tar $NAME"
cd $INPUTDIR
cd $NAME
make clean
cd ..
$TAR cvvf $TEMPDIR/$NAME-$VERSION.tar $NAME
$GZIP $TEMPDIR/$NAME-$VERSION.tar

echo "#!/bin/sh" > $TEMPDIR/$EXE
echo >> $TEMPDIR/$EXE
echo "cat > $NAME-$VERSION.uuencoded <<'REAL_EOF'" >> $TEMPDIR/$EXE
$UUENCODE $TEMPDIR/$NAME-$VERSION.tar.gz $NAME-$VERSION.tar.gz >>$TEMPDIR/$EXE
echo "REAL_EOF" >> $TEMPDIR/$EXE
echo "uudecode $NAME-$VERSION.uuencoded" >> $TEMPDIR/$EXE
echo "rm -rf $NAME-$VERSION.uuencod*" >> $TEMPDIR/$EXE
echo "gunzip $NAME-$VERSION.tar.gz" >> $TEMPDIR/$EXE
echo "rm -rf $NAME-$VERSION.tar.gz" >> $TEMPDIR/$EXE
echo "tar -xvf $NAME-$VERSION.tar" >> $TEMPDIR/$EXE 
echo "rm -rf $NAME-$VERSION.tar" >> $TEMPDIR/$EXE
echo "cd $NAME && ./configure && make && make install" >> $TEMPDIR/$EXE

# set the permissions on the file so it can be executed...
chmod +x $TEMPDIR/$EXE

# clean up the tarball we made...
rm -rf $TEMPDIR/$NAME-$VERSION.tar.gz
