#!/bin/sh
if test "$1" = ""
then
   echo "Usage : gnatchop [-ksw] filename [directory]"
   echo " "
   echo "  k         limit filenames to 8 characters"
   echo "  s         generate a compilation script"
   echo "  w         overwrite existing filenames"
   echo "  filename  source file"
   echo "  directory directory to place split files (default is ./)"
   exit 1;
fi
parms=
k8option=
#
# Scan through the options, checking for validity and especially looking for
# the 'k' option since this will be used for the call to gcc to get the offset
# information.
#
while getopts ksw c
do
   case $c in
      k)      k8option="-k8";;
      s | w)  parms="$parms -$c";;
      \?)     echo "Usage : gnatchop [-ksw] filename [directory]"; exit 1;;
   esac
done
shift `expr $OPTIND - 1`
#
# Check that there is a filename argument after the option list, and that the
# file actually exists.
#
if test "$1" = ""
then
   echo "missing filename"
   echo "Usage : gnatchop [-ksw] filename [directory]"; exit 1
   exit 1
elif test ! -r $1
then
    echo "$1 not found"; exit 1
fi
# Call gnatf on the source filename argument with special options to generate
# offset information. If this special compilation completes succesfully call
# the gnatchp program to actuall split the source file in one file per
# compilation unit in the optional directory if given otherwise in the current
# directory.
#
gnatf -d2 -s -u $k8option $1 >tmpfile
if [ $? -eq 0 ] ; then
	gnatchp $parms $1 $2 <tmpfile
else
        echo "parse errors detected"; exit 1
fi
rm tmpfile
