#! /bin/sh
# Install modified versions of certain ANSI-incompatible system header files
# which are fixed to work correctly with ANSI C
# and placed in a directory that GNU C will search.
# This works properly on a Sun in system version 3.4;
# for other versions, you had better check.

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

mkdir $LIB > /dev/null 2>&1

echo 'Finding header files:'
cd /usr/include
files=`find . -type f -print`
echo 'Checking header files:'
for file in $files; do
  if egrep -s '[ 	]_IO[A-Z]*\(|#define._IO|CTRL' $file; then
    echo Fixing $file
    if [ -r $file ]; then
      cp $file ${LIB}/$file >/dev/null 2>&1	\
      || (mkdir `dirname ${LIB}/$file`		\
	  && cp $file ${LIB}/$file)		\
      || echo "Can't copy $file"
      chmod +w ${LIB}/$file
      ex ${LIB}/$file <<EOF
      g/[ 	]_IO[A-Z]*(/s/(\(.\),/('\1',/
      g/#define._IO/s/'x'/x/g
      g/CTRL[ 	]*(/s/\(.\))/'\1')/
      g/#define.CTRL/s/'c'/c/g
      wq
EOF
    fi
  fi
done


# Fix one other error in this file: a mismatched quote not inside a C comment.
file=sundev/vuid_event.h
if [ -r $file ]; then
  if [ ! -r ${LIB}/$file ]; then
    cp $file ${LIB}/$file >/dev/null 2>&1	\
    || (mkdir `dirname ${LIB}/$file`		\
        && cp $file ${LIB}/$file)		\
    || echo "Can't copy $file"
    chmod +w ${LIB}/$file
  fi
fi

if [ -r ${LIB}/sundev/vuid_event.h ]; then
  echo Fixing sundev/vuid_event.h comment
  ex ${LIB}/sundev/vuid_event.h <<EOF
  g/doesn't/s/doesn't/doesn''t/
  wq
EOF
fi