#!/bin/sh
# make_libpano13.sh is a script that generates a DEB libpano13 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..."
sudo aptitude -y install \
  build-essential autoconf automake libtool flex bison libc6-dev \
  libgcc1 cmake g++ subversion checkinstall zlib1g-dev libpng12-dev \
  libjpeg62-dev libtiff4-dev

LOG=$HOME/libpano13.log
DBG=${1---disable-debug}
PAKDBG=""
if [ "${DBG}" = "debug" ]; then DBG="--enable-debug"; PAKDBG="dbg"; echo "Enabling Debug version" | tee -a $LOG; fi

mkdir -p $HOME/src
cd $HOME/src
#SVNDATE="20090224"
#svn co --revision {${SVNDATE}} https://panotools.svn.sourceforge.net/svnroot/panotools/trunk/libpano libpano13 | tee $LOG
#REL=`cat $LOG | grep "Checked out revision" | sed 's/Checked out revision //' | sed 's/\.//'`
version=libpano13-2.9.17
sourcefile=${version}_beta1.tar.gz
if [ ! -f ${sourcefile} ]; then
  wget http://switch.dl.sourceforge.net/sourceforge/panotools/${sourcefile}
fi
rm -rf ${version}
tar -xvzf ${sourcefile}
cd ${version}

# Patch to allow full fish-eye lens
mv filter.h filter.h_old
sed 's/MAX_FISHEYE_FOV\t\t179/MAX_FISHEYE_FOV\t\t720/' > filter.h < filter.h_old

./bootstrap $DBG --prefix=/usr | tee -a $LOG
if [ -f Makefile ]; then make clean; fi
make | tee -a $LOG

cat > description-pak <<'EOF'
This is the pano13 library, part of the Panorama Tools by Helmut Dersch 
of the University of Applied Sciences Furtwangen.
EOF

cat > postinstall-pak <<'EOF'
#!/bin/sh
set -e
ldconfig
EOF

echo "*** Going to create deb package with checkinstall..."
sudo checkinstall \
  --pkgname "libpano13" \
  --pkgversion "2.9.17" \
  --pkgrelease "beta1" \
  --pkglicense "GPL 3 SIFT" \
  --pkggroup "Applications/Graphics" \
  --maintainer "Milan Knizek at volny dot cz" \
  --requires "zlib1g, libtiff4, libpng12-0, libjpeg62" \
  --docdir "/usr/local/share/doc" \
  -D --install=no --default --pakdir $HOME --backup=no | tee -a "$LOG"

echo "Script $0 completed successfully."

