#!/bin/sh
# $Id: fixclassic 36205 2005-02-23 00:42:44Z cxh $
# fixclassic - a script that does a half hearted attempt at converting
#  Ptolemy Classic .c code to .java code.  fixclassic is called by
#  the harvest script.

if [ $# -lt 1 ]; then
    destinationSubdir=ptolemy/codegen
    destinationDirectory=$PTII/$destinationSubdir/lib
    actors=`ls -1 $destinationDirectory/*.java`
else
    actors=$@
fi

for actor in $actors
do
    superclass=`awk '$0 ~ /^public class .* extends .* {/ {print $5}' $actor`
    echo "$actor: superclass = $superclass"
    cat $actor |
       sed -e s@${superclass}::@super.@g \
	   -e 's@super.setup()@super.generateInitializeCode()@g' \
	   -e 's@//# line [0-9]*.*$@@' \
           -e 's@[ 	]*StringList \([a-z_0-9A-Z]*\) = \(".*"\);$@StringBuffer \1 = new StringBuffer(\2);@' \
	   -e 's@[ 	]*{[	 ]*StringList \([a-z_0-9A-Z]*\); \([a-z_0-9A-Z]*\) <<[	 ]*$@{ StringBuffer \1 = new StringBuffer(); \2.append(@' \
           -e 's@\([ 	]*addCode[^;]*;\) }[ 	]*$@); \1 } @' \
           -e 's@\([ 	]*addCode[^;]*;\)@\1 @' \
           -e 's@\([ 	]*[a-zA-Z0-9_]*\) << \(.*\);$@\1.append(\2);@' \
           -e 's@StringList \([a-z_0-9A-Z]*\);@StringBuffer \1 = new StringBuffer();@' \
	   -e 's@StringList \([a-z_0-9A-Z]*\) = \([^;]*\);$@StringBuffer \1 = new StringBuffer(\2);@' \
           -e 's@Error::abortRun[ ]*(\*this,@throw new IllegalActionException(this,@' \
           -e 's@Error::warn[ ]*(\*this,@throw new IllegalActionException(this,@' \
           -e 's@" <<@" +@g' \
           -e 's@<< "@ + "@g' \
           -e 's@(const char *)\([a-z_0-9A-Z]\)@\1.toString()@' \
           -e 's@const char *\*@String@' \
           -e 's@int(\([a-z_0-9A-Z]*\))@((IntToken)((\1).getToken())).intValue()@g' \
           -e 's@double(\([a-z_0-9A-Z]*\))@((DoubleToken)((\1).getToken())).doubleValue()@g' \
           -e 's@\([a-9_0-9A-Z]*\)[ ]*::[ ]*\([a-9_0-9A-Z]*\)@\1.\2@g' \
	   -e 's@this->@this.@g' \
	   -e 's@\.fullName(@.getFullName(@g' \
	   -e 's@FALSE@false@g' \
	   -e 's@TRUE@true@g' \
	   -e 's@NULL@null@g' \
	   -e 's@\(i>1."complex temp;":"/.Unit gain - no multiplication required./"\)@(\1)@' \
	   -e 's@(int)sizeof(int)@/*sizeof(int)*/ 16 @g' \
	   -e 's@(unsigned int)sizeof(int)@/*(unsigned int)sizeof(int)*/ 16 @g' \
	   -e 's@(unsigned int)(sizeof(int)@/*(unsigned int)(sizeof(int)*/ ( 16 @g' \
	   -e 's@if (((IntToken)((\([a-z_0-9A-Z]*\)).getToken())).intValue())@if (((IntToken)((\1).getToken())).intValue() == 1) // FIXME \1 should be a Boolean@' \
	   -e 's@if (!((IntToken)((\([a-z_0-9A-Z]*\)).getToken())).intValue()) @if (((IntToken)((\1).getToken())).intValue() == 0) // FIXME \1 should be a Boolean@' \
	   -e 's@public void  generateInitializeCode() {@public void  generateInitializeCode() throws IllegalActionException {@' \
	   -e 's@strcasecmp(\([a-z_0-9A-Z]*\), \(["a-z_0-9A-Z]*\)) == 0@\1.equalsIgnoreCase(\2)@' \
	   -e 's@strcmp(\([a-z_0-9A-Z]*\), \(["a-z_0-9A-Z]*\)) == 0@\1.equals(\2)@' \
	   -e 's@((String)s)@(s.toString())@' \
           -e 's@\(^[ 	]\)*addFixedPointSupport()@\1 //addFixedPointSupport@' \
           > $actor.tmp

    # FIXME: fix the package
    dirname=`dirname $actor`
    dirdirname=`dirname $dirname`
    basedirname=`basename $dirdirname`
    if [ "x$basedirname" != "x." -a "$basedirname" != "codegen" ]; then
	cat $actor.tmp |
	    sed -e "s/package ptolemy.codegen.lib;/package ptolemy.codegen.${basedirname}.lib;/" > $actor.tmp2
	mv $actor.tmp2 $actor.tmp
    fi	
    diff $actor $actor.tmp 
    cp $actor.tmp $actor
done
