#!/bin/sh 
#
# Configure -- configure lsof
#
# See the LSOF_HLP here document for usage.
#
# When configuring for a particular dialect, <target-dialect>, this script
# requires that the subdirectory ./dialects/<target-dialect> contain a
# shell script, named $LSOF_MK, that places its source modules in this
# directory.
#
# $Id: Configure,v 1.96 96/05/20 07:50:15 abe Exp $

#LSOF_DISTRIBKVM may be instroduced through the environment to specify the
#	    Sun4 kernel virtual memory type of distrib.cf

LSOF_F="ddev.c dfile.c dlsof.h dmnt.c dnode*.c dproc.c dproto.h dsock.c dstore.c kernelbase.h machine.h machine.h.old new_machine.h"
LSOF_HLP=/tmp/cfghlp.$$

#LSOF_LOCALSUFFIX may be introduced through the environment to select a local
#	          version of a Makefile.  It is used as a suffix to $LSOF_MKF.

LSOF_MK=Mksrc

#LSOF_MKC is the dialect's Mksrc create command -- default "ln -s".

# LSOF_MKFC may be introduced though the environment to change the name
# used for the created make file.

if test "X$LSOF_MKFC" = "X"
then
  LSOF_MKFC=Makefile
fi

LSOF_MKF=Makefile

LSOF_VF=version

# Make sure no other variable important to Makefile construction is
# already set in the environment.
#
#	$LSOF_AFS	    AFS temporary
#	$LSOF_AFSV	    AFS version
#	$LSOF_CC	    C compiler name (default = none)
#	$LSOF_CCV	    C compiler version (default = none)
#	$LSOF_CFGD	    depend options
#	$LSOF_CFGDN	    depend file name
#	$LSOF_CFGF	    C flags -- e.g., -D's
#	$LSOF_CFGL	    loader flags -- e.g., -l's
#	$LSOF_DEBUG	    Makefile's DEBUG string
#	$LSOF_DINC	    include flags -- -I's
#	$LSOF_DOC	    special document (man page) directory path
#	$LSOF_LD	    loader name if not $LSOF_CC
#	$LSOF_LOCALSUFFIX   local suffix for Makefile -- e.g., PUCC for
#			    Purdue University Computing Center's special
#			    Makefile
#	$LSOF_SCRIPT_CALL   Customize and Inventory scripts call status
#	$LSOF_TMPC	    /tmp temporary C source file name

LSOF_AFS=""
LSOF_AFSV=""
LSOF_CC=""
LSOF_CCV=""
LSOF_CFGD=""
LSOF_CFGDN=""
LSOF_CFGF=""
LSOF_CFGL=""
LSOF_DEBUG=""
LSOF_DINC=""
LSOF_DOC=""
LSOF_LD=""
LSOF_SCRIPT_CALL="yes"
LSOF_TMPC=/tmp/lsof_Configure_tmp_$$

# Make sure the help file is removed before an abnormal exit.

trap 'rm -f $LSOF_HLP ${LSOF_TMPC}.[co]; exit 1' 1 2 3 15

rm -f $LSOF_HLP
cat > $LSOF_HLP << LSOF_HLP
Usage: Configure <options> <target-dialect>
  <options> may be:
    -clean     : clean up previous configuration
    -h|-help   : display help information
    -n         : avoid AFS, customization, and inventory checks
  <target-dialect> may be:
    aix        : IBM AIX
    bsdi       : BSDI BSD/OS 2.0, 2.0.1, and 2.1-BETA
    decosf     : DEC OSF/1 2.0, 3.0, 3.2, and 4.0-BETA
    epix211    : EP/IX 2.1.1 for the CDC 4680
    freebsd    : FreeBSD 1.1.5.1, 2.0, and 2.0.5
    hpux       : HP-UX 8, 9, 10.01, or 10.10, using HP C compiler
    hpuxgcc    : HP-UX 8, 9, 10.01, or 10.10, using gcc
    irix       : SGI IRIX 4.0.5H, 5.2, 5.3, 6.0, 6.0.1, 6.1, and 6.2-BETA
    linux      : Linux through 1.3.56
    netbsd     : NetBSD 1.0 and 1.1
    next3      : NEXTSTEP 3.[0123], all architectures
    ptx        : Sequent PTX 2.1.[156], 4.0.[23], and 4.1.[02]
    riscos     : RISCos 4.52 for RS2030
    sco        : SCO Open Desktop or Open Server 1.1, 3.0, and 5.0
    solaris    : Solaris 2.[12345], using gcc
    solariscc  : Solaris 2.[12345], using Sun's cc
    sunos413   : SunOS 4.1.3, using gcc
    sunos413cc : SunOS 4.1.3, using Sun's cc
    ultrix22   : Purdue version of Ultrix 2.2
    ultrix     : Ultrix 4.2, 4.3, 4.4, or 4.5
LSOF_HLP

LSOF_TGT="no-target"

