#!/bin/csh

# (C) Copyright International Business Machines Corporation 23 January 
# 1990.  All Rights Reserved. 
#  
# See the file USERAGREEMENT distributed with this software for full 
# terms and conditions of use. 

# 'getdeps foo.p bar.p frotz.d mumble.d ...' prints out make-style
# dependencies derived from the using and linking lists found in the
# named source modules

cat <<'getdeps.end' > /tmp/getdeps.$$
BEGIN {
  using = ""
  inusing = 0
  linking = ""
  inlinking = 0
  gotmodname = 0
  gotmodtype = 0
  firstline = 1
}

{
  if (firstline == 1) {
    localfiles = " " $0 " "
    firstline = 0
  }
  else {

    lin = $0
    commentpos = index(lin, "--")
    if (commentpos != 0)
      lin = substr(lin, 1, commentpos - 1)

    if (gotmodname == 0) {
      colonpos = index(lin, ":")
      if (colonpos != 0) {
	modname = substr(lin, 1, colonpos - 1)
	while (substr(modname, length(modname)) == " ")
	  modname = substr(modname, 1, length(modname)-1)
	while (index(modname, " ") != 0)
	  modname = substr(modname, index(modname, " ")+1)
	gotmodname = 1
      }
    }

    if (gotmodtype == 0)
      if (index(lin, "definitions") != 0) {
	modtype = "do"
	srctype = "d"
	gotmodtype = 1
      }
      else if (index(lin, "process") != 0) {
	modtype = "po"
	srctype = "p"
	gotmodtype = 1
      }

    if (inusing == 0) {
      usingpos = index(lin, "using")
      if (usingpos != 0) {
	inusing = 1
	lin = substr(lin, usingpos+5)
      }
    }
    if (inusing == 1) {
      parenpos = index(lin, ")")
      if (parenpos != 0) {
	using = using substr(lin, 1, parenpos)
	inusing = 2
      }
      else
	using = using lin
    }

    if (inlinking == 0) {
      linkingpos = index(lin, "linking")
      if (linkingpos != 0) {
	inlinking = 1
	lin = substr(lin, linkingpos + 7)
      }
    }
    if (inlinking == 1) {
      parenpos = index(lin, ")")
      if (parenpos != 0) {
	linking = linking substr(lin, 1, parenpos)
	inlinking = 2
      }
      else
	linking = linking lin
    }
  }
}

END {
  maxlen = 72
  out = sprintf("%s.%s:", modname, modtype)
  newout = sprintf("%s.%s", modname, srctype)
  if (length(out) + length(newout) > maxlen) {
    print out " \\"
    out = "\t" newout
    maxlen = 65
  }
  else
    out = out " " newout

  lparen = index(using, "(")
  rparen = index(using, ")")
  using = substr(using, lparen+1, (rparen-lparen)-1)
  spacepos = index(using, " ")
  while (spacepos != 0) {
    using = substr(using, 1, spacepos-1) substr(using, spacepos+1)
    spacepos = index(using, " ")
  }
  if (length(using) != 0)
    using = using ","
  commapos = index(using, ",")
  while (commapos != 0) {
    newmod = substr(using, 1, commapos-1)
    using = substr(using, commapos+1)
    if (index(localfiles, " " newmod ".d ") != 0) {
      newout = newmod ".do"
      if (length(out) + length(newout) > maxlen) {
	print out " \\"
	out = "\t" newout
	maxlen = 65
      }
      else
	out = out " " newout
    }
    commapos = index(using, ",")
  }

  lparen = index(linking, "(")
  rparen = index(linking, ")")
  linking = substr(linking, lparen+1, (rparen-lparen)-1)
  spacepos = index(linking, " ")
  while (spacepos != 0) {
    linking = substr(linking, 1, spacepos-1) substr(linking, spacepos+1)
    spacepos = index(linking, " ")
  }
  if (length(linking) != 0)
    linking = linking ","
  commapos = index(linking, ",")
  while (commapos != 0) {
    newmod = substr(linking, 1, commapos-1)
    linking = substr(linking, commapos+1)
    if (index(localfiles, " " newmod ".p ") != 0) {
      newout = newmod ".po"
      if (length(out) + length(newout) > maxlen) {
	print out " \\"
	out = "\t" newout
	maxlen = 65
      }
      else
	out = out " " newout
    }
    commapos = index(linking, ",")
  }
  print out
}
'getdeps.end'

foreach x ($*)
  ((((echo *.p *.d; cat $x) | tr '[A-Z]' '[a-z]') \
	| awk -f /tmp/getdeps.$$) | tr '[A-Z]' '[a-z]')
end
rm -f /tmp/getdeps.$$

	
