#!/bin/sh
# make_lensfun.sh is a script that generates a DEB Lensfun package
# from SVN source. Aimed at Ubuntu 8.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'

LOG=$HOME/lensfun.log
echo " " > $LOG
SVNDATE="20080710"
DBG=${1---disable-debug}
PAKDBG=""
if [ "${DBG}" = "debug" ]; then DBG="--enable-debug"; PAKDBG=""; echo "Debug is not available for lensfun!" | tee -a $LOG; fi

echo "*** Going to install dependencies from repository..."
sudo apt-get install \
  build-essential python libglib2.0-dev libpng12-dev zlib1g-dev checkinstall subversion

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

# Download source
svn checkout --revision {${SVNDATE}} svn://svn.berlios.de/lensfun/trunk | tee -a $LOG
REL=`cat $LOG | grep "Checked out revision" | sed 's/Checked out revision //' | sed 's/\.//'`
cd trunk

cat > description-pak <<'EOF'
The goal of the lensfun library is to provide a open source database
of photographic lenses and their characteristics. In the past there
was a effort in this direction (see http://www.epaperpress.com/ptlens/),
but then author decided to take the commercial route and the database
froze at the last public stage. This database was used as the basement
on which lensfun database grew, thanks to PTLens author which gave his
permission for this, while the code was totally rewritten from scratch
(and the database was converted to a totally new, XML-based format).

The lensfun library not only provides a way to read the database and
search for specific things in it, but also provides a set of algorithms
for correcting images based on detailed knowledge of lens properties
and calibration data. Right now lensfun is designed to correct distortion,
transversal (also known as lateral) chromatic aberrations, vignetting
and colour contribution index of the lens.
EOF

# Ugly way how to create pkgconfig file
cat > preinstall-pak <<'EOFF'
#!/bin/sh
set -e
if [ ! -d /usr/local/lib64 ]; then
  ln -s /usr/local/lib /usr/local/lib64
fi
if [ ! -d /usr/local/lib64/pkgconfig ]; then
  mkdir /usr/local/lib64/pkgconfig
fi
cat > /usr/local/lib64/pkgconfig/lensfun.pc << 'EOF'
Name: lensfun
Description: A photographic lens database and access library
Version: 0.2.2b
Requires: glib-2.0
Libs: -L/usr/local/lib64/ -llensfun
Cflags: -I/usr/local/include/
EOF
exit 0
EOFF

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

cat > preremove-pak <<'EOF'
#!/bin/sh
set -e
if [ -f /usr/local/lib64/pkgconfig/lensfun.pc ]; then
  rm -f /usr/local/lib64/pkgconfig/lensfun.pc
fi
exit 0
EOF

./configure --prefix=/usr/local | tee -a $LOG

echo "*** Going to create deb package with checkinstall..."
sudo checkinstall \
  --pkgname "lensfun" \
  --pkgversion "0.2.2beta$PAKDBG" \
  --pkgrelease "$REL" \
  --pkggroup "Development/Libraries" \
  --maintainer "Milan Knizek at volny dot cz" \
  --requires "libglib2.0-0" \
  --docdir "/usr/local/share/doc" \
  -D --install=no --default --pakdir $HOME --backup=no \
  make install-lensfun install-lensdb | tee -a "$LOG"

echo "Script $0 completed successfully."

