#! /bin/sh

# This script automates the compilation and installation of tilp & gfm
# from the SVN repository.
# IMPORTANT: * remove equivalent packages, if any, before running this script.
#            * see below for prerequisites (dependencies).
#
# Copyright (C) Lionel Debroux 2009, 2010.

# libti* and tilp are compiled with a proposed set of configuration options,
# but you may wish to use others. The complete list is available through
# `./configure --help` run in $SRCDIR/tilp/libticonv, $SRCDIR/tilp/libtifiles,
# $SRCDIR/tilp/libticables, $SRCDIR/tilp/libticalcs, $SRCDIR/tilp/tilp and $SRCDIR/tilp/gfm.


# The prefix where the binaries will be installed, e.g. $HOME, /usr, /usr/local, /usr/share.
#
# NOTES:
# * if installing to e.g. $HOME or /usr/local, you'll certainly have to execute
# $ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$HOME/lib/pkgconfig
# (or the equivalent setenv command for csh derivatives).
# and put it in your ~/.bashrc (~/.cshrc, etc.), before executing this script.
#
# * after successful installation, you may have to add $PREFIX/bin to $PATH,
# and $PREFIX/lib to $LD_LIBRARY_PATH, for the SVN versions of libti*, tilp & gfm
# to get picked up.
PREFIX="$HOME"


# The place where the sources will be stored.
SRCDIR="$HOME/lpg"


# MANDATORY dependencies for compiling and running libti* and tilp:
# (Debian and Fedora package names are given)
# * pkg-config
# * GNU autoconf, automake, libtool
# * glib 2.x development files (libglib2.0-dev, glib2-devel)
# * zlib development files (zlib1g-dev, zlib-devel)
# * libusb development files (libusb-dev, libusb-devel) - WARNING, only libusb 0.1.x (not even the compatibility layer for 1.0) supported for now !
# * GTK+ 2.x and Glade development files (libgtk2.0-dev + libglade2-dev, gtk2-devel + libglade2-devel)
# * SDL 1.2 development files (libsdl1.2-dev, SDL-devel)
# * GNU gettext
# * GNU bison, flex
# * GNU groff, texinfo
# * XDG utils
#
# Optional dependencies:
# * KDE 3.x development files (kdelibs4-dev, kdelibs3-devel), if you want to compile tilp with support for the KDE file dialog.
#   (this implies Qt development files)


# Checkout/update, `configure`, `make` and `make install` the given module
handle_one_module() {
  module_name="$1"
  shift # Kick the first argument, so as to be able to pass the rest to configure.

  if [ -d "$module_name" -a -d "$module_name/.svn" ]; then
    echo "Updating $module_name"
    cd "$module_name"
    svn up || return 1
    cd ..
  else
    echo "Checking out $module_name"
    svn co "http://svn.tilp.info/repos/tilp/$module_name/trunk" "$module_name" || return 1
  fi

  cd "$module_name"
  
  if ["$module_name" == "libticables"]; then
    patch -Np3 -i ../0001-libticables-add-autotools-infrastructure-for-build-v2.patch
    patch -Np3 -i ../0002-WIP-libticables-add-glue-code-for-libusb-1.0-support.patch
    cp ../link_usb1.c ./src/linux
  fi
  
  echo "Configuring $module_name"
  ./configure "--prefix=$PREFIX" $@ || return 1
  echo "Building $module_name"
  make || return 1
  echo "Installing $module_name"
  make install || return 1
  cd ..
}

echo -e "\033[4mBefore proceeding further, make sure that you're ready to go (look inside the install script):\033[m"
echo -e "1) configured \033[1mPREFIX\033[m and \033[1mSRCDIR\033[m the way you wish;"
echo -e "2) configured \033[1mPKG_CONFIG_PATH\033[m if necessary (otherwise the build will fail!);"
echo -e "3) installed the build dependencies listed in the script;"
echo -e "\033[1mENTER to proceed, CTRL + C to abort\033[m."
read


echo "Creating output folder if necessary"
mkdir -p "$SRCDIR/tilp" || exit 1

cd "$SRCDIR/tilp"
echo "=== libticonv ==="
handle_one_module libticonv || exit 1
# Useful configure options include --disable-nls.
echo "=== libtifiles ==="
handle_one_module libtifiles || exit 1
# Useful configure options include --disable-nls, --enable-logging.
echo "=== libticables ==="
handle_one_module libticables --enable-logging --enable-libusb10 --disable-libusb || exit 1
# Useful configure options include --disable-nls.
echo "=== libticalcs ==="
handle_one_module libticalcs || exit 1

# Use --with-kde if you want to use the native KDE file dialogs (it defaults to disabled because it requires a slew of development package dependencies).
echo "=== gfm ==="
handle_one_module gfm || exit 1
echo "=== tilp ==="
handle_one_module tilp || exit 1

echo "=================================================="
echo "=== libti* + gfm + tilp installed successfully ==="
echo "=================================================="
echo ""
echo ""
echo ""
echo "=================================================="
echo "IMPORTANT NOTES                    IMPORTANT NOTES"
echo "=================================================="
echo "If you want to use TILP as a non-root user, follow the instructions in $SRCDIR/tilp/libticables/CONFIG"
