#!/bin/sh
# This script compiles and runs Amanda and Frejus classes as part of mmc package
# Change JAVA_DIRECTORIES to include the location of your java distribution

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

NULL=/dev/null
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
JAVADOC="$JAVA_HOME"/bin/javadoc
CONT=0

remove() { rm -f $* > $NULL 2>&1; }
echr() { echo $* 1>&2; }
compile() { echr Compiling $* ...
if test "$*" = ""; then echr '***Error*** nothing to compile'; exit 1; fi
SRC=`echo "$* " | sed 's/\([^ ]\) /\1.java /g'`
CLS=`echo "$* " | sed 's/\([^ ]\) /\1.class /g'`
if test -d mmc; then remove $CLS mmc/*.class mmc.jar; "$JAVAC" $SRC
if test $? != 0 ; then echr '***Error*** compilation failed'; exit 1; fi
$JAR -cMf mmc.jar $CLS mmc/*.class; remove $CLS mmc/*.class
else echr '***Warning*** sources not found'
for i in $*; do
if test -e mmc.jar && test ""`$JAR -tf mmc.jar | sed -n 's/^'$i'.class/1/p'` = 1
then echr Found compiled program $i
else echr '***Error*** program '$i' not found'; exit 1; fi; done; fi; }

MAINPROG=Amanda
for i in $@; do if test $i = "-a" || test $i = "-amanda" ; then MAINPROG=Amanda; fi; done
for i in $@; do if test $i = "-f" || test $i = "-frejus" ; then MAINPROG=Frejus; fi; done
for i in $@; do if test $i = "-t" || test $i = "-test" ; then MAINPROG=TestPropagate; fi; done
for i in $@; do if test `echo $i | sed 's/^-p=.*$/-p=/'` = "-p="
then MAINPROG=`echo $i | sed 's/^-p=//; s/.java$//'`; 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"
elif test "$MAINPROG" = Frejus ; then
echo -e "\nUse this program for Frejus data processing in pipes"; fi
"$JAVA" "$MAINPROG" -help 2> $NULL; if test $? != 0 ; then
echo -e "\nhelp for \"$MAINPROG\" is not available\n"; fi
exit; fi; done

for i in $@; do if test $i = "-clean" ; then
remove -r *.class *~ mmc/*.class mmc/*~ .*.data *.data manual index.html core
if test -d mmc; then remove mmc.jar; fi; CONT=1; fi; done

for i in $@; do if test $i = "-hf" || test $i = "-hifi" ; then
if test -d mmc; then echr Generating high fidelity version of mmc ...
else echr '***Error*** sources not found'; exit 1; fi
for i in mmc/*.java; do cat $i | sed '/40, 1.e-.)/s/1.e-./1.e-7/;
/s\..\.c.J/s/(100,/(1000,/; /in Java/s/";/ (hf)";/' > foo; mv -f foo $i; done
echr "
This version of the program will take longer to initialize, since it computes
the values for the parametrization tables with 1.e-7 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."
CONT=1; fi; done

for i in $@; do if test $i = "-m" || test $i = "-manual" ; then
if test -d mmc; then echr Creating javadoc manual pages ...
remove -r manual; mkdir manual; $JAVADOC -d manual *.java mmc/*.java > $NULL 2>&1; 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/index.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

ALRCMP=0
for i in $@; do if test $i = "-ca" || test $i = "-compall" ; then
compile Amanda Frejus TestPropagate \
`ls *.java 2> $NULL | sed 's/Amanda.java//g; s/Frejus.java//g; s/TestPropagate.java//g; s/.java//g'`
ALRCMP=1
CONT=1; fi; done

for i in $@; do if test $i = "-c" || test $i = "-compile" ; then
if test $ALRCMP = "0"; then compile "$MAINPROG"; fi
if test "$MAINPROG" = Amanda ; then
echo | time -p "$JAVA" "$MAINPROG" $@
if test $? != 0 ; then echr '***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 echr '***Error*** table compilation failed'; exit 1; fi
fi; CONT=1; fi; done

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

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.
echo
MAINPROG=TestPropagate
TEST=`echo $i | sed 's/^-test=//'`
if test $ALRCMP = "0"; then compile "$MAINPROG"; fi
restore() { remove testfile.*;  }
finish() { echo Restoring files ...; restore; kill $$; }
trap "finish" 2

if test "$TEST" = "1"; then
echo checking precision of parametrizations of cross sections
echo

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

"$JAVA" "$MAINPROG" -proc=1 -intr=none -RND1=$IND -RND2=$RND > testfile.np 2> $NULL; echo -n .
"$JAVA" "$MAINPROG" -proc=1 -intr=crs -RND1=$IND -RND2=$RND > testfile.pa 2> $NULL; echo .

STEPS=5
ENERGY=1
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'
ae=be=ce=0
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 "$TEST" = 2; then
echo checking precision of parametrizations of energy and tracking integrals
echo

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 .

"$JAVA" "$MAINPROG" -proc=2 -intr=crs -RND1=$RND -RND2=$XFD > testfile.np 2> $NULL; echo -n .
"$JAVA" "$MAINPROG" -proc=2 -intr=all -RND1=$RND -RND2=$XFD > 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'
ae=be=ce=de=0
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*** test "'"$TEST"'"' is not defined; fi
exit; fi; done

for i in $@; do if test $i = "-r" || test $i = "-run" ; then
"$JAVA" "$MAINPROG" $@
if test $? != 0 ; then echr '***Error*** program failed'; 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
       \"-compall\"  to compile everything
       \"-run\"      to run the program
       \"-help\"     for help
       \"-amanda\"   to use AMANDA program (default setting)
       \"-frejus\"   to use Frejus program
       \"-test\"     to use TestPropagate program
       \"-p=[name]\" to select some other program
       \"-clean\"    clean up distribution directory
       \"-delete\"   delete everything except class and data files
       \"-manual\"   create javadoc manual pages
       \"-test=1\"   test parametrizations of cross sections
       \"-test=2\"   and of energy and tracking integrals
       \"-hifi\"     generate high precision version of mmc"
fi
