#!/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"`

echo
echo "Configuration script for cb2Bib (Unix/Linux/MacOSX)"
echo
f_prefix=/usr/local
f_bindir=/usr/local/bin
f_datadir=/usr/local/cb2bib

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

while test -n "$1";  do
  case $1 in
    --prefix | -prefix)
       shift; f_prefix=$1 ; f_bindir=$1/bin ; f_datadir=$1/cb2bib
       ;;
    --bindir | -bindir)
       shift; unset f_prefix ; f_bindir=$1
       ;;
    --datadir | -datadir)
       shift; unset f_prefix ; f_datadir=$1/cb2bib
       ;;
    -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: 

  --help                Print this help
  --prefix dir          Installation prefix directory
                        [default:  /usr/local]
  --bindir dir          Executable installation directory
                        [default:  /usr/local/bin]
  --datadir dir         Data installation directory
                        [default:  /usr/local/bin/cb2bib]
  --disable_cbpoll      Disables clipboard poll (Unix/Linux only)

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

echo "Setting installation directories..."
if test "$f_prefix"; then
  echo "Setting instal relative to dir: $f_prefix"
fi
if test "$f_bindir"; then
  echo "Setting instal executable to dir: $f_bindir"
fi
if test "$f_datadir"; then
  echo "Setting instal cb2Bib data files relative to dir: $f_datadir"
fi

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

echo
echo "Checking for Qt/qmake:"
  if 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/lib/qt3/bin /usr/lib/qt/bin /usr/share/qt3/bin /usr/lib/qt3/bin $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..."
    if test ! -d "$QTDIR/lib"; then
      echo "QTDIR is set, but library directory does not exist."
      exit 2
    fi
    if test ! -d "$QTDIR/include"; then
      echo "QTDIR is set, but include directory does not exist."
      exit 2
    fi
    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
    echo -n "Checking for uic tool... "
    if test ! -x "$QTDIR/bin/uic"; then
      echo "QTDIR is set, but the uic tool could not be found in $QTDIR/bin."
      exit 2
    else
      echo "using $QTDIR/bin/uic."
    fi
    echo -n "Checking for moc tool... "
    if test ! -x "$QTDIR/bin/moc"; then
      echo "QTDIR is set, but the moc tool could not be found in $QTDIR/bin."
      exit 2
    else
      echo "using $QTDIR/bin/moc."
    fi
  fi

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

echo
echo "Checking for additional tools:"
echo -n "Checking for make tool... "
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 "not found!";
  echo
  exit 2
fi
echo "using $f_make"

# - 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


#- 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
if test "$f_disable_cbpoll" = y; then
  echo "Setting clipboard poll 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 clipboard poll 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 APP_JABF .*$%const QString APP_JABF = "'$f_datadir'/data/abbreviations.txt";%g' src/cb2bib_par.h > tmp.par
sed 's%^const QString APP_REGEXPF .*$%const QString APP_REGEXPF = "'$f_datadir'/data/regexps.txt";%g' tmp.par > src/cb2bib_par.h
sed 's%^const QString APP_NETQIF .*$%const QString APP_NETQIF = "'$f_datadir'/data/netqinf.txt";%g' src/cb2bib_par.h > tmp.par
sed 's%^const QString APP_PPBIBTEX_BIN .*$%const QString APP_PPBIBTEX_BIN = "'$f_datadir'/c2btools/bib2pdf";%g' tmp.par > src/cb2bib_par.h
sed 's%^const QString APP_PREPARSER_BIN .*$%const QString APP_PREPARSER_BIN = "'$f_datadir'/c2btools/ris2bib";%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%^const QString APP_PDF2TXT_BIN = "pdftotext".*$%const QString APP_PDF2TXT_BIN = "'$f_pdftotext'";%g' src/cb2bib_par.h > tmp.par
else
  sed 's%^const QString APP_PDF2TXT_BIN = "pdftotext".*$%const QString APP_PDF2TXT_BIN = "pdftotext";%g' src/cb2bib_par.h > tmp.par
fi
mv -f tmp.par src/cb2bib_par.h

echo "Running $f_qmake -o Makefile";
$f_qmake -o Makefile
echo " "
echo "Configuration ended. Type 'make' and 'make install'."