args=$#
while test $args -gt 0
do
  case $1 in
    -clean)
      if test -r $LSOF_MKFC
      then
	make -f $LSOF_MKFC clean
      fi
      rm -f $LSOF_F $LSOF_MKFC $LSOF_HLP AFSHeaders AFSVersion version.h 
      echo rm -f $LSOF_F $LSOF_MKFC $LSOF_HLP AFSHeaders AFSVersion version.h
      exit 0
      ;;
 
    -h|-help) cat $LSOF_HLP
      rm -f $LSOF_HLP
      exit 0
      ;;

    -n*)
      LSOF_SCRIPT_CALL="no"
      ;;

    *)
      if test $LSOF_TGT != no-target
      then
	echo "Only one dialect may be configured at a time."
	echo 'Both "$LSOF_TGT" and "$1" were specified.'
	cat $LSOF_HLP
	rm -f $LSOF_HLP
	exit 1
      else
	LSOF_TGT=$1
      fi
      ;;
  esac
  shift
  args=`expr $args - 1`
done

case $LSOF_TGT in
 no-target)
   echo "No target dialect was specified."
   cat $LSOF_HLP
   rm -f $LSOF_HLP
   exit 1
   ;;

  aix)
    if test -x /usr/bin/oslevel
    then
      echo "Determining AIX version with /usr/bin/oslevel."
      echo "This may take a while, depending on your maintenance level."
      LSOF_CFGF=`/usr/bin/oslevel | sed 's/[^0-9]*\(.*\)/\1/' | sed 's/\.//g'`
      echo "Oslevel reports the version is $LSOF_CFGF."
    else
      LSOF_CFGF=`uname -rv | awk '{printf "%d%d00\n",\$2,\$1}'`
      echo "WARNING: can't execute /usr/bin/oslevel; uname -rv reports the"
      echo "         version is $LSOF_CFGF; edit CFGF in the Makefile to correct"
      echo "         the terminating 00; it should be 10 X the last AIX "
      echo "         version component -- e.g., 40 for AIX 3.2.4."
    fi
    LSOF_CFGF="-D_AIXV=$LSOF_CFGF"

    # Test for AFS.

    if test -r /usr/vice/etc/ThisCell
    then
      if test "X$LSOF_SCRIPT_CALL" = "Xno"
      then
	if test -r ./AFSHeaders -a -r ./AFSVersion
	then
	  LSOF_AFS="yes"
	fi
      else
        if test ! -x ./AFSConfig
        then
	  echo "Can't find or execute the AFSConfig script"
	  exit 1
        fi
        ./AFSConfig
        if test $? -eq 0 -a -r ./AFSHeaders -a -r ./AFSVersion
        then
	    LSOF_AFS="yes"
        fi
      fi
      if test "X$LSOF_AFS" = "Xyes"
      then
	LSOF_AFSV=`cat ./AFSVersion | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1 \2/' | awk '{printf "%d%02d\n",\$1,\$2}'`
	LSOF_CFGF="$LSOF_CFGF -DHAS_AFS=$LSOF_AFSV"
	LSOF_DINC="-I`cat ./AFSHeaders`"
      fi
    fi
    LSOF_CFGL=""
    LSOF_DIALECT_DIR=aix
    ;;

  bsdi)
    LSOF_CC="gcc"
    LSOF_LD="shlicc2"
    case "`uname -r`" in
      2.0*)
        LSOF_CFGF="-D_BSDIV=20000"
        ;;
      2.1*)
        LSOF_CFGF="-D_BSDIV=20100"
        ;;
      *)  
        echo Unknown BSDI release: `uname -r`
        echo Assuming BSDI 2.0
        LSOF_CFGF="-D_BSDIV=20000"
        ;;
    esac
    LSOF_CFGL="-lkvm"
    LSOF_DIALECT_DIR=bsdi
    ;;

  decosf)
    LSOF_OSF1VRD=""
    case "`uname -r`" in
      V2.0)
	LSOF_OSF1V=20000
	LSOF_SYSDIR=/sys
	;;
      V3.0)
	LSOF_OSF1V=30000
	LSOF_OSF1VRD=-DUSELOCALREADDIR
	LSOF_SYSDIR=/sys
	;;
      V3.2)
	LSOF_OSF1V=30200
	LSOF_OSF1VRD=-DUSELOCALREADDIR
	LSOF_SYSDIR=/sys
	;;
      ?4.0)
	LSOF_OSF1V=40000
	LSOF_SYSDIR=/usr/sys
	;;
      *)
	echo "WARNING: unknown OSF/1 version; assuming version is 2.0"
	LSOF_OSF1V=20000
	LSOF_SYSDIR=/sys
	;;
    esac
    echo ""
    echo "Please enter the name of the subdirectory in $LSOF_SYSDIR that"
    echo "contains the configuration files for this host.  Its name is"
    echo "usually an upper case derivative of the host name -- e.g.,"
    echo "TOMIS from tomis.bio.purdue.edu.  If you don't have a sub-"
    echo "directory with a name like that, you may have one named BINARY,"
    echo "and you can try to use it.  The lsof compilation needs header"
    echo "files that are located in the subdirectory."
    LSOF_LOOP=1
    while test $LSOF_LOOP = 1
    do
	echo ""
	echo "$LSOF_SYSDIR contains:"
	echo ""
	ls -CF $LSOF_SYSDIR
	echo ""
        echo -n "Configuration subdirectory name? "
        read LSOF_CDIR LSOF_EXCESS
	if test -d ${LSOF_SYSDIR}/$LSOF_CDIR -a "X$LSOF_CDIR" != "X"
	then
	    LSOF_LOOP=0
	else
	    echo ""
	    echo Cannot access directory /sys/$LSOF_CDIR.
	fi
    done
    echo "Determining the ADVFS version -- this will take a while."
    LSOF_ADVFSV=`/usr/sbin/setld -i | grep "^OSFADVFSBIN[0-9]" | sed 's/\([^ ]*\).*/\1/' | sort -u | tail -1 | sed 's/OSFADVFSBIN//'`
    case $LSOF_ADVFSV in
      1*)
	LSOF_ADVFSV=100
	echo "The ADVFS version is 1."
	;;
      2*)
	LSOF_ADVFSV=200
	echo "The ADVFS version is 2."
	;;
      3*)
	LSOF_ADVFSV=300
	echo "The ADVFS version is 3."
	;;
      4*)
	LSOF_ADVFSV=400
	echo "The ADVFS version is 4."
	;;
      *)
	echo "The ADVFS version is unknown; it will be assumed to be 1."
	LSOF_ADVFSV=100
	;;
    esac
    LSOF_CFGF="-D_OSF1V=$LSOF_OSF1V -D_ADVFSV=$LSOF_ADVFSV $LSOF_OSF1VRD"
    LSOF_DINC="-I${LSOF_SYSDIR}/$LSOF_CDIR"
    LSOF_CFGL="-lmld"
    LSOF_DIALECT_DIR=osf
    ;;

  epix211)
    LSOF_CFGF="-D_EPIXV=20101"
    LSOF_CFGL="-lmld -lsun"
    LSOF_DIALECT_DIR=epix
    ;;

  freebsd)
    LSOF_CC="gcc"
    case "`uname -r`" in
    1.*)
      LSOF_CFGF="-D_FREEBSDV=100"
      LSOF_CFGL="-lutil"
      LSOF_DINC="-I./dialects/freebsd/include/1"
      LSOF_N_UNIXV=/386bsd
      ;;
    2.0-*)
      LSOF_CFGF="-D_FREEBSDV=200"
      LSOF_CFGL="-lkvm"
      LSOF_DINC="-I./dialects/freebsd/include/2"
      LSOF_N_UNIXV=`/usr/sbin/sysctl -n kern.bootfile`
      if test "X$LSOF_N_UNIXV" = "X"
      then
	LSOF_N_UNIXV=/kernel
      fi
      ;;
    2.0.5-*)
      LSOF_CFGF="-D_FREEBSDV=205"
      LSOF_CFGL="-lkvm"
      LSOF_DINC="-I./dialects/freebsd/include/2"
      LSOF_N_UNIXV=`/usr/sbin/sysctl -n kern.bootfile`
      if test "X$LSOF_N_UNIXV" = "X"
      then
	LSOF_N_UNIXV=/kernel
      fi
      ;;
    2.1*)
      LSOF_CFGF="-D_FREEBSDV=210"
      LSOF_CFGL="-lkvm"
      LSOF_DINC="-I./dialects/freebsd/include/2"
      LSOF_N_UNIXV=`/usr/sbin/sysctl -n kern.bootfile`
      if test "X$LSOF_N_UNIXV" = "X"
      then
	LSOF_N_UNIXV=/kernel
      fi
      ;;
    *)
      echo Unknown FreeBSD release: `uname -r`
      echo Assuming FreeBSD 1.x
      LSOF_CFGF="-D_FREEBSDV=100"
      LSOF_CFGL="-lutil"
      LSOF_DINC="-I./dialects/freebsd/include/1"
      LSOF_N_UNIXV=/386bsd
      ;;
    esac
    if test ! -x $LSOF_N_UNIXV
    then
      echo "Hmmm -- $LSOF_N_UNIXV doesn't appear to be your kernel file."
      echo "Please enter the name of the file in / that contains"
      echo "the kernel for this host.  It must be a regular file,"
      echo "not a directory, and must be executable."
      LSOF_LOOP=1
      while test $LSOF_LOOP = 1
      do
	  echo ""
	  echo "/ contains:"
	  echo ""
	  ls -CF /
	  echo ""
          echo -n "Kernel file name? "
          read LSOF_N_UNIXV LSOF_EXCESS
	  LSOF_N_UNIXV="/$LSOF_N_UNIXV"
	  if test ! -d $LSOF_N_UNIXV -a -x $LSOF_N_UNIXV
	  then
	    LSOF_LOOP=0
	  else
	    echo ""
	    echo $LSOF_N_UNIXV is not a regular executable file.
	  fi
      done
    fi
    LSOF_N_UNIXV=`echo $LSOF_N_UNIXV | sed 's#^/*#/#'`
    LSOF_CFGF="$LSOF_CFGF -DN_UNIXV=$LSOF_N_UNIXV"
    LSOF_DIALECT_DIR=freebsd
    ;;

  hpux*)
    if test "X$LSOF_TGT" = "Xhpuxgcc"
    then
      LSOF_CC=gcc
      LSOF_TGT="hpux"
    else
      LSOF_CC=cc
    fi
    LSOF_CFGF=`uname -r | awk -F. '{printf "%d%02d",\$2,\$3}'`
    LSOF_CFGF="-D_HPUXV=$LSOF_CFGF"
    if test -d /etc/conf/x25
    then
      LSOF_CFGF="$LSOF_CFGF -DHPUX_CCITT"
      LSOF_DINC="-I/etc/conf"
    fi
    if test -r /usr/include/sys/fs/vx_inode.h
    then
      LSOF_CFGF="$LSOF_CFGF -DHASVXFS"
    fi
    if test "X$LSOF_CC" = "Xgcc"
    then
      LSOF_CFGF="$LSOF_CFGF -DHAS_CONST"
    fi

    # Test for AFS.

    if test -r /usr/vice/etc/ThisCell
    then
      if test "X$LSOF_SCRIPT_CALL" = "Xno"
      then
	if test -r ./AFSHeaders -a -r ./AFSVersion
	then
	  LSOF_AFS="yes"
	fi
      else
        if test ! -x ./AFSConfig
        then
	  echo "Can't find or execute the AFSConfig script"
	  exit 1
        fi
        ./AFSConfig
        if test $? -eq 0 -a -r ./AFSHeaders -a -r ./AFSVersion
        then
	    LSOF_AFS="yes"
        fi
      fi
      if test "X$LSOF_AFS" = "Xyes"
      then
	LSOF_AFSV=`cat ./AFSVersion | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1 \2/' | awk '{printf "%d%02d\n",\$1,\$2}'`
	LSOF_CFGF="$LSOF_CFGF -DHAS_AFS=$LSOF_AFSV"
	LSOF_DINC="$LSOF_DINC -I`cat ./AFSHeaders`"
      fi
    fi
    LSOF_CFGL=""
    LSOF_DIALECT_DIR=hpux
    ;;

  irix)
    if test ! -x dialects/sgi/MkKernOpts
    then
      echo "Can't execute dialects/sgi/MkKernOpts"
      rm -f $LSOF_HLP
      exit 1
    fi
    case "`uname -r`" in
      4.0.5*)
	LSOF_CFGF="-D_IRIXV=40005"
	LSOF_CFGL="-lmld -lsun -lc_s"
	;;
      5.2*)
	LSOF_CFGF="-D_IRIXV=50200"
	LSOF_CFGL="-lmld"
	LSOF_DINC="-I./dialects/sgi/irix5hdr"
	;;
      5.3*)
	LSOF_CFGF="-D_IRIXV=50300"
	LSOF_CFGF="$LSOF_CFGF `dialects/sgi/MkKernOpts 50300`"
	LSOF_CFGL="-lmld"
	LSOF_DINC="-I./dialects/sgi/irix5hdr"
	;;
      6.0)
	LSOF_CFGF="-D_IRIXV=60000 -woff 1411,1174,1107"
	LSOF_CFGF="$LSOF_CFGF `dialects/sgi/MkKernOpts 60000`"
	LSOF_CFGL="-lelf"
	LSOF_DINC="-I./dialects/sgi/irix6hdr"
	;;
      6.0.1)
	LSOF_CFGF="-D_IRIXV=60001 -woff 1411,1174,1107"
	LSOF_CFGF="$LSOF_CFGF `dialects/sgi/MkKernOpts 60001`"
	LSOF_CFGL="-lelf"
	LSOF_DINC="-I./dialects/sgi/irix6hdr"
	;;
      6.1*)
	LSOF_CFGF="-D_IRIXV=60100"
        if test `uname -s` = "IRIX64"
        then
          LSOF_CFGF="$LSOF_CFGF -mips3"
        fi
	LSOF_CFGF="$LSOF_CFGF `dialects/sgi/MkKernOpts 60100`"
	LSOF_CFGL="-lelf"
	LSOF_DINC="-I./dialects/sgi/irix61hdr -I/usr/include/sys/fs"
	;;
      6.2*)
	LSOF_CFGF="-D_IRIXV=60200"
        if test `uname -s` = "IRIX64"
        then
          LSOF_CFGF="$LSOF_CFGF -mips3"
        fi
        if test -r /usr/include/cachefs/cachefs_fs.h
        then
          LSOF_CFGF="$LSOF_CFGF -DHASCACHEFS"
        fi
	LSOF_CFGF="$LSOF_CFGF `dialects/sgi/MkKernOpts 60200`"
	LSOF_CFGL="-lelf"
	if test ! -r /usr/include/sys/fs/xfs_inode.h
	then
	  LSOF_DINC="-I./dialects/sgi/irix62hdr"
	fi
	;;
      *)
	echo Unknown IRIX release: `uname -r`
	rm -f $LSOF_HLP
	exit 1
	;;
    esac
    if test `uname -s` = "IRIX64"
    then
      LSOF_CFGF="$LSOF_CFGF -64"
    else
      LSOF_CFGF="$LSOF_CFGF -DIRIX32BITS"
    fi
    LSOF_DIALECT_DIR=sgi
    ;;

  linux)
    LSOF_CC="gcc"
    LSOF_CFGF=`uname -r|sed 's/\./ /g'|awk '{printf "%d%d%02d",\$1,\$2,\$3}'`
    LSOF_CFGF="-DLINUXV=$LSOF_CFGF"
    if test -r /usr/src/linux/net/inet/sock.h
    then
      LSOF_CFGL="net/inet/sock.h"
    else
      if test -r /usr/src/linux/include/net/sock.h
      then
        LSOF_CFGL="include/net/sock.h"
	LSOF_DINC="-I/usr/src/linux/include"
      else
	LSOF_CFGL=""
      fi
    fi
    if test "X$LSOF_CFGL" != "X"
    then
      grep protinfo /usr/src/linux/${LSOF_CFGL} > /dev/null 2>&1
      if test $? -eq 0
      then
	LSOF_CFGF="${LSOF_CFGF} -DHASUNMINSOCK"
      fi
    fi
    if test -r /usr/include/linux/netdevice.h
    then
      LSOF_CFGF="$LSOF_CFGF -DHASNETDEVICE_H"
    fi
    if test ! -r /zSystem.map
    then
      if test -r /System.map
      then
	LSOF_CFGF="$LSOF_CFGF -DN_UNIXV=/System.map"
      else
	echo "WARNING: can't find /zSystem.map or /System.map;"
	echo "         assuming /zSystem.map"
      fi
    fi
    if test -r /usr/src/linux/include/linux/autoconf.h
    then
      egrep "^#define[ 	]*CONFIG_KERNEL_ELF" /usr/src/linux/include/linux/autoconf.h > /dev/null
      if test $? -eq 0
      then
        LSOF_CFGF="$LSOF_CFGF -DKERN_LD_ELF"
      fi
    fi
    LSOF_CFGL=""
    LSOF_DIALECT_DIR=linux
    ;;

  netbsd)
    LSOF_CC="gcc"
    case "`uname -r`" in
      1.0*)
	LSOF_CFGF="-DNETBSDV=1000"
	;;
      1.1*)
	LSOF_CFGF="-DNETBSDV=1010"
        ;;
      *)
	echo Unknown NetBSD release: `uname -r`
	echo Assuming NetBSD 1.0
	LSOF_CFGF="-DNETBSDV=1000"
	;;
      esac
    if test -r /usr/include/nfs/nfsproto.h
    then
	LSOF_CFGF="$LSOF_CFGF -DHASNFSPROTO"
    fi
    LSOF_CFGL="-lkvm"
    LSOF_DIALECT_DIR=netbsd
    ;;

  next3)
    LSOF_CCV=`cc -v 2>&1 | sed -n 's/.*version \(.*\)/\1/p'`
    LSOF_CFGF=""
    LSOF_CFGL=""
    LSOF_DIALECT_DIR=next

    # Test for AFS.

    if test -r /usr/vice/etc/ThisCell
    then
      if test "X$LSOF_SCRIPT_CALL" = "Xno"
      then
	if test -r ./AFSHeaders -a -r ./AFSVersion
	then
	  LSOF_AFS="yes"
	fi
      else
        if test ! -x ./AFSConfig
        then
	  echo "Can't find or execute the AFSConfig script"
	  exit 1
        fi
        ./AFSConfig
        if test $? -eq 0 -a -r ./AFSHeaders -a -r ./AFSVersion
        then
	    LSOF_AFS="yes"
        fi
      fi
      if test "X$LSOF_AFS" = "Xyes"
      then
	LSOF_AFSV=`cat ./AFSVersion | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1 \2/' | awk '{printf "%d%02d\n",\$1,\$2}'`
	LSOF_CFGF="$LSOF_CFGF -DHAS_AFS=$LSOF_AFSV"
	LSOF_DINC="-I`cat ./AFSHeaders`"
      fi
    fi
    ;;

  ptx)
    LSOF_CFGF=`uname -v|awk -F. '{printf "%d%d%d",substr($1,2), $2, $3}'`
    if test $LSOF_CFGF -ge 400
    then

    # PTX 4.x tests

      if test $LSOF_CFGF -lt 410
      then
        LSOF_CFGL="-lelf -linet -lnsl -lseq"
      else
        LSOF_CFGL="-lelf -lnsl -lseq -lsocket"
      fi
      if test -r /usr/conf/uts/nfs/rnode.h
      then
	LSOF_CFGF="$LSOF_CFGF -DHAS_NFS"
      fi
    else

    # PTX 2.x tests

      if test -r /usr/include/nfs/rnode.h
      then
	LSOF_CFGF="$LSOF_CFGF -DHAS_NFS"
      fi
      LSOF_CFGL="-linet -lnsl -lseq"
    fi
    LSOF_CFGF="-D_PTXV=$LSOF_CFGF"
    if test -r /usr/include/sys/cdfsinode.h
    then
	LSOF_CFGF="$LSOF_CFGF -DHAS_CDFS"
    fi
    if test -r /usr/include/sys/vx_inode.h
    then
      LSOF_CFGF="$LSOF_CFGF -DHAS_VXFS"
    fi
    LSOF_DIALECT_DIR=ptx
    ;;

  riscos)
    case "`uname -r`" in
      4_52)
	LSOF_RISCOSV=452
	;;
      *)
	echo Unknown RISCos version: `uname -r`
	echo Assuming version is 4_52.
	LSOF_RISCOSV=452
	;;
    esac
    LSOF_CFGF="-D_RISCOSV=$LSOF_RISCOSV"
    LSOF_CFGL="-lmld -lsun"
    LSOF_DIALECT_DIR=riscos
    ;;

  sco)
    case "`uname -X 2>/dev/null | grep Release | sed 's/Release = \(.*\)/\1/'`" in
      3.2v2.0)
	LSOF_CFGF="-D_SCOV=20"
	LSOF_CFGL="-lsocket -lc_s"
	LSOF_DEBUG="-Ox"
	LSOF_MKC="cp"
	;;
      3.2v2.1)
	LSOF_CFGF="-D_SCOV=21 -nointl"
	LSOF_CFGL="-lsocket -lc_s"
	LSOF_DEBUG="-Ox"
	LSOF_MKC="cp"
	;;
      3.2v4.0)
	LSOF_CFGF="-D_SCOV=40 -nointl"
	LSOF_CFGL="-lsocket -lc_s"
	LSOF_DEBUG="-Ox"
	;;
      3.2v4.1)
	LSOF_CFGF="-D_SCOV=41 -nointl"
	LSOF_CFGL="-lsocket -lc_s"
	LSOF_DEBUG="-Ox"
	;;
      3.2v4.2)
	LSOF_CFGF="-D_SCOV=42 -nointl"
	LSOF_CFGL="-lsocket -lc_s"
	LSOF_DEBUG="-Ox"
	;;
      3.2v5.0.*)
	LSOF_CFGF="-D_SCOV=500 -belf"
	LSOF_CFGL="-lsocket"
	LSOF_DEBUG="-O3 -Kspace"
	;;
      *)
	echo Unknown SCO Unix release: `uname -X | grep Release`
	echo Assuming 3.2.0 or 3.2.1
	LSOF_CFGF="-D_SCOV=0 -nointl"
	LSOF_CFGL="-lsocket -lc_s"
	LSOF_DEBUG="-Ox"
	LSOF_MKC="cp"
	;;
    esac
    nm /lib/libc.a | grep statlstat > /dev/null 2>&1
    if test $? -eq 0
    then
      LSOF_CFGF="$LSOF_CFGF -DHAS_STATLSTAT"
    fi
    if test -r /usr/include/sys/fs/nfs/rnode.h
    then
      LSOF_CFGF="$LSOF_CFGF -DHAS_NFS"
    fi
    if test ! -r /usr/include/netdb.h
    then
      LSOF_DINC="-I./dialects/sco/include"
    fi
    LSOF_DIALECT_DIR=sco
    ;;

  solaris*)
    if test "X$LSOF_TGT" = "Xsolariscc"
    then
      LSOF_CC=cc
      LSOF_TGT="solaris"
    else
      LSOF_CC=gcc
    fi

