#!/bin/sh

# $Id: changeIncludes,v 1.5 1995/08/24 09:08:56 svein Exp $
# Author: Svein Bøe, 1994

usage () {
  echo "usage: $0 [-h] [-n] [<files>]"
  echo "  <files>  : File names of the form"
  echo "             <headerFileName>.<sourceFileName>.grep"
  echo "             Default: All *.grep-files in cwd."
  echo "  -h       : Print this text."
  echo "  -n       : Do not change any files, only tell what would be done."
  echo ""
  echo "Purpose:"
  echo "Change include-directives of the source-files with names given by the"
  echo "input files <headerFileName>.<sourceFileName>.grep."
  echo "Use the constants XITE_* instead of e.g. <malloc.h>."

  exit 1
}

#############
firstWord () { # $1=word1.word2.word3.word4.....
  if [ -n "$1" ]; then
    old="$1"
  else
    return 1
  fi
  echo $1 | awk - -F. '{print $1}'
#
# Alternative:
#
#  new=`expr $old : '\(.*\)\.'`
#  while [ -n "$new" ]; do
#    old=$new
#    new=`expr $old : '\(.*\)\.'`
#  done
#  echo $old

  return 0
} # firstWord

#############
lastWord () { # $1=word1.word2.word3.word4.....
  expr $1 : '.\{1,\}\.\(.*\)'
#
# Alternative:
#
#  echo $1 | awk - -F. '{print $NF}'

} # lastWord

#############
remainingWords () { # $1=<whole word>, $2=<initial word to be removed>
  expr $1 : "$2\.\(.*\)"
} # remainingWords

#############
firstTwoWords () { # $1=word.word2.word3.word4...
  w1=`firstWord $1`
  rest=`remainingWords $1 $w1`
  w2=`firstWord $rest`
  echo $w1.$w2
}

files=""
help=0
dont=0
while [ $# -gt 0 ]; do
  case $1 in
    -h|-help)  help=1; usage;;
    -n)        dont=1;
               shift;;
    *)         if [ -z "$files" ]; then
	         files=$1
	       else
		 files="$files $1"
	       fi
	       shift;;
  esac
done

if [ -z "$files" ]; then
  files=`ls *.grep 2>/dev/null`
fi

if [ -z "$files" ]; then
  echo "No such files." 1>&2
  usage
fi

for f in $files; do
  include=`firstTwoWords $f`
  rest=`remainingWords $f $include`
  sourceFile=`firstTwoWords $rest`

  case "$include" in
    "<fcntl.h>"|"<sys/fcntl.h>")
	INCLUDE=XITE_FCNTL_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <fcntl.h>/#include XITE_FCNTL_H/' $sourceFile | \
	    sed 's/#include <sys\/fcntl.h>/#include XITE_FCNTL_H/' \
	    > $sourceFile.ch
	fi;;
    "<sys/file.h>"|"<sys/io.h>")
	INCLUDE=XITE_FILE_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <sys\/file.h>/#include XITE_FILE_H/' $sourceFile |\
	    sed 's/#include <sys\/io.h>/#include XITE_FILE_H/' \
	    > $sourceFile.ch
	fi;;
    "<io.h>")
	INCLUDE=XITE_IO_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <io.h>/#include XITE_IO_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<vfork.h>")
	INCLUDE=XITE_FORK_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <vfork.h>/#include XITE_FORK_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<malloc.h>") 
	INCLUDE=XITE_MALLOC_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <malloc.h>/#include XITE_MALLOC_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<memory.h>") 
	INCLUDE=XITE_MEMORY_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <memory.h>/#include XITE_MEMORY_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<sys/stat.h>")
	INCLUDE=XITE_STAT_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <sys\/stat.h>/#include XITE_STAT_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<varargs.h>"|"<stdarg.h>") 
	INCLUDE=XITE_STDARG_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <varargs.h>/#include XITE_STDARG_H/' $sourceFile |\
	    sed 's/#include <stdarg.h>/#include XITE_STDARG_H/' \
	    > $sourceFile.ch
	fi;;
    "<stdio.h>")
	INCLUDE=XITE_STDIO_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <stdio.h>/#include XITE_STDIO_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<string.h>")
	INCLUDE=XITE_STRING_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <string.h>/#include XITE_STRING_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<strings.h>")
	INCLUDE=XITE_STRING_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <strings.h>/#include XITE_STRING_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<time.h>")
	INCLUDE=XITE_TIME_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <time.h>/#include XITE_TIME_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<sys/types.h>")
	INCLUDE=XITE_TYPES_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <sys\/types.h>/#include XITE_TYPES_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<unistd.h>") 
	INCLUDE=XITE_UNISTD_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <unistd.h>/#include XITE_UNISTD_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    "<limits.h>") 
	INCLUDE=XITE_LIMITS_H
	if [ "$dont" -eq 0 ]; then
	    sed 's/#include <limits.h>/#include XITE_LIMITS_H/' \
	    $sourceFile > $sourceFile.ch
	fi;;
    *) echo "Unknown include name $include."
       continue;;
  esac

  if [ "$dont" -eq 1 ]; then
      continue
  fi

  if [ -s "$sourceFile.ch" -a -n "`diff "$sourceFile" "$sourceFile.ch"`" ]
  then
    mv $sourceFile $sourceFile.old
    mv $sourceFile.ch $sourceFile
  elif [ ! -s "$sourceFile.ch" ]; then
    echo "File $sourceFile.ch has size zero."
    rm $sourceFile.ch
    continue
  elif [ -z "`diff "$sourceFile" "$sourceFile.ch"`" ]; then
    rm $sourceFile.ch
  fi
done

echo "Check order of include directives, by using the script checkIncOrder,"
echo "and remember to delete the files"
echo "$files"
