#!/bin/sh
# make_hugin.sh is a script that generates a DEB Hugin package
# from SVN source. Aimed at Ubuntu 9.10

set -o errexit  # Exit when simple command fails.  Same as `set -e'
set -o nounset  # Trigger error when expanding unset variables.  Same as `set -u'

echo "*** Going to install dependencies from repository..."
# TODO: Check the list - not all are probably needed.
sudo aptitude -y install build-essential autoconf automake libtool \
  flex bison libc6-dev libgcc1 cmake g++ subversion checkinstall \
  cmake libopenexr-dev libboost-dev boost-build libglew1.5-dev \
  libboost-thread-dev libboost-graph-dev gettext libwxgtk2.8-dev \
  libexiv2-dev libimage-exiftool-perl libpng12-dev libjpeg62-dev libtiff4-dev \
  libpano13 freeglut3-dev libxmu-dev libxi-dev

mkdir -p $HOME/src
cd $HOME/src

LOG="$HOME/hugin.log"
if [ -f $LOG ] ; then rm $LOG ; fi

DBG=${1-Release}
PAKDBG=""
if [ "${DBG}" = "debug" ]; then DBG="Debug"; PAKDBG="dbg"; echo "Enabling Debug version" | tee -a $LOG; fi

SVNDATE="20100217"
svn co --revision {${SVNDATE}} https://hugin.svn.sourceforge.net/svnroot/hugin/hugin/trunk/ hugin | tee -a $LOG
REL=svn`cat $LOG | grep "Checked out revision" | sed 's/Checked out revision //' | sed 's/\.//'`${PAKDBG}

WHO="`whoami` <mail@home.net>"
PKGN="hugin"
PKGV="0.9.0"
PKGR="${REL}"
#PKGF=${PKGN}-${PKGV}.tar.gz
TOP="${HOME}/src"
DEST="$HOME/tmp/${PKGN}"
ARCH="amd64" # or x86
DEB="${PKGN}_${PKGV}-${PKGR}_${ARCH}.deb"

cd $HOME/src/hugin
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=$DBG . | tee -a $LOG
make clean
make | tee -a $LOG

make DESTDIR=${DEST} install 
mkdir -p -m 0777 ${DEST}/DEBIAN

# Basic description
echo "Package: ${PKGN}" > ${DEST}/DEBIAN/control
echo "Version: ${PKGV}-${PKGR}" >> ${DEST}/DEBIAN/control
echo "Section: Applications/Graphics" >> ${DEST}/DEBIAN/control
echo "Priority: optional" >> ${DEST}/DEBIAN/control
echo "Architecture: ${ARCH}" >> ${DEST}/DEBIAN/control
echo "Depends: libatk1.0-0, libboost-thread1.34.1, libc6, libcairo2, libfontconfig1, libfreetype6, libgcc1, libglib2.0-0, libgtk2.0-0, libjpeg62, libpango1.0-0, libpano13, libpng12-0, libstdc++6, libtiff4, libglew1.5, libwxbase2.8-0, libwxgtk2.8-0, libx11-6, libxcomposite1, libxcursor1, libxdamage1, libxext6, libxfixes3, libxi6, libxinerama1, libxrandr2, libxrender1, zlib1g, libexiv2-5, libimage-exiftool-perl, libpano13, libopenexr6, freeglut3, libxmu, libxi" >> ${DEST}/DEBIAN/control
#echo "Replaces: " >> ${DEST}/DEBIAN/control
echo "Maintainer: ${WHO}" >> ${DEST}/DEBIAN/control
echo "Description: Hugin is a panorama photo stitching program." >> ${DEST}/DEBIAN/control
echo " Hugin is a panorama photo stitching program." >> ${DEST}/DEBIAN/control

# Update dynamic linker cache
cat > ${DEST}/DEBIAN/postinst << 'EOF'
#!/bin/sh
set -e
ldconfig
exit 0
EOF

# Do some cleaning
chmod 0755 ${DEST}/DEBIAN/postinst
find ${DEST} -type d | xargs chmod 755   # this is necessary on Debian Woody, don't ask me why

# Put it all together
fakeroot dpkg-deb --build ${DEST} ${HOME}/${DEB}

echo "Script $0 completed successfully."


