#!/bin/csh -f
if ( ! -d libpcap || ! -r libpcap/libpcap.a ) then
	set libpcapdir
	set dir=`ls .. | sed -e 's/\/$//' | egrep '^libpcap-[0-9]*\.[0-9]*(\.[0-9]*)?([ab])?$'`
	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 README for more info)"
		exit 1
	endif
	if ( "$libpcapdir" != "/usr/lib" && \
	    "$libpcapdir" != "/usr/local/lib" ) then
		rm -rf libpcap
		ln -s $libpcapdir libpcap
		echo "configure: using libpcap.a installed in $libpcapdir"
	endif
endif
if ( -x /bin/uname || -x /usr/bin/uname ) then
	set md=`uname -m`
	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$vers" == "sunos5") then
		set path=($path /usr/ccs/bin)
	endif
else if ( -x /bin/arch ) then
	# sun3 only supported machine without uname
	if ( `/bin/arch` != sun3 ) goto bail
	set md=sun3
	set name=sunos
	set vers=3
else
	goto bail
endif
if ( $name == "bsd/386" || $name == "bsd/os" ) then
	set os=bsd
else if ( $name == "linux" ) then
	set os=linux
else
	set os=$name$vers
endif
switch ( $md )
	case alpha:
	case i586:
	case i486:
	case i386:
		if ( $name == "linux" ) then
			set md=linux
		endif
		breaksw

	case sun3:
		set md=sun3
		breaksw

	case sparc:
	case sun4*:
		set md=sun4
		breaksw

	case RISC:
		set md=mips
		breaksw

	case IP[0-9]*:
		set md=mips
		breaksw

	default:
		goto bail
		breaksw
endsw
rm -f Makefile md.h os.h
set bpf
set brokenmake
set cslip
set dnet
set ethers
set gcc
set gcc1
if ( -e /dev/bpf0 ) set bpf='-e s/^#have-bpf#//'
if ( -f /usr/include/net/slip.h || $name == "linux" ) set cslip='-e s/^#have-cslip#//'
if ( -f /usr/lib/libdnet.a ) set dnet='-e s/^#have-dnet#//'
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
	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#//'
	set ver=`gcc -v |& sed -n -e '$s/.* //' -e '$s/\..*//p'`
	if ( "$ver" == 1 ) set gcc1='-e s/^#have-gcc1#//'
endif
if ("$os" == "ultrix4" || "$os" == "sunos3") then
	set brokenmake='-e s/^#have-broken-make#//'
endif
if ( ! -f /usr/include/stdlib.h ) ln -s /dev/null stdlib.h
if ( ! -f md-$md.h ) then
	echo "configure: unsupported machine type $md"
	exit 1
endif
ln -s md-$md.h md.h
if ( ! -f os-$os.h ) then
	echo "configure: unsupported os type $os"
	exit 1
endif
ln -s os-$os.h os.h
sed -e "s/^#have-$md-$os#//" -e "s/^#have-$os#//" \
    $bpf $cslip $gcc $gcc1 $ethers $dnet $brokenmake Makefile.in > Makefile
set mflags=""
if ( "$gcc" != "" ) set mflags="CC=gcc"
chmod ug+w Makefile
make depend $mflags
exit 0
bail:
echo 'configure: unsupported system'
exit 1
