#!/bin/sh
# make_enblend.sh is a script that generates a DEB enblend package
# from CVS 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++ cvs subversion checkinstall \
  pkg-config libtiff4-dev libboost-graph-dev libboost-thread-dev liblcms1-dev \
  libglew-dev libplot-dev libglut3-dev libopenexr-dev libopenexr6 libxi-dev

LOG=$HOME/enblend.log
CVSDATE="-D 2009-11-13"
#REL=`date --rfc-3339=date | sed 's/-//g'`
REL=`echo $CVSDATE | sed 's/-D //' | sed 's/-//g'`
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

cat >> $HOME/.cvspass <<'EOF'
/1 :pserver:anonymous@enblend.cvs.sourceforge.net:2401/cvsroot/enblend A
EOF

cvs -z3 -d:pserver:anonymous@enblend.cvs.sourceforge.net:/cvsroot/enblend co $CVSDATE -P enblend | tee $LOG
cd enblend

cat > gcc.patch <<'EOF'
diff -rup -x CVS enblend/configure.in enblend-new/configure.in
--- enblend/configure.in	2008-09-09 07:33:07.000000000 +0200
+++ enblend-new/configure.in	2009-01-12 11:54:19.000000000 +0100
@@ -35,6 +35,11 @@ LIBS="${OPENEXR_LIBS} $LIBS"
 CFLAGS="${OPENEXR_CFLAGS} $CFLAGS"
 CXXFLAGS="${OPENEXR_CFLAGS} $CXXFLAGS"
 
+# Add --param inline-unit-growth=60 because otherwise enblend can segfault when compiled with newer gcc versions
+if test "$GXX" = yes ; then
+     CXXFLAGS="--param inline-unit-growth=60 $CXXFLAGS"
+fi
+
 
 # Replaced with custom M4 macros, JD Smith, June, 2007
 #AC_CHECK_LIB(glut,glutInitDisplayMode,,AC_MSG_WARN([GLUT is required to compile enblend.]),[-lGL -lGLU])
EOF
patch -p1 < gcc.patch

make -f Makefile.cvs | tee -a $LOG
./configure --prefix=/usr/local $DBG | tee -a $LOG
make clean
make | tee -a $LOG

cat > description-pak <<'EOF'
Enblend is a tool for compositing images using a Burt & Adelson multiresolution spline. Enblend does not align images for you. Use a tool like Hugin or PanoTools to do this.

Enfuse is a tool for automatic exposure and contrast blending. It can be used to fuse an exposure bracketed step automatically into a nicely looking image.
EOF

echo "*** Going to create deb package with checkinstall..."
sudo checkinstall \
  --pkgname "enblend" \
  --pkgversion "4.0beta$PAKDBG" \
  --pkgrelease "$REL" \
  --pkglicense "GPL, VIGRA" \
  --pkggroup "Applications/Graphics" \
  --maintainer "Milan Knizek at volny dot cz" \
  --requires \
    "freeglut3, libc6, libgcc1, libgl1-mesa-glx, libgl1, libglew1.5, libglu1-mesa, \
    libglu1, liblcms1, libplot2c2, libstdc++6, libtiff4" \
  --docdir "/usr/local/share/doc" \
  -D --install=no --default --pakdir $HOME --backup=no | tee -a "$LOG"

echo "Script $0 completed successfully."