# Solaris patch 101318-32 creates a longer kernel tcp_s structure,
# and 101318-45 changes the way the vnode's v_filocks member is
# handled.  The following code creates a symbol definition for
# patch 101318 whose value is the patch level.  No symbol is defined
# if the patch level is not greater than zero.

    case "`uname -r`" in
      5.3)
	LSOF_PL=`grep -h SUNW_PATCHID=101318 /var/sadm/pkg/SUNWcar*/pkginfo | sed 's/.*-//' | sort -u | tail -1`
	: ${LSOF_PL:=0}
	if test $LSOF_PL -gt 0
	then
	  LSOF_CFGF="-Dsolaris=20300 -DP101318=$LSOF_PL"
	else
	  LSOF_CFGF="-Dsolaris=20300"
	fi;;
      5.[0-2])
	  LSOF_CFGF="-Dsolaris=20300";;
      5.4)
	LSOF_PL=`grep -h SUNW_PATCHID=101945 /var/sadm/pkg/SUNWcar*/pkginfo | sed 's/.*-//' | sort -u | tail -1`
	: ${LSOF_PL:=0}
	if test $LSOF_PL -ge 32
	then
	  LSOF_PL=`grep -h SUNW_PATCHID=102303 /var/sadm/pkg/SUNWhea*/pkginfo | sed 's/.*-//' | sort -u | tail -1`
	  : ${LSOF_PL:=0}
	  if test $LSOF_PL -ge 2
	  then
	    echo "WARNING: your Solaris 2.4 system appears to have patches 101945-32 and 102303-2"
	    echo "         installed.  This probably means the NUM_*_VECTORS definitions in"
	    echo "         <sys/auxv.h> don't match the ones used to build your kernel.  Consult"
	    echo "         section 3.11.5 of the 00FAQ file of the lsof distribution for an"
	    echo "         explanation of the work-around being supplied by including the header"
	    echo "         file ./dialects/sun/include/sys/auxv.h."
	    LSOF_DINC="-I./dialects/sun/include"
	  fi
	fi
	LSOF_CFGF="-Dsolaris=20400";;
      5.5)
	  LSOF_CFGF="-Dsolaris=20500";;
      *)
	  echo Unknown Solaris version: `uname -r`
	  rm -f $LSOF_HLP
	  exit 1
    esac

    # Test for AFS.

    if test -r /usr/vice/etc/ThisCell
    then
      if test "X$LSOF_SCRIPT_CALL" = "Xno"
      then
	if test -r ./AFSHeaders -a -r ./AFSVersion
	then
	  LSOF_AFS="yes"
	fi
      else
        if test ! -x ./AFSConfig
        then
	  echo "Can't find or execute the AFSConfig script"
	  exit 1
        fi
        ./AFSConfig
        if test $? -eq 0 -a -r ./AFSHeaders -a -r ./AFSVersion
        then
	    LSOF_AFS="yes"
        fi
      fi
      if test "X$LSOF_AFS" = "Xyes"
      then
	LSOF_AFSV=`cat ./AFSVersion | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1 \2/' | awk '{printf "%d%02d\n",\$1,\$2}'`
	LSOF_CFGF="$LSOF_CFGF -DHAS_AFS=$LSOF_AFSV"
	LSOF_DINC="$LSOF_DINC -I`cat ./AFSHeaders`"
      fi
    fi
    LSOF_CFGL="-lkvm -lelf -lsocket -lnsl"
    LSOF_DIALECT_DIR=sun
    if test "X$LSOF_LOCALSUFFIX" = "XPUCC"
    then
      LSOF_DOC="\${DESTDIR}/usr/local/man"
    fi
    ;;

  sunos413*)
    if test "X$LSOF_TGT" = "Xsunos413"
    then
      LSOF_CC=gcc
      LSOF_CFGF="-ansi"
    else
      LSOF_CC=cc
      rm -f ${LSOF_TMPC}.[co]
      echo "main() { const x; }" > ${LSOF_TMPC}.c
      cc -c ${LSOF_TMPC}.c -o ${LSOF_TMPC}.o > /dev/null 2>&1
      if test $? -ne 0
      then
        LSOF_CFGF="-DNOCONST"
      fi
      rm -f ${LSOF_TMPC}.[co]
    fi
    LSOF_CFGF="$LSOF_CFGF -DSUNOSV=40103"
    LSOF_CFGL="-lkvm"
    LSOF_DIALECT_DIR=sun
    if test "X$LSOF_LOCALSUFFIX" = "XPUCC"
    then
      LSOF_DOC="\${DESTDIR}/usr/man"
    fi

    # Test for AFS.

    if test -r /usr/vice/etc/ThisCell
    then
      if test "X$LSOF_SCRIPT_CALL" = "Xno"
      then
	if test -r ./AFSHeaders -a -r ./AFSVersion
	then
	  LSOF_AFS="yes"
	fi
      else
        if test ! -x ./AFSConfig
        then
	  echo "Can't find or execute the AFSConfig script"
	  exit 1
        fi
        ./AFSConfig
        if test $? -eq 0 -a -r ./AFSHeaders -a -r ./AFSVersion
        then
	    LSOF_AFS="yes"
        fi
      fi
      if test "X$LSOF_AFS" = "Xyes"
      then
	LSOF_AFSV=`cat ./AFSVersion | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1 \2/' | awk '{printf "%d%02d\n",\$1,\$2}'`
	LSOF_CFGF="$LSOF_CFGF -DHAS_AFS=$LSOF_AFSV"
	LSOF_DINC="-I`cat ./AFSHeaders`"
      fi
    fi

    ;;

  ultrix22)
    LSOF_CFGF="-DULTRIXV=20200 -DHASPWSTAYOPEN"
    LSOF_CFGL=""
    LSOF_DIALECT_DIR=ultrix
    ;;

  ultrix)
    case "`uname -r`" in
      4.2)
	LSOF_CFGF="-DULTRIXV=40200"
	;;
      4.3)
	LSOF_CFGF="-DULTRIXV=40300"
	;;
      4.4)
	LSOF_CFGF="-DULTRIXV=40400"
	;;
      4.5)
	LSOF_CFGF="-DULTRIXV=40500"
	;;
      *)
	echo Unknown Ultrix release: `uname -r`
	rm -f $LSOF_HLP
	exit 1
	;;
    esac
    case "`uname -m`" in
      RISC)
	;;
      VAX)
	LSOF_CFGF="$LSOF_CFGF -DVAXULTRIX"
	;;
      *)
	echo Unknown machine hardware name: `uname -m`
	rm -f $LSOF_HLP
	exit 1
	;;
    esac
    if test -r /usr/lib/libdnet.a -a -r /usr/include/netdnet/dn.h
    then
      LSOF_CFGL="-ldnet"
      LSOF_CFGF="$LSOF_CFGF -DDECnet"
    else
      LSOF_CFGL=""
    fi
    LSOF_DIALECT_DIR=ultrix
    ;;

  *) 
    echo "Can't configure for $LSOF_TGT."
    cat $LSOF_HLP
    rm -f $LSOF_HLP
    exit 1
    ;;
