#!/bin/bash
echo 'This is a wrapper to Trolltech qmake.'
echo 'It will try to detect Sword automatically and setup appropriately.'
if 
[ "$1" = '--help' ]
 then
echo 'Available options are as follows:'
echo
echo '--help                (This help screen)'
echo
echo '--prefix={pathname}   (Install BibleMemorizer components under "pathname".'
echo '                      e.g. {pathname}/bin/biblememorizer, etc.  Often "/usr"'
echo '                      or "/usr/local" or something similar on Linux/Unix.)'
echo
echo '--enable-sword        (Use Sword, default if detected)'
echo '--disable-sword       (Force Sword not to be used)'
echo '--sword-path={path}   (Set Sword include paths.  Only necessary if not'
echo '                      autodetected.)'
echo '--sword-lib={lib}     (Set Sword library compiler flag.  Only necessary if'
echo '                      not autodetected.)'
echo
echo '--enable-debug        (Debug mode)'
echo '--disable-debug       (Release mode, default)'
echo
echo 'Mac specific options:'
echo '--universal           (Universal binary)'
echo '--sdk={path}          (OSX SDK path, needed for universal binary when building'
echo '                      on a PowerPC system.)'
exit 0
fi
SWORDCONFIG=''
SWORDDIR=''
SWORD_LIB=''
if pkg-config --exists sword
then
  SWORDCONFIG="CONFIG += sword"
  SWORDDIR="SWORD_PATH = `pkg-config sword --cflags-only-I | cut -c3-`"
  SWORD_LIB="SWORD_LIB = `pkg-config --libs sword`"
  echo "Sword detected automatically."
else
  echo "Sword not detected."
fi
PATHINFO='MAIN_PATH = /usr'
MODEINFO='CONFIG += release'
MAC_ARCH_INFO=''
MAC_SDK_INFO=''
for param
do
case $param in
--prefix=*)
 PATHINFO="MAIN_PATH = ${param:9}"
  ;;
--sword-path=*)
 SWORDCONFIG="CONFIG += sword"
 SWORDDIR="SWORD_PATH = ${param:13}"
  ;;
--sword-lib=*)
 SWORDCONFIG="CONFIG += sword"
 SWORD_LIB="SWORD_LIB = ${param:12}"
  ;;
--enable-sword)
 SWORDCONFIG="CONFIG += sword"
  ;;
--disable-sword)
 unset SWORDCONFIG
 unset SWORDDIR
 unset SWORD_LIB
 echo "Sword disabled."
  ;;
--enable-debug)
 MODEINFO="CONFIG += debug"
  ;;
--disable-debug)
 MODEINFO="CONFIG += release"
  ;;
--universal)
 MAC_ARCH_INFO="CONFIG += x86 ppc"
  ;;
--sdk=*)
 MAC_SDK_INFO="QMAKE_MAC_SDK = ${param:6}"
 ;;
esac
done
echo 'Script generated options:'
echo $SWORDCONFIG
echo $SWORDDIR
echo $SWORD_LIB
echo $PATHINFO
echo $MODEINFO
echo $MAC_ARCH_INFO
echo $MAC_SDK_INFO
echo "#Variables generated by configure script:" > qmake_vars
echo $SWORDCONFIG >> qmake_vars
echo $SWORDDIR >> qmake_vars
echo $SWORD_LIB >> qmake_vars
echo $PATHINFO >>qmake_vars
echo $MODEINFO >> qmake_vars
echo $MAC_ARCH_INFO >> qmake_vars
echo $MAC_SDK_INFO >> qmake_vars
rm -f Makefile
rm -f src/Makefile
rm -f plugins/Makefile
rm -f plugins/crosswire/Makefile
rm -f plugins/lang_en/Makefile
echo 'Running qmake...'
qmake -o Makefile biblememorizer.pro
echo 'qmake finished.'
echo 'It is normal for qmake to take a very short time and not generate output.'
echo 'Go ahead and run 'make' now (though reading INSTALL first is recommended).'
