#!/bin/sh
#
# Configures to build Valkyrie
#
# This file is part of Valkyrie, a front-end for Valgrind
# Copyright (c) 2000-2005, OpenWorks LLP <info@open-works.co.uk>
# This program is released under the terms of the GNU GPL v.2
# See the file LICENSE.GPL for the full license details.
#

#-------------------------------------------------------------------
# our identity
#-------------------------------------------------------------------
vk_name="valkyrie"
Vk_Name="Valkyrie"
VK_VERSION="1.1.0"
VK_COPYRIGHT="(c) 2003-2005"
VK_AUTHOR="OpenWorks LLP"
VK_EMAIL="info@open-works.co.uk"
VG_COPYRIGHT="(c) 2000-2005, and GNU GPL'd, by Julian Seward et al."


#-------------------------------------------------------------------
# script initialization
#-------------------------------------------------------------------
# the name of this script
relconf=`basename $0`
# the directory of this script is the "source tree"
relpath=`dirname $0`
relpath=`(cd $relpath; /bin/pwd)`
# the current directory is the "build tree" or "object tree"
outpath=`/bin/pwd`
# where we keep all our test scripts
cfgtests=$relpath/config.tests

# later cache the command line in config.status
OPT_CMDLINE=`echo $@ | sed "s,-v ,,g; s,-v$,,g"`


#-------------------------------------------------------------------
# operating system detection
#-------------------------------------------------------------------
UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown
UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
UNAME_SYSTEM=`(uname -s) 2>/dev/null`  || UNAME_SYSTEM=unknown
UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown


#-------------------------------------------------------------------
# initalize variables used for installation
#-------------------------------------------------------------------
# path to our source tree
VK_SOURCE_TREE=$relpath
# path to our build tree
VK_BUILD_TREE=$outpath
# default outpath
VK_INSTALL_PREFIX=$outpath
# default PREFIX/doc
VK_INSTALL_DOCS=       #$VK_INSTALL_PREFIX/doc/
# default PREFIX/bin
VK_INSTALL_BIN=        #$VK_INSTALL_PREFIX/bin/


#-------------------------------------------------------------------
# initalize variables for other people's stuff
#-------------------------------------------------------------------
VG_DIR=             # path to valgrind's install dir
VG_EXEC_PATH=       # path to valgrind executable
VG_SUPP_DIR=        # path to valgrind's suppressions dir

MERGE_EXEC_PATH=    # path to vk_logmerge

#MYSQL_LIB=          # path to MySQL library ( -lmysqlclient )
#MYSQL_INC=          # path to MySQL include directory

QT_DIR=             # path to qt install dir ( /usr/lib/qt3/ )
QT_INC_DIR=         # path to qt include dir ( /usr/lib/qt3/include/ )
QT_LIB_DIR=         # path to qt lib dir     ( /usr/lib/qt3/lib/ )
QT_MOC=             # path to moc            ( /usr/lib/qt3/bin/moc )
QT_UIC=             # path to uic            ( /usr/lib/qt3/bin/uic )
QT_QMAKE=           # path to qmake          ( /usr/lib/qt3/bin/qmake )

#-------------------------------------------------------------------
# initalize variables used for building
#-------------------------------------------------------------------
# window system detection
PLATFORM_X11=yes
SUPPORTED=

# option defaults
OPT_HELP=no
OPT_VERBOSE=no

CFG_SHARED=yes       # whether we are linking dynamically or statically
CFG_THREAD=auto      # if [ auto | yes ] CONFIG += thread if libqt-mt* found

# internal options
CFG_DEBUG=no         # CONFIG += [ debug | release ]
CFG_PROFILE=no       # if yes, add -pg to compiler flags

XPLATFORM=
PLATFORM=$QMAKESPEC

QMAKE_CONFIG=        # CONFIG flags

QMAKE_CFLAGS=        # extra c flags to pass to the compiler
QMAKE_CXXFLAGS=      # extra c++ flags to pass to the compiler
QMAKE_LFLAGS=        # 
QMAKE_LIBS=          # name of the library, eg. '-lmysqlclient'
QMAKE_LIBDIR_FLAGS=  # library search paths
QMAKE_INCLUDEPATH=   # include search paths