esac

# Do an inventory of the distribution, as required.

if test "X$LSOF_SCRIPT_CALL" = "Xyes" -a ! -r ./.neverInv
then
  if test ! -f ./Inventory	# Want -x, but Ultrix doesn't grok it.
  then
    echo "Can't find Inventory script."
    rm -f $LSOF_HLP
    exit 1
  fi
  ./Inventory
fi

# Make sure target directory exists.

if test ! -d ./dialects/$LSOF_DIALECT_DIR
then
  echo "Can't configure for $LSOF_TGT -- ./dialects/$LSOF_DIALECT_DIR doesn't exist."
  rm -f $LSOF_HLP
  exit 1
fi

# Make sure $LSOF_MK exists in the target directory.

if test ! -r ./dialects/$LSOF_DIALECT_DIR/$LSOF_MK
then
  echo "Can't configure for $LSOF_TGT -- ./dialects/$LSOF_DIALECT_DIR/$LSOF_MK doesn't exist."
  rm -f $LSOF_HLP
  exit 1
fi

# Make sure $LSOF_MKF (or $LSOF_MKF.$LSOF_LOCALSUFFIX) exists in the
# target directory.

if test "X$LSOF_LOCALSUFFIX" != "X"
then
  LSOF_REST=$LSOF_MKF.$LSOF_LOCALSUFFIX
