#!/bin/sh
##
 # configure
 # Copyright (C) 2011 - 2013 Peter Belkner <pbelkner@snafu.de>
 # 
 # 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 3 of the License, or
 # (at your option) any later version.
 # 
 # This program 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.
 # 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
srcdir=`dirname "$0"|sed -e 's,/$,,g'`
pfx=/usr/local
winamp='/c/Program Files (x86)/Winamp'
msvc='/c/Program Files (x86)/Microsoft Visual Studio 12.0/VC'
mssdk='/c/Program Files (x86)/Microsoft SDKs/Windows/v7.1A'
wasdk='/c/Program Files (x86)/Winamp SDK'
nsis='/c/Program Files (x86)/NSIS'
bindir='/c/Program Files (x86)/Winamp/Plugins'
tooldir="$srcdir/tools"
reldir="release"
modes="full tiny"
install="full"
release="tiny"
app="in_ffsox-2a"

###############################################################################
help() {
  echo "Usage: $0 [OPTION]... [VAR=VALUE]..."
  exit $1
}

extract() {
  echo "$1"|sed -e "s/$2=//g"
}

extract_enable() {
  case `echo "$1"|sed -e "s/--enable-$2//g"` in
    '') echo "yes" ;;
    =yes) echo "yes" ;;
    =no) echo "no" ;;
    *) help 1 ;;
  esac
}

extract_disable() {
  case `echo "$1"|sed -e "s/--disable-$2//g"` in
    '') echo "no" ;;
    =yes) echo "no" ;;
    =no) echo "yes" ;;
    *) help 1 ;;
  esac
}

maintain() {
  case $1 in
    yes) echo "`echo "$3"|sed -e "s/$2//g"` $2" ;;
    no) echo `echo "$3"|sed -e "s/$2//g"` ;;
    *) help 1 ;;
  esac
}

maintain_enable() {
  maintain "`extract_enable "$1" "$2"`" "$2" "$3"
}

maintain_disable() {
  maintain "`extract_disable "$1" "$2"`" "$2" "$3"
}

while test "$1" != ''; do
  case $1 in
    --help) help 0 ;;
    --prefix=*) pfx=`extract "$1" '--prefix'` ;;
    --bindir=*) bindir=`extract "$1" '--bindir'` ;;
    --libdir=*) libdir=`extract "$1" '--libdir'` ;;
    --includedir=*) includedir=`extract "$1" '--includedir'` ;;
    --tooldir=*) tooldir=`extract "$1" '--tooldir'` ;;
    --reldir=*) reldir=`extract "$1" '--reldir'` ;;
    --release=*) host=`extract "$1" '--release'` ;;
    --install=*) install=`extract "$1" '--install'` ;;
    --libgomp=*) libgomp=`extract "$1" '--libgomp'` ;;
    --libpthread=*) libpthread=`extract "$1" '--libpthread'` ;;
    --winamp=*) winamp=`extract "$1" '--winamp'` ;;
    --msvc=*) msvc=`extract "$1" '--msvc'` ;;
    --mssdk=*) mssdk=`extract "$1" '--mssdk'` ;;
    --wasdk=*) wasdk=`extract "$1" '--wasdk'` ;;
    --nsis=*) nsis=`extract "$1" '--nsis'` ;;
    --host=*) host=`extract "$1" '--host'` ;;
    --enable-full) modes=`maintain_enable "$1" 'full' "$modes"` ;;
    --disable-full) modes=`maintain_disable "$1" 'full' "$modes"` ;;
    --enable-tiny) modes=`maintain_enable "$1" 'tiny' "$modes"` ;;
    APP=*) cflags=`extract "$1" 'APP'` ;;
    CFLAGS=*) cflags=`extract "$1" 'CFLAGS'` ;;
    CPPFLAGS=*) cppflags=`extract "$1" 'CPPFLAGS'` ;;
    LDFLAGS=*) ldflags=`extract "$1" 'LDFLAGS'` ;;
    LIBS=*) libs=`extract "$1" 'LIBS'` ;;
    *) echo "Error: Unknow option: $1."; help 1 ;;
  esac
  shift
done

###############################################################################
#if [ 'x' == "x${libgomp}" ]; then
#  libgomp=`find /mingw -name libgomp.a`
#fi
#
#if [ 'x' == "x${libpthread}" ]; then
#  libpthread=`find /mingw -name libpthread.a`
#fi

###############################################################################
normpath() {
  if [ 'x' == "x$1" ] || [ "." == "$1" ]; then
    echo "$PWD"
  else
    echo "$1"|sed -e "s,^\([^/]\),$PWD/\1,g"
  fi
}

pfx=`normpath "$pfx"`
srcdir=`normpath "$srcdir"`
tooldir=`normpath "$tooldir"`
reldir=`normpath "$reldir"`

###############################################################################
pfxpath() {
  if [ 'x' == "x$1" ] || [ "." == "$1" ]; then
    echo "$pfx/$2"
  else
    normpath "$1"
  fi 
}

bindir=`pfxpath "$bindir" bin`
libdir=`pfxpath "$libdir" lib`
includedir=`pfxpath "$includedir" include`

###############################################################################
rm -f config.mak
touch config.mak

echo "OS:=$os" >> config.mak
echo "PFX:=$pfx" >> config.mak
echo "SRCDIR:=$srcdir" >> config.mak
echo "BINDIR:=$bindir" >> config.mak
echo "LIBDIR:=$libdir" >> config.mak
echo "INCLUDEDIR:=$includedir" >> config.mak
echo "TOOLDIR:=$tooldir" >> config.mak
echo "RELDIR:=$reldir" >> config.mak
echo "HOST:=$host" >> config.mak

#echo "LIBGOMP:=${libgomp}" >> config.mak
#echo "LIBPTHREAD:=${libpthread}" >> config.mak

if [ 'x' != "x$app" ]; then echo "APP:=$app" >> config.mak; fi
if [ 'x' != "x$cflags" ]; then echo "CFLAGS+=$cflags" >> config.mak; fi
if [ 'x' != "x$cppflags" ]; then echo "CPPFLAGS+=$cppflags" >> config.mak; fi
if [ 'x' != "x$ldflags" ]; then echo "LDFLAGS+=$ldflags" >> config.mak; fi
if [ 'x' != "x$libs" ]; then echo "LIBS+=$libs" >> config.mak; fi

for i in $modes; do
  target="all-${i}"
  case $i in
    full) echo "ALL_FULL+=${target}" >> config.mak ;;
    tiny) echo "ALL_TINY+=${target}" >> config.mak ;;
    *) ;;
  esac
done

echo "ALL:=\$(ALL_FULL) \$(ALL_TINY)" >> config.mak
echo "INSTALL:=install-${install}" >> config.mak
echo "RELEASE:=release-${release}" >> config.mak
echo "WINAMP:=${winamp}" >> config.mak
echo "MSVC:=${msvc}" >> config.mak
echo "MSSDK:=${mssdk}" >> config.mak
echo "WASDK:=${wasdk}" >> config.mak
echo "NSIS:=${nsis}" >> config.mak

if [ ! \( './' -ef "${srcdir}" \) ]; then # [
  cp -p "${srcdir}/Makefile" './'
fi # ]
