#!/bin/sh
#
# Makesrc - 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".
#
#		Cp may be used when it is necessary to assemble a
#		source module from fragments.
#
# $Id: Mksrc,v 1.5 95/11/22 09:58:16 abe Exp $


D=dialects/linux
DC=/usr/src/linux/fs/dcache.c
F=dialects/common
ID=${D}/include
KNC=${ID}/kncache.h
L="dlsof.h dfile.c dnode.c dproc.c dproto.h dsock.c dstore.c machine.h"
ND=/usr/src/linux/fs/nfs/dir.c

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

# Assemble ddev.c.

NM=ddev.c
P="$D/$NM $F/dvch.frag $F/fchi.frag $F/lkud.frag $F/pcdn.frag $F/rdev.frag"
rm -f $NM
cat $P > $NM
echo "$NM assembled."

# Assemble dmnt.c.

NM=dmnt.c
P="$D/$NM $F/rmnt.frag"
rm -f $NM
cat $P > $NM
echo "$NM assembled."

# 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
  egrep "^#define[ 	]*DCACHE_SIZE" $DC >> $KNC
  egrep "^#define[ 	]*DCACHE_NAME_LEN" $DC >> $KNC
  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
  echo "" >> $KNC
  echo "" >> $KNC
fi
if test -r $ND
then
  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
fi

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

grep dir_cache_entry $KNC > /dev/null
if test $? -eq 0
then
  grep hash_list $KNC > /dev/null
  if test $? -eq 0
  then
    egrep "^#define[ 	]*DCACHE_SIZE" $KNC > /dev/null
    if test $? -eq 0
    then
      egrep "^#define[ 	]*DCACHE_NAME_LEN" $KNC > /dev/null
      if test $? -eq 0
      then
	echo "" >> $KNC
	echo "#define HASDEVKNC 1" >> $KNC
      fi
    fi
  fi
fi
grep nfs_lookup_cache_entry $KNC > /dev/null
if test $? -eq 0
then
  if test -r /usr/include/linux/nfs.h
  then
    if test -r /usr/include/linux/nfs_fs.h
    then
      echo "" >> $KNC
      echo "#define HASNFSKNC 1" >> $KNC
      echo "#include <linux/nfs.h>" >> $KNC
      echo "#include <linux/nfs_fs.h>" >> $KNC
    fi
  fi
fi
echo "Created $KNC."