else
  LSOF_REST=$LSOF_MKF
fi
if test ! -r ./dialects/$LSOF_DIALECT_DIR/$LSOF_REST
then
  echo "Can't configure for $LSOF_TGT -- ./dialects/$LSOF_DIALECT_DIR/$LSOF_REST doesn't exist."
  rm -f $LSOF_HLP
  exit 1
fi

# Make sure $LSOF_VF exists.  Extract the version number from it.

if test ! -r $LSOF_VF
then
  echo "Version number file, ./$LSOF_VF, does not exist."
  rm -f $LSOF_HLP
  exit 1
else
  LSOF_VN=`sed "s/.ds VN \(.*\)/\1/" < version`
fi

# Clean up in advance.

rm -f $LSOF_F $LSOF_MKFC
echo rm -f $LSOF_F $LSOF_MKFC

# Make the dialect sources.

if test "X$LSOF_MKC" = "X"
then
  LSOF_MKC="ln -s"
fi
LSOF_MKC=$LSOF_MKC ./dialects/$LSOF_DIALECT_DIR/$LSOF_MK $LSOF_TGT

# Make $LSOF_MKFC.

echo "# $LSOF_TGT Makefile for lsof version $LSOF_VN" > $LSOF_MKFC
echo "" >> $LSOF_MKFC
if test "X$LSOF_CC" != "X"
then
  echo "CC=	$LSOF_CC" >> $LSOF_MKFC
