#!/bin/sh
# $Id: harvest 36205 2005-02-23 00:42:44Z cxh $
# Script to harvest Ptolemy Classic SDF CGC actors.
#
if [ ! -d $PTOLEMY ]; then
    PTOLEMY=/users/ptolemy
fi

cgc=$PTOLEMY/src/domains/cgc
if [ ! -d "$cgc" ]; then
    echo "$0: \"$cgc\" not found, exiting."
    exit 2
fi

ptIIlang=$PTII/ptolemy/apps/classic/ptIIlang/ptIIlang
if [ ! -x "$ptIIlang" ]; then
    echo "$0: $ptIIlang not found, exiting."
    echo 3
fi

destinationSubdir=ptolemy/codegen
destination=$PTII/$destinationSubdir;
if [ ! -d $destination ]; then
    mkdir -p $destination
fi


# Stars from Ptolemy Classic CGC.  Include only stars and dsp
stars=`find $cgc -name "*.pl" | egrep -v "targets|tcltk|contrib|vis" | sort`
#stars="/users/ptolemy/src/domains/cgc/stars/CGCAdd.pl /users/ptolemy/src/domains/cgc/dsp/stars/CGCIIR.pl"

for star in $stars
do
  echo $star
  $ptIIlang $star
  basestar=`basename $star .pl`
  rm -f ${basestar}.cc ${basestar}.h ${basestar}.htm
  dirname=`dirname $star`
  dirname=`dirname $dirname`
  basedir=`basename $dirname`
  if [ "$basedir" = "dsp" ]; then
      if [ ! -d "$destination/dsp/lib" ]; then
	  mkdir -p "$destination/dsp/lib"
      fi	  
      mv ${basestar}.java $destination/dsp/lib
  else
      if [ ! -d "$destination/lib" ] ; then
	  mkdir -p "$destination/lib"
      fi	  
      mv ${basestar}.java $destination/lib
  fi
done

cat > $PTII/$destinationSubdir/codegen.xml <<EOF4
<?xml version="1.0" standalone="no"?>
<!DOCTYPE plot PUBLIC "-//UC Berkeley//DTD MoML 1//EN"
    "http://ptolemy.eecs.berkeley.edu/xml/dtd/MoML_1.dtd">
<entity name="Dataflow CGC Actors" class="ptolemy.moml.EntityLibrary">
  <configure>
    <?moml
      <group>
      <doc>Dataflow CGC Actors</doc>

EOF4

# Create README.txt, package.html makefiles
dirs="lib dsp/lib"
for dir in $dirs
do
    destinationDirectory=$PTII/$destinationSubdir/$dir
    fixclassic $destinationDirectory/*.java

    echo "	    <input source=\"$destinationSubdir/$dir/index.xml\"/>" >> $PTII/$destinationSubdir/codegen.xml

    # README.txt
cat > $destinationDirectory/README.txt <<EOF2
\$Id\$
See package.html
EOF2

    # package.html
    packagefile=$destinationDirectory/package.html
    package=`echo $package | sed 's@/@\.@g'`
    since=`grep -h @since *.java | sort | uniq | head -1`
cat > $packagefile << EoF1
<!-- \$Id\$ -->
<html>
<head>
<title>$package</title>
</head>
<body>
The $package package.
<p>
$since
</body>
</html>
EoF1


    # makefile
   subdir=`echo $dir | sed 's@/*lib@ @'`
   if [ "$subdir" = " " ]; then
       subdir="";
   fi       

   root=`echo $destinationSubdir/$dir | sed 's@[^/]*/@\.\./@g' | sed 's@/lib@/..@'`

   javafiles=`(cd $destinationDirectory; ls -1 *.java | awk '{printf("%s \\\\\n"), $1}')`


cat >$destinationDirectory/makefile <<EOF
# Makefile for Ptolemy II Dataflow CGC actors imported from Ptolemy Classic
#
# @Authors: Christopher Brooks (makefile only)
#
# @Version: \$Id: harvest 36205 2005-02-23 00:42:44Z cxh $
#
# @Copyright (c) 2005 The Regents of the University of California.
# All rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in all
# copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
# THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
# PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
# CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS.
#
#                                               PT_COPYRIGHT_VERSION_2
#                                               COPYRIGHTENDKEY

ME =            $destinationSubdir/$dir

# Rule for index.xml uses this
TITLE = 	"Dataflow CGC ${subdir}Actors"

DIRS =		demo test    	

# Root of the Java directory
ROOT =          $root

CLASSPATH =	\$(ROOT)

# Get configuration info
CONFIG =        \$(ROOT)/mk/ptII.mk
include $(CONFIG)

# Used to build jar files
PTPACKAGE =     lib
PTCLASSJAR = 	$(PTPACKAGE).jar

# Keep this list alphabetized.
JSRCS = \
       $javafiles

OTHER_FILES_TO_BE_JARED = \
	lib.xml

EXTRA_SRCS =	\$(JSRCS) \$(OTHER_FILES_TO_BE_JARED)

JCLASS = \$(JSRCS:%.java=%.class)

# Sources that may or may not be present, but if they are present, we don't
# want make checkjunk to barf on them.
# Don't include demo or DIRS here, or else 'make sources' will run 'make demo'
MISC_FILES =	test

# make checkjunk will not report OPTIONAL_FILES as trash
# make distclean removes OPTIONAL_FILES
OPTIONAL_FILES = \
        index.xml \
	demo \

all: jclass index.xml
install: jclass jars

# Get the rest of the rules
include \$(ROOT)/mk/ptcommon.mk
EOF

    # Run make
    (cd $destinationDirectory; make -k)
    echo "$destinationDirectory: Number of .java files created:"
    (cd $destinationDirectory; ls -1 *.java | wc -l)    
    echo "$destinationDirectory: Number of .class files created:"
    (cd $destinationDirectory; ls -1 *.class | wc -l)    
done

cat >> $PTII/$destinationSubdir/codegen.xml <<EOF5
      </group>
    ?>
  </configure>
</entity>

EOF5
