#! /bin/sh
# Usage: target [[--prefix=] [--exec-prefix=] build_dirname [cc]]
# Examples:
#   target make.gcc gcc
#   target make.cc cc
#   target obj

prefixcmd=""
execprefixcmd=""
need_dirname=1
need_compiler=1
for arg
do
  # Handle --exec-prefix with a space before the argument.
  if test x$next_exec_prefix = xyes; then exec_prefix=$arg; next_exec_prefix=
  # Handle --prefix with a space before the argument.
  elif test x$next_prefix = xyes; then prefix=$arg; next_prefix=
  else
    case $arg in
      # For backward compatibility, recognize -exec-prefix and --exec_prefix.
      -exec-prefix=* | --exec_prefix=* | --exec-prefix=* | --exec-prefi=* | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* | --exec=* | --exe=* | --ex=* | --e=*) 
      exec_prefix=`echo $arg | sed 's/[-a-z_]*=//'` ;;
      -exec-prefix | --exec_prefix | --exec-prefix | --exec-prefi | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- | --exec | --exe | --ex | --e)
      next_exec_prefix=yes ;;
      -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
      prefix=`echo $arg | sed 's/[-a-z_]*=//'` ;;
      -prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
      next_prefix=yes ;;
      *) if [ $need_dirname -eq 1 ] ; then
           DIRNAME=$arg
           need_dirname=0
         elif [ $need_compiler -eq 1 ] ; then
           COMPILER=$arg
           need_compiler=0
         fi
         ;;
    esac
  fi
done
if [ "x$prefix" != "x" ] ; then
  prefixcmd="--prefix=$prefix"
fi
if [ "x$exec_prefix" != "x" ] ; then
  execprefixcmd="--exec-prefix=$exec_prefix"
fi

if [ "$COMPILER" != "" ] ; then
  CC="$2"
  export CC
fi
if [ "$DIRNAME" = "" ] ; then
  DIRNAME=src
  chmod a+x $DIRNAME/configure
  chmod a+x $DIRNAME/readline/configure
  chmod a+x $DIRNAME/stdwin/configure
  echo "executing $DIRNAME/configure ..."
  cd $DIRNAME
  ${CONFIG_SHELL-/bin/sh} configure $prefixcmd $execprefixcmd
  echo "executing $DIRNAME/readline/configure ..."
  cd readline
  ${CONFIG_SHELL-/bin/sh} configure $prefixcmd $execprefixcmd
  cd ..
  echo "executing $DIRNAME/stdwin/configure ..."
  cd stdwin
  ${CONFIG_SHELL-/bin/sh} configure $prefixcmd $execprefixcmd --with-x11 
  cd ..
else

# checking how to copy files
echo "blabla" > conftest.x
err=`/bin/sh -c "cp -p conftest.x conftest.y 2>&1"`
if test -z "$err"; then
  CP='cp -p'
else
  CP='cp'
fi
rm -f conftest*

  if [ ! -d $DIRNAME ] ; then
    mkdir $DIRNAME
  fi
  for f in glob readline readline/doc readline/examples \
           stdwin stdwin/H stdwin/Gen stdwin/Tools stdwin/Ports \
           stdwin/Ports/alfa stdwin/Ports/msdos stdwin/Ports/vtrm \
           stdwin/Ports/x11 stdwin/Packs stdwin/Packs/textedit stdwin/Packs/vt
  do
    if [ ! -d $DIRNAME/$f ] ; then
      mkdir $DIRNAME/$f
    fi
    ( cd src
      for g in $f/* ; do
        if [ -f $g ] ; then
          if [ "$g" = readline/Makefile ] ; then
            $CP -i $g ../$DIRNAME/$g
          else
            rm -f ../$DIRNAME/$g
            ln $g ../$DIRNAME/$g || $CP -f $g ../$DIRNAME/$g
          fi
        fi
      done
    )
  done
  for f in configure unixconf.h.in makemake.in ; do
    rm -f $DIRNAME/$f
    ln src/$f $DIRNAME/$f || $CP -f src/$f $DIRNAME/$f
  done
  chmod a+x $DIRNAME/configure
  chmod a+x $DIRNAME/readline/configure
  chmod a+x $DIRNAME/stdwin/configure
  echo "executing $DIRNAME/configure ..."
  cd $DIRNAME
  ${CONFIG_SHELL-/bin/sh} configure --srcdir=../src $prefixcmd $execprefixcmd
  echo "executing $DIRNAME/readline/configure ..."
  cd readline
  ${CONFIG_SHELL-/bin/sh} configure --srcdir=../../src/readline $prefixcmd $execprefixcmd
  cd ..
  echo "executing $DIRNAME/stdwin/configure ..."
  cd stdwin
  ${CONFIG_SHELL-/bin/sh} configure --srcdir=../../src/stdwin $prefixcmd $execprefixcmd --with-x11
  cd ..
fi
