#!/bin/bash

grep -E '^[^C].*(CALL|SUBROUTINE)' bulletin*.for \
  | sed 's/ *! .*//;s/:.*\(CALL\|SUBROUTINE\) / \1 /;s/(.*//' \
  | awk '
        BEGIN {
          e="graph {"
        }
        {
          if ($1 != f) {
            print e;
            sg=$1; gsub(".for", "", sg);
            print "  subgraph " sg " {";
            e="  }";
          }
        }
        $2 == "SUBROUTINE" {
          f=$1; s=$3;
          print "    " s;
        }
        $2 == "CALL" {
          if ($1 != f) {
            f=$1; s="main";
            print "    " s;
          }
          print "    " s " -> " $3;
          
        }
        END {
          print e;
          print "}";
        }
        ' \
  | sed 's/\$/__/g'