#-------------------------------------------------------------------
# parse command line arguments
#-------------------------------------------------------------------
INVALID_ARG=none
while [ "$#" -gt 0 ]; do

  CURRENT_OPT="$1"
  optarg=`expr "x$CURRENT_OPT" : 'x[^=]*=\(.*\)'`

  case $1 in
    # options without an argument
    -h | --help | -help)
      OPT_HELP=yes ;;
    -q | --quiet | -quiet)
      OPT_VERBOSE=no ;;
    -v | --verbose | -verbose)
      OPT_VERBOSE=yes ;;
    # installation paths
    --prefix=* | -prefix=*)
      VK_INSTALL_PREFIX=$optarg ;;
    --bindir=* | -bindir=*)
      VK_INSTALL_BIN=$optarg ;;
    --docdir=* | -docdir=*)
      VK_INSTALL_DOCS=$optarg ;;
    # program paths
    --vg-dir=* | -vg-dir=*)
      VG_DIR=$optarg ;;
    --vg-exec=* | -vg-exec=*)
      VG_EXEC_PATH=$optarg ;;
    --vg-supp=* | -vg-supp=*)
      VG_SUPP_DIR=$optarg ;;
    --qt-dir=* | -qt-dir=*)
      QT_DIR=$optarg ;;
    --qt-lib=* | -qt-lib=*)
      QT_LIB_DIR=$optarg ;;
#    --mysql-lib=* | -mysql-lib=*)
#      MYSQL_LIB=$optarg ;;
#    --mysql-inc=* | -mysql-inc=*)
#      MYSQL_INC=$optarg ;;
    # compile options (might have multiple of these)
    --shared | -shared)
      CFG_SHARED=yes ;;
    --static | -static)
      CFG_SHARED=no ;;
    --thread=* | -thread=*)
      if [ "$optarg" = "yes" ] || [ "$optarg" = "no" ]; then
        CFG_THREAD=$optarg
      else
        INVALID_ARG="--thread: '$optarg'"
      fi ;;
    # these are internal options
    --release | -release)
      CFG_DEBUG=no ;;
    --debug | -debug)
      CFG_DEBUG=yes ;;
    --profile | -profile)
      CFG_PROFILE=yes ;;
    # system options
#    --platform=* | -platform=*)
#      PLATFORM=$optarg
#      case $PLATFORM in
#        # keep compatibility with old platform names
#        aix-64)        PLATFORM=aix-xlc-64     ;;
#        hpux-o64)      PLATFORM=hpux-acc-o64   ;;
#        hpux-n64)      PLATFORM=hpux-acc-64    ;;
#        hpux-acc-n64)  PLATFORM=hpux-acc-64    ;;
#        irix-o32)      PLATFORM=irix-cc-o32    ;;
#        irix-n32)      PLATFORM=irix-cc        ;;
#        irix-64)       PLATFORM=irix-cc-64     ;;
#        irix-cc-n64)   PLATFORM=irix-cc-64     ;;
#        reliant-64)    PLATFORM=reliant-cds-64 ;;
#        solaris-64)    PLATFORM=solaris-cc-64  ;;
#        solaris-64)    PLATFORM=solaris-cc-64  ;;
#        openunix-cc)   PLATFORM=unixware-cc    ;;
#        openunix-g++)  PLATFORM=unixware-g++   ;;
#        unixware7-cc)  PLATFORM=unixware-cc    ;;
#        unixware7-g++) PLATFORM=unixware-g++   ;;
#      esac ;;
    # anything else
    *)
      OPT_HELP=yes
      echo "${1}: unknown argument"
      echo "${CURRENT_OPT}: invalid command-line switch"
      exit 0
      shift
      continue
  esac
  shift
done

#-------------------------------------------------------------------
# see if we have anything in INVALID_ARG
#-------------------------------------------------------------------
if [ ! "$INVALID_ARG" = "none" ]; then
  echo
  echo "ERROR: Invalid arg given to $INVALID_ARG"
  OPT_HELP=yes
fi


#-------------------------------------------------------------------
# help: emit a usage message if -h flag given or something failed
# Future options:
#   --mysql-lib=dir ....... Path to MySQL library (-lmysqlclient)
#   --mysql-inc=dir ....... Path to MySQL include directory
# Internal options:
#   --profile ............. Compile and linke with profiling turned on
#   --release ............. Compile and link with debugging turned off
#   --debug ............... Compile and link with debugging turned on
#-------------------------------------------------------------------
if [ "$OPT_HELP" = "yes" ]; then

cat <<_HPEOF

Usage: ./$relconf [-h] [--prefix=dir] [--bindir=dir] [--docdir=dir] 
       [--vg-dir] [--vg-exec=] [--vg-supp=] [--qt-dir=] [--qt-lib=] 
       [--shared] [--static] [--thread=]

