#!/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

NULL=/dev/null

JAVA_DIRECTORIES='/usr/*/jdk1.* /opt/jdk1.*'

if test "$HOSTTYPE" = alpha; then JAVA_HOME=/usr
else JAVA_HOME=`ls -d $JAVA_DIRECTORIES 2> $NULL | awk 'BEGIN {RS=" "} /jdk/ {print}' |
sed 's-\([^ ]*\)jdk\([^ ]*\)-\2.0 \1jdk\2-g' | sort -n | tail -1 | awk '{print $2}'`; fi
if test "$JAVA_HOME" = ""; then JAVA_HOME=$HOME/jdk1.*; fi

cd `echo $0 | sed 's/[^/]*$//'`
LANG=en_US
CLASSPATH=mmc.jar:.:"$JAVA_HOME"/src.jar
export LANG CLASSPATH
JAVA="$JAVA_HOME"/bin/java
JAVAC="$JAVA_HOME"/bin/javac
JAR="$JAVA_HOME"/bin/jar
CONT=0

compile() { if test -d mmc; then echo Compiling $* ...
else echo '***Error*** sources not found'; exit 1; fi
rm -f mmc.jar > $NULL 2>&1; "$JAVAC" $*.java
if test $? != 0 ; then echo '***Error*** compilation failed'; exit 1; fi
$JAR -cvf mmc.jar mmc/*.class > $NULL 2>&1; rm -f mmc/*.class > $NULL 2>&1; }

for i in $@; do if test `echo $i | sed 's/^-test=.*$/-test=/'` = "-test=" ; then
echo This script will test parametrizations.
echo Values in the tables are relative errors.
MAINPROG=TestPropagate
PROGRAM=`echo $i | sed 's/^-test=//'`

restore() { mv -f $MAINPROG.save $MAINPROG.java; rm -rf testfile.* > $NULL 2>&1;  }
finish() { echo Restoring files ...; restore; kill $$; }
trap "finish" 2

if test "$PROGRAM" = "1"; then
cp $MAINPROG.java $MAINPROG.save

for RND in 0.1 0.5 0.9; do
for IND in 0.001 0.999; do
echo -n RND=$RND IND=$IND .

cat $MAINPROG.save | sed 's/p.interpolate(".*")/p.interpolate("")/; s/program=.*;/program=1;/;
s/double rnd=.*;/double rnd='$RND';/; s/double ind=.*;/double ind='$IND';/' > $MAINPROG.java
compile $MAINPROG > $NULL; "$JAVA" $MAINPROG > testfile.np 2> $NULL; echo -n .

cat $MAINPROG.save | sed 's/p.interpolate(".*")/p.interpolate("crs","")/; s/program=.*;/program=1;/;
s/double rnd=.*;/double rnd='$RND';/; s/double ind=.*;/double ind='$IND';/' > $MAINPROG.java
compile $MAINPROG > $NULL; "$JAVA" $MAINPROG > testfile.pa 2> $NULL; echo .

STEPS=5
ENERGY=0
for i in Ioniz Brems Photo Epair; do echo -n "   $i:  ";
awk '/^Energy / {i++; e[i]=$3} /'$i':/ {
if(FILENAME=="testfile.np"){ a1[i]=$2; b1[i]=$3; c1[i]=$4; k=i }
if(FILENAME=="testfile.pa"){ a2[i-k]=$2; b2[i-k]=$3; c2[i-k]=$4 }
} END {
ai=bi=ci='$STEPS'
for(i=1; i<=k; i++){
    if(a1[i]*a2[i]!=0){ if(ai==0){ a=1-a1[i]/a2[i]; ai=0 } else{ a=0; ai--; } } else{ a=0; ai='$STEPS' }
    if(b1[i]*b2[i]!=0){ if(bi==0){ b=1-b1[i]/b2[i]; bi=0 } else{ b=0; bi--; } } else{ b=0; bi='$STEPS' }
    if(c1[i]*c2[i]!=0){ if(ci==0){ c=1-c1[i]/c2[i]; ci=0 } else{ c=0; ci--; } } else{ c=0; ci='$STEPS' }
    if(a<0) a=-a; if(b<0) b=-b; if(c<0) c=-c;
    if(e[i]>'$ENERGY'){ if(a>ae) ae=a; if(b>be) be=b; if(c>ce) ce=c;
}
} print ae, be, ce}' testfile.*
done; done; done; restore

elif test "$PROGRAM" = 2; then
cp $MAINPROG.java $MAINPROG.save

for RND in 0.01 0.10 0.50 0.90 0.99; do
for XFD in 0.01 0.10 0.50 0.90 0.99; do
echo -n RND=$RND XFD=$XFD .

cat $MAINPROG.save | sed 's/p.interpolate(".*")/p.interpolate("crs","")/; s/program=.*;/program=2;/;
s/double rnd=.*;/double rnd='$RND';/; s/double xfd=.*;/double xfd='$XFD';/' > $MAINPROG.java
compile $MAINPROG > $NULL; "$JAVA" $MAINPROG > testfile.np 2> $NULL; echo -n .

cat $MAINPROG.save | sed 's/p.interpolate(".*")/p.interpolate("all","")/; s/program=.*;/program=2;/;
s/double rnd=.*;/double rnd='$RND';/; s/double xfd=.*;/double xfd='$XFD';/' > $MAINPROG.java
compile $MAINPROG > $NULL; "$JAVA" $MAINPROG > testfile.pa 2> $NULL; echo -n ". "

STEPS=0
ENERGY=0
awk '/^Energy / {i++; e[i]=$3} $0!~/^Energy/ {
if(FILENAME=="testfile.np"){ a1[i]=$1; b1[i]=$2; c1[i]=$3; d1[i]=$4; k=i }
if(FILENAME=="testfile.pa"){ a2[i-k]=$1; b2[i-k]=$2; c2[i-k]=$3; d2[i-k]=$4 }
} END {
ai=bi=ci=di='$STEPS'
for(i=1; i<=k; i++){
    if(a1[i]*a2[i]!=0){ if(ai==0){ a=1-a1[i]/a2[i]; ai=0 } else{ a=0; ai--; } } else{ a=0; ai='$STEPS' }
    if(b1[i]*b2[i]!=0){ if(bi==0){ b=1-b1[i]/b2[i]; bi=0 } else{ b=0; bi--; } } else{ b=0; bi='$STEPS' }
    if(c1[i]*c2[i]!=0){ if(ci==0){ c=1-c1[i]/c2[i]; ci=0 } else{ c=0; ci--; } } else{ c=0; ci='$STEPS' }
    if(d1[i]*d2[i]!=0){ if(di==0){ d=1-d1[i]/d2[i]; di=0 } else{ d=0; di--; } } else{ d=0; di='$STEPS' }
    if(a<0) a=-a; if(b<0) b=-b; if(c<0) c=-c; if(d<0) d=-d;
    if(e[i]>'$ENERGY'){ if(a>ae) ae=a; if(b>be) be=b; if(c>ce) ce=c; if(d>de) de=d;
}
} print ae, be, ce, de}' testfile.*
done; done; restore

else echo '***Error*** cannot find program "'"$PROGRAM"'"'; fi
exit; fi; done

MAINPROG=Amanda
for i in $@; do if test $i = "-f" || test $i = "-frejus" ; then MAINPROG=Frejus; fi; done
VERSION=`if test -e mmc/Output.java; then sed -n '/version/s/^.*\"\(.*\)\".*$/\1/p' mmc/Output.java; else echo MMC; fi`

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 -rf *.class *~ mmc/*.class mmc/*~ mmc.jar .*.data *.data manual index.html > $NULL 2>&1; CONT=1; fi; done

for i in $@; do if test $i = "-m" || test $i = "-manual" ; then
if test -d mmc; then mkdir manual; javadoc -d manual *.java mmc/*.java; fi
cat << EOF > index.html
<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.first">README.first</a> for installation and use 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=$VERSION">harass author here</a><br>
</div></td></tr></table>
</body></html>
EOF
CONT=1; fi; done

for i in $@; do if test $i = "-hf" || test $i = "-hifi" ; then
if test -d mmc; then echo Generating high fidelity version of mmc ...
else echo '***Error*** sources not found'; exit 1; fi
for i in mmc/*.java; do cat $i | sed '/Integral(5, 40, 1.e-.)/s/1.e-./1.e-8/;
/s\..\.c.J/s/(100,/(1000,/; /in Java/s/";/ (hf)";/' > foo; mv -f foo $i; done
echo "
This version of the program will take longer to initialize, since it computes
the values for the parametrization tables with 1.e-8 precision. The tables are
also somewhat larger, since more grid points are added for some integrals. The
execution time after initialization should not be affected, though."
CONT=1; fi; done

for i in $@; do if test $i = "-c" || test $i = "-compile" ; then
compile $MAINPROG
if test $MAINPROG = Amanda ; then
echo | time -p "$JAVA" $MAINPROG $@
if test $? != 0 ; then echo '***Error*** table compilation failed'; exit 1; fi
"$JAVA" $MAINPROG -help
elif test $MAINPROG = Frejus ; then
echo -n | time -p "$JAVA" $MAINPROG $@
if test $? != 0 ; then echo '***Error*** table compilation failed'; exit 1; fi
fi; CONT=1; fi; done

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

for i in $@; do if test $i = "-r" || test $i = "-run" ; then
"$JAVA" $MAINPROG $@
if test $? != 0 ; then echo '***Error*** program failed' 1>&2; exit 1; fi
exit; fi; done

if test $CONT = "0"; then
echo $VERSION
echo
echo This computer is $HOSTTYPE, java was found in \"$JAVA_HOME\"
echo -n "testing java ... "; $JAVA -version 2>&1 | awk '{if($0~/java version/) v=$3; print} END {
    split(v,a,"[.\"]"); for(i=2; i<5; i++){ if(a[i]=="") a[i]=0; r*=10; r+=a[i] }
    if(r<122){ printf "\n***WARNING*** Found java version %s is insufficient.\n", v
    print "***WARNING*** Upgrade java to at least version \"1.2.2\""}}'
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
       \"-hifi\"    generate high precision version of mmc
       \"-test=1\"  or  \"-test=2\"  to check the parametrizations"
fi
