#! /bin/sh
# Install modified versions of certain ANSI-incompatible
# native Sequent DYNIX/ptx System V Release 3.2 system include files.
# Copyright (C) 1994, 1996 Free Software Foundation, Inc.
# Contributed by Bill Burton <billb@progress.com>
# Portions adapted from fixinc.svr4 and fixincludes.
#
# 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, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
#	This script munges the native include files provided with DYNIX/ptx
#	so as to remove things which are violations of the ANSI C standard.
#	This is done by first running fixinc.svr4 which does most of the
#	work.  A few includes have fixes made to them afterwards  by this
#	script.  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
#	DYNIX/ptx 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?"fixincludes: output directory not specified"}

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

ORIG_DIR=`pwd`

# Make LIB absolute if it is relative.
# Don't do this if not necessary, since may screw up automounters.
case $LIB in
/*)
	;;
*)
 	LIB=$ORIG_DIR/$LIB
	;;
esac

echo 'Running fixinc.svr4'
# DYNIX/ptx has dirname so this is no problem
`dirname $0`/fixinc.svr4 $*
echo 'Finished fixinc.svr4'

echo 'Building fixincludes in ' ${LIB}

# Copied from fixincludes.
# Don't use or define the name va_list in stdio.h.
# This is for ANSI and also to interoperate properly with gcc's varargs.h.
file=stdio.h
if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
  cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
  chmod +w ${LIB}/$file 2>/dev/null
  chmod a+r ${LIB}/$file 2>/dev/null
fi

if [ -r ${LIB}/$file ]; then
  echo Fixing $file, use of va_list
  # Arrange for stdio.h to use stdarg.h to define __gnuc_va_list
  (echo "#define __need___va_list"
   echo "#include <stdarg.h>") > ${LIB}/${file}.sed
  # Use __gnuc_va_list in arg types in place of va_list.
  # On 386BSD use __gnuc_va_list instead of _VA_LIST_. We're hoping the
  # trailing parentheses and semicolon save all other systems from this.
  # Define __va_list__ (something harmless and unused) instead of va_list.
  # Don't claim to have defined va_list.
  sed -e 's@ va_list @ __gnuc_va_list @' \
      -e 's@ va_list)@ __gnuc_va_list)@' \
      -e 's@ _VA_LIST_));@ __gnuc_va_list));@' \
      -e 's@ va_list@ __va_list__@' \
      -e 's@\*va_list@*__va_list__@' \
      -e 's@ __va_list)@ __gnuc_va_list)@' \
      -e 's@_NEED___VA_LIST@_NEED___Va_LIST@' \
      -e 's@VA_LIST@DUMMY_VA_LIST@' \
      -e 's@_NEED___Va_LIST@_NEED___VA_LIST@' \
    ${LIB}/$file >> ${LIB}/${file}.sed
  
  rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
  if cmp $file ${LIB}/$file >/dev/null 2>&1; then
    rm -f ${LIB}/$file
  fi
fi

# In pwd.h, PTX 1.x needs stdio.h included since FILE * was added in a
# prototype later on in the file.
file=pwd.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 stdio $file_to_fix > /dev/null; then
    true
  else
    sed -e '/#include \<sys\types\.h\>/a\
\
#if defined(__STDC__) || defined(__cplusplus)\
#include <stdio.h>\
#endif  /* __STDC__ */' \
    $file_to_fix > ${LIB}/${file}.sed
    rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
    echo Fixed $file_to_fix
  fi
fi

# Copied from fixincludes.
# math.h puts the declaration of matherr before the definition
# of struct exception, so the prototype (added by fixproto) causes havoc.
file=math.h
if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
  cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
  chmod +w ${LIB}/$file 2>/dev/null
  chmod a+r ${LIB}/$file 2>/dev/null
fi

if [ -r ${LIB}/$file ]; then
  echo Fixing $file, matherr declaration
  sed -e '/^struct exception/,$b' \
      -e '/matherr/i\
struct exception;
'\
    ${LIB}/$file > ${LIB}/${file}.sed
  rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
  if cmp $file ${LIB}/$file >/dev/null 2>&1; then
    rm -f ${LIB}/$file
  fi
fi

# In netinet/in.h, the network byte swapping asm functions supported by the
# native cc compiler on PTX 1.x and 2.x is not supported in gcc.  Instead,
# include <sys/byteorder.h> written out by the fixinc.svr4 script which has
# these same routines written in an asm format supported by gcc.
file=netinet/in.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 __GNUC__ $file_to_fix > /dev/null; then
    true
  else
    sed -e '/#define NETSWAP/a\
\
#if defined (__GNUC__) || defined (__GNUG__)\
#include <sys/byteorder.h>\
#else  /* not __GNUC__ */\
' \
    -e '/#endif[ 	]*\/\* NETSWAP \*\//i\
#endif /* not __GNUC__ */' \
    $file_to_fix > ${LIB}/${file}.sed
    rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
    echo Fixed $file_to_fix
  fi
fi

# /usr/include/sys/mc_param.h has an embedded asm for the cpuid intruction
# on the P5. This is not used by anything else so we ifdef it out.
file=sys/mc_param.h
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 __GNUC__ $file_to_fix > /dev/null; then
    true
  else
    sed -e '/__asm/,/}/{
/__asm/i\
#if !defined (__GNUC__) && !defined (__GNUG__)
/}/a\
#endif
}' \
    $file_to_fix > ${LIB}/${file}.sed
    rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
    echo Fixed $file_to_fix
  fi
fi

# /usr/include/sys/mc_param.h has an embedded asm for the cpuid intruction
# on the P5. This is not used by anything else so we ifdef it out.
file=sys/mc_param.h
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 __GNUC__ $file_to_fix > /dev/null; then
    true
  else
    sed -e '/__asm/,/}/{
/__asm/i\
#if !defined (__GNUC__) && !defined (__GNUG__)
/}/a\
#endif
}' \
    $file_to_fix > ${LIB}/${file}.sed
    rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
    echo Fixed $file_to_fix
  fi
fi

exit 0

