#!/bin/sh
src=`dirname $0`
pfx=/usr/local

oss="linux mingw"
downloaddir="download"

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

if test "" = "$src"; then src="."; fi

for i in $oss; do
  if test 1 -eq `uname|grep -i $i|wc -l`; then os=$i; fi
done

while test "$1" != ""; do
  case $1 in
    --help) help ;;
    --prefix=*) pfx=`echo $1|sed -e 's/--prefix=//g'` ;;
    --bindir=*) bindir=`echo $1|sed -e 's/--bindir=//g'` ;;
    --libdir=*) libdir=`echo $1|sed -e 's/--libdir=//g'` ;;
    --includedir=*) includedir=`echo $1|sed -e 's/--includedir=//g'` ;;
    --downloaddir=*) downloaddir=`echo $1|sed -e 's/--downloaddir=//g'` ;;
    --host=*) host=`echo $1|sed -e 's/--host=//g'` ;;
    CFLAGS=*) cflags=`echo $1|sed -e 's/CFLAGS=//g'` ;;
    CPPFLAGS=*) cppflags=`echo $1|sed -e 's/CPPFLAGS=//g'` ;;
    LDFLAGS=*) ldflags=`echo $1|sed -e 's/LDFLAGS=//g'` ;;
    LIBS=*) libs=`echo $1|sed -e 's/LIBS=//g'` ;;
    *) echo "Error: Unknow option: $1."; help ;;
  esac
  shift
done

# transform relative path into absolute
src=`echo "$src"|sed -e "s,^\([^/]\),$PWD/\1,g"`
pfx=`echo "$pfx"|sed -e "s,^\([^/]\),$PWD/\1,g"`

if test "" = "$bindir"; then
  bindir=$pfx/bin
else
  bindir=`echo "$bindir"|sed -e "s,^\([^/]\),$PWD/\1,g"`
fi 

if test "" = "$libdir"; then
  libdir=$pfx/lib
else
  libdir=`echo "$libdir"|sed -e "s,^\([^/]\),$PWD/\1,g"`
fi 

if test "" = "$includedir"; then
  includedir=$pfx/include
else
  includedir=`echo "$includedir"|sed -e "s,^\([^/]\),$PWD/\1,g"`
fi 

rm -f config.mak
touch config.mak

echo "OS:=$os" >> config.mak
echo "SRC:=$src" >> config.mak
echo "PFX:=$pfx" >> config.mak
echo "BINDIR:=$bindir" >> config.mak
echo "LIBDIR:=$libdir" >> config.mak
echo "INCLUDEDIR:=$includedir" >> config.mak
echo "DOWNLOADDIR:=$downloaddir" >> config.mak
echo "HOST:=$host" >> config.mak
if test "" != "$cflags"; then echo "CFLAGS+=$cflags" >> config.mak; fi
if test "" != "$cppflags"; then echo "CPPFLAGS+=$cppflags" >> config.mak; fi
if test "" != "$ldflags"; then echo "LDFLAGS+=$ldflags" >> config.mak; fi
if test "" != "$libs"; then echo "LIBS+=$libs" >> config.mak; fi

if test ! . -ef $src; then
  cp -p $src/Makefile .
fi
