#!/bin/sh
#
# Mksrc - make Linux source files
#
# WARNING: This script assumes it is running from the main directory
#	   of the lsof, version 3 distribution.
#
# One environment variable applies:
#
# LSOF_MKC	is the method for creating the source files.
#		It defaults to "ln -s".  A common alternative is "cp".
#
# $Id: Mksrc,v 1.9 98/01/06 07:27:37 abe Exp $


if test "X$LINUX_KERNEL" = "X"
then
  LINUX_KERNEL="/usr/src/linux"
fi

D=dialects/linux/kmem
DC=${LINUX_KERNEL}/fs/dcache.c
DE=0
ID=${D}/include
KNC=${ID}/kncache.h
L="dfile.c dlsof.h dmnt.c dnode.c dproc.c dproto.h dsock.c dstore.c machine.h"
ND=${LINUX_KERNEL}/fs/nfs/dir.c

if test -r ${LINUX_KERNEL}/include/linux/dcache.h
then
  grep "^struct dentry {" ${LINUX_KERNEL}/include/linux/dcache.h > /dev/null
  if test $? -eq 0
  then
    DE=1
  fi
fi

if test "X${LSOF_INCLUDE}" = "X"
then
  STD_INC=/usr/include
else
  STD_INC=${LSOF_INCLUDE}
fi

# If this is Linux 2.1.x, add __lseek.s to the file list.

if test $2 -ge 2100
then
  L="$L __lseek.s"
fi

for i in $L
do
  rm -f $i
  $LSOF_MKC $D/$i $i
  echo "$LSOF_MKC $D/$i $i"
done

# Create the Linux kernel name cache header file.

if test ! -d ${ID}
then
  mkdir ${ID}
  echo "Created ${ID}."
fi
rm -f $KNC
echo "/*" > $KNC
echo -n " * kncache.h -- " >> $KNC
date >> $KNC
echo " */" >> $KNC
echo "" >> $KNC
echo "" >> $KNC
if test -r $DC
then
  echo "/*" >> $KNC
  echo " * From $DC:" >> $KNC
  echo " */" >> $KNC
  echo "" >> $KNC
  echo "" >> $KNC
  if test $DE -eq 0
  then

    # Get name cache parameters for kernels without a struct dentry.

    egrep "^#define[ 	]*DCACHE_SIZE" $DC >> $KNC
    if test $? -eq 0
    then
      STATUS_DCS=1
    else
      STATUS_DCS=0
      echo "WARNING!!!  DCACHE_SIZE isn't defined in "
      echo "            ${KNC}."
    fi
    egrep "^#define[ 	]*DCACHE_NAME_LEN" $DC >> $KNC
    if test $? -eq 0
    then
      STATUS_DNL=1
    else
      STATUS_DNL=0
      echo "WARNING!!!  DCACHE_NAME_LEN isn't defined in"
      echo "            ${KNC}."
    fi
    echo "" >> $KNC
    awk  '/^struct[ 	]*dir_cache_entry[ 	]*{/ { is = 1; }\
	  /^struct[ 	]*hash_list[ 	]*{/ { is = 1; }\
	  /^};/ { if (is == 1) { print $0; is = 0; }}\
	  { if (is == 1) print $0; }' < $DC >> $KNC
  else

    # Get name cache parameters for kernels with a struct dentry.

    egrep "^#define[ 	]D_HASHBITS" $DC >> $KNC
    if test $? -eq 0
    then
      STATUS_HB=1
    else
      STATUS_HB=0
      echo "WARNING!!!  D_HASHBITS isn't defined in"
      echo "            ${KNC}."
    fi
    egrep "^#define[ 	]D_HASHSIZE" $DC >> $KNC
    if test $? -eq 0
    then
      STATUS_HS=1
    else
      STATUS_HS=0
      echo "WARNING!!!  D_HASHSIZE isn't defined in"
      echo "            ${KNC}."
    fi
    egrep "^#define[ 	]D_HASHMASK" $DC >> $KNC
    if test $? -eq 0
    then
      STATUS_HM=1
    else
      STATUS_HM=0
      echo "WARNING!!!  D_HASHMASK isn't defined in"
      echo "            ${KNC}."
    fi
    if test $STATUS_HB -eq 1 -a $STATUS_HS -eq 1 -a $STATUS_HM -eq 1
    then
      echo "#define	HASINOKNC	1" >> $KNC
      echo "#define	DCACHE_SIZE	4096" >> $KNC
    else
      echo "WARNING!!!  $KNC is incomplete."
      echo "            Kernel name cache path lookup may be inhibited."
    fi
  fi
