#!/bin/sh
# -[Sun Feb  7 04:47:01 1999 by cxh]-
# Simple script that builds a test directory

usage() {
    echo "$0: Usage: $0 [-chr] directory"
    echo " -c clean up files generated by this script in directory"
    echo " -h print this help message"
    echo " -r run make in the newly created directory."
    echo " -v add files to version control"
}

case $1 in
    -c) # clean
        if [ -d $2 ]; then
            set -x
            rm -f $2/makefile $2/alljtests.tcl $2/testDefs.tcl $2/junit/makefile $2/junit/JUnitTclTest.java $2/junit/JUnitTclTest.class
            set +x
            echo "$0: $2 cleaned, now run $0 $2"
            exit
        else
            echo "$0: $2 does not exist, so no cleanup is necessary?"
            exit 2
        fi
        ;;
    -h) usage
        exit
        ;;
    -r) # run
        if [ -f $2/makefile ]; then
            echo "Running make in $2"
            cd $2; make
            echo "Running make in $2/junit"
            cd junit; make
            echo "If there are problems and the test files need to be regenerated, run '$0 -c $2' to clean up and then '$0 $2' to rebuild."
            echo "If there are not problems, to add files to version control, run $0 -v $2"
            exit
        else
            echo "$0: $2/makefile does not exist, perhaps you need to first run $0 $2"
            exit 3
        fi
        ;;
    -v) # Add files to version control
        if [ ! -f $2/makefile ]; then
            echo "$0: $2/makefile does not exist.  run $0 $2"
        else
            echo "$0: Creating junit/README.txt and junit/package.html to avoid doccheck warnings."
            package=`echo $2 | sed 's@/@.@g'`
            $PTII/adm/bin/mkpackagehtml $package.junit

            echo "$0: Adding newly created files to svn"
            svn add $2 $2/junit $2/auto $2/makefile $2/testDefs.tcl $2/junit/makefile $2/junit/JUnitTclTest.java $2/junit/README.txt $2/junit/package.html

            echo "$0: Adding .class file to svn:ignore"
            (cd $2; $PTII/adm/bin/svnignoreupdate)

            svn delete --force $2/junit/*.class

            svn status $2

            echo "$0: Don't forget to add the test directory to the makefile in the parent directory of $2"
            echo "$0: Next step: run svn commit -m \"Set up test infrastructure.\" $2"
            exit
        fi
esac    



if [ $# -ne  1 ]; then
    usage
    exit 2
fi

directoryname=$1

if [ "`basename $directoryname`" != "test" ]; then
	echo "$0: '$directoryname' does not end in test"
	exit 3
fi

#if [ -d $directoryname ]; then
#	echo "$0: '$directoryname' already exists. exiting."
#	exit 4
#fi



# 1) Create the test directory
if [ ! -d $directoryname ]; then
   mkdir $directoryname
fi


if [ ! -d $directoryname/auto ]; then
   mkdir $directoryname/auto
fi

cd $directoryname

# Figure out the relative path
absolute_ptii=`cd $PTII; pwd`
absolute_dirpath=`pwd`
RELPATH=`echo "$absolute_ptii $absolute_dirpath" |
	awk  '{ptIIdepth = split($1,f,"/")
		dirpathdepth = split($2,f,"/")
		for(i=ptIIdepth; i< (dirpathdepth-1); i++) {
			printf "../"
	        }
		printf("..");
	      }'`
echo "Relative path is '$RELPATH'"

# 2) Get the Java files above and generate a list of tcl files
#JSIMPLE_TESTS=`cd ..; ls -1 *.java |  sed 's/.java$/.tcl/' |
#	 awk '{printf("\t%s %c\n", $0, 92) }' `

if [ ! -f makefile ]; then

    echo "Generating makefile"
    YEAR=`date +%Y`

cat > makefile <<EoF
# Makefile for the Java classes used to test the JavaScript actors
#
# @Authors: Christopher Brooks (makefile only)
#
# @Version: $Id: mkpttest 72874 2015-07-26 21:01:52Z cxh $
#
# @Copyright (c) $YEAR 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

# Location of this directory, relative to the Ptolemy II directory
ME =		$directoryname

# Root of the Ptolemy II directory
ROOT =		$RELPATH

CLASSPATH =	\$(ROOT)

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

# Tcl sources that are part of the testing infrastructure
TCL_SRCS = \
	testDefs.tcl

JSRCS =

# Non-graphical Java tests written in Tcl
# If there are no tests, we use a dummy file so that the script that builds
# alljtests.tcl works.  If you add a test, be sure to add
# \$(JSIMPLE_TESTS) to EXTRA_SRCS
JSIMPLE_TESTS = \
	dummy.tcl

# Graphical Java tests.
# If there are no tests, we use a dummy file so that the script that builds
# alljtests.tcl works.  If you add a test, be sure to add
# \$(JGRAPHICAL_TESTS) to EXTRA_SRCS
JGRAPHICAL_TESTS = \
	dummy.tcl

EXTRA_SRCS =	\$(TCL_SRCS) \$(JSRCS) #\$(JSIMPLE_TESTS) #\$(JGRAPHICAL_TESTS)

# Sources that may or may not be present, but if they are present, we don't
# want make checkjunk to barf on them.
MISC_FILES =	alljtests.tcl  junit \
	auto

# make checkjunk will not report OPTIONAL_FILES as trash
# make distclean removes OPTIONAL_FILES
OPTIONAL_FILES =

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

# Don't include all or install rules here, we want the user
# to run 'make tests' to run the tests.

tests:: \$(EXTRA_SRCS) jclass test_java test_jsimple

test_java: jclass

# PtolemyII-wide test definitions.
KERNEL_TESTDEFS = \$(ROOT)/util/testsuite/testDefs.tcl

# These tests do not require a graphics terminal, but do use Jacl and Java
test_jsimple: \$(EXTRA_SRCS) jclass \$(KERNEL_TESTDEFS) alljtests.tcl test_auto
	\$(JTCLSH) alljtests.tcl

test_jgraphical: test_auto

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

fi

# 2) Create testDefs.tcl
if [ ! -f testDefs.tcl ]; then
    echo "Creating testDefs.tcl"
    RELPATHWITHSPACES=`echo $RELPATH | sed 's@/@ @g'`
    sed "s/\.\. \.\. \.\. \.\./$RELPATHWITHSPACES/" $PTII/ptolemy/kernel/util/test/testDefs.tcl > testDefs.tcl
fi

# 3) Create stub files
# JAVAFILES=`cd ..; ls -1 *.java | sed 's/.java$//'`
# for file in $JAVAFILES
# do
# 	classname=$file

# 	echo "Generating $file.tcl"
# 	cat > $file.tcl << EoF2
# # Tests for the $classname class
# #
# # @Author: Christopher Hylands
# #
# # @Version: $Id: mkpttest 72874 2015-07-26 21:01:52Z cxh $
# #
# # @Copyright (c) 1999-2015 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
# #######################################################################

# # Ptolemy II bed, see $PTII/doc/coding/testing.html for more information.

# # Load up the test definitions.
# if {[string compare test [info procs test]] == 1} then {
#     source testDefs.tcl
# } {}

# if {[info procs enumToObjects] == "" } then {
#      source enums.tcl
# }

# # Uncomment this to get a full report, or set in your Tcl shell window.
# # set VERBOSE 1

# ######################################################################
# ####
# #
# test $classname-2.1 {} {
# } {}
# EoF2

if [ ! -d junit ]; then
    mkdir junit
fi    

package=`echo $directoryname | sed 's@/@.@g'`

if [ ! -f junit/makefile ]; then
    echo "Creating junit/makefile"
    cat > junit/makefile << EoF3
# Makefile for the JUnit Kernel tests
# Autogenerated by adm/bin/mktestdir
#
# @Authors: Christopher Brooks
#
# @Version: $Id: mkpttest 72874 2015-07-26 21:01:52Z cxh $
#
# @Copyright (c) $YEAR 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

# Location of this directory, relative to the Ptolemy II directory
ME =		$directoryname/junit

# Root of the Ptolemy II directory
ROOT =		$RELPATH/..

# JUNIT_JAR is searched for by $PTII/configure and set in $PTII/mk/ptII.mk
CLASSPATH =	\$(ROOT)\$(CLASSPATHSEPARATOR)\$(JUNIT_JARS)\$(CLASSPATHSEPARATOR)\$(PTJACL_JAR)\$(CLASSPATHSEPARATOR)

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

JSRCS = \
	JUnitTclTest.java

# Non-graphical Java tests written in Tcl
# If there are no tests, we use a dummy file so that the script that builds
# alljtests.tcl works.  If you add a test, be sure to add
# \$(JSIMPLE_TESTS) to EXTRA_SRCS
JSIMPLE_TESTS = \
	dummy.tcl

# Graphical Java tests that use Tcl.
# If there are no tests, we use a dummy file so that the script that builds
# alljtests.tcl works.  If you add a test, be sure to add
# \$(JGRAPHICAL_TESTS) to EXTRA_SRCS
JGRAPHICAL_TESTS = \
	dummy.tcl

EXTRA_SRCS =	\$(TCL_SRCS) \$(JSRCS) #\$(JSIMPLE_TESTS) #\$(JGRAPHICAL_TESTS)

# Sources that may or may not be present, but if they are present, we don't
# want make checkjunk to barf on them.
MISC_FILES =

# make checkjunk will not report OPTIONAL_FILES as trash
# make distclean removes OPTIONAL_FILES
OPTIONAL_FILES =


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

# Don't include all or install rules here, we want the user
# to run 'make tests' to run the tests.

# JUNIT_JAVA_ARGS is set in $PTII/mk/ptII.mk
tests:: \$(EXTRA_SRCS) jclass test_java #test_jsimple
	(cd ..; CLASSPATH="\$(PTII)\$(CLASSPATHSEPARATOR)\$(CLASSPATH)" "\$(JAVA)" \$(JUNIT_JAVA_ARGS) org.junit.runner.JUnitCore $package.junit.JUnitTclTest)

test_java: jclass

# PtolemyII-wide test definitions.
KERNEL_TESTDEFS = \$(ROOT)/util/testsuite/testDefs.tcl

# These tests do not require a graphics terminal, but do use Jacl and Java
#test_jsimple: \$(EXTRA_SRCS) jclass \$(KERNEL_TESTDEFS)
#	\$(JTCLSH) alljtests.tcl



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

fi

if [ ! -f junit/JUnitTclTest.java ]; then
    echo "Creating junit/JUnitTclTest.java"
    
    cat > junit/JUnitTclTest.java <<EoF4
/* Run the Tcl tests in testDefs.tcl
 Autogenerated by adm/bin/mktestdir

 Copyright (c) $YEAR 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

 */

