#!/bin/sh
#
# Copyright (c) 2013 Mark Heily <mark@heily.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
   
usage() {
cat << "EOF"
Usage: configure [options]

Installation options:
        --bindir [DIRECTORY]         TODO describe this [$(EPREFIX)/bin]
        --datadir [DIRECTORY]        TODO describe this [$(DATAROOTDIR)]
        --datarootdir [DIRECTORY]    TODO describe this [$(PREFIX)/share]
        --docdir [DIRECTORY]         TODO describe this [$(DATAROOTDIR)/doc/$(PACKAGE)]
        --eprefix [DIRECTORY]        TODO describe this [$(PREFIX)]
        --includedir [DIRECTORY]     TODO describe this [$(PREFIX)/include]
        --infodir [DIRECTORY]        TODO describe this [$(DATAROOTDIR)/info]
        --libdir [DIRECTORY]         TODO describe this [$(EPREFIX)/lib]
        --libexecdir [DIRECTORY]     TODO describe this [$(EPREFIX)/libexec]
        --localedir [DIRECTORY]      TODO describe this [$(DATAROOTDIR)/locale]
        --localstatedir [DIRECTORY]  TODO describe this [$(PREFIX)/var]
        --mandir [DIRECTORY]         TODO describe this [$(DATAROOTDIR)/man]
        --oldincludedir [DIRECTORY]  TODO describe this [/usr/include]
        --pkgconfigdir [DIRECTORY]   where to install pkg-config files [$(LIBDIR)/pkgconfig]
        --pkgdatadir [DIRECTORY]     TODO describe this [$(DATADIR)/$(PACKAGE)]
        --pkgincludedir [DIRECTORY]  TODO describe this [$(INCLUDEDIR)/$(PACKAGE)]
        --pkglibdir [DIRECTORY]      TODO describe this [$(LIBDIR)/$(PACKAGE)]
        --prefix [DIRECTORY]         TODO describe this [/usr/local]
        --sbindir [DIRECTORY]        TODO describe this [$(EPREFIX)/sbin]
        --sharedstatedir [DIRECTORY] TODO describe this [$(PREFIX)/com]
        --sysconfdir [DIRECTORY]     TODO describe this [$(PREFIX)/etc]

Project options:

System types:
        --build BUILD                set the system type for building
        --host HOST                  cross-compile programs to run on a different system type
        --target TARGET              build a compiler for cross-compiling

Optional Features:

Common options:
        --disable-static             Disable generation of static libraries
        --disable-option-checking
    -h, --help                       Show this message
    -V, --version                    Display version information and exit
EOF
}

err() {
    echo "*** ERROR ***  $*"
    exit 1
}

require_var() {
    key=$1
    eval "val=\$$key"
    if [ "$val" = "" ] ; then
       echo "ERROR: you must provide --$2 as a command line option"
       exit 1
    fi
}


echo "# Automatically generated by ./configure -- do not edit" > config.mk
for arg in $*
do
  if [ `echo "$arg" | grep "^--"` = "$arg" ] ; then
     key=`echo $arg | sed "s/^--//; s/=.*//; s/^with-//;"`
     val=`echo $arg | sed "s/.*=//"`
     uc_key=`echo "$key" | tr "a-z" "A-Z" | tr "-" "_"`
     case $key in
     "help")
       usage
       exit 1
       ;;
     bindir|datadir|datarootdir|docdir|includedir|infodir|libdir|libexecdir|localedir|localstatedir|mandir|pkgconfigdir|pkgdatadir|pkgincludedir|pkglibdir|prefix|sbindir|sharedstatedir|sysconfdir)
       echo "$uc_key=$val" >> config.mk
       ;;
     exec-prefix)
       echo "EPREFIX=$val" >> config.mk
       ;;
     program-prefix)
       if [ "$val" != "" ] ; then err "FIXME - not implemented" ; fi
       ;;
     disable-static)
       echo "$uc_key=1" >> config.mk
       ;;
     build|host)
       # TODO: we should split this up, and override the other Makeconf
       # variables like *_VENDOR, *_ARCH, *_CPU, *_KERNEL, *_SYSTEM 
       echo "$uc_key=$val" >> config.mk
       eval "${key}_system_type=\"$val\""
       ;;
     disable-option-checking)
        # Not implemented, this behavior is the default (for now)
        ;;
     disable-dependency-tracking)
        # Not implemented, dependency tracking is done in Ruby (for now)
        ;;
     # Android-specific variables
     ndk|sdk)
       echo "$uc_key=$val" >> config.mk
       eval "$key=\"$val\""
       ;;
     *)
       echo "Warning: unrecognized option: $arg"
       ;;
     esac
  fi
done

# Android-specific options
if [ "`echo ${host_system_type} | grep androideabi`" != "" ] ; then
    exec ./configure.rb $*
#    require_var ndk with-ndk
#    require_var sdk with-sdk
fi

printf "checking for a C compiler... "
for cmd in ${host_system_type}-cc cc gcc gcc4 clang
do
    $cmd --version >/dev/null 2>&1
    if [ $? -eq 0 ] ; then cc="$cmd" ; break ; fi
done
if [ -n "$CC" ] ; then cc="$CC" ; fi
if [ -n "$cc" ] ; then
    echo "$cc"
    echo "CC=$cc" >> config.mk
    if [ "$cc" != "cc" ] ; then echo "LD=$cc" >> config.mk ; fi
    if [ -n "$CFLAGS" ] ; then echo "CFLAGS=$CFLAGS" >> config.mk ; fi
else
    echo "not found"
    err "Please install a compiler and add it to your PATH"
fi

printf "checking for ar.. "
for ar in ${host_system_type}-ar ar gar
do
    $ar --version >/dev/null 2>&1
    if [ $? -eq 0 ] ; then ar="$ar" ; break ; fi
done
if [ -n "$ar" ] ; then
  echo "$ar"
  echo "AR=$ar" >> config.mk
else
    echo "not found"
    err "Please install an archiver and add it to your PATH"
fi

printf "checking for ranlib.. "
for ranlib in ${host_system_type}-ranlib ranlib
do
    $ar --version >/dev/null 2>&1
    if [ $? -eq 0 ] ; then ranlib="$ranlib" ; break ; fi
done
if [ -n "$ranlib" ] ; then
  echo "$ranlib"
  echo "RANLIB=$ranlib" >> config.mk
else
    echo "not found"
    err "Please install ranlib and add it to your PATH"
fi

printf "checking for a usable make command... "
for cmd in make gmake
do
    $cmd --version >/dev/null 2>&1
    if [ $? -eq 0 ] ; then make="$cmd" ; break ; fi
done
if [ -n "$make" ] ; then
    echo "yes"
    echo "MAKE=$make" >> config.mk
else
    echo "not found"
    err "Please install GNU Make and add it to your PATH as either make or gmake"
fi

# Allow additional variables from the environment to override the defaults
#
test -n "$LD" && echo "LD=$LD" >> config.mk
test -n "$LDFLAGS" && echo "LDFLAGS=$LDFLAGS" >> config.mk
# TODO: need to add Makefile support for: LIBS, CPP, CPPFLAGS

rm -f config.h
$make config.h