Option defaults (*) are usually acceptable. Here is a short explanation 
of each option:

   -h, --help ............ Print this help and usage information

Fine tuning of the installation directories (optional):
   --prefix=dir .......... This will install everything relative to dir
                             [ default $VK_INSTALL_PREFIX ]
   --bindir=dir .......... Executables will be installed to dir
                             [ default $VK_INSTALL_PREFIX/bin ]
   --docdir=dir .......... Documentation will be installed to dir
                             [ default $VK_INSTALL_PREFIX/doc ]

Program paths:
   --vg-dir=dir .......... Path to Valgrind's installation directory
   --vg-exec=path ........ Path to the Valgrind executable
                             [ default VG-DIR/bin/ ]
   --vg-supp=dir ......... Path to Valgrind suppression files directory
                             [ default VG-DIR/lib/valgrind/ ]
   --qt-dir=dir .......... Path to the Qt installation directory
   --qt-lib=dir .......... Path to the Qt libraries directory

Compile options:
 * --shared .............. Use a shared Qt library [libqt so]
   --static .............. Use a static Qt library [libqt a]
 * --thread=arg .......... Do / do not compile threading support

_HPEOF
  exit 0
fi



#-------------------------------------------------------------------
# where to find which
#-------------------------------------------------------------------
WHICH=`which which 2>/dev/null`
if [ -z "$WHICH" ]; then
  if which which >/dev/null 2>&1; then
    WHICH=which
  else
    SEARCH_DIRS="/usr/ucb /usr/bin /bin /usr/local/bin"
    for dir in $SEARCH_DIRS; do
      if [ -x $dir/which ]; then
        WHICH=$dir/which
        break;
      fi
    done
  fi
fi

if [ ! -z "$WHICH" ]; then
  w=`which "$1" 2>/dev/null`
  if [ ! -z "$w" -a -x "$w" ]; then
    echo "$w"
  fi
else
  IFS=:
  for w in $PATH; do
    if [ -x $w/$1 ]; then
      echo "$w/$1"
    fi
  done
fi



#-------------------------------------------------------------------
# find out which awk we want to use
# prefer gawk, then nawk, then awk
#-------------------------------------------------------------------
AWK=
for e in gawk nawk awk; do
  if $WHICH $e >/dev/null 2>&1 && ( $e -f /dev/null /dev/null ) >/dev/null 2>&1; then
    AWK=$e
    break
  fi
done
if [ -z "$AWK" ]; then
  echo "You don't seem to have any flavour of 'awk' in your PATH"
  echo "Cannot proceed."
  exit 1
fi


#-------------------------------------------------------------------
# find a make command
#-------------------------------------------------------------------
MAKE=
for mk in make gmake; do
  if $WHICH $mk >/dev/null 2>&1; then
    MAKE=`$WHICH $mk`
    break
  fi
done
if [ -z "$MAKE" ]; then
  echo "You don't seem to have 'make' or 'gmake' in your PATH."
  echo "Cannot proceed."
  exit 1
fi



#-------------------------------------------------------------------
# create a temporary cache file for the tests to write to
#-------------------------------------------------------------------
VK_CACHE=$relpath/.vk.cache
[ -f $VK_CACHE ] && rm -f $VK_CACHE
cat >>$VK_CACHE<<_VKEOF
_VKEOF


#-------------------------------------------------------------------
# where is Qt ?
# if not supplied on the cmd-line, see if which can find it
#-------------------------------------------------------------------
$cfgtests/qt.test $VK_CACHE $CFG_THREAD $QT_DIR $QT_LIB_DIR && QT_RESULT="$QT_RESULT qt"
if [ -z "$QT_RESULT" ] ; then
  rm -f $VK_CACHE
  exit 1
fi


#-------------------------------------------------------------------
# where is valgrind ?
# if not supplied on the cmd-line, see if which can find it
#-------------------------------------------------------------------
if [ -z "$VG_DIR" ] ; then
  VG_DIR="no"
  if [ -z "$VG_EXEC_PATH" ] ; then
    vg_exec=`$WHICH valgrind | sed "s/*//g"`
    if [ -x "$vg_exec" ] ; then
      VG_EXEC_PATH="$vg_exec"
    fi
  fi
fi

$cfgtests/valgrind.test $VK_CACHE $VG_DIR $VG_EXEC_PATH $VG_SUPP_DIR