else
  echo "CC=	cc" >> $LSOF_MKFC
fi
if test "X$LSOF_CCV" != "X"
then
  echo "CCV=	$LSOF_CCV" >> $LSOF_MKFC
else
  if test "X$LSOF_CC" = "Xgcc"
  then
    LSOF_CCV=`gcc -v 2>&1 | sed -n 's/.*version \(.*\)/\1/p'`
    echo "CCV=	$LSOF_CCV" >> $LSOF_MKFC
  fi
fi
echo "" >> $LSOF_MKFC
if test "X$LSOF_LD" != "X"
then
  echo "LD=	$LSOF_LD" >> $LSOF_MKFC
  echo "" >> $LSOF_MKFC
fi
if test "X$LSOF_CFGD" != "X"
then
  echo "CFGD=	$LSOF_CFGD" >> $LSOF_MKFC
fi
if test "X$LSOF_CFGDN" != "X"
then
  echo "CFGDN=	$LSOF_CFGDN" >> $LSOF_MKFC
fi
echo "CFGF=	$LSOF_CFGF" >> $LSOF_MKFC
echo "CFGL=	$LSOF_CFGL" >> $LSOF_MKFC
if test "X$LSOF_DEBUG" != "X"
then
  echo "DEBUG=	$LSOF_DEBUG" >> $LSOF_MKFC
fi
echo "" >> $LSOF_MKFC
if test "X$LSOF_DINC" != "X"
then
  echo "DINC=	$LSOF_DINC" >> $LSOF_MKFC
  echo "" >> $LSOF_MKFC
fi
if test "X$LSOF_DOC" != "X"
then
  echo "DOC=$LSOF_DOC" >> $LSOF_MKFC
fi
echo "" >> $LSOF_MKFC
if test "X$LSOF_DISTRIBKVM" != "X" -a "X$LSOF_DISTRIBKVM" != "XKVM"
then
  echo "KVM=	$LSOF_DISTRIBKVM" >> $LSOF_MKFC
  echo "" >> $LSOF_MKFC
fi
cat ./dialects/$LSOF_DIALECT_DIR/$LSOF_REST >> $LSOF_MKFC
echo $LSOF_MKFC created.
rm -f $LSOF_HLP

# Call Customize, as required.

if test "X$LSOF_SCRIPT_CALL" = "Xyes" -a ! -r ./.neverCust
then
  if test ! -f ./Customize	# Want -x, but Ultrix doesn't grok it.
  then
    echo "Can't find Customize script."
    exit 1
  fi
  ./Customize
fi
exit 0