package $package.junit;

import ptolemy.util.test.junit.JUnitTclTestBase;

///////////////////////////////////////////////////////////////////
//// JUnitTclTest
/**
 * Run the auto/ and Tcl tests under JUnit.
 * <pre>
 * (cd $PTII/ptolemy/actor/lib/js/test/junit; java -classpath ${PTII}:${PTII}/lib/ptjacl.jar:${PTII}/lib/junit-4.8.2.jar:${PTII}/lib/JUnitParams-0.3.0.jar org.junit.runner.JUnitCore $package.junit.JUnitTclTest)
 * </pre>
 *
 * <p> A copy of this file appears in each test/junit subdirectory
 * so that it is easy for developers to run tests.  The master
 * file is in $PTII/util/testsuite/JUnitTclTest.java.in.
 * To update all the files, run
 * <pre>
 * cd $PTII
 * ptolemy/util/test/junit/updateJUnitTclTest
 * </pre>
 *
 * @author Christopher Brooks
 * @version $Id: mkpttest 72874 2015-07-26 21:01:52Z cxh $
 * @since Ptolemy II 10.0
 * @Pt.ProposedRating Green (cxh)
 * @Pt.AcceptedRating Green (cxh)
 */
public class JUnitTclTest extends JUnitTclTestBase {
    // This class is empty, all the action occurs in the parent.
}
EoF4
fi

if [ ! -f junit/package.html -a -f $PTII/adm/bin/mkpackagehtml ]; then
    echo "Creating junit/package.html and junit/README.txt"
    (cd $PTII; $PTII/adm/bin/mkpackagehtml $directoryname/junit)
fi

echo "$0: Next: run $0 -r $directoryname"