#-------------------------------------------------------------------
# where is mysql ( -lmysqlclient and mysql.h ) ?
# if not supplied on the cmd-line, try to find it
#-------------------------------------------------------------------
#$cfgtests/mysql.test $VK_CACHE $MYSQL_LIB $MYSQL_INC && MYSQL_RESULT="$MYSQL_RESULT mysql"
#if [ -z "$MYSQL_RESULT" ] ; then
#  rm -f $VK_CACHE
#  exit 1
#fi


#---------------------------------------------------------------------------
# rescue the stashed options/flags from the temporary cache
#--------------------------------------------------------------------------
#MYSQL_LIB_PATH=""
if [ ! -f $VK_CACHE ] ; then
  echo "Cache '$VK_CACHE' not found - Aborting"
  exit 1
fi
if [ -r $VK_CACHE ]; then
  while read line; do
    VAR=`echo $line | sed "s,^\(.*\)=.*,\1,"`
    VAL=`echo $line | sed "s,^.*=\(.*\),\1,"`
    case $VAR in 
      QT_DIR)         QT_DIR=$VAL ;;
      QT_INC_DIR)     QT_INC_DIR=$VAL ;;
      QT_LIB_DIR)     QT_LIB_DIR=$VAL ;;
      QT_MOC)         QT_MOC=$VAL ;;
      QT_UIC)         QT_UIC=$VAL ;;
      QT_QMAKE)       QT_QMAKE=$VAL ;;
      VG_EXEC_PATH)   VG_EXEC_PATH=$VAL ;;  # only used in vk_include_h
      VG_SUPP_DIR)    VG_SUPP_DIR=$VAL ;;   # ditto
#     MYSQL_LIB)      MYSQL_LIB=$VAL ;;
#     MYSQL_LIB_PATH) MYSQL_LIB_PATH=$VAL ;;
#     MYSQL_INC)      MYSQL_INC=$VAL ;;
      *) ;;
    esac
  done < $VK_CACHE
fi

# delete the temporary cache
[ -f $VK_CACHE ] && rm -f $VK_CACHE


# FIXME: kludging this for now...
MERGE_EXEC_PATH="$relpath/bin/vk_logmerge"


#-------------------------------------------------------------------
# we have all the info we need to generate vk_include.h at this point.
# note: if the #includes are changed in any way, update vk_utils.cpp
#-------------------------------------------------------------------
echo "Creating ./src/vk_include.h..."
vk_include_h=$outpath/src/vk_include.h
[ -f $vk_include_h ] && rm -f $vk_include_h

# get the date
today=`date`

cat > $vk_include_h <<_INC_EOF
/* ---------------------------------------------------------------------- 
 * Automatically generated by configure on $today            vk_include.h
 * Do not edit this file - all changes will be lost
 * ----------------------------------------------------------------------
 * This file is part of Valkyrie, a front-end for Valgrind
 * Copyright (c) 2000-2005, OpenWorks LLP <info@open-works.co.uk>
 * This program is released under the terms of the GNU GPL v.2
 * See the file LICENSE.GPL for the full license details.
 */

#ifndef __VK_INCLUDE_H
#define __VK_INCLUDE_H

/* valkyrie install paths ---------------------------------------------- */
#define PREFIX        "$VK_INSTALL_PREFIX"
#define VK_DOC_PATH   "/doc/"
#define VG_DOC_PATH   "/doc/vg-html/"
#define VK_ICONS_PATH "/icons/"
#define VK_DBASE_DIR  "/dbase/"
#define VK_LOGS_DIR   "/logs/"
#define VK_SUPPS_DIR  "/suppressions/"

/* valgrind paths ------------------------------------------------------ */
#define CFG_VG_EXEC_PATH  "$VG_EXEC_PATH"
#define CFG_VG_SUPP_DIR   "$VG_SUPP_DIR"

/* other executables --------------------------------------------------- */
#define CFG_MERGE_EXEC_PATH "$MERGE_EXEC_PATH"

/* names --------------------------------------------------------------- */
#define Vk_Name      "$vk_name"
#define VK_NAME      "$Vk_Name"

/* version, as 'MAJOR-NUMBER.MINOR-NUMBER[.PATCHLEVEL]'.
   A version in the form 'YYYY-MM-DD' is a release-of-the-day, 
   i.e. a snapshot of the current development tree --------------------- */
#define VK_VERSION    "$VK_VERSION"
#define VK_COPYRIGHT  "$VK_COPYRIGHT"
#define VK_AUTHOR     "$VK_AUTHOR"
#define VK_EMAIL      "$VK_EMAIL"
/* so we do not have to update hard-wired-in dates for valgrind */
#define VG_COPYRIGHT  "$VG_COPYRIGHT"

