#!/bin/sh
#------------------------------------------------------------------------------
# cb2Bib configuration script:
# - Checks for QTDIR
#   -- If set, checks for qmake at QTDIR
#   -- If not set, checks for qmake at PATH and other usual locations
# - Manages Installation directory data
# - Runs qmake
#
#
# ( originally based upon 'shell script to configure doxygen',
#   http://www.stack.nl/~dimitri/doxygen/
#   Copyright (C) 1997-2001 by Dimitri van Heesch )
#
# Permission to use, copy, modify, and distribute this software and its
# documentation under the terms of the GNU General Public License is hereby
# granted. No representations are made about the suitability of this software
# for any purpose. It is provided "as is" without express or implied warranty
# See the GNU General Public License for more details
#------------------------------------------------------------------------------

bin_dirs=`echo $PATH | sed -e "s/:/ /g"`
platform=`uname -s`

echo
echo "Configuration script for cb2Bib (Unix/Linux/MacOSX)"
echo
f_prefix=/usr
f_bindir=/usr/bin
f_datadir=/usr/share/cb2bib
f_desktopdatadir=/usr/share/applications
f_icondir=/usr/share/pixmaps

f_qmake=NO
f_make=NO
f_kfmclient=NO
f_pdftotext=NO
f_exiftool=NO

while test -n "$1";  do
  case $1 in
    --prefix | -prefix)
       shift; f_prefix=$1 ; f_bindir=$1/bin ; f_datadir=$1/share/cb2bib ; f_desktopdatadir=$1/share/applications ; f_icondir=$1/share/pixmaps
       ;;
    --bindir | -bindir)
       shift; unset f_prefix ; f_bindir=$1
       ;;
    --datadir | -datadir)
       shift; unset f_prefix ; f_datadir=$1/cb2bib
       ;;
    --desktopdatadir | -desktopdatadir)
       shift; unset f_prefix ; f_desktopdatadir=$1
       ;;
    --icondir | -icondir)
       shift; unset f_prefix ; f_icondir=$1
       ;;
    --qmakepath | -qmakepath)
       shift; unset f_qmake ; f_qmake=$1
       ;;
    -h | -help | --help)
       f_help=y
       ;;
    --disable_cbpoll | -disable_cbpoll)
       f_disable_cbpoll=y
       ;;
    *)
       echo $1: unknown argument
       f_help=y
       f_error=y
       ;;
  esac
  shift
done

if test "$f_help" = y; then
  cat <<EOF
Usage: $0 [--help] [--prefix dir] [--bindir dir] [--datadir dir] ...

Options:

  General:
  --help                Print this help
  --qmakepath dir       qmake fullpath name
                        [example:  --qmakepath /usr/bin/qmake]

  --prefix dir          Installation prefix directory
                        [default:  /usr]
  --bindir dir          Executable installation directory
                        [default:  /usr/bin]
  --datadir dir         Data installation directory
                        [default:  /usr/share/bin/cb2bib]

  Unix/Linux only:
  --desktopdatadir dir  Desktop data installation directory
                        [default:  /usr/share/applications]
  --icondir dir         Icon installation directory
                        [default:  /usr/share/pixmaps]
  --disable_cbpoll      Disables clipboard poll

EOF
  test "$f_error" = y && exit 1
  exit 0;
fi

echo "-----------------------------------------------------------------------"
echo "Platform: $platform"
echo "-----------------------------------------------------------------------"
echo "Setting installation directories..."
if test "$f_prefix"; then
  echo "Setting installation relative to dir: $f_prefix"
fi
if test "$f_bindir"; then
  echo "Setting installation executable to dir: $f_bindir"
fi
if test "$f_datadir"; then
  echo "Setting installation cb2Bib data files relative to dir: $f_datadir"
fi
if test "$f_desktopdatadir"; then
  echo "Setting installation desktop data files to dir: $f_desktopdatadir"
fi
if test "$f_icondir"; then
  echo "Setting installation icon file to dir: $f_icondir"
fi
echo "-----------------------------------------------------------------------"

