#!/bin/bash

DEST_DIR="$(pwd)"
PREFIX="/usr/local/Geopsy.org"
GCC_PREFIX=""
COMMANDLINE=""

BASE_SOURCE_DIR=$(pwd)
SOURCE_DIR=$BASE_SOURCE_DIR
BASE_BUILD_DIR=$BASE_SOURCE_DIR
BUILD_DIR=$SOURCE_DIR

# $1 is the path in the mingw form: /c/usr/... or "/c/usr/..."
function winPath()
{
  if [ "$PLATFORM" = "MINGW" ] &&
     [ "${1:0:1}" = "/" ] && [ "${1:2:1}" = "/" ]; then
    echo $1 | sed -r "s/^\"(.*)\"$/\1/" | sed -r "s/^\/([a-zA-Z])/\1:/" | sed "s/\//\\\\/g"
  else
    echo $1
  fi
}

#Platform options
case "$(uname)" in
  Linux) PLATFORM=LINUX ;;
  SunOS) PLATFORM=LINUX ;;
  Darwin) PLATFORM=MAC ;;
  MINGW*) PLATFORM=MINGW ;;
  *)
    echo "Unknown platform $(uname)"
    exit 2
  ;;
esac

while [ "$1" ]; do
  case "$1" in
    "-prefix" | "--prefix")
      shift
      PREFIX=$1
      COMMANDLINE="$COMMANDLINE -prefix \"$1\""
    ;;
    "-dest-prefix" | "--dest-prefix")
      shift
      DEST_DIR=$1
      COMMANDLINE="$COMMANDLINE -dest-prefix \"$1\""
    ;;
    "-win32" | "--win32")
      PLATFORM="MINGW"
      GCC_PREFIX="i686-w64-mingw32-"
      COMMANDLINE="$COMMANDLINE -win32"
    ;;
    "-win64" | "--win64")
      PLATFORM="MINGW"
      GCC_PREFIX="x86_64-w64-mingw32-"
      COMMANDLINE="$COMMANDLINE -win64"
    ;;
    "-sourcedir" | "--sourcedir")
      shift
      BASE_SOURCE_DIR=$1
      COMMANDLINE="$COMMANDLINE -sourcedir \"$1\""
    ;;
    "-builddir" | "--builddir")
      shift
      BASE_BUILD_DIR=$1
      if [ "$BUILD_DIR" = "$SOURCE_DIR" ]; then
        BUILD_DIR=$BASE_BUILD_DIR
      fi
      COMMANDLINE="$COMMANDLINE -builddir \"$1\""
    ;;
    "-buildsubdir" | "--buildsubdir")
      shift
      REGEXP_BUILD_SUBDIR=".*/$1$"
      if ! [[ "$BUILD_DIR" =~ $REGEXP_BUILD_SUBDIR ]]; then
        BUILD_DIR="$BASE_BUILD_DIR/$1"
      fi
      COMMANDLINE="$COMMANDLINE -buildsubdir \"$1\""
    ;;
    "-h" | "-help" | "--help")
      echo
      echo "Usage: configure [options]"
      echo
      echo "Options:"
      echo
      echo " -prefix <DIR>          Base directory for installation. It must be before any -*dir"
      echo "                          (default $PREFIX)"
      echo " -dest-prefix <DIR>     Destination prefix for all binaries"
      echo "                          (default $DEST_DIR)"
      echo " -win32                 Turn on cross compilation with i686-mingw32"
      echo "                          (under Linux only)"
      echo " -win64                 Turn on cross compilation with x86_64-mingw32"
      echo "                          (under Linux only)"
      echo " -sourcedir <DIR>       Source are in DIR (default $SOURCE_DIR)"
      echo " -builddir <DIR>        Building in DIR (default $BASE_BUILD_DIR)"
      echo " -buildsubdir <DIR>     Add subdir DIR to build directory"
      echo
      exit
    ;;
  esac
  shift
