#! /bin/sh
#
#   fixinc.svr4  --  Install modified versions of certain ANSI-incompatible
#   native System V Release 4 system include files.
#
#   Written by Ron Guilmette (rfg@ncd.com).
#
# This file is part of GNU CC.
# 
# GNU CC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# GNU CC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with GNU CC; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#
#	This script munges the native include files provided with System V
#	Release 4 systems so as to remove things which are violations of the
#	ANSI C standard.  Once munged, the resulting new system include files
#	are placed in a directory that GNU C will search *before* searching
#	the /usr/include directory. This script should work properly for most
#	System V Release 4 systems.  For other types of systems, you should
#	use the `fixincludes' script instead.
#
#	See README-fixinc for more information.

# Directory containing the original header files.
INPUT=${2-${INPUT-/usr/include}}

# Fail if no arg to specify a directory for the output.
if [ x$1 = x ]
then echo fixincludes: no output directory specified
exit 1
fi

# Directory in which to store the results.
LIB=${1-${LIB-/usr/local/lib/gcc-include}}

# Make sure it exists.
if [ ! -d $LIB ]; then
  mkdir $LIB || exit 1
fi

ORIG_DIR=`pwd`

# Make LIB absolute.
cd $LIB; LIB=`pwd`

# This prevents /bin/ex from failing if the current terminal type is
# unrecognizable.
TERM=unknown
export TERM

echo 'Building fixincludes in ' ${LIB}

# Determine whether this filesystem has symbolic links.
if ln -s X $LIB/ShouldNotExist 2>/dev/null; then
  rm -f $LIB/ShouldNotExist
  LINKS=true
else
  LINKS=false
fi

echo 'Making directories:'
cd ${INPUT}
if $LINKS; then
  files=`ls -LR | sed -n s/:$//p`
else
  files=`find . -type d -print | sed '/^.$/d'`
fi
for file in $files; do
  rm -rf $LIB/$file
  if [ ! -d $LIB/$file ]
  then mkdir $LIB/$file
  fi
done

# treetops gets an alternating list
# of old directories to copy
# and the new directories to copy to.
treetops="${INPUT} ${LIB}"

if $LINKS; then
  echo 'Making internal symbolic directory links'
  for file in $files; do
    dest=`ls -ld $file | sed -n 's/.*-> //p'`
    if [ "$dest" ]; then    
      cwd=`pwd`
      # In case $dest is relative, get to $file's dir first.
      cd ${INPUT}
      cd `echo ./$file | sed -n 's&[^/]*$&&p'`
      # Check that the target directory exists.
      # Redirections changed to avoid bug in sh on Ultrix.
      (cd $dest) > /dev/null 2>&1
      if [ $? = 0 ]; then
	cd $dest
	# X gets the dir that the link actually leads to.
	x=`pwd`
	# If link leads back into ${INPUT},
	# make a similar link here.
	if expr $x : "${INPUT}/.*" > /dev/null; then
	  # Y gets the actual target dir name, relative to ${INPUT}.
	  y=`echo $x | sed -n "s&${INPUT}/&&p"`
	  echo $file '->' $y ': Making link'
	  rm -fr ${LIB}/$file > /dev/null 2>&1
	  ln -s ${LIB}/$y ${LIB}/$file > /dev/null 2>&1
	else
	  # If the link is to outside ${INPUT},
	  # treat this directory as if it actually contained the files.
# This line used to have $dest instead of $x.
# $dest seemed to be wrong for links found in subdirectories
# of ${INPUT}.  Does this change break anything?
	  treetops="$treetops $x ${LIB}/$file"
	fi
      fi
      cd $cwd
    fi
  done
fi