echo
echo "-----------------------------------------------------------------------"
echo "cb2Bib Info:"
echo "-----------------------------------------------------------------------"
echo "- This version requires Qt 4.3.0 or later"
echo " "
echo "- Set the environment variable QTDIR if this script cannot determine"
echo "  the right Qt qmake"
echo "- QTDIR will only be used during configuration; it can later be reset"
echo "  to its original value"
echo " "
echo "- Alternatively, set Qt 4.3 qmake's fullpath name with --qmakepath flag"
echo "-----------------------------------------------------------------------"

#- check for qt ---------------------------------------------------------------

echo
echo "Checking for Qt/qmake:"
  if test "$f_qmake" != NO; then
    echo -n "Checking for qmake tool... "
    if test ! -x "$f_qmake"; then
      echo "Specified $f_qmake could not be found."
      exit 2
    else
      echo "using $f_qmake."
    fi
  elif test -z "$QTDIR"; then
    echo "Environment variable QTDIR not set, checking elsewhere for qmake..."
    echo -n "Checking for qmake tool... "
    if test "$f_qmake" = NO; then
      qmake_name="qmake"
      qmake_dirs="/usr/lib64/qt4/bin /usr/lib/qt4/bin /usr/bin /usr/lib/qt/bin /usr/share/qt/bin /Developer/Tools/Qt $bin_dirs"
      qmake_prog=NO
      for j in $qmake_dirs; do
        if test -x "$j/$qmake_name"; then
          qmake_prog="$j/$qmake_name"
          break 1
        fi
      done
      f_qmake="$qmake_prog" 
    fi
    if test "$f_qmake" = NO; then
      echo "not found!"
      echo
      echo "Please, check if required Qt/qmake is properly installed."
      echo "If so, set QTDIR environment variable to your Qt root directory."
      exit 2
    fi
    echo "using $f_qmake"
  else
    echo "Using QTDIR environment variable for Qt directory location."
    echo "Checking for Qt tools at $QTDIR..."
    echo -n "Checking for qmake tool... "
    if test ! -x "$QTDIR/bin/qmake"; then
      echo "QTDIR is set, but the qmake could not be found in $QTDIR/bin."
      exit 2
    else
      f_qmake="$QTDIR/bin/qmake"
      echo "using $QTDIR/bin/qmake."
    fi
  fi
  $f_qmake -v

# - check for make ------------------------------------------------------------

echo
echo "Checking for additional tools:"
echo -n "Checking for make... "
if test "$f_make" = NO; then
  make_names="gmake make"
  make_dirs="/usr/bin /usr/local/bin /bin /sbin $bin_dirs"
  make_prog=NO
  for i in $make_names; do
    for j in $make_dirs; do
      if test -x "$j/$i"; then
        if test -n "`$j/$i --version 2>/dev/null | grep GNU`"; then
          make_prog="$j/$i"
          break 2
        fi
      fi
    done
  done
  f_make="$make_prog" 
fi

if test "$f_make" != NO; then
  echo "using $f_make"
else
  echo "not found"
fi

# - check for kfmclient -------------------------------------------------------

echo -n "Checking for kfmclient... "
if test "$f_kfmclient" = NO; then
  kfmclient_name="kfmclient"
  kfmclient_dirs="/usr/bin /usr/local/bin /bin /sbin $bin_dirs"
  kfmclient_prog=NO
  for j in $kfmclient_dirs; do
    if test -x "$j/$kfmclient_name"; then
      if test -n "`$j/$kfmclient_name --version 2>/dev/null | grep KDE`"; then
        kfmclient_prog="$j/$kfmclient_name"
        break 1
      fi
    fi
  done
  f_kfmclient="$kfmclient_prog" 
fi

if test "$f_kfmclient" != NO; then
  echo "using $f_kfmclient"
else
  echo "not found"
fi

# - check for pdftotext -------------------------------------------------------

echo -n "Checking for pdftotext... "
if test "$f_pdftotext" = NO; then
  pdftotext_name="pdftotext"
  pdftotext_dirs="/usr/bin /usr/local/bin /bin /sbin $bin_dirs"
  pdftotext_prog=NO
  for j in $pdftotext_dirs; do
    if test -x "$j/$pdftotext_name"; then
        pdftotext_prog="$j/$pdftotext_name"
        break 1
    fi
  done
  f_pdftotext="$pdftotext_prog"
