#!/bin/sh
# This script compiles and runs Amanda and Frejus classes as part of mmc package
# Change JAVA_HOME to point to the directory where your java distribution resides

#JAVA_HOME=/usr/java/jdk1.3
JAVA_HOME=/usr/local/jdk1.3

WHICH=/usr/bin/which
if test -e $WHICH; then
FULL=$($WHICH $0); else FULL=$0; fi

cd $(echo $FULL | sed 's/[^/]*$//')
LANG=en_US
CLASSPATH=mmc.jar:.:$JAVA_HOME/src.jar
JAVA=$JAVA_HOME/bin/java
JAVAC=$JAVA_HOME/bin/javac
NULL=/dev/null
CONT=0

MAINPROG=Amanda
for i in $@; do if test $i = "-f" || test $i = "-frejus" ; then MAINPROG=Frejus; fi; done

for i in $@; do if test $i = "-h" || test $i = "-help" ; then
if test $MAINPROG = Amanda ; then
echo -e "\nUse this prorgam for F2000 stream processing in pipes"
if test -e $MAINPROG.class; then
$JAVA $MAINPROG -help; else echo; fi
elif test $MAINPROG = Frejus ; then
echo -e "\nUse this program for Frejus data processing in pipes\n"
fi; exit; fi; done

for i in $@; do if test $i = "-clean" ; then
rm -r {mmc/,}*{~,.class} mmc.jar {.*,*}.data manual index.html 2> $NULL; CONT=1; fi; done

for i in $@; do if test $i = "-m" || test $i = "-manual" ; then
mkdir manual; javadoc -d manual *.java mmc/*.java
cat > index.html <<EOF
<html><head>
<title>MUON PROPAGATION CODE IN JAVA</title>
<style><!--
a{text-decoration:none}
//--></style>
</head>
<body bgcolor="#000000" text="#D0D0D0" link="#00D044" vlink="#00D044" alink="#D0D0D0">
<table height="100%" width="100%"><tr><td valign="top">
<center><font size=+1 color="#00C0C0">Muon propagation code in Java</font></center><p>
Please find Muon Monte Carlo (MMC) manual pages produced by javadoc <a href="manual/overview-tree.html">here</a>.<p>
Refer to the <a href="mmc/README">README</a> file for a short description of the program.<br>
<a href="mmc/HISTORY">HISTORY</a> file for latest changes.<br>
<a href="README.amanda">README.amanda</a> for AMANDA-specific installation instructions.<p>
You can download the latest version of the program at one of these locations:
<a href="http://lisa.physics.berkeley.edu/~dima/work/MUONPR/muonPropagation.html">lisa</a>,
<a href="http://zombie.physics.berkeley.edu/~dima/work/MUONPR/muonPropagation.html">zombie</a> or
<a href="http://area51.berkeley.edu/~dima/work/MUONPR/muonPropagation.html">area51</a>.<p>
</td></tr><tr><td valign="bottom"><div align="right">
<a href="http://lisa.physics.berkeley.edu">my homepage</a><br>
<a href="mailto:chirkin@physics.berkeley.edu?subject=Let the harassment commence">harass author here</a><br>
</div></td></tr></table>
</body></html>
EOF
CONT=1; fi; done

for i in $@; do if test $i = "-c" || test $i = "-compile" ; then
rm mmc.jar 2> $NULL; $JAVAC $MAINPROG.java
# coment out jar line (does not exist) 250501 fs
#jar -cvf mmc.jar mmc/*.class > $NULL; rm mmc/*.class 2> $NULL
if test $MAINPROG = Amanda ; then
time echo | $JAVA $MAINPROG $@
$JAVA $MAINPROG -help
elif test $MAINPROG = Frejus ; then
time echo -n | $JAVA $MAINPROG $@
fi; CONT=1; fi; done

for i in $@; do if test $i = "-delete" ; then
rm -r mmc *.java README* manual index.html 2> $NULL
exit; fi; done

for i in $@; do if test $i = "-r" || test $i = "-run" ; then
$JAVA $MAINPROG $@ | sed '1,/---/d'
exit; fi; done

if test $CONT = "0"; then
if test -e mmc/Output.java; then sed -n '/version/s/^.*\"\(.*\)\".*$/\1/p' mmc/Output.java; fi
echo "Usage: \"$0 [options]\". Options are:
       \"-compile\" to compile the program
       \"-run\"     to run it
       \"-help\"    for help
       \"-frejus\"  to use Frejus program (Amanda is run by default)
       \"-clean\"   clean up distribution directory
       \"-delete\"  delete everything, but class and data files
       \"-manual\"  create java manual pages"
fi
