#!/bin/bash -ev

# Based on instructions at https://wiki.debian.org/IntroDebianPackaging

# Before using this you probably need to install
#   sudo pbuilder devscripts lintian cdeboostrap (or debootstrap)
# and maybe dpkg-sig.  Also:
# set up for sudo
# set up pbuilder with 
#   sudo emacs -nw /etc/pbuilderrc
#   ...change MIRRORSITE
#   sudo pbuilder create --distribution sarge/etch/whatever
# and/or update with 
#   sudo pbuilder update
# Expect a lot of warnings re LOGNAME - see Debian bug Bug#275118

# TODO: DEBEMAIL

VER=`./VERSION`

TARBALL=fracplanet-${VER}.tar.gz

if [ ! -s ${TARBALL} ] ; then
    echo "Could't find ${TARBALL}" ; 
    exit ;
fi

export DISTRIBUTION=`lsb_release -s -c`

echo "*** Will package ${TARBALL} for distribution \"${DISTRIBUTION}\""
echo -n "*** Starting in 5 seconds..."
for t in 5 4 3 2 1 ; do sleep 1 ; echo -n "." ; done

PROJECT=`echo $TARBALL | sed 's/-.*//'`
TARBALLORIG="${PROJECT}_${VER}.orig.tar.gz"
REV="1${DISTRIBUTION}1"

WORKDIR=pkg_${VER}-${REV}
rm -r -f ${WORKDIR}
mkdir ${WORKDIR}
cd ${WORKDIR}

cp ../${TARBALL} ${TARBALLORIG}

tar xfz ${TARBALLORIG}

if [! -d fracplanet-${VER} ] ; then
    echo "Could't find fracplanet-${VER} after unpacking" ; 
    exit ;
fi

cd fracplanet-${VER}

mkdir debian
dch --create -v ${VER}-${REV} --package fracplanet Create debian package from tarball

echo "9" > debian/compat

cat <<EOF > debian/control
Source: fracplanet
Section: graphics
Priority: optional
Standards-Version: 3.9.4
Maintainer: ${DEBEMAIL}
Build-Depends: debhelper (>=9),qt4-qmake,libqt4-dev,libqt4-opengl-dev,libboost-dev,libboost-program-options-dev,xsltproc
Homepage: http://www.bottlenose.demon.co.uk/share/fracplanet/

Package: fracplanet
Architecture: any
Depends: \${shlibs:Depends}, \${misc:Depends}
Description: Fractal terrain generator
 Fracplanet generates random planets and terrain with oceans, 
 mountains, icecaps and rivers.  Parameters are specified interactively 
 and the results displayed using OpenGL.  The generated objects can be
 exported as geometry for Pov-Ray or Blender, or as textures.
Suggests: blender
Homepage: http://www.bottlenose.demon.co.uk/share/fracplanet/
EOF

echo "Copyright 2014 ${DEBEMAIL}" > debian/copyright
echo "GPL2 license; see /usr/share/common-licenses/GPL-2" >> debian/copyright

echo '#!/usr/bin/make -f' > debian/rules
echo '%:' >> debian/rules
echo -e '\tdh $@' >> debian/rules
echo '' >> debian/rules
echo 'override_dh_auto_configure:' >> debian/rules
echo -e '\tQTDIR=/usr/share/qt4 bash -c "`dpkg-buildflags --export=configure` ./configure"' >> debian/rules
echo '' >> debian/rules
echo 'override_dh_auto_install:' >> debian/rules
echo -e '\tmkdir -p debian/fracplanet/usr/bin && cp fracplanet debian/fracplanet/usr/bin/' >> debian/rules
echo -e '\tmkdir -p debian/fracplanet/usr/share/man/man1 && cp man/man1/fracplanet.1 debian/fracplanet/usr/share/man/man1/' >> debian/rules
echo -e '\tmkdir -p debian/fracplanet/usr/share/doc/fracplanet && cp fracplanet.htm fracplanet.css BUGS TODO NEWS THANKS debian/fracplanet/usr/share/doc/fracplanet/' >> debian/rules

cp fracplanet.menu debian/

chmod 755 debian/rules

echo <<EOF > debian/fracplanet.dirs
/usr/bin
/usr/share/man/man1
/usr/share/doc/fracplanet
EOF

mkdir debian/source
echo "3.0 (quilt)" > debian/source/format

debuild -us -uc -S  # -S just builds dsc.
#debuild -us -uc -b  # Builds binary package, but not in isolated enironment.

cd ..

echo ${PROJECT}_${VER}-${REV}.dsc
ls -lh ${PROJECT}_${VER}-${REV}.dsc
mkdir pbuilder
sudo pbuilder --build --buildresult pbuilder ${PROJECT}_${VER}-${REV}.dsc
sudo chown ${USER}:${USER} pbuilder/*

#RESULT=`(cd .. ; find ${WORKDIR} -name '*.deb')`
#echo "Results: ${RESULT}"
#echo "Don't forget to lintian ${RESULT}"
#echo 'Also dpkg-sig --sign builder -k $DPKGSIG_KEYID any .deb files'