#endif
_INC_EOF


#-------------------------------------------------------------------
# auto-detect all that hasn't been specified in the arguments
#-------------------------------------------------------------------
if [ -z "$PLATFORM" ]; then
  case "$UNAME_SYSTEM:$UNAME_RELEASE" in
    Darwin:*)   PLATFORM=darwin-g++   ;;
    AIX:*)      PLATFORM=aix-xlc      ;;
    QNX:*)      PLATFORM=qnx-g++      ;;
    GNU:*)      PLATFORM=hurd-g++     ;;
    dgux:*)     PLATFORM=dgux-g++     ;;
    ULTRIX:*)   PLATFORM=ultrix-g++   ;;
    FreeBSD:*)  PLATFORM=freebsd-g++  ;;
    OpenBSD:*)  PLATFORM=openbsd-g++  ;;
    NetBSD:*)   PLATFORM=netbsd-g++   ;;
    IRIX*:*)    PLATFORM=irix-cc      ;;
    HP-UX:*)    PLATFORM=hpux-acc     ;;
    OSF1:*)     PLATFORM=tru64-cxx    ;;
    Linux:*)    PLATFORM=linux-g++    ;;
    SunOS:5*)   PLATFORM=solaris-cc   ;;
    CYGWIN*:*)  PLATFORM=cygwin-g++   ;;
    LynxOS*:*)  PLATFORM=lynxos-g++   ;;
    OpenUNIX:*) PLATFORM=unixware-cc  ;;
    UnixWare:*) PLATFORM=unixware-cc  ;;
    SCO_SV:*)   PLATFORM=sco-cc       ;;
    UNIX_SV:*)  PLATFORM=unixware-g++ ;;
    BSD/OS:*|BSD/386:*)         PLATFORM=bsdi-g++    ;;
    ReliantUNIX-*:*|SINIX-*:*)  PLATFORM=reliant-cds ;;
    *)
      echo
      for p in $PLATFORMS; do
        echo "   $relconf $* -platform $p"
      done
      echo >&2
      echo "   The build script does not currently recognize all" >&2
      echo "   platforms supported by Qt." >&2
#      echo "   Rerun this script with a --platform option listed" >&2
#      echo "   to set the system/compiler combination you use." >&2
      echo >&2
      exit 2
  esac
fi


PLATFORMS=`find $QT_DIR/mkspecs/ -type f | grep -v qws | sed "s,$QT_DIR/mkspecs/qws/,,"`
[ -z "$XPLATFORM" ] && XPLATFORM="$PLATFORM"
if [ -d "$PLATFORM" ]; then
  QMAKESPEC="$PLATFORM"
else
  QMAKESPEC="$QT_DIR/mkspecs/${PLATFORM}"
fi
if [ -d "$XPLATFORM" ]; then
  XQMAKESPEC="$XPLATFORM"
else
  XQMAKESPEC="$QT_DIR/mkspecs/${XPLATFORM}"
fi

# check specified platforms are supported --------------------------
if [ '!' -d "$QMAKESPEC" ]; then
  echo "   The specified system/compiler is not supported:"
  echo "     $QMAKESPEC"
  echo "   Please see the Qt PLATFORMS file for a complete list."
  exit 2
fi

# now look at the configs and figure out what platform we are config'd for
[ '!' -z "`grep QMAKE_LIBS_X11 $XQMAKESPEC/qmake.conf | awk '{print $3;}'`" ] && PLATFORM_X11=yes
# find default thread option for target
if grep >/dev/null '^QMAKE_LIBS_THREAD' $XQMAKESPEC/qmake.conf; then
  AUTOTHREAD=yes
else
  AUTOTHREAD=never
fi
# common qmake.conf file but dependency on OS version
if [ "$UNAME_SYSTEM" = "HP-UX" ]; then
  if echo $UNAME_RELEASE | grep -E "^B\.(09|10)" >/dev/null 2>&1; then
    AUTOTHREAD=never
  fi
fi

if [ $AUTOTHREAD = never ]; then
  if [ "$CFG_THREAD" = "yes" ]; then
    echo
    echo "ERROR: Qt is not configured to support threading on this platform"
    echo "       See the THREAD settings in $XQMAKESPEC/qmake.conf"
    echo
    exit 2
  fi
  AUTOTHREAD=no
fi
[ "$CFG_THREAD" = "auto" ] && CFG_THREAD=$AUTOTHREAD


