#!/bin/perl

# Use: ftn2html.pl filenames
# Input: fortran source code
# Output: file1.html, file2.html, etc
# Action: adds links between subroutine/function definitions and
#  corresponding calls.

# Feel free to take, use and update this script in any way, excluding only 
# commercial use. I'd appreciate a mail if you find it useful or find any
# bugs.
# (c) 1996 W. M. Connolley wmc@bas.ac.uk http://www.nbs.ac.uk/public/icd/wmc/

undef $/;

&DoOnce(0,@ARGV);
&DoOnce(1,@ARGV);

sub DoOnce {

  ($Wr,@FILES)=@_;

  while ($FILE=shift @FILES) {

    print "file: $FILE\n" unless (!$Wr);
    open(IN,$FILE); $In="<h1>$FILE</h1><pre>\n".<IN>."</pre>"; close(IN);
    $In=~s/(\n\s*)(subroutine|function)\s([\w]+)[\s]*\(/&Isin($1,$2,$3)/eig;
    if ($Wr) {
      $In=~s/(\n\s*)call\s([\w]*)/&Href($1,$2)/eig;
      open(OUT,"> ".$FILE.".html"); print OUT $In; close(OUT)
    }

  }

}

sub Isin {
  ($S,$A,$B)=@_;
  $B=~tr/[A-Z]/[a-z]/;
  $IsIn{$B}="$FILE.html#$B";
  return "$S<font size=+1 color=green>$A <a name=$B>$B</font> ("
}

sub Href {
  ($S,$A)=@_;
  ($A1=$A)=~tr/[A-Z]/[a-z]/;
  if ($In=~/(subroutine|function)\s$A/i) {
    return "$S<font size=+1 color=red>call <a href=$FILE.html#$A1>$A</a></font>"
  } elsif ($IsIn{$A1}) {
    return "$S<font size=+1 color=red>call <a href=$IsIn{$A1}>$A</a></font>"
  } else {
    if ($Wr && !$NoRef{$A1}) {
      print "No ref found for: >$A<\n";
      $NoRef{$A1}=1
    };
    "${S}call $A"
  }
}
