#!/bin/ksh
# $Id$
##############################################################
# Original : C. Talandier for ESOPA
# Contact  : opatlod@locean-ipsl.upmc.fr
#
# It is called by the assessment.ksh script for a 
# given configuration 
#
# INPUT ARGS: 6
#     - zmxcu  : (0/1) mon${CONF}_err file (do/do not) exist"
#     - zxrs   : (0/1) Ref memory_size.txt file (do/do not) exist"
#     - zxrm   : (0/1) Ref mon${CONF}_err file (do/do not) exist"
#     - zconf  : configuration name"
#     - zout   : output file name"
#     - ztag   : tag name to which are compared files"
#
# WORK: It aims to :
#
#      - Treat output timing from the osxxlf target for 
#        mon, mpi runs
#     *- compare it to a reference version 
#
# * this step is performed only if a reference tag 
#   is specified with the -t option and if files exist.
#   See the Makefile under ./config/NVTK directory
#
##############################################################
#set -xv
##############################################################

# Get the name of the present script
nscript=$( (basename $0) )

#
# help_CPU_time() To print help
#
function help_CPU_time
{
  echo "Usage: $nscript [-a] [-b] [-d] [-c] [-o] [-t]"
  echo "Options: These are optional argument"
  echo " -a : (0/1) mon${CONF}_err file (do/do not) exist"
  echo " -b : (0/1) Ref memory_size.txt file (do/do not) exist"
  echo " -d : (0/1) Ref mon${CONF}_err file (do/do not) exist"
  echo " -c : configuration name"
  echo " -o : output file name"
  echo " -t : tag name to which are compared files"
  echo " their values are not taken)"
  exit 1
}

# Initialization
zmxcu=1
zxrs=1
zxrm=1
zconf=
zout=
ztag=

# Get arguments
while getopts a:b:d:c:o:t: opt
do
  case "$opt" in
    a) zmxcu="$OPTARG";;
    b) zxrs="$OPTARG";;
    d) zxrm="$OPTARG";;
    c) zconf="$OPTARG";;
    o) zout="$OPTARG";;
    t) ztag="$OPTARG";;
    *) help_MEM_size;;
  esac
done

# Check if results must be compared to the reference version
test -n "${ztag}"
evaltag=$?

zfilecs='mon/LONG/memory_size.txt'
zfilers='mon/LONG/REF/memory_size.txt'

# Select only appropriate lines
grep -v Stack ${zfilecs} | head -1 > temp_cs_sck
tail -1 ${zfilecs}                 > temp_cs_sic

# Select only appropriate lines
grep -v Stack ${zfilers} | head -1 > temp_rs_sck
tail -1 ${zfilers}                 > temp_rs_sic

# If reference version files exist
if [ ${evaltag} == 0 -a ${zxrs} == 0 ] ;  then
    # Get values from the reference tag
    Mo_stack_ms=$( (awk '{print $5/(1024*1024) }' temp_rs_sck ) )
    Mo_stati_ms=$( (awk '{print $5/(1024*1024) }' temp_rs_sic ) )

    # Print: Variable long name | current version values | reference version values | variations
    echo "                                                 " >> ${zout}
    echo "                                      Version    " >> ${zout}
    echo "             Memory          Current /       \ ${ztag}        Variation " >> ${zout}
    awk ' { val=$5/(1024*1024) } {printf("%-9s %-16s %8.1f      |   %8.1f     |    %5.1f \n ", " ", "Stack     (Mo) :", val, inck, ((val-inck)/inck)*100 ) }' inck=${Mo_stack_ms} temp_cs_sck >> ${zout}
    awk ' { val=$5/(1024*1024) } {printf("%-8s %-16s %8.1f      |   %8.1f     |    %5.1f \n ", " ", "Static    (Mo) :", val, inti, ((val-inti)/inti)*100 ) }' inti=${Mo_stati_ms} temp_cs_sic >> ${zout}
else
    # Print: Variable long name | current version values
    echo "                                                 " >> ${zout}
    echo "             Memory          Current version "     >> ${zout}
    awk ' {printf("%-9s %-.16s %11.1f\n ", " ", "Stack     (Mo) :", $5/(1024*1024) ) }' temp_cs_sck >> ${zout}
    awk ' {printf("%-8s %-.16s %11.1f\n ", " ", "Static    (Mo) :", $5/(1024*1024) ) }' temp_cs_sic >> ${zout}
fi

