#!/bin/sh

set -e

source functions.sh

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)
srcTarballName=massxpert-${VERSION}.tar.gz
echo "Tarball name: ${srcTarballName}"

##########################################################################


##########################################################################
# Make sure we are not going to package backup files.
# Removed code and put into the makeSourceTarball() function.


##########################################################################
# Begin make sure that the documentation is ok

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


# First ensure all the subdocuments are inputted in master tex file.

checkUsermanualIntegrity $HOME/devel/b/usermanual || { echo "The User Manual does not include all subdocuments"; exit 1; }


# Second, make sure that the version of the user manual matches the
# version of the source package.

version=$(usermanualVersion $HOME/devel/b/usermanual)

if [ "${version}" != "${VERSION}" ]
    then
    echo "User Manual has version different than Source package"
    exit 1
fi


# Finally make the User Manual documentation (pdf)
echo "Compiling Usermanual"

makeUsermanualPdf  $HOME/devel/b/usermanual || { echo "Failed to compile the User Manual PDF."; exit 1; }

# End make sure that the documentation is ok
##########################################################################

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

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

# Remove ~/tmp/massxpert-${VERSION} if it exists
rm -rf ~/tmp/massxpert-${VERSION}

# Create source tarball in /tmp
makeSourceTarball ${srcTarballName} || { echo "Failed to prepare source tarball."; exit 1; }

echo "Produced source tarball ${srcTarballName}" ; sleep 2

#pwd ; ls ; read

mv ~/tmp/${srcTarballName} ~/devel/debbench

cd ~/devel/debbench

#echo "Change dir to ~/devel/debbench"
#pwd ; ls ; read

# Remove any preexisting directory by the same names.
rm -rfv massxpert-${VERSION}
rm -rfv massxpert

#pwd ; ls ; read

# Create the source directory we'll modify for use in the creation of
# the Debian package
echo -n "Extracting upstream source tarball... "
tar xzf ${srcTarballName} || { echo "Failed to extract the ${srcTarballName} tarball"; exit1; }
echo "OK"

#echo $? ; pwd ; ls ; read

# Copy the source tarball to the orig.tar.gz that will be used for the
# diff in the Debian packaging process. This is because the tar.gz
# source tarball that is uploaded to /source MUST be identical
# (sha1sum-wise) to the orig.tar.gz tarball that is the basis for the
# construction of the Debian package.

cp ${srcTarballName} massxpert_${VERSION}.orig.tar.gz


#######################################################################
################ REAL DEBIAN PACKAGING IS HERE ########################

# Go back down to source tree root.

# Issue dpkg-buildpackage -rfakeroot That should build the package in
# the ../directory, that is debbench.

# If we are doing pbuilder stuff, then make it without spawning a new
# konsole and without the debsign call.

cd massxpert-${VERSION}

if [ -d ~/.kde ]
then 
    DEB_BUILD_OPTIONS='parallel=2'  konsole --noclose --name "kt-debbench" --geometry 1587x391+38-25 \
	--workdir $(pwd) -e dpkg-buildpackage -rfakeroot -k"massxpert-maintainer@massxpert.org"&
    else
     DEB_BUILD_OPTIONS='parallel=2' dpkg-buildpackage -rfakeroot &
fi

exit

rusconi@filo:~/devel/debbench$ linda massxpert-doc_1.6.0-rc2-1_all.deb
rusconi@filo:~/devel/debbench$ lintian massxpert-doc_1.6.0-rc2-1_all.deb
rusconi@filo:~/devel/debbench$ dpkg -c massxpert-doc_1.6.0-rc2-1_all.deb | less
..... installs usermanual in ./usr/share/doc/massxpert/usermanual


rusconi@filo:~/devel/debbench$ linda massxpert_1.6.0-rc2-1_i386.deb
rusconi@filo:~/devel/debbench$ lintian massxpert_1.6.0-rc2-1_i386.deb
rusconi@filo:~/devel/debbench$ dpkg -c massxpert_1.6.0-rc2-1_i386.deb | less
..... install ./usr/bin/massxpert
..... install ./usr/share/massxpert/pol-seqs/
..... install ./usr/share/massxpert/pol-chem-defs/

