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

# 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"
CVSDATE="-D 2008-09-14"
#REL=`date --rfc-3339=date | sed 's/-//g'`dbg
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


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 checkinstall lensfun

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

# 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 $HOME/src/ufraw
./autogen.sh | tee -a $LOG
# Possibly optimise for Core 2 Duo: CFLAGS="-s -O3 -march=nocona -fomit-framepointer" CXXFLAGS="-s -O3 -march=nocona -fomit-frame-pointer" LDFLAGS=-s \ ./configure ...
./configure --prefix=/usr/local --enable-mime --disable-extras --with-lensfun $DBG | tee -a $LOG
make clean | tee -a $LOG
make | tee -a $LOG


# Generate description-pak for checkinstall
cat README | grep -A 15 "UFRaw - Unidentified Flying Raw" > description-pak

# Generate postinstall-pak for scripts run after DEB installation
cat > postinstall-pak <<'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 >> postinstall-pak

cat >> postinstall-pak <<'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 postremove-pak for scripts run after DEB uninstallation
cat > preremove-pak <<'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

echo "*** Going to create deb package with checkinstall..."
sudo checkinstall \
  --pkgname "ufraw" \
  --pkgversion "0.14beta$PAKDBG" \
  --pkgrelease "$REL" \
  --pkglicense "GPL" \
  --pkggroup "Applications/Graphics" \
  --maintainer "Milan Knizek at volny dot cz" \
  --requires \
    "desktop-file-utils, gconf2, libatk1.0-0, libbz2-1.0, libc6, \
    libcairo2, libexiv2-2, libgcc1, libglib2.0-0, libgtk2.0-0, \
    libgtkimageview0, libjpeg62, liblcms1, libpango1.0-0, libpng12-0, \
    libstdc++6, libtiff4, zlib1g, lensfun" \
  --docdir "/usr/local/share/doc" \
  -D --install=no --default --pakdir $HOME --backup=no | tee -a "$LOG"

echo "Script $0 completed successfully."

