#! /usr/bin/env bash
# $Header: /home/ubuntu/mnt/e9_copy/mitgcm.org/front_content/make_summary,v 1.54 2009/06/30 03:28:07 jmc Exp $
#
#  The purpose of this script is to create HTML summaries of the
#  directories produced by the "parse_emails" script.
usage()
{
    echo
    echo "Usage:  $0 [OPTIONS]"
    echo 
    echo "where possible OPTIONS are:"
    echo "  (-help|-h)           print usage"
    echo "  (-date |-d )PERIOD   run for PERIOD=\"YYYY_MM\""
    echo "                         [def=\"$PERIOD\"]"
    echo 
    exit 1
}
CURR_PER=`date +%Y`"_"`date +%m`
# defaults
PERIOD=$CURR_PER
#  Parse options
ac_prev=
for ac_option ; do
    # If the previous option needs an argument, assign it.
    if test -n "$ac_prev"; then
        eval "$ac_prev=\$ac_option"
        ac_prev=
        continue
    fi
    ac_optarg=`expr "x$ac_option" : 'x[^=]*=\(.*\)'`
    case $ac_option in
	
	-help | --help | -h | --h)
	    usage ;;
	
        -date | --date | -d | --d)
            ac_prev=PERIOD ;;
        --date=* | -date=*)
            PERIOD=$ac_optarg ;;
        *)
	    echo "Error: don't understand argument \"$ac_option\""
	    usage
            ;;
     esac
done
#INDIR="/net/orwell/export/export-9/mitgcm-testing/results/$PERIOD"
#OUTDIR="/home/jmc/mitgcm/test_web/summary"
INDIR="/u/u0/httpd/html/testing/results/$PERIOD"
OUTDIR="/u/u0/httpd/html/testing/summary"
OUTFILE=$OUTDIR"/output_"$PERIOD".html"
res_url="http://mitgcm.org/testing/"
#  Create the links in $OUTFILE :
echo "Creating the \"latest\" file for each machine: "
the_date=`date`
cat > $OUTFILE << EOF
  
    MITgcm testing summary
    
    
    
    
    
    
    
    
  
  
    The MITgcm model is tested
      (compiled and run) in an automated fashion on a varirety of
      different machines.  The following is a summary of the MITgcm
      verification suite for the time period: $PERIOD.
    The machine naming scheme is:
    
       | Machine Type |  Nickname | 
        Notes |  
      
        | Intel P4 |  "faulks" |  Fedora Core 6  |  
      
        |  Intel P4  |  "meander" |  Ubuntu 8.10  |  
      
        |  Intel P4  |  "lagoon" |  Fedora 8  |  
      
        |  Opteron Barcelona  |  "harbor" |  Ubuntu 8.04 LTS (x86_64) |  
      
        |  Intel Xeon (Woodcrest)  |  "dickens" |  Fedora 10 (x86_64) |  
      
        |  Intel Core 2 duo  |  "danton" |  Fedora 8 (x86_64) |  
      
        |  ACESgrid Dell Xeon  | 
        "aces" | Fedora Core 2 |  
      
        | Intel Xeon (Woodcrest) cluster
         |  "beagle" | Rocks 5.2 (x86_64) |  
      
      
        |  SGI Altix Cluster  |  "columbia" | 
        
        NAS Columbia  |  
      
        |  IBM POWER4 SP  |  "edvir" |   AIX 5.2  |  
      
        |  IBM POWER5+ SP  |  "blueice" | 
        NCAR Blueice
         |  
      
        |  AMD Opteron  |  "starp" |  SuSE SLES 10 (AMD64) |  
      
        |  AMD Opteron  |  "solasrv" |  Solaris 10 |  
      
        |  Sun UltraSparc IV  |  "rays" |  Solaris 10 |  
      
      
        |  NEC SX-8  |  "sx8" |  Super-UX  |  
      
        |  Cray XD1 (AMD Opteron)  |  "xd1" | 
        Cray HPC enhanced Linux 2.6.5  |  
      
        |  PowerPC970MP cluster  |  "bigred" | 
        SuSE SLES 9 (PPC)  |  
      
      
    
    
      The complete output for the verification runs can be found in
      the summary pages
      and the testing archives.
      The latest reports are:
    
  |  Nickname  | 
   OPTFILE Name  | 
   Type  | 
   Date  | 
   Summary  | 
   Ratio  | 
EOF
color="#bbffdd"
ncolor="#bbddff"
MACHINES="faulks meander lagoon harbor dickens danton aces"
MACHINES="$MACHINES beagle columbia edvir rays solssrv solasrv sx8 xd1"
MACHINES="$MACHINES bigred starp"
( cd $INDIR ; ls -1 -t */summary.txt | sed 's/\/summary.txt//' ) > ./dir_all
MALL=`cat ./dir_all | sed -e 's|_| |g' | awk '{print $2}' | sort | uniq`
for madd in $MALL ; do
    present=0
    for m in $MACHINES ; do
        echo $madd | grep $m > /dev/null 2>&1
        RETVAL=$?
        test $RETVAL = 0  &&  present=1
        continue
    done
    test $present = 0  &&  MACHINES="$MACHINES $madd"
