#!/bin/bash
# make_cinepaint.sh is a script that generates a DEB CinePaint package
# from CVS source. Aimed at Ubuntu 9.04

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'

# In later versions, the style how existing ICC profile list is retrieved has changed and does
# not work without Oyranos
CVSDATE="-D 2009-06-05"
REL=`echo $CVSDATE | sed 's/-D //' | sed 's/-//g'`

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

# Start from a green field
echo "*** Deleting previous code..."
sudo rm -rf ${DEST}

echo "*** Going to install dependencies from repository..."
sudo apt-get install build-essential automake libtool gettext cvs fakeroot \
 libgtk1.2-dev libjpeg62-dev libtiff4-dev libx11-dev libfltk1.1-dev \
 libxpm-dev libftgl-dev libopenexr-dev libgtk2.0-dev libxxf86vm-dev libxmu-dev \
 libgutenprintui2-dev libgutenprint-dev liblcms1-dev flex python2.6 python2.6-dev python-gtk2-dev

echo "*** Last CVS version known to compile correctly is as of ${CVSDATE}"
echo ""
mkdir -p $TOP && cd $TOP
 
read -p "*** Should I checkout the CVS? [Type \"yes\" to do so.]" CVSC
if [ $CVSC = "yes" ]; then
  echo "*** Getting CVS source code..."
  if [ -d ${TOP} ] ; then echo "src dir already exists."; else mkdir ${TOP}; fi
  cd ${TOP}
  if ! `grep -q -s ":pserver:anonymous@cinepaint.cvs.sourceforge.net:2401/cvsroot/cinepaint A" < $HOME/.cvspass`; then
    cat >> $HOME/.cvspass <<'EOF'
/1 :pserver:anonymous@cinepaint.cvs.sourceforge.net:2401/cvsroot/cinepaint A
EOF
  fi
  cvs -z3 -d:pserver:anonymous@cinepaint.cvs.sourceforge.net:/cvsroot/cinepaint co $CVSDATE -P cinepaint-project/cinepaint
fi

cd ${TOP}/cinepaint-project/${PKGN}

#echo "*** Getting and applying the patches..."
#wget http://www.milan-knizek.net/files/scripts/cinepaint/2008-11-30_app_cms.c.patch
#patch -p0 < 2008-11-30_app_cms.c.patch

echo "*** Compiling..."
if [ -f Makefile ]; then make clean; fi
sh autogen.sh
./configure --enable-gtk2 --enable-pygimp --prefix=/usr/local

make
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: libfltk1.1, libftgl2, libopenexr6, liblcms1, libgutenprintui2-1, libgtk2.0-0, libjpeg62, libpng12-0, libtiff4, flex, gimp-python" >> ${DEST}/DEBIAN/control
echo "Replaces: iccexamin" >> ${DEST}/DEBIAN/control
echo "Maintainer: ${WHO}" >> ${DEST}/DEBIAN/control
echo "Description: Cinepaint - high bit depth editor" >> ${DEST}/DEBIAN/control
echo " Cinepaint - high bit depth editor" >> ${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."

