#!/bin/sh
# --------------------------------------------------------------------------
# Copyright 1992-1993 by Forschungszentrum Informatik (FZI)
#
# You can use and distribute this software under the terms of the license
# version 1 you should have received along with this software.
# If not or if you want additional information, write to
# Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
# D-76131 Karlsruhe, Germany.
# ------------------------------------------------------------------------
# 'mkdefs - 21:9:93 - dietmar theobald'
#
# mkdefs [-rootmacro <name>] [-libmacro <name>] [-marker <txt> <name>] [<dir>]
#
# 'mkdefs' generates a macro definition list for an OBST source tree which can
# be used as a 'definitions.mk' file.
# 
# Definitions are generated for files (transitively) contained in directory
# <dir>.  If no <dir> is given, the current working directory is used, instead.
# <dir> must be a pathname relative to the current working directory.
#
# If the '-rootmacro' option is used, the pathname of the current working
# directory upon invocation is replaced by "$(<name>)".
#
# The first group of macros contains a macro for each header file ("*.h") and
# source file ("*.c", "*.C", "*.cc") contained in the searched directory.
# Each macro is expanded to the list of files included in the corresponding
# file plus the file itself.
# The basename of the files is also used as the name of the corresponding
# macro.
# No macros are generated for "*_scp.c", "*_scp.C", "*_scp.cc" files.
#
# The last group is made up of a macro for each source code file ("*.c", "*.C",
# "*.cc") contained in the searched directory. Those macros will be expanded to
# the absolute path for the object file generated from the source file. The
# basename of a source file with the changed suffix '.o' is used as the name of
# the corresponding macro.
#
# By default, these macros will be rooted at the current working directory.
# Using the '-libmacro' option, their location can be specified based on the
# macro given in the option, i.e. the object files will be placed in the
# directory "$(<name>)".
#
# If '-marker' is present, then "$(<name>)" will be appended to the libary
# macro iff <txt> is contained in the respective source file.
#
# Two kinds of schema files are handled in special ways:
#  - A "<p>_obst.o" macro is generated even if no file "<p>_obst.C" (_obst.c)
#    is found - if a file "<p>.obst" exists.
#  - "<p>_scp.o" macros are not generated.
#
self=mkdefs
usage='[-rootmacro <name>] [-libmacro <name>] [-marker <txt> <name>] [<dir>]'
searchdir='.'
stop_list='-name "*_scp.[Cc]" -o -name "*_scp.cc"'
 rootname="`pwd`"
  libname=
  libsuff=
   marker=
   letter="A-Za-z0-9_+-."

while [ $# -gt 0 ]
do
   case "$1" in
      -rootmacro) rootname="\$($2)"; shift 2;;
       -libmacro)  libname="\$($2)"; shift 2;;
         -marker)   marker="$2"
		   libsuff="\$($3)"; shift 3;;
	       *) break;;
   esac
done
[ "$libname" ] || libname="$rootname"

if [ $# -eq 1 ]; then
   searchdir="$1"
elif [ $# -gt 0 ]; then
   echo >&2 "*** usage: $self $usage"
   exit 1
fi

{ # This is to bracket the output generating operations, see the end.

echo "# $self - definition file for $searchdir - "`date "+%d. %h 19%y"`"

# Dependency list for header and c-files
"
{  for file in `find ./$searchdir \( -type f -o -type l \) \( -name "*.[Cch]" -o -name "*.cc" \) -print`
   do
      egrep '^("[^"]*")*[^"]*#include[^"]*"' $file /dev/null \
      | { empty='+'
          while read line
          do
	     empty=''
	     echo $line
          done
          [ "$empty" ] && echo $file': ""'
        }
   done
}\
 | sed -n -e "s|^./\([^:]*\):.*\"\([^\"]*\)\".*$|\1 \2|p" \
 |\
{  current=
   while read file incl
   do
      [ "$file" = "$current" ] || {
         [ "$current" ] && echo "$current:$curr_dep"
          current="$file"
         curr_dep=''
         }
      curr_dep="$curr_dep $incl"
   done
   [ "$current" ] && echo "$current:$curr_dep"
}\
 | sed -e "\
s|^\([$letter/]*\)/\([$letter]*.[Cch]\):|\2=$rootname/\1/\2|
s|^\([$letter/]*\)/\([$letter]*.cc\):|\2=$rootname/\1/\2|
s| [$letter/]*/\([$letter][$letter]*\)| \$(\1)|g
s| \([$letter][$letter]*\)| \$(\1)|g
/^[^ ]*_scp.[Cc]/d
s|\(=$rootname/\)./|\1|"

echo '

# Object file placement
'
for file in `eval 'find ./$searchdir \( -name "*.[Cc]" -o -name "*.cc" \) \( \( '"$stop_list"' \) -o -print \)'`
do
   ofile="`basename \`echo $file | sed -e 's|.cc$||' -e 's|.[cC]$||'\``.o"
    suff=
   case "$file" in
      *_obst.[Cc]) continue ;;
   esac
   [ "$libsuff" ] && { egrep -s "$marker" $file && suff="$libsuff"; }
   echo "$ofile=$libname$suff/$ofile"
done
for schema in `find ./$searchdir -name "*.obst" -print`
do
   file="`basename $schema .obst`_obst."
   [ -f $schema ] && \
      echo "${file}o=$libname$libsuff/${file}o"
done

} | sed 's|^_| _|'

# The 'make' on a AIX 3.2 (uname -a: AIX ifsaix1 2 3 000078603500) seems not
# to be able to handle macro definitions properly if the macro name starts
# with an underscore in the first column.