done
#MACHINES="faulks"
for mname in $MACHINES ; do
    echo "  $mname"
    dir_list=`grep $mname ./dir_all`
    echo -n "" > ./mlist
    for i in $dir_list ; do
	dir=$INDIR"/"$i
	OPTFILE=
	if test -r $dir/summary.txt ; then
	    comm=`grep 'OPTFILE=' $dir/summary.txt`
	    eval $comm
	    OPTFILE=${OPTFILE##*/}
	fi
	if test "x$OPTFILE" = x -a -r "$dir/genmake_state" ; then
	    comm=`grep 'OPTFILE=' $dir/genmake_state 2>/dev/null`
	    eval $comm
	    OPTFILE=${OPTFILE##*/}
	fi
	if test "x$OPTFILE" = x ; then
	    comm=`grep '^# OPTFILE=' $dir/*/Makefile* 2>/dev/null | head -1`
	    comm=${comm##*#}
	    eval $comm
	    OPTFILE=${OPTFILE##*/}
	fi
	if test "x$OPTFILE" = x ; then
	    OPTFILE="not_explicitly_specified"
	fi
	ADJOINT=
	RESTART=0
	NOI3E=0
	MTH=0
	if test -r $dir/summary.txt ; then
	    comm=`grep 'ADJOINT=true' $dir/summary.txt 2>/dev/null`
	    eval $comm
	    RESTART=`grep -c 'test 2+2=4 summary' $dir/summary.txt`
	    NOI3E=`grep -c "^run: .*testreport.* '*-noieee'*" $dir/summary.txt`
	    MTH=`grep -c "^run: .*testreport.* -mth " $dir/summary.txt`
	fi
	if test "x$RESTART" = x0 ; then
	    kind="forward"
	    test "x$ADJOINT" = xtrue  &&  kind="adjoint"
	else
	    kind="restart"
	fi
	if test "x$NOI3E" = x1 ; then
	    OPTFILE="${OPTFILE}.noieee"
	fi
	if test "x$MTH" = x1 ; then
	    yy=`echo $OPTFILE | grep -c 'mth$'`
	    if test $yy = 0 ; then OPTFILE="${OPTFILE}+mth" ; fi
	fi
	t_pass="--"
	t_tot="--"
	if test -r $dir/summary.txt ; then
	    grep '^[YN] [YN] [YN] [YN]' $dir/summary.txt > ./all_tests 2>/dev/null
	    t_tot=`cat ./all_tests | wc -l | sed -e 's| ||g'`
	    t_pass=`grep '^Y Y Y Y' ./all_tests | grep 'pass ' | wc -l | sed -e 's| ||g'`
	fi
	rm -f ./all_tests
	# echo "${dir##*/} : $t_pass out of $t_tot"
	tokens=`echo $i | sed -e 's|_| |g'`
	echo "" > ./ms_tmp
	for tok in $tokens ; do
	    echo $tok >> ./ms_tmp
	done
	DAY=`cat ./ms_tmp | awk '(length($1)==8 && substr($1,0,3)=="200")'`
	rm -f ./ms_tmp
	echo "$OPTFILE$kind $DAY $OPTFILE $kind $i $t_pass:$t_tot" >> ./mlist
    done
    # helpful for debugging
    # cat ./mlist
    #  Do we have any data?  If so, create the latest pointer.
    num=`wc -l ./mlist | awk '{print $1}'`
    if test $num -gt 0 ; then
	# swap colors
        ctmp=$color
        color=$ncolor
        ncolor=$ctmp
	keys=`cat ./mlist | cut -d " " -f 1 | sort | uniq`
	for key in $keys ; do
	    tline=`grep "^$key " ./mlist | head -1`
	    ratio=`echo $tline | cut -d " " -f 6`
	    ldir=`echo $tline | cut -d " " -f 5`
	    kind=`echo $tline | cut -d " " -f 4`
	    optf=`echo $tline | cut -d " " -f 3`
	    DAY=`echo $tline | cut -d " " -f 2`
	    URL="results/$PERIOD/$ldir"
	    cat <>$OUTFILE
  |  $mname  | 
   $optf  | 
   $kind  | 
   $DAY  | 
    summary.txt   | 
   $ratio  | 
EOF
	done
    fi
done
cat >> $OUTFILE << EOF
  | This table generated on: $the_date | 
    
Examples of the scripts used for these testing runs can be obtained from: 
MITgcm/tools/example_scripts.
  
EOF
rm -f ./dir_all ./mlist
#- put the file in place
chgrp gcmpack $OUTFILE
chmod 664 $OUTFILE
LATEST=$OUTDIR"/latest_"$PERIOD".html"
mv -f $OUTFILE $LATEST
if test "x$PERIOD" = "x$CURR_PER" ; then
    cp $LATEST ./testing.xml
    (
	cd $OUTDIR
	rm -f latest.html
	ln -s $LATEST latest.html
    )
fi