#!/bin/sh

# this is a script around make which basicly checks
# if it's in srcdir or in builddir and changes to 
# the right directory for calling /usr/bin/make
# (C) Stephan Kulow

file=Makefile
dir=.
args=

while test $# -gt 0 ; do
   case "${1}" in
      -f)
	shift
	file="${1}" 
	shift 
	args="$args -f $file"
	;;
      -C)
	shift
	dir="${1}"
	shift ;;
      *)
	args="$args ${1}"
	shift 
	;;
    esac
done

if test ! -f $dir/$file; then
  if test -n "$OBJ_SUBDIR"; then
    dir=$PWD
    subdir=.
    while test ! -f $dir/$OBJ_SUBDIR/$file; do
       subdir=`basename $dir`"/$subdir"
       dir=`dirname $dir`
       if test "$dir" = "/"; then
         # the case that someone puts the compile dir in /
         # is very unlikely, so we better skip here ;)
         echo "can't find $OBJ_SUBDIR above current dir"
         exit 1
       fi
    done
    cd $dir/$OBJ_SUBDIR/$subdir
  else
    if test -n "$OBJ_REPLACEMENT"; then
      pwd=`echo $PWD | sed -e "$OBJ_REPLACEMENT"`
      if test ! -f $pwd/$dir/$file; then
  	 echo "no objdir found. Tried $pwd"
	 exit 1
      fi
      cd $pwd/$dir
    fi
  fi
else
  cd $dir
fi

echo "makeobj[0]: Entering directory \`$PWD'"
LANGUAGE=C /usr/bin/make $args
retval=$?
echo "makeobj[0]: Leaving directory \`$PWD'"
exit $retval