else
  echo "WARNING!!!  $DC isn't accessible."
  echo "            Thus, ${KNC} is incomplete, and"
  echo "            kernel name cache path lookup may be inhibited."
  STATUS_DCS=0
  STATUS_DNL=0
fi

# Get NFS name cache parameters.  (Kernel doesn't have a struct dentry.)

if test $DE -eq 0
then
  if test -r $ND
  then
    echo "" >> $KNC
    echo "" >> $KNC
    echo "/*" >> $KNC
    echo " * From $ND:" >> $KNC
    echo " */" >> $KNC
    echo "" >> $KNC
    echo "" >> $KNC
    awk  '/^static[ 	]*struct[ 	]*nfs_lookup_cache_entry/ {\
	     if (ft == 0) {\
		print "struct nfs_lookup_cache_entry {";\
		ft = 1; is = 1; next;\
	     }}\
	    /^}/ { if (is == 1) { print "};"; is = 0; }}\
	    { if (is == 1) print $0; }' < $ND >> $KNC
  else
    echo "WARNING!!!  $ND isn't accessible."
    echo "            Kernel NFS name cache access isn't possible."
  fi
fi

# Check the non-dentry-struct kernel name cache header file for completeness
# and set the HASDEVKNC and HASNFSKNC symbols in it.

if test $DE -eq 0
then
  egrep '^struct[ 	]*dir_cache_entry[ 	]*{' $KNC > /dev/null
  if test $? -eq 0
  then
    STATUS_DCE=1
  else
    STATUS_DCE=0
    echo "WARNING!!!  The dir_cache_entry structure isn't defined in"
    echo "            ${KNC}."
  fi
  egrep '^struct[ 	]*hash_list[ 	]*{' $KNC > /dev/null
  if test $? -eq 0
  then
    STATUS_HL=1
  else
    STATUS_HL=0
    echo "WARNING!!!  The hash_list structure isn't defined in"
    echo "            ${KNC}."
  fi
  if test $STATUS_DCE -eq 1 -a $STATUS_HL -eq 1 -a $STATUS_DCS -eq 1 \
       -a $STATUS_DNL -eq 1
  then
    echo "" >> $KNC
    echo "#define HASDEVKNC 1" >> $KNC
  else
    echo "WARNING!!!  $KNC is incomplete."
    echo "            Kernel name cache path lookup may be inhibited."
  fi
  grep "^struct nfs_lookup_cache_entry {" $KNC > /dev/null
  if test $? -eq 0
  then
    STATUS_NLE=1
  else
    STATUS_NLE=0
    echo "WARNING!!!  The nfs_lookup_cache_entry structure isn't defined in"
    echo "            ${KNC}."
  fi
  if test -r ${STD_INC}/linux/nfs.h
  then
    STATUS_NFS_H=1
  else
    STATUS_NFS_H=0
    echo "WARNING!!!  ${STD_INC}/linux/nfs.h isn't accessible."
  fi
  if test -r ${STD_INC}/linux/nfs_fs.h
  then
    STATUS_NFS_FS_H=1
  else
    STATUS_NFS_FS_H=0
    echo "WARNING!!!  ${STD_INC}/linux/nfs_fs.h isn't accessible."
  fi
  if test $STATUS_NLE -eq 1 -a $STATUS_NFS_H -eq 1 -a $STATUS_NFS_FS_H -eq 1
  then
    echo "" >> $KNC
    echo "#define HASNFSKNC 1" >> $KNC
    echo "#include <linux/nfs.h>" >> $KNC
    echo "#include <linux/nfs_fs.h>" >> $KNC
  else
    echo "WARNING!!!  $KNC is incomplete."
    echo "            Kernel NFS name cache path lookup may be inhibited."
  fi
fi
echo "Created $KNC."
