#!/bin/sh
# This script compiles and runs programs from mmc package
# Change JDK_DIR to include the location of your java distribution
# Change GCC_DIR to point to your gcc v 3.0 or higher distribution

cd `echo $0 | sed 's/[^/]*$//'`
MMCDIR=$PWD

JDK_DIR="$MMCDIR/jdk1.* $HOME/jdk1.* /usr/*/jdk1.* /opt/jdk1.*"
GCC_DIR=/home/dima/DIMA/gcc

NULL=/dev/null
LANG=C; export LANG
if test "$HOSTTYPE" = alpha; then JHOME=/usr/bin
else JHOME=`for i in $JDK_DIR; do if test -e $i/bin/java; then echo $i; fi; done |
sed 's-\([^ ]*\)jdk\([^ ]*\)-\2.0 \1jdk\2/bin-g' | sort -n | tail -1 | awk '{print $2}'`
if test "$JHOME" = ""; then JHOME=/usr/bin; fi; fi

if test -e "$JHOME"/java; then
CLASSPATH=mmc.jar:.
PATH="$JHOME:$PATH"
export CLASSPATH PATH
else JHOME=none; fi

echr() { echo "$*" 1>&2; }
jerr() { if test "$JHOME" = none; then echr '***ERROR*** unable to locate java'; exit; fi; }
remove() { rm -f "$@" > $NULL 2>&1; }
compile() { jerr; 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; }

CONT=0; 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=Test; fi; done
for i in "$@"; do if test ""`echo $i | sed -n 's/^-p=.*$/-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 jerr
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 `ls *.java 2> $NULL | sed 's/.java//g'`; 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
if test ""`echo $VERSION | sed 's/^.* v. [.0-9]*//; s/ //g'` = ""; then
for f in mmc/*.java; do cat $f | sed '/40, 1.e-.)/s/1.e-./1.e-7/;
/s\..\.c.J/s/(100,/(1000,/; /in Java/s/";/ (hf)";/' > foo; mv -f foo $f; 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."
else echr '***ERROR*** option cannot be applied to this mmc version.'; fi
CONT=1; fi; done

for i in "$@"; do if test "$i" = "-fl" || test "$i" = "-float"; then
if test -d mmc; then echr 'Generating float (lower precision) version of mmc ...'
else echr '***ERROR*** sources not found'; exit 1; fi
if test ""`echo $VERSION | sed 's/^.* v. [.0-9]*//; s/ //g'` = ""; then
for f in mmc/*.java; do cat $f | sed '/40, 1.e-.)/s/1.e-5/1.e-3/; /40, 1.e-.)/s/1.e-6/1.e-4/;' > foo; mv -f foo $f; done
cat mmc/PhysicsModel.java | sed 's/computerPrecision=1.e-10;/computerPrecision=1.e-5;/' > foo; mv foo mmc/PhysicsModel.java
for f in *.java mmc/*.java; do cat $f | sed 's/double/float/g; s/Double/Float/g;' |
sed 's/\([^0-9[:alpha:]][0-9][0-9]*\.[0-9]*\(e[+-]*[0-9][0-9]*\)\?\)/\1f/g' |
sed 's/\(Math\.log\)/(float)\1/g; s/\(Math\.exp\)/(float)\1/g;
s/\(Math\.cos\)/(float)\1/g; s/\(Math\.sin\)/(float)\1/g;
s/\(Math\.PI\)/(float)\1/g; s/\(Math\.acos\)/(float)\1/g;
s/\(Math\.atan\)/(float)\1/g; s/\(Math\.sqrt\)/(float)\1/g;
s/\(Math\.random\)/(float)\1/g; s/\(Math\.pow\)/(float)\1/g;' > foo; mv foo $f; done
cat mmc/Output.java | sed '/in Java/s/f";/ (fl)";/' > foo; mv foo mmc/Output.java
echr "
This version of MMC has all double variables replaced with floats. It should be
faster than the default version. Raw data parametrization tables of the regular
version are not compatible with this mmc fl version.
"
else echr '***ERROR*** option cannot be applied to this mmc version.'; fi
CONT=1; fi; done

for i in "$@"; do if test "$i" = "-m" || test "$i" = "-manual"; then
MMCLS=`if test -d mmc; then for i in . mmc; do
echo '<tr><td>&nbsp;</td></tr><tr><td></td><td>+ <font color="red">'$i'</font></td><td></td></tr>'
cd $i; echo *.java | sed 's/.java//g' | awk '{
a=int(NF/3); if(NF/3-a>0) a++
for(i=1;i<=NF;i++){ if(i==1) print "<tr><td>"
printf "<a href=\"'$i'/%s.java\">%s</a><br>\n", $i, $i
if(i==2*a||i==a) print "</td><td valign=\"top\">"
if(i==NF) print "</td></tr>" }
}'; cd $MMCDIR; done; else echo '<center>are not available</center>'; 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">$VERSION</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="README">README</a> file for a short description of the program, installation and use instructions.<br>
All changes are documented in the <a href="HISTORY">HISTORY</a> file.<br>
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>&nbsp;<p><table width="100%">
<center><font size=+1 color="#00C0C0">Sources</font></center>
$MMCLS
</table><p>&nbsp;</tr></td>
<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

if test -d mmc; then echr Creating javadoc manual pages ...; jerr
remove -r manual; mkdir manual; javadoc -d manual *.java mmc/*.java > $NULL 2>&1; fi
CONT=1; fi; done

ALRCMP=0
for i in "$@"; do if test "$i" = "-ca" || test "$i" = "-compall"; then
compile Amanda Frejus Test \
`ls *.java 2> $NULL | sed 's/Amanda.java//g; s/Frejus.java//g; s/Test.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 *.java README HISTORY manual index.html
exit; fi; done

for i in "$@"; do if test ""`echo $i | sed -n 's/^-test=.*$/-test=/p'` = "-test="; then
echo This script will test parametrizations.
echo Values in the tables are relative errors.
echo
MAINPROG=Test
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
jerr; java "$MAINPROG" "$@"
if test $? != 0; then echr '***ERROR*** program failed'; exit 1; fi
exit; fi; done

for i in "$@"; do if test "$i" = "-gcj"; then
PATH="$GCC_DIR/bin:$PATH"
LD_LIBRARY_PATH="$GCC_DIR"/lib
export PATH LD_LIBRARY_PATH
echo Compiling "$MAINPROG" ...
gcj --main="$MAINPROG" -o "$MAINPROG" "$MAINPROG".java mmc/*.java -O2 -static -Dfile.encoding=C
exit; fi; done

if test $CONT = "0"; then
echo $VERSION
echo
if test "$JHOME" = none; then echo '***WARNING*** No suitable java installation was found'
else echo This computer is $HOSTTYPE, java was found in \"$JHOME\"
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\""}}'; fi
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 program Amanda (default setting)
       \"-frejus\"   to use program Frejus
       \"-test\"     to use program Test
       \"-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
       \"-float\"    replace doubles with floats (fl mmc)
       \"-gcj\"      compile the program using gcj"
fi