#---------------------------------------------------------------------------
# Give feedback on configuration
#---------------------------------------------------------------------------
PRNT_VG_EXEC_PATH=$VG_EXEC_PATH
if [ "$PRNT_VG_EXEC_PATH" == "" ]; then
		PRNT_VG_EXEC_PATH="(not found)"
fi

echo
echo "Configuration:"
echo "  Installing to ........ $VK_INSTALL_PREFIX/"
if [ "$XPLATFORM" = "$PLATFORM" ]; then
echo "  Build type ........... $PLATFORM"
else
echo "  Building on .......... $PLATFORM"
echo "  Building for ......... $XPLATFORM"
fi
echo "  Qt ................... $QT_DIR/"
echo "  Valgrind ............. $PRNT_VG_EXEC_PATH"
output="yes"
#[ -z "$MYSQL_LIB" ] && output="no"
#echo "  MySQL support ........ $output"
echo "  Thread support ....... $CFG_THREAD"
output="dynamically (shared)"
[ "$CFG_SHARED" = "no" ] && output="statically"
echo "  Linking: ............. $output"
output="release"
[ "$CFG_DEBUG" = "yes" ] && output="debug"
echo "  Compile type ......... $output"
if [ "$CFG_PROFILE" = "yes" ] ; then
echo "  Profiling enabled..... $CFG_PROFILE"
fi
output="warn_off"
[ "$CFG_DEBUG" = "yes" ] && output="warn_on"
echo "  Compiler warnings .... $output"


#---------------------------------------------------------------------------
# Looking good to go -- create any necessary dirs
#---------------------------------------------------------------------------
# default PREFIX/doc
VK_INSTALL_DOCS=$VK_INSTALL_PREFIX/doc
# default PREFIX/bin
VK_INSTALL_BIN=$VK_INSTALL_PREFIX/bin

if [ ! -d $VK_INSTALL_PREFIX ]; then
  echo 
  echo "creating '$VK_INSTALL_PREFIX'"
  mkdir -p $VK_INSTALL_PREFIX
fi

#---------------------------------------------------------------------------
# If we are installing to anything other than the default, create symlinks
#---------------------------------------------------------------------------
if [ ! "$VK_INSTALL_PREFIX" = "$outpath" ] ; then
  echo "Creating symlinks"
echo "relpath = $relpath;  outpath = $outpath"
  # delete old link; symlink the bin directory
   rm -f $VK_INSTALL_BIN
   ln -s $relpath/bin $VK_INSTALL_BIN
  # delete old link; symlink the doc directory
  rm -f $VK_INSTALL_DOCS
  ln -s $relpath/doc $VK_INSTALL_DOCS
fi


#---------------------------------------------------------------------------
# Create the project file, including all headers/sources
#---------------------------------------------------------------------------
echo
echo "Creating project file $vk_name.pro..."
# name of this project file
VK_PROJECT=$relpath/$vk_name.pro
[ -f $VK_PROJECT ] && rm -f $VK_PROJECT

# compose the configuration flags
QMAKE_CONFIG="qt x11"
if [ "$CFG_DEBUG" = "yes" ]; then
  QMAKE_CONFIG="$QMAKE_CONFIG debug warn_on"
  DEBUG_FLAGS="-O0 -g -Wundef -Wredundant-decls -Wno-unreachable-code -Werror -fno-inline"
  QMAKE_CFLAGS="$QMAKE_CFLAGS $DEBUG_FLAGS"
  QMAKE_CXXFLAGS="$QMAKE_CXXFLAGS $DEBUG_FLAGS"
elif [ "$CFG_DEBUG" = "no" ]; then
  QMAKE_CONFIG="$QMAKE_CONFIG release warn_off"
fi
if [ "$CFG_THREAD" = "yes" ]; then
  QMAKE_CONFIG="$QMAKE_CONFIG thread"
fi
if [ "$QMAKESPEC" = "$XQMAKESPEC" ]; then
  QMAKE_CONFIG="$QMAKE_CONFIG nocrosscompiler"
fi

# compose the compiler flags
if [ "$CFG_PROFILE" = "yes" ] ; then
  QMAKE_CFLAGS="$QMAKE_CFLAGS -pg"
  QMAKE_CXXFLAGS="$QMAKE_CXXFLAGS -pg"
  QMAKE_LFLAGS="$QMAKE_LFLAGS -pg"
fi

