#!/bin/sh
## File:     configure of ode_robots
## Author:   Georg Martius 
## Date:     Oct 2011

usage="\
Usage: configure [--prefix=DIR] [--srcprefix=DIR] [--system=LINUX|MAC] [--type=DEVEL|USER]"

#get full qualified dirname
DIRNAME=`dirname $0`;
if [ -n "${DIRNAME%%/*}" ]; then
    if [ $DIRNAME = '.' ]; then
        DIRNAME=`pwd`
    else
        DIRNAME=`pwd`/$DIRNAME;
    fi
fi

prefix="/usr/local"
srcprefix=$DIRNAME
system=LINUX
type=USER
version=`cat $DIRNAME/VERSION`;
package=ode_robots

if test $# -eq 0; then
      echo "${usage}" 1>&2
      exit 1
fi

while test $# -gt 0; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case $1 in
    --prefix=*)
      prefix=$optarg
      ;;
    --srcprefix=*)
      srcprefix=$optarg
      ;;
    --type=*)
      type=$optarg
      ;;
    --system=*)
      system=$optarg
      ;;
    *)
      echo "${usage}" 1>&2
      exit 1
      ;;
  esac
  shift
done

# test for selforg-config
if ! type selforg-config; then
    echo "could not find \"selforg-config\" in PATH" 1>&2;
    exit 1;
fi

# test for ode-dbl
if ! type ode-dbl-config; then
    echo "could not find \"osg-dbl-config\" in PATH" 1>&2;
    exit 1;
fi

if [ "$type"="DEVEL" -a -z "$srcprefix" ]; then
    echo "${usage}" 1>&2
    echo " --> provide srcprefix for type DEVEL!" 1>&2
    exit 1
fi

if m4 -DPREFIX="$prefix" -DSRCPREFIX="$srcprefix" -D$system -D$type -DVERSION=$version $GSL $DIRNAME/$package-config.m4 > $DIRNAME/$package-config; then
   chmod u+x $DIRNAME/$package-config;
   echo "configuration done!"
fi