set - $treetops
while [ $# != 0 ]; do
  # $1 is an old directory to copy, and $2 is the new directory to copy to.
  echo "Finding header files in $1:"
  cd ${INPUT}
  cd $1
  files=`find . -name '*.h' -type f -print`
  echo 'Checking header files:'
  for file in $files; do
      echo Fixing $file
      if [ -r $file ]; then
	cp $file $2/$file >/dev/null 2>&1 || echo "Can't copy $file"
	chmod +w $2/$file

# The following have been removed from the sed command below
# because it is more useful to leave these things in.
# The only reason to remove them was for -pedantic,
# which isn't much of a reason. -- rms.
#	  /^[ 	]*#[ 	]*ident/d

# The change of u_char, etc, to u_int
# applies to bit fields.
	sed -e '
	  s%^\([ 	]*#[ 	]*endif[ 	]*\)\([^/ 	].*\)$%\1/* \2 */%
	  s%^\([ 	]*#[ 	]*else[ 	]*\)\([^/ 	].*\)$%\1/* \2 */%
  	  s/#lint(on)/defined(lint)/g
  	  s/#lint(off)/!defined(lint)/g
  	  s/#machine(\([^)]*\))/defined(__\1__)/g
  	  s/#system(\([^)]*\))/defined(__\1__)/g
  	  s/#cpu(\([^)]*\))/defined(__\1__)/g
	  /#[a-z]*if.*[	 (]m68k/		s/\([^_]\)m68k/\1__m68k__/g
	  /#[a-z]*if.*[	 (]__i386/	s/__i386/__i386__/g
	  /#[a-z]*if.*[	 (]i386/		s/\([^_]\)i386/\1__i386__/g
	  /#[a-z]*if.*[	 (]sparc/	s/\([^_]\)sparc/\1__sparc__/g
	  /#[a-z]*if.*[	 (]mc68000/	s/\([^_]\)mc68000/\1__mc68000__/g
	  /#[a-z]*if.*[	 (]vax/		s/\([^_]\)vax/\1__vax__/g
	  /#[a-z]*if.*[	 (]sun/		s/\([^_]\)\(sun[a-z0-9]*\)\([^a-z0-9_]\)/\1__\2__\3/g
	  /#[a-z]*if.*[	 (]sun/		s/\([^_]\)\(sun[a-z0-9]*\)$/\1__\2__/g
	  /#[a-z]*if.*[	 (]ns32000/	s/\([^_]\)ns32000/\1__ns32000__/g
	  /#[a-z]*if.*[	 (]pyr/		s/\([^_]\)pyr/\1__pyr__/g
	  /#[a-z]*if.*[	 (]is68k/	s/\([^_]\)is68k/\1__is68k__/g
	  s/u_char\([ 	][ 	]*[a-zA-Z0-9_][a-zA-Z0-9_]*[ 	]*:[ 	]*[0-9][0-9]*\)/u_int\1/
	  s/u_short\([ 	][ 	]*[a-zA-Z0-9_][a-zA-Z0-9_]*[ 	]*:[ 	]*[0-9][0-9]*\)/u_int\1/
	  s/ushort\([ 	][ 	]*[a-zA-Z0-9_][a-zA-Z0-9_]*[ 	]*:[ 	]*[0-9][0-9]*\)/u_int\1/
	  s/evcm_t\([ 	][ 	]*[a-zA-Z0-9_][a-zA-Z0-9_]*[ 	]*:[ 	]*[0-9][0-9]*\)/u_int\1/
	  s/Pbyte\([ 	][ 	]*[a-zA-Z0-9_][a-zA-Z0-9_]*[ 	]*:[ 	]*SEQSIZ\)/unsigned int\1/
	  s/__STDC__ == 0/!defined (__STRICT_ANSI__)/g
	  s/__STDC__ != 0/defined (__STRICT_ANSI__)/g
	  s/__STDC__ - 0 == 0/!defined (__STRICT_ANSI__)/g
	' $2/$file > $2/$file.sed
	mv $2/$file.sed $2/$file
	if cmp $file $2/$file >/dev/null 2>&1; then
	   echo Deleting $2/$file\; no fixes were needed.
	   rm $2/$file
	fi
      fi
  done
  shift; shift
done

# Fix first broken decl of getcwd present on some svr4 systems.

file=stdlib.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  sed -e 's/getcwd(char \*, int)/getcwd(char *, size_t)/' $file_to_fix > /tmp/$base
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Fix second broken decl of getcwd present on some svr4 systems.  Also
# fix the incorrect decl of profil present on some svr4 systems.

file=unistd.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  sed -e 's/getcwd(char \*, int)/getcwd(char *, size_t)/' $file_to_fix \
    | sed -e 's/profil(unsigned short \*, unsigned int, unsigned int, unsigned int)/profil(unsigned short *, size_t, int, unsigned)/' > /tmp/$base
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Fix the definition of NULL in <sys/param.h> so that it is conditional
# and so that it is correct for both C and C++.

file=sys/param.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  cp $file_to_fix /tmp/$base
  chmod +w /tmp/$base
  ex /tmp/$base <<EOF
  /^#define[ 	]*NULL[ 	]*0$/c
#ifndef NULL
#ifdef __cplusplus
#define __NULL_TYPE
#else /* !defined(__cplusplus) */
#define __NULL_TYPE (void *)
#endif /* !defined(__cplusplus) */
#define NULL (__NULL_TYPE 0)
#endif /* !defined(NULL) */
.
  wq
EOF
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Likewise fix the definition of NULL in <stdio.h> so that it is conditional
# and so that it is correct for both C and C++.

file=stdio.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  cp $file_to_fix /tmp/$base
  chmod +w /tmp/$base
  ex /tmp/$base <<EOF
  /^#define[ 	]*NULL[ 	]*0$/c
#ifdef __cplusplus
#define __NULL_TYPE
#else /* !defined(__cplusplus) */
#define __NULL_TYPE (void *)
#endif /* !defined(__cplusplus) */
#define NULL (__NULL_TYPE 0)
.
  wq
EOF
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Add some missing declarations to <sys/mman.h>.

file=sys/mman.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  cp $file_to_fix /tmp/$base
  chmod +w /tmp/$base
  ex /tmp/$base <<EOF
  /^extern caddr_t mmap();$/c
#ifdef __STDC__
extern caddr_t mmap (caddr_t addr, size_t len, int prot, int flags,
                     int fd, off_t off);
#else /* !defined(__STDC__) */
extern caddr_t mmap ();
#endif /* !defined(__STDC__) */
.
  wq
EOF
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No changed needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Fix declarations of `ftw' and `nftw' in <ftw.h>.

file=ftw.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  cp $file_to_fix /tmp/$base
  chmod +w /tmp/$base
  ex /tmp/$base <<EOF
  /^extern int ftw(const/c
#if !defined(_STYPES)
static
#else
extern
#endif
  int ftw(const char *, int (*)(const char *, const struct stat *, int), int);
.
  /^extern int nftw/c
#if defined(_STYPES)
static
#else
extern
#endif
  int nftw(const char *, int (*)(const char *, const struct stat *, int, struct FTW *),int, int);
.
  /^extern int ftw(),/c
#if !defined(_STYPES)
static
#else
extern
#endif
  int ftw();
#if defined(_STYPES)
static
#else
extern
#endif
  int nftw();
.
  wq
EOF
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Add a `static' declaration of `getrnge' into <regexp.h>.

file=regexp.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  cp $file_to_fix /tmp/$base
  chmod +w /tmp/$base
  ex /tmp/$base <<EOF
  /^static int[ 	]*size;/c
static int	size ;

static int getrnge ();
.
  wq
EOF
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Add a #define of _SIGACTION_ into <sys/signal.h>.

file=sys/signal.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  cp $file_to_fix /tmp/$base
  chmod +w /tmp/$base
  ex /tmp/$base <<EOF
  /^struct sigaction {/c
#define _SIGACTION_
struct  sigaction  {
.
  wq
EOF
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Fix declarations of `makedev', `major', and `minor' in <sys/mkdev.h>.

file=sys/mkdev.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  cp $file_to_fix /tmp/$base
  chmod +w /tmp/$base
  ex /tmp/$base <<EOF
  /^dev_t makedev(const/c
static dev_t makedev(const major_t, const minor_t);
.
  /^dev_t makedev()/c
static dev_t makedev();
.
  /^major_t major(const/c
static major_t major(const dev_t);
.
  /^major_t major()/c
static major_t major();
.
  /^minor_t minor(const/c
static minor_t minor(const dev_t);
.
  /^minor_t minor()/c
static minor_t minor();
.
  wq
EOF
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Fix reference to NMSZ in <sys/adv.h>.

file=sys/adv.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  sed 's/\[NMSZ\]/\[RFS_NMSZ\]/g' $file_to_fix > /tmp/$base
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Fix reference to NC_NPI_RAW in <sys/netcspace.h>.  Also fix types of
# array initializers.

file=sys/netcspace.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  sed 's/NC_NPI_RAW/NC_TPI_RAW/g' $file_to_fix \
    | sed 's/NC_/(unsigned long) NC_/' > /tmp/$base
  if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
    echo No change needed in $file_to_fix
  else
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
  fi
  rm -f /tmp/$base
fi

# Conditionalize all of <fs/rfs/rf_cache.h> on _KERNEL being defined.

file=fs/rfs/rf_cache.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  if grep _KERNEL $file_to_fix > /dev/null; then
    echo No change needed in $file_to_fix
  else
    echo '#ifdef _KERNEL' > /tmp/$base
    cat $file_to_fix >> /tmp/$base
    echo '#endif /* defined(_KERNEL) */' >> /tmp/$base
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
    rm -f /tmp/$base
  fi
fi

# Conditionalize all of <sys/erec.h> on _KERNEL being defined.

file=sys/erec.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  if grep _KERNEL $file_to_fix > /dev/null; then
    echo No change needed in $file_to_fix
  else
    echo '#ifdef _KERNEL' > /tmp/$base
    cat $file_to_fix >> /tmp/$base
    echo '#endif /* defined(_KERNEL) */' >> /tmp/$base
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
    rm -f /tmp/$base
  fi
fi

# Conditionalize all of <sys/err.h> on _KERNEL being defined.

file=sys/err.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  if grep _KERNEL $file_to_fix > /dev/null; then
    echo No change needed in $file_to_fix
  else
    echo '#ifdef _KERNEL' > /tmp/$base
    cat $file_to_fix >> /tmp/$base
    echo '#endif /* defined(_KERNEL) */' >> /tmp/$base
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
    rm -f /tmp/$base
  fi
fi

# Conditionalize all of <sys/char.h> on _KERNEL being defined.

file=sys/char.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  if grep _KERNEL $file_to_fix > /dev/null; then
    echo No change needed in $file_to_fix
  else
    echo '#ifdef _KERNEL' > /tmp/$base
    cat $file_to_fix >> /tmp/$base
    echo '#endif /* defined(_KERNEL) */' >> /tmp/$base
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
    rm -f /tmp/$base
  fi
fi

# Conditionalize all of <sys/getpages.h> on _KERNEL being defined.

file=sys/getpages.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  if grep _KERNEL $file_to_fix > /dev/null; then
    echo No change needed in $file_to_fix
  else
    echo '#ifdef _KERNEL' > /tmp/$base
    cat $file_to_fix >> /tmp/$base
    echo '#endif /* defined(_KERNEL) */' >> /tmp/$base
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
    rm -f /tmp/$base
  fi
fi

# Conditionalize all of <sys/map.h> on _KERNEL being defined.

file=sys/map.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  if grep _KERNEL $file_to_fix > /dev/null; then
    echo No change needed in $file_to_fix
  else
    echo '#ifdef _KERNEL' > /tmp/$base
    cat $file_to_fix >> /tmp/$base
    echo '#endif /* defined(_KERNEL) */' >> /tmp/$base
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
    rm -f /tmp/$base
  fi
fi

# Conditionalize all of <sys/cmn_err.h> on _KERNEL being defined.

file=sys/cmn_err.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  if grep _KERNEL $file_to_fix > /dev/null; then
    echo No change needed in $file_to_fix
  else
    echo '#ifdef _KERNEL' > /tmp/$base
    cat $file_to_fix >> /tmp/$base
    echo '#endif /* defined(_KERNEL) */' >> /tmp/$base
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
    rm -f /tmp/$base
  fi
fi

# Conditionalize all of <sys/kdebugger.h> on _KERNEL being defined.

file=sys/kdebugger.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
  file_to_fix=${LIB}/$file
else
  if [ -r ${INPUT}/$file ]; then
    file_to_fix=${INPUT}/$file
  else
    file_to_fix=""
  fi
fi
if [ \! -z "$file_to_fix" ]; then
  echo Checking $file_to_fix
  if grep _KERNEL $file_to_fix > /dev/null; then
    echo No change needed in $file_to_fix
  else
    echo '#ifdef _KERNEL' > /tmp/$base
    cat $file_to_fix >> /tmp/$base
    echo '#endif /* defined(_KERNEL) */' >> /tmp/$base
    echo Fixed $file_to_fix
    rm -f ${LIB}/$file
    cp /tmp/$base ${LIB}/$file
    rm -f /tmp/$base
  fi
fi

echo 'Removing unneeded directories:'
cd $LIB
files=`find . -type d -print | sort -r`
for file in $files; do
  rmdir $LIB/$file > /dev/null 2>&1
done

if $LINKS; then
  echo 'Making internal symbolic non-directory links'
  cd ${INPUT}
  files=`find . -type l -print`
  for file in $files; do
    dest=`ls -ld $file | sed -n 's/.*-> //p'`
    if expr "$dest" : '[^/].*' > /dev/null; then    
      target=${LIB}/`echo file | sed "s|[^/]*\$|$dest|"`
      if [ -f $target ]; then
        ln -s $dest ${LIB}/$file >/dev/null 2>&1
      fi
    fi
  done
fi

cd ${ORIG_DIR}

echo 'Replacing <sys/byteorder.h>'
rm -f ${LIB}/sys/byteorder.h
cp byteorder.h ${LIB}/sys/byteorder.h

exit 0

