#! /usr/bin/env bash
# makes all graphs (.eps and .gif) for histogram config files in directory HistoConfigs
# run with command ./MakeAllHistos.sh

usage(){
    echo "Usage: $0 [options]"
    echo " options:"
    echo "  -h, --help               This help"
    echo "  --html <file name>       HTML file name [default=index.html]"
    echo "  --title <name>           HTML page title (replace spaces by \",\")"
    echo "  --size <percentage>      Scale gif images by this percentage [default=70]"
    echo "  --outdir <dirname>       Output directory [default=Graphs]"
    echo "  --configdir <dirname>    Plot config directory [default=HistoConfigs]"
    echo "  --filelist <filelist>    File list used to make plots"
    echo "  --lumi <luminosity>      To specify non-default luminosity"
    echo "  --png                    Make .png files instead of .gif"
    echo "  --nohtml                 Do not produce an html file"
    echo "  --htmlonly               Only produce html file and bitmap images"
    echo "  --force                  Overwrite existing output"
    echo "  --allplots               Make many more plots [default: only main plots]"
    echo "  --goodnbins <nbins>      Optimal number of bins, applying only to"
    echo "                           histograms with at least nbins bins"
    echo "  --yields <nbins>         Print yields in the legend"
    echo "  --KS <nbins>             Show Kolmogorov-Smirnov test"
    exit 1
}

#Defaults
SIZE=70
TITLE="Single top e+jets"
OutputDir=Graphs
ConfigDir=HistoConfigs
ImageType=gif
HTML=index.html
NoHTML=0
HTMLonly=0
FORCE=0
AllPlots=0
goodnbins=-1
showyields=""
KS=""

#Read command line options
args=`getopt -l "help,html:,nohtml,htmlonly,outdir:,size:,title:,configdir:,png,filelist:,force,lumi:,allplots,goodnbins:,yields,KS" "h" "$@"`
[ $? != 0 ] && usage
set -- $args
while [ ! -z "$1" ]
do
  #echo $1
  case "$1" in
    -h | --help) usage;;
    --size) shift; SIZE=`echo "$1" | tr -d "'"`;;
    --html) shift; HTML=`echo "$1" | tr -d "'"`;;
    --title) shift; TITLE=`echo ${1:1:$((${#1}-2))}|tr ',' ' '`;;
    --outdir) shift; OutputDir=`echo "$1" | tr -d "'"`;;
    --configdir) shift; ConfigDir=`echo "$1" | tr -d "'"`;;
    --filelist) shift; FileList="--filelist "`echo "$1" | tr -d "'"`;;
    --goodnbins) shift; goodnbins=`echo "$1" | tr -d "'"`;;
    --lumi) shift; Lumi="--lumi "`echo "$1" | tr -d "'"`;;
    --png) ImageType=png;;
    --nohtml) NoHTML=1;;
    --htmlonly) HTMLonly=1;;
    --force) FORCE=1;;
    --allplots) AllPlots=1;;
    --yields) showyields="--yields";;
    --KS) KS="--KS";;
    --) shift; # get all arguments without name
	REMAINS=`echo "$*" | tr -d "'"`
	[ "${REMAINS}x" != "x" ] && usage
	break;;
    *) echo "Unknown option $1"; usage; break;;
  esac
  shift
done

#make output directory
if [ ! -d $OutputDir ]; then
    mkdir -p $OutputDir
elif [ $FORCE = 0 ]; then
    echo "Output exists: $OutputDir"
   
fi




#Write html file if not asked not to
if [ $NoHTML -eq 0 ]; then
    #Get the user name and email address
    source /D0/ups/etc/setups.sh
    setup whod0 >& /dev/null
    AuthorName=`whod0 $USER 2> /dev/null | grep -A1 name | grep -B1 ${USER}@ | head -1|cut -f2- -d' '`
    AuthorName=${AuthorName:-$USER}
    AuthorEmail=`whod0 $USER 2> /dev/null | grep -A1 name | grep -B1 $USER@ | tail -1|cut -f2 -d' '`
    AuthorEmail=${AuthorEmail:-${USER}@fnal.gov}


    cat >${OutputDir}/$HTML <<EOF
<HTML>
<HEAD>
<title>$TITLE</title>
</HEAD>
<BODY>
<h1 align=center>$TITLE</h1>
<p align=center>
EOF


	   

for epsplot in `cd ${OutputDir}; ls *.eps`
do
    
    tmb=${epsplot/.eps/.$ImageType}
    echo "<a href=\"${epsplot}\"><img alt=\"\" src=\"$tmb\"></a>" >>${OutputDir}/$HTML
done

cat >>${OutputDir}/$HTML <<EOF
<p>
<hr style="width: 100%; height: 2px;">
<address><a href="mailto:${AuthorEmail}">${AuthorName}</a></address>
Last modified: <SCRIPT type="text/JavaScript" language="JavaScript">
 <!--
testdate = new Date(document.lastModified);
testdate = testdate.toLocaleString();
document.writeln(testdate);
 -->
</SCRIPT>
</BODY>
</HTML>
EOF
   echo " (includes a web page)"
else
   echo
fi #NoHTML

exit 0


