#!/bin/bash
#
#                          configure  -  description
#                             -------------------
#    begin                : Wed Apr  9 2008
#    copyright            : (C) 2008 by Diederik van der Boor
#    email                : "vdboor" --at-- "codingdomain.com"
#
#
#   Emulation script of GNU ./configure to make compiling of KDE 4 software more standard.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   Note for packagers:
#     this script is only written to provide a more user friendly
#     interaction between the command line and the cmake invocation.
#     If you are familiar with CMake, you may call it directly too.
#

# ------------------------------
#
# Variables
#

KDE4_PREFIX=""
DEFAULT_PREFIX="/usr/local"

# auto-detect KDE Installation on the Mac.
KDE4_CONFIG=""
for i in `whereis kde4-config` /opt/local/bin/kde4-config
do
  if [ -x "$i" ]; then
    KDE4_CONFIG="$i"
	break
  fi
done

# auto-detect default prefix
if [ -n "$KDE4_CONFIG" ]; then
  KDE4_PREFIX=`$KDE4_CONFIG --prefix`
  DEFAULT_PREFIX="$KDE4_PREFIX"
fi

# To configure
PREFIX=$DEFAULT_PREFIX
BUILD_TYPE="relwithdebinfo"
BUILD_DIR="build"
REAL_BUILD_DIR=""
DEBUG=0

# Globals
cd `dirname $0`
SRC_DIR="$PWD"
KMESS_VER="`./cmake/get-git-version.sh`"



# ------------------------------
#
# Print the help page
#

function print_help
{
  echo "configure script for package: KMess $KMESS_VER

Usage: ./configure [arguments]

This is an emulation script of GNU ./configure to give compiling of KDE 4
software a more familiar usage. Experienced users are free to use cmake
instead. This configure script should provide everything you need though.

Configuration:
  -h, --help              display this help and exit

Installation directories:
  --prefix=PREFIX         install files in PREFIX. default: $DEFAULT_PREFIX

Optional Features:
  --enable-debug-output   enable debugging output.  default=no
  --build-dir=PATH        path to store the build files. default: ./build
  --build-type=TYPE       define the build configuration to use, values:
                            release           - standard release
                            relwithdebinfo    - release with debug info
                            minsizerel        - minimum size release
                            debug             - debug build
                            debugfull         - full debug build


After running ./configure, you can type 'make' to build this project.
"

  # Hint for kde installation
  if [ "$KDE4_PREFIX" != "" ]; then
    if [ "$KDE4_PREFIX" != "$PREFIX" ]; then
      echo "  NOTICE:

    The installation prefix is set to '$PREFIX'. You may want to set
    the prefix to '$KDE4_PREFIX' to install the files in the KDE 4 folder.
"
    fi
  else
    echo "  WARNING:

    kde4-config was not found. Please verify that
    you've installed the KDE 4 development packages!
"
  fi
}



# ------------------------------
#
# Run cmake
#

function run_cmake
{
  # Check prefix
  if [ ! -e "$PREFIX" ]; then
    echo "$0: installation directory not found: $PREFIX" >&2
    exit 1
  fi

  # Check build type
  case "$BUILD_TYPE" in
    debugfull) DEBUG=1 ;;
    release|relwithdebinfo|minsizerel|debug) ;;
    *)
      echo "$0: invalid build type: $BUILD_TYPE" >&2
      exit 1
      ;;
  esac

  # Make the directory,
  # and switch so build messages are ok.
  test -e "$BUILD_DIR" || mkdir "$BUILD_DIR" || exit 1
  cd "$BUILD_DIR"
  REAL_BUILD_DIR=`pwd`

  # debug about cmake
  echo "configuring KMess $KMESS_VER"
  echo "checking build directory"
  echo "running cmake from $REAL_BUILD_DIR"
  echo "command: cmake -D CMAKE_INSTALL_PREFIX=$PREFIX \ "
  echo "               -D CMAKE_BUILD_TYPE=$BUILD_TYPE \ "
  echo "               -D KMESS_DEBUG_OUTPUT=$DEBUG \ "
  echo "               "$SRC_DIR" "
  echo ""

  # finally start cmake
  cmake -D CMAKE_INSTALL_PREFIX=$PREFIX \
        -D CMAKE_BUILD_TYPE=$BUILD_TYPE \
        -D KMESS_DEBUG_OUTPUT=$DEBUG \
        "$SRC_DIR" \
      && cmake_ok || cmake_error
}


