#!/bin/bash

IDIR=`dirname $0`
source "$IDIR"/cnt_utils.sh

## TOOL / USAGE / OPTION
TOOL=cnt_lp
VERSION=0.1

HELP="
version: $VERSION
usage:   $TOOL <working dir> <option>
options:
   -v/-vv   verbose/more verbose
input: a working dir _cnt-FILE-FUNC,
  - initialized with cnt_init
  - possibly completed with cnt_bounds,
  - completed with cnt_pagai
  - completed with cnt_owcet
THAT MUST CONTAIN:
  - $(n_lp $1)
  - $(n_pagai_invs $1)
output:
requires:
  - owcet.arm
"

function usage {
   echo "$HELP"
}

USE_FFX=true
DO_LP=true
VERBOSE=

OPTS=`getopt -a -o hv --long noffx,vv,help -n '$TOOL' -- "$@"`
checkret "Error: try '$TOOL -h' for usage"

eval set -- "$OPTS"
while true ; do
case "$1" in
	-h|--help) usage ; exit 0;;
   -v)       VERBOSE=-v; shift ;;
   -vv|--vv) VERBOSE=-vv; shift ;;
	--) shift ; break ;;
esac
done

if [ $# != 1 ]; then
	usage ; exit 0
fi


#-------------------------------
# 0 - GET WORKING DIR
#     deduce BASE and FUNC
#     check necessary files ...
#-------------------------------
ODIR=${1%/}
if [ ! -d "$ODIR" ]; then
	error "Error: can't open dir \"$ODIR\""
fi

# split ODIR
BASE=`base_of_workdir "$ODIR"`
FUNC=`func_of_workdir "$ODIR"`


# INIT ALL VARS
init_vars "$BASE"

echo BASE=$BASE
echo FUNC=$FUNC

checkfile "$ODIR/$PAGAI_INVS" "Error: file \"%s\" is missing"
checkfile "$ODIR/$LP" "Error: file \"%s\" is missing"

#============================================
# 1 - Run lp_solve on the lp (probably re-run !)
# -> use lp_solve_int a home-made hack to 
#    print result as integer even if it is "big"
# e.g. 
# Value of objective function: 8749538
# and not:
# Value of objective function: 8.74954e+06
#============================================

LP_SOLVE=lp_solve_int
checkcommand "$LP_SOLVE" "Error: command \"%s\" is missing"

BASIC_WCET=basic.wcet
(cd  "$ODIR"; "$LP_SOLVE" -S1 "$LP" > "$BASIC_WCET"  )
LPRET0=$?
#checkret "Error: \"$LP_SOLVE -S1 $LP\" failed, abort..."

if [ $LPRET0 = 0 ]; then
wcet0=`cat $ODIR/$BASIC_WCET | grep -i objective | sed -e "s/^.* \([0-9]*\)$/\1/"`
else
	echo "# WARNING: \"$LP_SOLVE -S1 $LP\" failed"
	wcet0=`cat $ODIR/$BASIC_WCET`
fi

#============================================
# 2 - Run lp_solve on the lp (probably re-run !)
#============================================
PAGAI_WCET=pagai.wcet

PAGAI_LP="ref_$BASE.pagai.lp"
checkcommand pagai2lp "Error: command \"%s\" is missing"
(cd "$ODIR";  pagai2lp "$CPTR2BB" "$PAGAI_RES" "$FUNC" -add "$LP" $VERBOSE > "$PAGAI_LP")
checkret "Error: pagai2lp failed, abort ..."

(cd  "$ODIR"; "$LP_SOLVE" -S1 "$PAGAI_LP" > "$PAGAI_WCET" )
LPRET1=$?
#checkret "Error: \"$LP_SOLVE -S1 $PAGAI_LP\" failed, abort..."

if [ $LPRET1 = 0 ]; then
wcet1=`cat $ODIR/$PAGAI_WCET | grep -i objective | sed -e "s/^.* \([0-9]*\)$/\1/"`
else
	echo "# WARNING: \"$LP_SOLVE -S1 $PAGAI_LP\" failed"
	wcet1=`cat $ODIR/$PAGAI_WCET` 
fi

#2017/08 - give some information on traceability
CPTR2BB_LOG=$ODIR/ref_$BASE.cptr2bb
NB_C_BLOCKS=`cat $CPTR2BB_LOG | wc -l`
NB_TRACED_BLOCKS=`cat $CPTR2BB_LOG | grep -v "?" | wc -l`
if [ $NB_C_BLOCKS -gt 0 ]; then
   percent=`echo "$NB_TRACED_BLOCKS * 100 / $NB_C_BLOCKS" | bc -l`
	printf "Traceability: %.0f%% blocks traced (%d out of %d)\n" $percent $NB_TRACED_BLOCKS $NB_C_BLOCKS
fi

echo -n "Elapsed time in owcet: "; cat $ODIR/owcet.time
echo -n "Elapsed time in pagai: "; cat $ODIR/pagai.time
chcolor bold blue 
echo "Estimation WITHOUT PAGAI: \"$wcet0\""
echo "Estimation WITH    PAGAI: \"$wcet1\""

(expr $wcet0 + $wcet1 &> /dev/null) && \
      gain=`echo "( ($wcet0 - $wcet1) * 100 ) / $wcet0" | bc -l` && \
      printf "Gain: %4.2f%%\n" $gain

chcolor normal



