#! /bin/sh
# $Id: jad2java,v 1.4 2003/05/05 20:54:59 cxh Exp $
# Simple script that converts jad output of a ptolemy class to java
# we can compile

# Convert
#        obj3 = JVM INSTR new #35  <Class TypedIORelation>;
#        ((TypedIORelation) (obj3)).TypedIORelation(this, "relation6");
# to
#         obj3 = new TypedIORelation(this, "relation6")
# or
#        TypedIORelation typediorelation = JVM INSTR new #35  <Class TypedIORelation>;
#        typediorelation.TypedIORelation(this, "relation");
# to
#         TypedIORelation typediorelation = new TypedIORelation(this, "relation");

awk ' { if ($0 ~ /JVM INSTR/) {
            sawjvminstr = 1
            # remove the JVM INSTR
            printf("        ");
            for (i=1; i <= NF ;i ++) {
                if ($i == "JVM") {
                    printf("new ")
                    break;
                } else {
                    printf("%s ", $i)
                }
            }
        } else {
            if (sawjvminstr == 1) {
                sawjvminstr = 0
                # remove text up to the first .
                #position = match($0, "/\./")
                position = index($0, ".")
                printf("%s\n", substr($0,position+1, length($0)));
            } else {
                print $0
            }
        }
      }' $1 |
awk '{if ($0 ~ /public void __CGInit/) {
            print $0, "throws IllegalActionException, NameDuplicationException"
        } else {
            print $0
        }
    }' |
awk '{ if ($0 ~ /^package/) {
            print $0
            printf("\n");
            print "import ptolemy.kernel.util.IllegalActionException;"
            print "import ptolemy.kernel.util.NameDuplicationException;"
        } else {
            print $0
        }
    }' |
awk '{ if ($0 ~ /__CGInit\(\);/) {
            indent = "        ";
            print indent "try {"
                print indent $0
            print indent "} catch (Exception e) {"
            print indent indent "e.printStackTrace();"
            print indent indent "throw new RuntimeException(e.toString());"
            print indent "}"
        } else {
            print $0
        }
    }' |
awk '{ if ( $0 ~ /CompositeEntity compositeentity, String s\)$/) {
            print $0, "throws IllegalActionException, NameDuplicationException"
        } else {
            print $0
        }
    '}

