#! /bin/sh

prefix=/usr/local
destdir=
config=release
QMAKE=
syssqlite=no
mac=no
universal=no
target=10.3
sdk=/Developer/SDKs/MacOSX10.6.sdk

usage="Usage: configure [-prefix DIR] [-destdir DIR] [-qmake PATH] [-debug]
                 [-system-sqlite] [-universal] [-target VERSION] [-sdk PATH]

General options:

  -prefix DIR    Set the instalation prefix to DIR (default: /usr/local)
  -destdir DIR   Set the destination path to DIR
  -qmake PATH    Full path to the 'qmake' program (default: autodetect)
  -debug         Build with debugging symbols
  -system-sqlite Use system SQLite library

OS X options:

  -universal     Build for x86_64, x86 and PPC platforms
  -target        Set OS X deployment target (default: 10.3)
  -sdk           Set OS X SDK (default: /Developer/SDKs/MacOSX10.6.sdk)
"

if test ! -f ./webissues.pro; then
  echo "*** ERROR: Cannot find project file in current directory." >&2
  exit 1
fi

while test $# -gt 0; do
  case $1 in
    -prefix )
      prefix=$2
      shift ; shift
      ;;
    -destdir )
      destdir=$2
      shift ; shift
      ;;
    -qmake )
      QMAKE=$2
      shift; shift
      ;;
    -debug )
      config=debug
      shift
      ;;
    -system-sqlite )
      syssqlite=yes
      shift
      ;;
    -universal )
      universal=yes
      mac=yes
      shift
      ;;
    -target )
      target=$2
      mac=yes
      shift ; shift
      ;;
    -sdk )
      sdk=$2
      mac=yes
      shift ; shift
      ;;
    -help | --help )
      echo "$usage"
      exit
      ;;
    * )
      echo "*** ERROR: Unrecognized option '$1'" >&2
      echo "$usage"
      exit 1
      ;;
  esac
done

echo "Testing for qmake..."

paths=

if test -n "$QMAKE"; then
  if test -x "$QMAKE"; then
    paths=$QMAKE
  else
    echo "*** ERROR: '$QMAKE' is not an executable program." >&2
    exit 1
  fi
else
  for i in qmake qmake-qt4; do
    if which $i >/dev/null 2>/dev/null; then
      paths="$paths `which $i`"
    fi
    if test -n "$QTDIR"; then
      if test -x "$QTDIR/bin/$i"; then
        paths="$paths $QTDIR/bin/$i"
      fi
    fi
  done
fi

if test -z "$paths"; then
  echo "*** ERROR: Cannot find 'qmake' in your PATH." >&2
  exit 1
fi

QMAKE=

for i in $paths; do
  version=`$i -query QT_VERSION`
  if test "$version" != "**Unknown**"; then
    major=`echo $version | cut -d . -f 1`
    minor=`echo $version | cut -d . -f 2`
    patch=`echo $version | cut -d . -f 3`
    if test "$major" -eq 4; then
      if test "$minor" -ge 7; then
        QMAKE=$i
        break
      fi
    fi
  fi
done

if test -z "$QMAKE"; then
  echo "*** ERROR: Cannot find 'qmake' from Qt 4.7 or newer." >&2
  exit 1
fi

echo "Using $QMAKE (Qt $version)"

echo "Writing configuration file..."

echo "# this file was generated by configure" >config.pri
echo "CONFIG += $config" >>config.pri
echo "PREFIX = $prefix" >>config.pri
echo "DESTINATION = $destdir" >>config.pri

if test "$syssqlite" = "yes"; then
  echo "CONFIG += system-sqlite" >>config.pri
fi

if test "$mac" = "yes"; then
  echo "mac {" >>config.pri
  echo "    QMAKE_MACOSX_DEPLOYMENT_TARGET = $target" >>config.pri
  echo "    QMAKE_MAC_SDK = $sdk" >>config.pri
  if test "$universal" = "yes"; then
    echo "    CONFIG += x86_64 x86 ppc" >>config.pri
  fi
  echo "}" >>config.pri
fi

echo "Generating Makefiles..."

if $QMAKE -recursive; then
  echo
  echo "Configure finished. Run 'make' now."
  echo
else
  echo "*** ERROR: Running 'qmake' failed." >&2
  exit 1
fi