done
# Fix DEST_DIR in case of separate BUILD_DIR
if [ "$BUILD_DIR" != "$SOURCE_DIR" ] && [ "$DEST_DIR" = "$SOURCE_DIR" ]; then
  DEST_DIR=$BASE_BUILD_DIR
fi

case "$PLATFORM" in
  "LINUX")
    CFLAGS="-O2 -Wall -fPIC"
  ;;
  "MINGW")
    #GCC_PREFIX="i686-w64-mingw32-"
    CFLAGS="-O2 -Wall"
  ;;
  "MAC")
    MACOSX_DEPLOYMENT_TARGET=10.6
    CFLAGS="${MAC_ARCH} $ISYSTEM -fPIC"
    LDFLAGS=""
  ;;
esac

###############################################################
# Store configure options
###############################################################
# If configure called from ./configure.cache, do not overwrite
[ -d $BUILD_DIR ] || mkdir -p $BUILD_DIR
if [ "$SAVE_CONFIGURE_OPTIONS_IN_CACHE" != "no" ]; then
  (
  cat << END
export SAVE_CONFIGURE_OPTIONS_IN_CACHE="no"
cd $SOURCE_DIR
echo "$SHELL $0 ${COMMANDLINE//\"/\\\"}"
$SHELL $0 $COMMANDLINE
END
  ) > $BUILD_DIR/configure.cache
  chmod a+x $BUILD_DIR/configure.cache
fi


# Create a wrapper Makefile
(
cat << END
export GCC_PREFIX=$GCC_PREFIX
export CC=\$(GCC_PREFIX)gcc
export CFLAGS=$CFLAGS
export FFLAGS=$CFLAGS
export LDFLAGS=$LDFLAGS
export MACOSX_DEPLOYMENT_TARGET=$MACOSX_DEPLOYMENT_TARGET

export DEST_PREFIX=$DEST_DIR
export DEST_PREFIX_LOCAL=$(winPath $DEST_DIR)

first: \$(DEST_PREFIX)/lib/libslink.a

\$(DEST_PREFIX)/lib/libslink.a: Makefile
	test -d \$(DEST_PREFIX)/slink || mkdir -p \$(DEST_PREFIX)/slink
	if [ "${SOURCE_DIR}" != "\$(DEST_PREFIX)/slink" ]; then \\
	  cp -r ${SOURCE_DIR}/src \$(DEST_PREFIX)/slink; \\
	fi
	make clean -C \$(DEST_PREFIX_LOCAL)/slink/src
	make -C \$(DEST_PREFIX_LOCAL)/slink/src
	test -d \$(DEST_PREFIX)/lib || mkdir -p \$(DEST_PREFIX)/lib
	\$(GCC_PREFIX)ranlib \$(DEST_PREFIX_LOCAL)/slink/src/libslink.a
	mv \$(DEST_PREFIX)/slink/src/libslink.a \$(DEST_PREFIX)/lib
	make clean -C \$(DEST_PREFIX_LOCAL)/slink/src

clean:
	rm -f \$(DEST_PREFIX)/lib/libslink.a
	
install: first
	[ -d $PREFIX/lib ] || mkdir -p $PREFIX/lib
	cp \$(DEST_PREFIX)/lib/libslink.a $PREFIX/lib
	[ -d $PREFIX/include/slink ] || mkdir -p $PREFIX/include/slink
	rm -f $PREFIX/include/slink.h
	cp ${SOURCE_DIR}/src/slink.h $PREFIX/include
	cp ${SOURCE_DIR}/src/libslink.h $PREFIX/include/slink
	cp ${SOURCE_DIR}/src/slplatform.h $PREFIX/include/slink

END
) > ${BUILD_DIR}/new_Makefile
if [ ! -e ${BUILD_DIR}/Makefile ] || ! diff ${BUILD_DIR}/Makefile ${BUILD_DIR}/new_Makefile; then
  mv ${BUILD_DIR}/new_Makefile ${BUILD_DIR}/Makefile
else
  rm ${BUILD_DIR}/new_Makefile
fi

