#! /bin/sh
# h2ph-all --- generate a complete perl header (.ph) mirror of system includes

# Author: Noah Friedman <friedman@splode.com>
# Created: 1996-02-15
# Public domain

# $Id: h2ph-all,v 1.3 1998/03/19 23:33:15 friedman Exp $

# Commentary:

# May require GNU `find'.

# Code:

include=${INCLUDE-/usr/include}
h2ph=${H2PH-h2ph}
sh=${H2PHALL_SUBSHELL-undef}
export include h2ph sh

# Use ksh if possible.
if [ ".$sh" = .undef ]; then
  if [ -f /bin/ksh ]; then
    sh=/bin/ksh
  else
    sh=/bin/sh
  fi
fi

# The obscure hackery here is to get around arg length limitations on some
# operating systems.  Using xargs isn't sufficient because the script text
# itself seems to overflow some systems (e.g. Solaris 2.3).

(cd "$include" && find . -follow \( -name '*.h' -o -type d \) -print) \
 | $sh 9<&0 <<'__EOF__'
     (
       # Put entire script in a subprocess because some versions of sh
       # don't cope with redirecting the file descriptor on which the
       # program text originated.  Forking first means that the process
       # reading the data input is different from the one reading program
       # text.
       exec 0<&9
       while read h; do
         if [ -d "$include/$h" ]; then
           if [ ! -d "$h" ]; then
             echo "mkdir $h"
             mkdir "$h"
           fi
         else
           ph=`echo "$h" | sed -e "s/\.h\$/.ph/"`
           # Note that the -nt flag isn't supported on some systems, e.g.
           # Solaris 2.3 (although it works in 2.5).  Use ksh or bash.
           if [ -f "$ph" ] && [ "$include/$h" -nt "$ph" ]; then
             echo "$ph is up to date."
           else
             echo "$h -> $ph"
             $h2ph < "$include/$h" > "$ph"
           fi
         fi
       done
     )
__EOF__

# h2ph-all ends here