#!/bin/sh
# make_ufraw.sh is a script that generates a DEB UFRaw package
# from CVS source. Aimed at Ubuntu.

# Note that there could be further UFRaw components installed if
# CinePaint or GIMP are installed (development versions)
# These two editors are not default dependency for this script, however.

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'

LOG="$HOME/ufraw.log"
WHO="`whoami` <mail@home.net>"
PKGN="ufraw"
PKGV="0.16"
CVSDATE="-D 2009-11-13"
#REL=`date --rfc-3339=date | sed 's/-//g'`dbg
PKGR="2-beta`echo $CVSDATE | sed 's/-D //' | sed 's/-//g'`"
TOP="$HOME/src"
DEST="$HOME/tmp/${PKGN}"
ARCH="amd64"

DBG=${1---disable-debug}
PAKDBG=""
if [ "${DBG}" = "debug" ]; then DBG="--enable-debug"; PAKDBG="dbg"; echo "Enabling Debug version" | tee -a $LOG; fi
DEB="${PKGN}_${PKGV}-${PKGR}${PAKDBG}_${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 libglib2.0-dev libbz2-dev \
  zlib1g-dev gettext libexiv2-dev libtiff4-dev libjpeg62-dev \
  libpng12-dev libgtkimageview-dev liblcms1-dev cvs fakeroot \
  liblensfun-dev

mkdir -p $TOP && cd $TOP

# Download the current CVS source code
cat >> $HOME/.cvspass <<'EOF'
/1 :pserver:anonymous@ufraw.cvs.sourceforge.net:2401/cvsroot/ufraw A
EOF

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

# Compile
cd $TOP/$PKGN
if [ -f Makefile ]; then make clean; fi
./autogen.sh | tee -a $LOG
# Possibly optimise for Core 2 Duo:
# (Cannot be used when compiling on different type of CPU)
#CFLAGS="-s -O3 -march=nocona -fomit-frame-pointer" CXXFLAGS="-s -O3 -march=nocona -fomit-frame-pointer" LDFLAGS=-s \
./configure --prefix=/usr/local --enable-mime --enable-extras $DBG | tee -a $LOG
make clean | tee -a $LOG
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}${PAKDBG}" >> ${DEST}/DEBIAN/control
echo "Section: Applications/Graphics" >> ${DEST}/DEBIAN/control
echo "Priority: optional" >> ${DEST}/DEBIAN/control
echo "Architecture: ${ARCH}" >> ${DEST}/DEBIAN/control
echo "Depends: desktop-file-utils, gconf2, libatk1.0-0, libbz2-1.0, libc6, libcairo2, libexiv2-5, libgcc1, libglib2.0-0, libgtk2.0-0, libgtkimageview0, libjpeg62, liblcms1, libpango1.0-0, libpng12-0, libstdc++6, libtiff4, zlib1g, liblensfun0" >> ${DEST}/DEBIAN/control
echo "Maintainer: ${WHO}" >> ${DEST}/DEBIAN/control
echo "Description: UFRaw - camera raw developer" >> ${DEST}/DEBIAN/control
echo " UFRaw is a digital camera raw developer" >> ${DEST}/DEBIAN/control

# Generate post-install for scripts run after DEB installation
cat > ${DEST}/DEBIAN/postinst <<'EOFF'
#!/bin/sh
set -e
mkdir -p /usr/local/share/mime/packages
cat > /usr/local/share/mime/packages/ufraw-mime.xml <<'EOF'
EOFF

cat ufraw-mime.xml >> ${DEST}/DEBIAN/postinst

cat >> ${DEST}/DEBIAN/postinst <<'EOFF'
EOF
GCONF_CONFIG_SOURCE=`gconftool-2 --get-default-source` gconftool-2 --makefile-install-rule /usr/local/share/gconf/schemas/ufraw.schemas
update-desktop-database
exit 0
EOFF

# Generate pre-remove for scripts run before DEB uninstallation
cat > ${DEST}/DEBIAN/prerm <<'EOFF'
#!/bin/sh
set -e
GCONF_CONFIG_SOURCE=`gconftool-2 --get-default-source` gconf-schemas --unregister /usr/local/share/gconf/schemas/ufraw.schemas
GCONF_CONFIG_SOURCE=`gconftool-2 --get-default-source` gconftool-2 --makefile-uninstall-rule /usr/local/share/gconf/schemas/ufraw.schemas
rm -f /usr/local/share/mime/packages/ufraw-mime.xml
update-desktop-database
exit 0
EOFF

# Do some cleaning
chmod 0755 ${DEST}/DEBIAN/postinst ${DEST}/DEBIAN/prerm
find ${DEST} -type d | xargs chmod 755   # this is neccessary on Debian Woody

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

echo "*** Script $0 completed."

