#!/bin/sh 
#
# make-source-dependencies:
#		this shell script prints GNU Make compatible
#	input from a list of prototype instantiations read
#	(one per line) from the input.
#
# instantiations instances: /tmp/make-source-dependencies.$$.0
# (cleaned up) instantiation instances: /tmp/make-source-dependencies.$$.1
# sorted list of generic prototypes (culled from above): /tmp/make-source-dependencies.$$.2

while read instance ; do
echo "${instance}" 
done >> /tmp/make-source-dependencies.$$.0

# instances
cat /tmp/make-source-dependencies.$$.0 | \
  gawk '{ for (i = 1 ; i <= NF ; i ++) print $i ; }' > /tmp/make-source-dependencies.$$.1

# prototypes
cat /tmp/make-source-dependencies.$$.0 | \
  gawk -F. '{ print $NF; }' | sort | uniq > /tmp/make-source-dependencies.$$.2

for t in `cat /tmp/make-source-dependencies.$$.2` ; do

  if [ -f ${PROTO_SOURCE}/${t}.hP ] ; then
    cat /tmp/make-source-dependencies.$$.1 | \
      gawk -F. '$NF == "'${t}'" { printf "$(PC)/%s.h ", $0 ; }' ;
    echo ": \$(${PP})/${t}.hP" ; 
    echo '	$(make-proto-file) $(prepend-proto-includes)' ;
    echo ;
  fi;

  if [ -f ${PROTO_SOURCE}/${t}.ccP ] ; then
    cat /tmp/make-source-dependencies.$$.1 | \
      gawk -F. '$NF == "'${t}'" { printf "$(PC)/%s.cc ", $0 ; }' ;
    echo ": \$(${PP})/${t}.ccP" ; 
    echo '	$(make-proto-file)' ;
    echo ;
  fi;
done;

rm -f /tmp/make-source-dependencies.$$.0
rm -f /tmp/make-source-dependencies.$$.1
rm -f /tmp/make-source-dependencies.$$.2