fi

if test "$f_pdftotext" != NO; then
  echo "using $f_pdftotext"
else
  echo "not found"
fi

# - check for exiftool -------------------------------------------------------

echo -n "Checking for exiftool... "
if test "$f_exiftool" = NO; then
  exiftool_name="exiftool"
  exiftool_dirs="/usr/bin /usr/local/bin /bin /sbin $bin_dirs"
  exiftool_prog=NO
  for j in $exiftool_dirs; do
    if test -x "$j/$exiftool_name"; then
        exiftool_prog="$j/$exiftool_name"
        break 1
    fi
  done
  f_exiftool="$exiftool_prog"
fi

if test "$f_exiftool" != NO; then
  echo "using $f_exiftool"
else
  echo "not found"
fi

#- Setting dirs and start qmake -----------------------------------------------

echo
sed 's%^.*cb2bibdata.path .*$%cb2bibdata.path = '$f_datadir'%g' src/src.pro > tmp.pro
sed 's%^.*target.path .*$%target.path = '$f_bindir'%g' tmp.pro > src/src.pro
sed 's%^.*script.path .*$%script.path = '$f_bindir'%g' src/src.pro > tmp.pro
sed 's%^.*desktop.path .*$%desktop.path = '$f_desktopdatadir'%g' tmp.pro > src/src.pro
sed 's%^.*icons.path .*$%icons.path = '$f_icondir'%g' src/src.pro > tmp.pro
mv -f tmp.pro src/src.pro
if test "$f_disable_cbpoll" = y; then
  echo "Setting clipboardPoll Disabled"
  sed 's% disable_cbpoll%%g' src/src.pro > tmp.pro
  sed 's%^CONFIG.*= %CONFIG += disable_cbpoll %g' tmp.pro > src/src.pro
else
  echo "Setting clipboardPoll Enabled (Unix/Linux only)"
  sed 's% disable_cbpoll%%g' src/src.pro > tmp.pro
  mv -f tmp.pro src/src.pro
fi
sed 's%^const QString C2B_DATA_DIR .*$%const QString C2B_DATA_DIR = "'$f_datadir'/";%g' src/cb2Bib_par.h > tmp.par
mv -f tmp.par src/cb2Bib_par.h
if test "$f_kfmclient" != NO; then
  sed 's%BIN = "kfmclient"%BIN = "'$f_kfmclient'"%g' src/cb2Bib_par.h > tmp.par
else
  sed 's%BIN = "kfmclient"%BIN = "kfmclient"%g' src/cb2Bib_par.h > tmp.par
fi
mv -f tmp.par src/cb2Bib_par.h
if test "$f_pdftotext" != NO; then
  sed 's%PDF2TXT_BIN = "pdftotext".*$%PDF2TXT_BIN = "'$f_pdftotext'";%g' src/cb2Bib_par.h > tmp.par
else
  sed 's%PDF2TXT_BIN = "pdftotext".*$%PDF2TXT_BIN = "pdftotext";%g' src/cb2Bib_par.h > tmp.par
fi
mv -f tmp.par src/cb2Bib_par.h
if test "$f_exiftool" != NO; then
  sed 's%C2B_METAPREPARSER_EXIFTOOL_BIN = "exiftool".*$%C2B_METAPREPARSER_EXIFTOOL_BIN = "'$f_exiftool'";%g' src/cb2Bib_par.h > tmp.par
else
  sed 's%C2B_METAPREPARSER_EXIFTOOL_BIN = "exiftool".*$%C2B_METAPREPARSER_EXIFTOOL_BIN = "exiftool";%g' src/cb2Bib_par.h > tmp.par
fi
mv -f tmp.par src/cb2Bib_par.h

if test "$platform" = Darwin; then
echo "Running $f_qmake -spec macx-g++ -o Makefile"
$f_qmake -spec macx-g++ -o Makefile
else
echo "Running $f_qmake -o Makefile"
$f_qmake -o Makefile
fi

echo " "
if test "$f_make" = NO; then
  echo "Configuration ended. 'make' not found."
  echo "Build toolchain might not be completed."
else
  echo "Configuration ended. Type 'make' and 'make install'."
fi
