#!/bin/sh

set -e

source functions.sh


if [ "$1" != "bin" ] && [ "$1" != "src" ]
    then
    echo "Should have one param: <bin | src>"
    echo "Second param can be <fresh>, in which case all the"
    echo " package is rebuilt for binary pacakge"
    exit 1
fi



cd /home/rusconi/devel

thisDir=$(pwd)

##########################################################################
# Grab the version of the source package and construct the name of the 
# source tarball that we'll need.

cd massxpert || { echo "Failed to change directory to massxpert"; exit 1; }

VERSION=$(sourceVersion)


##########################################################################
# Make sure we are not going to package backup files.

fileList=$(find -name "*~")

if [ ! -z "${fileList}" ]
then
    echo "There are backup files still present:"
    echo "${fileList}"
    echo "Please remove them first."
    exit 1
fi


# Go back to the parent directory
cd ${thisDir}

# At this point we are in /home/rusconi/devel.


########################## SOURCE TARBALL ##########################
if [ "$1" == "src" ] 
    then

    tarballName=massxpert-${VERSION}.tar.gz
    echo "Tarball name: ${tarballName}"
    
    # Create source tarball
    makeSourceTarball ${tarballName} || { echo "Failed to prepare source tarball"; exit 1; }
    echo "Produced source tarball ${tarballName}"

    exit 0
fi
########################## SOURCE TARBALL ##########################



########################## BINARY TARBALL ##########################
if [ "$1" == "bin" ] 
    then 

    srcTarballName=massxpert-${VERSION}.tar.gz
    echo "src Tarball name: ${srcTarballName}"
    binTarballName=massxpert-bin-${VERSION}.tar.gz
    echo "bin Tarball name: ${binTarballName}"

# See if the user wants to remove everything massxpert from
# /usr/local, so that we can install the soft in there, and then pick
# out everything to put in a binary tarball.
    
    prefix="/usr/local"
    
    if [ "$2" == "fresh" ]
	then

	echo "Should anything massxpert removed from ${prefix}? (y/n)"
	read char

	if [ "x$char" == "xy" ]
	    then
	    # Remove anything massxpert in ${prefix}.
	    for removedFile in $(find ${prefix} -name "*massxpert*")
	      do 
	     sudo rm -vrf ${removedFile}
	    done
	fi
    fi


# Create source tarball
    makeSourceTarball ${srcTarballName} || { echo "Failed to prepare source tarball"; exit 1; }
    echo "Produced source tarball ${srcTarballName}"
    sleep 2

# At this point ~/tmp/massxpert-${VERSION} contains the source
# directory which was used to compress and create
# massxpert-bin-${VERSION}.tar.gz. Give the path to that directory, so
# that the binary tarball can be created out of it.

    echo -n "Making binary with source tree at ~/tmp/massxpert-${VERSION}..."
    sleep 2
    makeBinary ~/tmp/massxpert-${VERSION} ~/tmp/massxpert-bin-${VERSION}
    if [ "$?" != "0" ]
    then
	echo "Failed making binary"
	exit 1
    fi

# At this point the binary is complete. Install it to /usr/local.

    makeInstall ~/tmp/massxpert-bin-${VERSION} || { echo "Failed to make install massxpert."; exit 1; }

    makeBinaryTarball ~/tmp/${binTarballName} ${prefix} || { echo "Failed to make binary tarball."; exit 1; }
    
    echo "Produced binary tarball ~/tmp/${binTarballName}"

    exit 0
fi

