#!/bin/perl

die "Usage: makearmpi <file>\n" if ($#ARGV);
while (<>)
  { if (/^-+\n$/o)
      { $current = '' }
    elsif (/^-- (\w+) -+\n$/o)
      { $current = $1 }
    elsif ($current ne '' && $_ ne "\n")
      { $_ = "$1\n" if (/^(.*?)\s*;/);
        if (($sr,$param) = /^\w*\s+<(\w+)>\s*(.*)\n$/o)
          { if (($tmp = $rout{$sr}) eq '')
              { $! = 1; die "Unknown routine $1\n" }
            if (/^(\w+)/o)
              { $rout{$current} .= "$1\n" }
            while ($param ne '')
              { ($a,$b,$c,$param) = $param =~ /(\w+)=(\w+)(,\s*)?(.*)/o;
                $tmp =~ s/([^%])\b$a\b/$1%$b/g;
              }
            $tmp =~ s/%//g;
            $rout{$current} .= "; routine $sr (begin)\n$tmp"
                              ."; routine $sr (end)\n"; }
        else
          { $rout{$current} .= $_ }
      }
  }
print $rout{MAIN};
