#!/bin/bash

#	Copyright (C) 2003-2005 Daniel Muller, dan at verliba dot cz
#	Copyright (C) 2006-2021 Verlihub Team, info at verlihub dot net
#
#	Verlihub is free software; You can redistribute it
#	and modify it under the terms of the GNU General
#	Public License as published by the Free Software
#	Foundation, either version 3 of the license, or at
#	your option any later version.
#
#	Verlihub is distributed in the hope that it will be
#	useful, but without any warranty, without even the
#	implied warranty of merchantability or fitness for
#	a particular purpose. See the GNU General Public
#	License for more details.
#
#	Please see http://www.gnu.org/licenses/ for a copy
#	of the GNU General Public License.

DEFAULT_PREFIX="/usr/local"
PREFIX=$DEFAULT_PREFIX
BUILD_TYPE="release"
BUILD_DIR="build"
LIB_DIR="lib"
PLUGIN_DIR=""
DEBUG=0
cd `dirname $0`
SRC_DIR="$PWD"

function print_help
{
  echo "configure script for package VerliHub

Usage: ./configure [OPTION]

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

Options:
	-h, --help              Display this help message and exit

	Installation directories:
	--prefix=PREFIX         Install files in PREFIX. Default: $DEFAULT_PREFIX
	--libdir=LIBDIR         Install libs in PREFIX/LIBDIR. Default: $LIB_DIR
	--plugindir=PLUGINDIR   Install plugins in PREFIX/PLUGINDIR. Default equal to LIBDIR

	Optional features:
	--enable-debug-output   Enable debugging output.  Default to no
	--build-dir=PATH        Path to store the build files. Default: ./$BUILD_DIR
	--build-type=TYPE       Define the build configuration to use. Available values are:
				    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."
exit
}

function run_cmake
{
	# 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
	test -e "$BUILD_DIR" || mkdir "$BUILD_DIR" || exit 1
	cd "$BUILD_DIR"
	REAL_BUILD_DIR=`pwd`
	# finally start cmake
	cmake -D CMAKE_INSTALL_PREFIX=$PREFIX \
        -D LIB_INSTALL_DIR=$LIB_DIR \
        -D PLUGIN_INSTALL_DIR=$PLUGIN_DIR \
        -D CMAKE_BUILD_TYPE=$BUILD_TYPE \
        -D VERBOSE=$DEBUG \
        "$SRC_DIR" \
	&& cmake_ok || cmake_error
}

function cmake_error
{
	echo "-- cmake failed
	Please fix the problems mentioned above, and run ./configure again."
}

function cmake_ok
{
	# 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
#   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
}

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

  mkdir build
  cd build
  cmake ..
" >&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
  eval set -- "$TEMP"

  while true ; do
    case "$1" in
    -h|--help)              print_help;      exit 0  ;;
    --prefix)               PREFIX=$2;       shift 2 ;;
    --libdir)               LIB_DIR=$2       shift 2 ;;
    --plugindir)            PLUGIN_DIR=$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

run_cmake
