#!/bin/sh

set -u
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

tmp_base=`$ENSTORE_DIR/external_distr/extract_config_parameters.py inquisitor | grep html_file`
if [ $? -ne 0 ]; then
    echo can not find web directory in configuration
    exit 1
fi
BASEPATH=`echo $tmp_base | cut -f2 -d\:`
#BASEPATH=/fnal/ups/prd/www_pages/enstore

node=`uname -n| sed -e 's/\([^\.]\)\..*/\1/'`
gang=`gang`
case $gang in
  stken) robot="AML/J"
          ;;
   d0en) robot="AML/2"
          ;;
       *) robot=UNKNOWN
          ;;
esac

t_www=`$ENSTORE_DIR/external_distr/extract_config_parameters.py inquisitor | grep www_host`
if [ $? -ne 0 ]; then
    www_host="http://${gang}srv2.fnal.gov"
else
    www_host=`echo $t_www | cut -f2,3 -d\:`
fi

AML2_DIR=/srv2/enstore/aml2
if [ `uname -n | grep -c ${gang}srv2` -ne 0 ]; then
  BASE=$AML2_DIR/aml2Shadow/AMU/LOGS-TRC
  DECODED=$AML2_DIR/aml2-log
# REPORTED=/srv2/aml2-report
fi

# if called with no arguments, then assume that we have get the logs first
# and then pass them along for decoding
if [ -z "${1-}" ]; then

   if [ -z "${BASE-}" ]; then
      BASE=/rip8a/enstore/aml2Shadow/AMU/LOGS-TRC
   fi
if [ ! -d $BASE ]; then
    mkdir -p $BASE
fi
if [ ! -d $DECODED ]; then
    mkdir -p $DECODED
fi
#echo $BASE
#echo $www_host
#exit 0
#  mirror $ENSTORE_DIR/sbin/aml2.mirror
   mirror $ENSTORE_DIR/sbin/aml2.mirror.d0en 
   find $BASE -type f -name "lo*" -maxdepth 1 -exec $0 {} \;

# make the nice calendar display of log files
  if [ `uname -n | grep -c ${gang}srv2` -ne 0 ]; then
      set -xv
      python $ENSTORE_DIR/src/enstore_make_log_calendar.py \
             --title="$robot Log Files" \
             --web_host="${www_host}/enstore/aml2log" \
             --logfile_dir="$DECODED" \
             --html_file="$DECODED/aml2_log_file.html" \
             --caption_title="$robot Log Files"

#     python $ENSTORE_DIR/src/enstore_make_log_calendar.py \
#            --title="$robot Report Files" \
#            --web_host="http://${gang}srv2.fnal.gov/enstore/aml2rpt" \
#            --logfile_dir="$REPORTED" \
#            --output="$REPORTED/aml2_report_file.html" \
#            --caption="$robot Summary Report Files"
  fi

# otherwise we need to create a version people can read
# then gzip it to save space - note that most browsers can decode gzip formats
else

    if [ -z "${DECODED-}" ]; then
       DECODED=$BASEPATH/aml2log
    fi
#   if [ -z "${REPORTED-}" ]; then
#      REPORTED=$BASEPATH/aml2rpt
#   fi

    fname=`basename $1`
    thisYear=`date +%Y`
    year=$thisYear  # THIS IS A PROBLEM WHEN THE YEAR ROLLS OVER!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if [ `echo $fname | grep -c log` -ne 0 ]; then
      binary=1
      month=`echo $fname | cut -b6-7`
      day=`echo $fname | cut -b4-5`
      asc=$DECODED/$fname.ascii
#     arpt=$REPORTED/$fname.rpt
    elif [ `echo $fname | grep -c lo` -ne 0 ]; then
      binary=0
      month=`echo $fname | cut -b5-6`
      day=`echo $fname | cut -b7-8`
      asc=$DECODED/log$day$month.ascii #force old format for now (until AML/2 upgraded)
#     arpt=$REPORTED/log$day$month.rpt #force old format for now (until AML/2 upgraded)
    else
      return
    fi
    HISTORY=history
    zipped=$DECODED/$HISTORY/$year/$month/$fname.ascii.gz
    stash=0
    if [ ! -r $zipped ]; then
      if [ ! -r $asc -o $asc -ot $1 ]; then
         echo $asc
         if [ $binary -eq 1 ]; then
           nice -n 20 $ENSTORE_DIR/sbin/dump_aml2_log.py $1 > $asc
         else
           cp $1 $asc
         fi
         chmod 644 $asc
#        nice -n 20 $ENSTORE_DIR/sbin/stataml2log.py $asc $arpt
#        nice -n 20 $ENSTORE_DIR/sbin/stataml2log.py $asc
#        chmod 644 $arpt
      fi
    fi
fi