# compose any extra libs
#if [ -n "$MYSQL_LIB" ] && [ -n "$MYSQL_LIB_PATH" ] ; then
#  QMAKE_LIBS="$QMAKE_LIBS $MYSQL_LIB"
#  QMAKE_LIBDIR_FLAGS="$QMAKE_LIBDIR_FLAGS $MYSQL_LIB_PATH"
#  QMAKE_INCLUDEPATH="$QMAKE_INCLUDEPATH $MYSQL_INC"
#fi
# this lots goes after 'compiler flags' below 
# additional libraries and includes
#LIBS += $QMAKE_LIBS
#QMAKE_LIBDIR_FLAGS += $QMAKE_LIBDIR_FLAGS
#INCLUDEPATH += $QMAKE_INCLUDEPATH


#some dists need this (e.g. old Debian)
QMAKE_LIBS="$QMAKE_LIBS -lfreetype"


# save to tmp.pro
cat > $relpath/tmp.pro <<_PRO_EOF
# Project file to create a Makefile
# Automatically generated by configure on $today

# the kind of Makefile to create
TEMPLATE = app
CONFIG   = $QMAKE_CONFIG

# target name and destination, + where to put the moc and object files
TARGET      = $vk_name
DESTDIR     = ./bin
MOC_DIR     = ./.moc
OBJECTS_DIR = ./.obj

# compiler flags
QMAKE_CFLAGS   += $QMAKE_CFLAGS
QMAKE_CXXFLAGS += $QMAKE_CXXFLAGS
QMAKE_LFLAGS   += $QMAKE_LFLAGS

# additional libraries and includes
LIBS += $QMAKE_LIBS
QMAKE_LIBDIR_FLAGS += $QMAKE_LIBDIR_FLAGS
INCLUDEPATH += $QMAKE_INCLUDEPATH

# paths to qt things
QMAKE_MOC   = $QT_MOC
QMAKE_UIC   = $QT_UIC -L $QT_DIR/plugins
QMAKE_QMAKE = $QT_QMAKE
QMAKE_INCDIR_QT = $QT_INC_DIR
QMAKE_LIBDIR_QT = $QT_LIB_DIR
QMAKE_RTLDIR_QT = $QT_LIB_DIR
QMAKESPEC       = $QT_DIR/mkspecs/linux-g++

# paths to valkyrie things
QMAKE_ABSOLUTE_SOURCE_ROOT = $VK_SOURCE_TREE
QT_SOURCE_TREE    = $VK_SOURCE_TREE
QT_BUILD_TREE     = $VK_BUILD_TREE
QT_INSTALL_PREFIX = $VK_INSTALL_PREFIX
docs.path         = $VK_INSTALL_DOCS
bins.path         = $VK_INSTALL_BIN

_PRO_EOF


# generate the list of headers + sources
EXEC="$QT_QMAKE -project -nopwd ./src ./icons/ -o $relpath/tmp.sources"
/bin/sh -c "$EXEC"

# concatenate the two files ...
EXEC="cat tmp.pro tmp.sources > $VK_PROJECT"
/bin/sh -c "$EXEC"

# ... and delete the temporary files
rm -f $relpath/tmp.pro
rm -f $relpath/tmp.sources


#---------------------------------------------------------------------------
# build a Makefile based on the configuration
#---------------------------------------------------------------------------
VK_MAKEFILE="Makefile.$vk_name"
echo "Creating $VK_MAKEFILE from $vk_name.pro..."

EXEC=""
OUTDIR=$outpath
EXEC="$QT_QMAKE -makefile -spec $XQMAKESPEC -o $VK_MAKEFILE $VK_PROJECT"
#echo "EXEC = $EXEC"

[ -f "${OUTDIR}/$VK_MAKEFILE" ] && chmod +rw ${OUTDIR}/$VK_MAKEFILE
/bin/sh -c "$EXEC"

# delete temporary files
rm -f config.log 





#---------------------------------------------------------------------------
# Create the logmerge project file, including all headers/sources
#---------------------------------------------------------------------------
vk_logmerge="vk_logmerge"

echo "Creating project file $vk_logmerge.pro..."
# name of this project file
LM_PROJECT=$relpath/$vk_logmerge.pro
[ -f $LM_PROJECT ] && rm -f $LM_PROJECT

# keep all the configurations flags from before...

# create project file:
cat > $LM_PROJECT <<_LM_PRO_EOF
# Project file to create a Makefile
# Automatically generated by configure on $today

# the kind of Makefile to create
TEMPLATE = app
CONFIG   = $QMAKE_CONFIG

# target name and destination, + where to put the moc and object files
TARGET      = $vk_logmerge
DESTDIR     = ./bin
MOC_DIR     = ./vk_logmerge/.moc
OBJECTS_DIR = ./vk_logmerge/.obj

