#!/bin/csh -f
if ( ! -f lib/libpcap.a || ! -d libpcap ) then
if ( ! -d libpcap || ! -r libpcap/libpcap.a ) then
        set libpcapdir
        set dir=`ls .. | egrep '^libpcap-[0-9]*\.[0-9ab]*$'`
        if ( $#dir > 1 ) set dir="$dir[$#dir]"
        foreach d ( /usr/lib /usr/local/lib ../libpcap ../$dir )
                if ( -d $d && -r $d/libpcap.a ) then
                        set libpcapdir=$d
                        break
                endif
        end
        if ( "$libpcapdir" == "" ) then
                echo "configure: can't find libpcap.a"
                echo "configure: (see the INSTALL file for more info)"
                exit 1
	else
                ln -s $libpcapdir libpcap
                echo "configure: using $libpcapdir as ./libpcap"
        endif
else
        echo "configure: found ./libpcap"
endif
if ( ! -f lib/libpcap.a ) then
        echo "configure: linking lib/libpcap.a from ./libpcap"
        ln -s ../libpcap/libpcap.a lib/libpcap.a
else
        echo "configure: using libpcap.a installed in ./lib"
endif
else
        echo "configure: found ./libpcap and ./lib/libpcap.a"
endif

set tcp_wrapperdir
if ( ! -f lib/libwrap.a || ! -d wrapper ) then
if ( ! -d wrapper || ! -r wrapper/libwrap.a ) then
        set dir=`ls .. | egrep '^wrapper$|^tcp_wrappers$|^tcp_wrappers[_-.][0-9]*\.[0-9ab]*$'| sort -ru | sed 's/^/..\//'`
#       if ( $#dir > 1 ) set dir="$dir[$#dir]"
        foreach d ( /usr/lib /usr/local/lib $dir )
                if ( -d $d && -r $d/libwrap.a && -r $d/patchlevel.h) then
                        set tcp_wrapperdir=$d
                        break
                endif
        end
        if ( "$tcp_wrapperdir" == "" ) then
                echo "configure: can't find libwrap.a"
                echo "configure: (see the INSTALL file for more info)"
                exit 1
        else
                ln -s $tcp_wrapperdir wrapper
                echo "configure: using $tcp_wrapperdir as ./wrapper"
        endif
else
        echo "configure: found ./libwrap"
	set tcp_wrapperdir='./wrapper'
endif
if ( ! -f lib/libwrap.a ) then
        echo "configure: linking lib/libwrap.a from ./wrapper"
        ln -s ../wrapper/libwrap.a lib/libwrap.a
else
        echo "configure: using libwrap.a installed in ./lib"
endif
else
        echo "configure: found ./wrapper and ./lib/libwrap.a"
	set tcp_wrapperdir='./wrapper'
endif
if ( -f $tcp_wrapperdir/patchlevel.h) then
   set vers=`fgrep patchlevel $tcp_wrapperdir/patchlevel.h | awk '{print $7}' | sed 's/";//'`
   set wrapper='-e s/^#have-tcpwrapper-'$vers'#//'
   echo "configure: found tcp_wrappers_"$vers
else
   echo "configure: cannot determine tcp_wrapper version"
   echo "configure: (see the INSTALL file for more info)"
   exit 1
endif

set bison
set ethers
set flex
set gcc
set irix
set sunos
if ( -f libpcap/libpcap.a ) then
set pcap=`strings libpcap/libpcap.a | egrep "^pcap-[a-z]*\.c" | sort -u | sed -e 's/pcap-//' -e 's/\.c//'`
if (pcap == "") then
	echo "configure: cannot determine packet capture interface"
	exit 1
endif
else
	echo "configure: fatal: cannot find lib/libpcap.a!!"
        exit 1
endif
echo "configure: using $pcap packet capture interface"
set ranlib
set solaris
if (-f /bin/uname || -f /usr/bin/uname) then
	set name=`uname -s | tr '[A-Z]' '[a-z]'`
	set vers=`uname -r | sed -e 's/\..*//' -e 's/T1/v1/'|tr '[A-Z]''[a-z]'`
	if ( "$name" == "irix" ) set irix='-e s/^#have-irix#//'
	if ( "$name" == "sunos") then
		if ( "$name$vers" == "sunos5") then
			set solaris='-e s/^#have-solaris#//'
			set path=($path /usr/ccs/bin)
	        else
			set sunos='-e s/^#have-sunos#//'
		endif
	endif
endif
if ( -x /usr/local/bin/flex || -x /usr/local/flex ) then
	set flex='-e s/^#have-flex#//'
	echo "configure: found flex executable"
else
	echo "configure: couldn't find flex executable; using lex"
endif
if ( -x /usr/local/bin/bison || -x /usr/local/bison ) then
	set bison='-e s/^#have-bison#//'
	echo "configure: found bison executable"
else
	echo "configure: couldn't find bison executable; using yacc"
endif
if ( ( "$flex" == "" && "$bison" != "" ) || \
     ( "$flex" != "" && "$bison" == "" ) ) then
	echo "configure: don't have both flex and yacc...reverting to lex/yacc"
	set flex
	set bison
endif
set libc
foreach f ( /usr/lib/libc.a /lib/libc.a /usr/lib/libc_s.a )
	if ( -f $f ) then
		set libc=$f
		break
	endif
end
if ( "$f" == "" ) then
	echo "configure: couldn't find libc"
else
	echo "configure: found libc in "$libc
	nm $libc | grep ether_ntoa > /dev/null
	if ( $status == 0 ) then
		set ethers='-e s/^#have-ethers#//'
		echo "configure: found support for /etc/ethers"
	endif
endif
if ( -x /usr/local/bin/gcc || -x /usr/local/gcc ) then
	set gcc='-e s/^#have-gcc#//'
endif
if ( -x /usr/bin/ranlib ) then
	set ranlib='-e s/^#have-ranlib#//'
endif

if ( ! -f /usr/include/stdlib.h ) ln -s /dev/null include/stdlib.h

foreach i (. ./server ./common ./clients)
   echo "configure: making "$i"/Makefile"
   rm -f $i/Makefile
   sed -e "s/^#have-$pcap#//" $irix $sunos $solaris $flex $bison $ethers\
   $gcc $ranlib $wrapper $i/Makefile.in > $i/Makefile
   chmod ug+w $i/Makefile
end

exit 0
