#!/bin/sh
#
#set -x
#
# some parts are from Perl's Configure
#
# List of routine:headerfilelist to search for
# CFLAGS will be prolonged by '-DNO_<NAME> if not found
#hdrpglist="dbopen:db.h:ndbm.h"
hdrpglist=""
#
# List of direcrories to search for headerfiles
headerdirs="/usr/include /usr/include/sys /usr/include/linux"
#
# List of libs to search for
# LIBS will pe prolonged by '-l<NAME> if found
#liblist="z db"
liblist="z"
#
# List of directories to search for libraries
libdirs="/lib /usr/lib /usr/local/lib /usr/X11R6/lib"
#
srcdir=`pwd`
# directory in which Configure runs
confdir=$srcdir/conf
# directory in which templates are
templ=$confdir/templates
# directory in which temporary files are put
tmp=$confdir/tmp
#
# setting defaults
#
PATH=".:$PATH:/bin:/usr/bin:/usr/local/bin:/usr/ucb:/usr/local:/usr/lbin:/etc:/usr/new:/usr/new/bin:/usr/nbin"

if test ! -t 0; then
    echo "Say 'sh Configure', not 'sh <Configure'"
    exit 1
fi

unset CDPATH

startsh="#!/bin/sh"

if [ ! -d $confdir ]; then
  echo "Cannot cd to $confdir"
  exit 0
fi
cd $confdir
if [ ! -d $tmp ];then
  mkdir $tmp
fi
#
##############################################################################
#
: creating some auxiliary programs
cd $tmp
defread=$tmp/defread
loc=$tmp/loc
libloc=$tmp/libloc
inhdr=$tmp/inhdr
/bin/rm -f defread loc libloc inhdr
#
: "defread"
cat <<EOSC >defread
$startsh
read ans
if [ -z "\$ans" ]; then
  echo \$@
else
  echo \$ans
fi
exit 0
EOSC
chmod +x defread
#
: "loc"
cat <<EOSC >loc
$startsh
case \$# in
0) exit 1;;
esac
thing=\$1
shift
dflt=\$1
shift
for dir in \$*; do
    case "\$thing" in
    .)
	if test -d \$dir/\$thing; then
	    echo \$dir
	    exit 0
	fi
	;;
    *)
	if test -f \$dir/\$thing; then
	    echo \$dir/\$thing
	    exit 0
	fi
	;;
    esac
done
echo \$dflt
exit 1
EOSC
chmod +x loc
#
: "libloc"
cat <<EOSC >libloc
$startsh
case \$# in
0) exit 1;;
esac
thing=\$1
shift
dflt=\$1
shift
for dir in \$*; do
    for ff in \$dir/lib\$thing.*; do
	if test -f \$ff; then
	    echo \$ff
	    exit 0
	fi
    done
done
echo \$dflt
exit 1
EOSC
chmod +x libloc
#
: inhdr
cat <<EOSC > inhdr
$startsh
f=\$1
shift
p=\$*
IFS=:
set \$f
entry=\$1
hdrlist=\$*
IFS=' '
for dir in \$p; do
  for hdr in \$hdrlist; do
    if [ -f \$dir/\$hdr ]; then
      if egrep "^[^/].*[ 	*]\$entry[ ;(]" \$dir/\$hdr > /dev/null 2>&1; then
        echo \$entry
        exit 1
      fi
    fi
  done
done
echo ''
exit 0
EOSC
chmod +x inhdr
#
cd $confdir
#
##############################################################################
#
extraflags=
for f in $hdrpglist; do
  xxx="`$inhdr $f $headerdirs`"
  prg=`echo $f | sed 's/^\([^:]*\):.*$/\1/' | tr '[a-z]' '[A-Z]'`
  if [ -z "$xxx" ]; then
    extraflags="$extraflags -DNO_$prg"
  fi
done
#
extralibs=
for file in $liblist; do
  xxx=`$libloc $file $file $libdirs`
  if [ -f $xxx ]; then
    extralibs="$extralibs -l$file"
  fi
done
#
xxx=`$libloc z z $libdirs`
if [ ! -f $xxx ]; then
  extralibs="$extralibs -lz"
fi

ether=
xxx2="`$inhdr BPF_MAJOR_VERSION:net/bpf.h $headerdirs`"
if [ -d /usr/include/linux ]; then
  ether="ether_socket.o"
elif [ -n "$xxx2" ]; then
  ether="ether_bpf.o"
else
  echo "cannot find access method for Ethernet, only socket and BPF are supported"
  exit 1
fi

test -f Makefile && mv Makefile Makefile.old
sed -e "s#X_EXTRAFLAGS#$extraflags#" \
    -e "s#X_ETHER#$ether#" \
    -e "s#X_EXTRALIBS#$extralibs#" $templ/Makefile-in > $srcdir/Makefile || exit 1
#
echo "Makefile generated"
