#!/bin/bash

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

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

HELP="
version: $VERSION
usage:   $TOOL <working dir> -- <pagai option>
options:
  -v/-vv     verbose/more verbose
  - any option accepted by pagai (run pagai to see them)
input: a working dir _cnt-FILE-FUNC, properly initialized
  with cnt_init, that MUST contain:
  - $(n_cptr file)
  - $(n_cptrloc file)
  - $(n_elf file)
output:
  - $(n_cptr2bb file) (not supposed to be used elsewhere ?)
  - pagai.time        elapsed time taken by pagai
requires:
  - pagai
"

function usage {
   echo "$HELP"
}

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

#get ODIR
if [ $# -lt 1 ]; then
   usage ; exit 0
fi
ODIR=${1%/}
shift
# all the rest are pagai options
## DEBUG cptr2bb only !
CPTR2BB_OPT=

PAGAI_OPTS=
for arg in "$@"; do
   case $arg in
## DEBUG cptr2bb only !
   	-nostrictcfg) CPTR2BB_OPT+=" -nostrictcfg";;
      -h|-help|--help) usage ; exit 0;;
   	-v)       VERBOSE=-v ;;
   	-vv|--vv) VERBOSE=-vv ;;
      *)                   PAGAI_OPTS+=" $arg" ;;
   esac
done

echo "ODIR=$ODIR"
echo "PAGAI_OPTS=$PAGAI_OPTS"

#-------------------------------
# 0 - GET WORKING DIR
#     deduce BASE and FUNC
#     check necessary files ...
#-------------------------------

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

REF_BASE="ref_$BASE"

# CPTR_REF, CPTRLOC, ELF
# are shared names (see utils)
checkfile "$ODIR/$CPTR_REF" "Error: cptr c file \"%s\" is missing"
checkfile "$ODIR/$CPTRLOC" "Error: cptrloc file \"%s\" is missing"
checkfile "$ODIR/$ELF" "Error: elf file \"%s\" is missing"

#============================================
# 1 - CALL pagai
#============================================
# PAGAI_RES is a shared name (see utils)

rm -f "$ODIR/$PAGAI_RES"
set -x
(cd "$ODIR"; /usr/bin/time -f %E -o pagai.time pagai --main "$FUNC" -i "$CPTR_REF" $PAGAI_OPTS -o "$PAGAI_RES") 
ec=$?
set +x
checkcode $ec "Error: pagai failed, abort ..."
# CERTAINLY AN (HIDDEN) error 
# if $PAGAI_RES is smaller than $CPTR_REF
psz=`cat "$ODIR/$PAGAI_RES" | wc -c`
rsz=`cat "$ODIR/$CPTR_REF" | wc -c`
# keep this ...
if [ ! -s "$ODIR/$PAGAI_RES" ]; then
	error "Error: $ODIR/$PAGAI_RES is empty, pagai probably failed ..."
fi
if [ $psz -lt $rsz ]; then
	error "Error: $PAGAI_RES smaller than $CPTR_REF, pagai probably failed ..."
fi

echo "-> pagai done" 
echo "--------------------"

#============================================
# 2 - update "cptr2bb" if needed, i.e. :
#     - it does not exists
#     - or it is older than elf
#     - or it is older than cptrloc
#============================================
#CPTR2BB is a shared name (see utils)

if \
true || \
   [ ! -f "$ODIR/$CPTR2BB" ] \
|| [ ! -s "$ODIR/$CPTR2BB" ] \
|| [ "$ODIR/$CPTR2BB" -ot "$ODIR/$CPTRLOC" ] \
|| [ "$ODIR/$CPTR2BB" -ot "$ODIR/$ELF" ]; then
	echo "-> (re) building $CPTR2BB"
	set -x
	(cd "$ODIR"; cptr2bb "$ELF" "$CPTRLOC" -f "$FUNC" $VERBOSE $CPTR2BB_OPT > "$CPTR2BB" && cat "$CPTR2BB")
	ec=$?
	set +x
	checkcode $ec "Error: \"cptr2bb $ELF $CPTRLOC\" failed, abort ..."
	echo "-> $CPTR2BB done"
	echo "-----------------------"
fi


#============================================
# 3 - convert pagai invariants into 
#     (otawa-compliant) BB ilp constraints
# uses: pagai2lp
#============================================
#PAGAI_INVS is a shared name (see utils)

checkcommand pagai2lp "Error: command \"%s\" is missing"

(cd "$ODIR"; \
pagai2lp -vv "$CPTR2BB" "$PAGAI_RES" "$FUNC" $VERBOSE 2> "$PAGAI2LPLOG" > "$PAGAI_INVS";\
checkret "Error: \"pagai2lp -v $CPTR2BB $PAGAI_RES $FUNC\" failed, abort ..."
) 
code=$?
cat "$ODIR/$PAGAI2LPLOG"
if [ $code != 0 ]; then
	exit $code
fi

echo -n "ELAPSED TIME (PAGAI): "
cat $ODIR/pagai.time