function cmake_ok
{
  # collect all settings
  local HAS_XSS="no   (extension not detected)"
  local HAS_DEBUG="no   (good!)"

  if [ $DEBUG = "1" ]; then
    HAS_DEBUG="yes  (KMess will be slow!)"
  fi

  if grep -q '^#define HAVE_XSCREENSAVER 1$' "config-kmess.h"; then
    HAS_XSS="yes"
  fi

  # create a makefile to redirect all calls to the build directory
  makefile=$SRC_DIR/Makefile
  targets=`grep '^[A-Za-z_\/\-]\+:' Makefile | cut -f 1 -d ':'`
  echo "# Generated by ./configure: NO NOT EDIT!
# Build features:
#
#   Install prefix:      $PREFIX
#   Auto-away feature:   $HAS_XSS
#   Debugging messages:  $HAS_DEBUG
#   Build type:          $BUILD_TYPE
#   Build directory:     $BUILD_DIR
#
#
# cmake command:
#
#   cd \"$REAL_BUILD_DIR\"
#   cmake -D CMAKE_INSTALL_PREFIX=$PREFIX \\
#         -D CMAKE_BUILD_TYPE=$BUILD_TYPE \\
#         -D KMESS_DEBUG_OUTPUT=$DEBUG \\
#         \"$SRC_DIR\"
#
" > $makefile

  for target in $targets; do
    echo "$target:"  >> $makefile
    echo -en "\t"    >> $makefile
    echo "@\$(MAKE) --no-print-directory -C '$BUILD_DIR' $target"  >> $makefile
    echo ""          >> $makefile
  done

  # display a configure report
  echo "-- configure completed


  Build features:

    Install prefix:      $PREFIX
    Auto-away feature:   $HAS_XSS
    Debugging messages:  $HAS_DEBUG
    Build type:          $BUILD_TYPE


  Enter the following command to continue:

    make
"
}

function cmake_error
{
echo "-- cmake failed

  Please fix the problems mentioned above, and run ./configure again.
  For example, make sure you have the KDE 4 devel packages installed.
"
}



# ------------------------------
#
# Main
#

# parse arguments
if [ -n "$*" ]; then

  # check against BSD / Mac OS getopt, which is seriously lacking.
  # It does not support additional options, so it will recognize everything
  # after --longoptions as the $@ input.
  if ! getopt --longoptions --prefix -- "" 2>/dev/null >/dev/null; then
    echo "$0: Sorry, can't handle arguments at this platform.
Your getopt version does not support long arguments.

Please run cmake directly, for example:

  mkdir build
  cd build
  cmake -D CMAKE_INSTALL_PREFIX=/usr ../
  make
  make install
" >&2
    exit 1
  fi

  # Parse the arguments
  longopts="help,prefix:,build-type:,build-dir:,build-folder:,enable-debug-output"
  TEMP=`getopt --name=configure \
               --options h \
               --longoptions "$longopts" -- "$@"`
  case "$?" in
     0) ;;
     *) exit 1 ;;
  esac


  # Note the quotes around `$TEMP': they are essential!
  eval set -- "$TEMP"

  while true ; do
    case "$1" in
    -h|--help)              print_help;      exit 0  ;;
    --prefix)               PREFIX=$2;       shift 2 ;;
    --enable-debug-output)  DEBUG=1;         shift   ;;
    --build-type)           BUILD_TYPE="$2"; shift 2 ;;
    --build-dir)            BUILD_DIR="$2";  shift 2 ;;
    --builddir|--build-directory|--build-folder)  BUILD_DIR="$2";  shift 2 ;;   # be forgiving with small errors.
    --) shift ; break ;;
    *) echo "$0: internal error at argument '$1'!" ; exit 1 ;;
    esac
  done

  if [ -n "$1" ]; then
    echo "$0: invalid arguments: $@"
    exit 1
  fi
fi


# Start cmake
run_cmake

