#!/bin/bash

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