# compiler flags
QMAKE_CFLAGS   += $QMAKE_CFLAGS
QMAKE_CXXFLAGS += $QMAKE_CXXFLAGS
QMAKE_LFLAGS   += $QMAKE_LFLAGS

# additional libraries and includes
LIBS += $QMAKE_LIBS
QMAKE_LIBDIR_FLAGS += $QMAKE_LIBDIR_FLAGS
INCLUDEPATH += $QMAKE_INCLUDEPATH

# paths to qt things
QMAKE_MOC   = $QT_MOC
QMAKE_UIC   = $QT_UIC -L $QT_DIR/plugins
QMAKE_QMAKE = $QT_QMAKE
QMAKE_INCDIR_QT = $QT_INC_DIR
QMAKE_LIBDIR_QT = $QT_LIB_DIR
QMAKE_RTLDIR_QT = $QT_LIB_DIR
QMAKESPEC       = $QT_DIR/mkspecs/linux-g++

# paths to valkyrie things
QMAKE_ABSOLUTE_SOURCE_ROOT = $VK_SOURCE_TREE
QT_SOURCE_TREE    = $VK_SOURCE_TREE
QT_BUILD_TREE     = $VK_BUILD_TREE
QT_INSTALL_PREFIX = $VK_INSTALL_PREFIX
docs.path         = $VK_INSTALL_DOCS
bins.path         = $VK_INSTALL_BIN


######################################################################
# Note: dependency on main valkyrie src tree: xml_parser
######################################################################

TEMPLATE = app
DEPENDPATH += vk_logmerge
INCLUDEPATH += vk_logmerge

# Input
HEADERS += vk_logmerge/vklm_main.h \
           vk_logmerge/vk_logmerge.h

SOURCES += vk_logmerge/vklm_main.cpp \
           vk_logmerge/vk_logmerge.cpp

_LM_PRO_EOF


#---------------------------------------------------------------------------
# build a Makefile based on the configuration
#---------------------------------------------------------------------------
LM_MAKEFILE="Makefile.$vk_logmerge"
echo "Creating $LM_MAKEFILE from $vk_logmerge.pro..."

EXEC=""
OUTDIR=$outpath
EXEC="$QT_QMAKE -makefile -spec $XQMAKESPEC -o $LM_MAKEFILE $LM_PROJECT"
#echo "EXEC = $EXEC"

[ -f "${OUTDIR}/$LM_MAKEFILE" ] && chmod +rw ${OUTDIR}/$LM_MAKEFILE
/bin/sh -c "$EXEC"

# delete temporary files
rm -f config.log 





#---------------------------------------------------------------------------
# Write the top-level Makefile
#---------------------------------------------------------------------------
echo "Creating top-level Makefile..."
cat > ${OUTDIR}/Makefile <<_MKFL_EOF
#############################################################################
# Top Level Makefile
# Automatically generated by configure on $today
#############################################################################

VK_MAKEFILE = Makefile.$vk_name
LM_MAKEFILE = Makefile.$vk_logmerge


all:
	make -f \$(VK_MAKEFILE)
	make -f \$(LM_MAKEFILE)

dist: 
	make -f \$(VK_MAKEFILE) dist
	make -f \$(LM_MAKEFILE) dist

mocclean:
	make -f \$(VK_MAKEFILE) mocclean
	make -f \$(LM_MAKEFILE) mocclean

clean:
	make -f \$(VK_MAKEFILE) clean
	make -f \$(LM_MAKEFILE) clean

distclean:
	make -f \$(VK_MAKEFILE) distclean
	make -f \$(LM_MAKEFILE) distclean

install:
	make -f \$(VK_MAKEFILE) install
	make -f \$(LM_MAKEFILE) install

_MKFL_EOF





#---------------------------------------------------------------------------
# save the configure command line to config.status
#---------------------------------------------------------------------------
cfg_status=$outpath/config.status
[ -f $cfg_status ] && rm -f $cfg_status
echo "#!/bin/sh" >$cfg_status
echo $relpath/$relconf $OPT_CMDLINE >>$cfg_status
chmod +x $cfg_status


#--------------------------------------------------------------------------
# we are done
#--------------------------------------------------------------------------
echo
echo Valkyrie is now configured for building - just run $MAKE.
echo To reconfigure, run $MAKE distclean and ./configure.
echo
echo Your command line and configuration options have been saved 
echo in $cfg_status.
echo 
