#! /bin/sh
# $Id: jode2java,v 1.14 2005/05/17 15:57:08 cxh Exp $
# Simple script that converts jode output of a ptolemy class to java
# we can compile

# Convert
#        parameter = new Parameter;
#        ((UNCONSTRUCTED)parameter).Parameter(editablechoicestyle_30_,
#                                             "choice1");
# To
#        parameter = new Parameter(editablechoicestyle_30_,
#                                             "choice1");
#
awk ' { if ($0 ~ /UNCONSTRUCTED/) {
            # In the lastline, Replace the trailing; with a (
            print substr(lastline, 1, length(lastline) -1) "("

            if ($0 !~ /\./) {
                # The line does not contain a '.', so we have something like:
                # EditableChoiceStyle editablechoicestyle_30_ = new EditableChoiceStyle;
                 # ((UNCONSTRUCTED)editablechoicestyle_30_)
                #    .EditableChoiceStyle(attribute_23_, "style");
                sawunconstructed = 1
                # skip the current line
            } else {
                # get everthing after the last ( and put it in lastline
                nf = split($0, f, "(")
                lastline = "          " f[nf]
            }
        } else {
            if (sawunconstructed == 1) {
                sawunconstructed = 0
                nf = split($0, f, "(")
                if (nf>1) {
                    #print lastline
                    lastline = "          " f[nf]
                } else {
                    sawunconstructed = 1
                }
            } else {
                print lastline
                lastline = $0
            }
        }
      }
   END {print lastline}
' $1 |
awk '{if ($0 ~ /public void __CGInit/ ) {
            # Skip the trailing {
            print substr($0,1, length($0)-2), "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 {
            if ( $0 ~ /String string\) {/ ) {
                print substr($0, 1, length($0)-1), "throws IllegalActionException, NameDuplicationException {"
            } else {
                print $0
            }
        }
    '} |
awk '{   if ($0 ~ /; */) {
            lastsemi = sawtrailingsemicolon;
            sawtrailingsemicolon = NR
         }
         if ( $1 ~ /\"/ && $0 !~ /"\);$/ && $0 !~ /"\)\);$/ ) {
             stringRecord = NR
             print "         String tmp" stringRecord " = " $0
             dontprint = 1
         }
         if ( NR - stringRecord < 10 \
                    && $0 ~ /throw new RuntimeException\(\);/) {
             print substr($0, 1, length($0)-2) "tmp" stringRecord ");"
             dontprint = 1
             stringRecord = 0
         } else {
             if (dontprint == 1) {
                dontprint = 0
             } else {
                print $0
             }
         }
     }' |
sed -e 's@ final @ /*final*/ @g' \
    -e 's@new RuntimeException;@new RuntimeException(); // fixed by jode2java@' |
sed -e 's@ String tmp[0-9]* =[         ]*\(settable_[0-9]*_.setExpression\)@\1@' |
sed -e 's@ String tmp[0-9]* =[         ]*\(settable.setExpression\)@\1@' |
sed -e 's@ String tmp[0-9]* =[         ]*("@("@'
