#!/bin/bash

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

## TOOL / USAGE / OPTION
TOOL=cnt_all
VERSION=0.3

HELP="
version: $VERSION
usage:   $TOOL file.c fun <options>
Runs all the steps of the counter method:
  init, bounds, pagai, owcet and lp
options:
   -ld                   long dir name with ~ all options passed to this script
   -dir                  just print the working dir and stop
   -v/-vv                verbose/more verbose
   -inline      (init)   recursively inline the function
   -noswitch    (init)   rewrite switches
   -nops        (init)   add extra nop's to ease C/bin traceability
   -xcount      (init)   experimental counter insertion
   -force       (init)   remove existing project, no prompt
	-eabi        (init)   use arm-none-eabi-gcc (dflt arm-elf-gcc) 
   -O0|-O1|-O2  (init)   optimization level for gcc (dflt -O0)
   -CO1|-CO2    (init)   more customized optimization level for gcc (dflt -O0)
   -l<lib>      (init)   library for gcc
   -I<dir>      (init)   include dir for gcc
   -nobounds             skip bounds step
   -orange      (bounds) use orange, incompatible with -nobounds
   -noprag      (bounds) dont use pragmas for seaching bounds
   -strict      (bounds) stop if unbounded loops remain
   -lmargin <n> (bounds) accept a margin of -<n> lines for loop head position (dflt 3)
   -noffx       (owcet)  don't USE the ffx files (if any)
   -nopagai              skip pagai step
   --           (pagai)  any option accepted by pagai
"

function usage {
   echo -e "$HELP"
}

# find BASE and  FUNC
if [ $# -lt 2 ]; then
	usage ; exit 0
fi
CFILE=$1
shift
#if [ ! -f "$CFILE" ]; then
#	error "Error: can't open file \"$CFILE\""
#fi
BASE=`basename "$CFILE" .c`
FUNC=$1
shift


# dispatch args
DO_BOUNDS=true
DO_PAGAI=true
INIT_OPTS=
BOUNDS_OPTS=
OWCET_OPTS=
PAGAI_OPTS=
LP_OPTS=
VERBOSE=

LONGDIR=false
ECHO_DIR=false

#for arg in "$@"; do
#	case $arg in
while true ; do
   arg="$1"
	case "$arg" in
		"") break;;
		-h|-help|--help) usage ; exit 0;;
		-ld)                 LONGDIR=true;;
		-dir)                ECHO_DIR=true;;
		-nobounds)           DO_BOUNDS=false;;
		-nopagai)            DO_PAGAI=false;;
		-i|-inline|--inline) INIT_OPTS+=" $arg" ;; 
		-noswitch|--noswitch) INIT_OPTS+=" $arg" ;; 
		-nops|--nops)        INIT_OPTS+=" $arg" ;; 
		-xcount|--xcount)    INIT_OPTS+=" $arg" ;; 
		-f|-force|--force)   INIT_OPTS+=" $arg" ;;
		-eabi)               INIT_OPTS+=" $arg" ;; 
		-O0|--O0)            INIT_OPTS+=" $arg" ;; 
		-O1|--O1)            INIT_OPTS+=" $arg" ;; 
		-O2|--O2)            INIT_OPTS+=" $arg" ;; 
		-CO1|--CO1)          INIT_OPTS+=" $arg" ;; 
		-CO2|--CO2)          INIT_OPTS+=" $arg" ;; 
      -lmargin)            shift; BOUNDS_OPTS+=" -lmargin $1" ;;
		-l*)                 INIT_OPTS+=" $arg" ;; 
		-I*)                 INIT_OPTS+=" $arg" ;; 
		-nostrictcfg)        INIT_OPTS+=" $arg"; PAGAI_OPTS+=" $arg" ;;
		-orange|--orange)    BOUNDS_OPTS+=" $arg" ;;
		-noprag|--noprag)    BOUNDS_OPTS+=" $arg" ;;
		-strict|--strict)    BOUNDS_OPTS+=" $arg" ;;
		-noffx|--noffx)      OWCET_OPTS+=" $arg" ;;
		-v)                  VERBOSE="-v" ;;
		-vv)                 VERBOSE="-vv" ;;
		*)                   PAGAI_OPTS+=" $arg" ;;
	esac
   shift
done

# if $LONGDIR, BOUNDS_OPTS, OWCET_OPTS, PAGAI_OPTS are concatenated
# to give a -ds (dir suffix) opt to init

if $LONGDIR ; then
	SFX=${BOUNDS_OPTS}${INIT_OPTS}${OWCET_OPTS}${PAGAI_OPTS}
	#replace white spaces with _
	SFX="$(echo -e "${SFX}" | tr -d '[[:space:]]')"
	INIT_OPTS+=" -ds $SFX" 
fi

## GENERATE WORKING DIR NAME
ODIR=`cnt_init "$CFILE" "$FUNC" -dir $INIT_OPTS`
## STOP HERE ?
if $ECHO_DIR ; then
	echo $ODIR
	exit 0
fi

## CHECK IF CFILE EXISTS
if [ ! -f "$CFILE" ]; then
	error "Error: can't open file \"$CFILE\""
fi

echo "INIT_OPTS=$INIT_OPTS"
echo "BOUNDS_OPTS=$BOUNDS_OPTS"
echo "OWCET_OPTS=$OWCET_OPTS"
echo "PAGAI_OPTS=$PAGAI_OPTS"

echo
chcolor bold blue
echo "######################################"
echo "#    INIT STEP                       #"
echo "######################################"
chcolor normal 
echo
cnt_init "$CFILE" "$FUNC" $INIT_OPTS $VERBOSE
checkret  "Error: init step failed, abort ..."


if $DO_BOUNDS ; then
	echo
	chcolor bold blue
	echo "######################################"
	echo "#    BOUNDS STEP                     #"
	echo "######################################"
	chcolor normal 
	echo
	cnt_bounds "$ODIR" $BOUNDS_OPTS $VERBOSE
	checkret  "Error: bounds step failed, abort ..."
else
	echo
	chcolor bold blue
	echo "######################################"
	echo "#    BOUNDS STEP SKIPED              #"
	echo "######################################"
	chcolor normal 
	echo
fi

echo
chcolor bold blue
echo "######################################"
echo "#    OWCET STEP                      #"
echo "######################################"
chcolor normal 
echo
cnt_owcet "$ODIR" $OWCET_OPTS $VERBOSE
checkret  "Error: owcet step failed, abort ..."

if $DO_PAGAI ; then
	echo
	chcolor bold blue
	echo "######################################"
	echo "#    PAGAI STEP                      #"
	echo "######################################"
	chcolor normal 
	echo
	cnt_pagai "$ODIR" $PAGAI_OPTS $VERBOSE
	checkret  "Error: pagai step failed, abort ..."

	echo
	chcolor bold blue
	echo "######################################"
	echo "#    LP (FINAL) STEP                 #"
	echo "######################################"
	chcolor normal 
	echo
	if $DO_BOUNDS ; then
		echo "#--- BOUNDS STEP SUMMARY -----"
		cat "$ODIR/$BOUNDSLOG"	
	fi
	# problems with pagai ?
	pwarns=`cat "$ODIR/$PAGAI2LPLOG" | grep "WARNING" | wc -l`
	if [ $pwarns != 0 ]; then
		chcolor bold red
		echo "#--- PAGAI WARNING DETECTED ----"
		echo "#(see $ODIR/$PAGAI2LPLOG for details)"
		cat "$ODIR/$PAGAI2LPLOG" | grep "WARNING"  
		echo "#--------------------------------"
		chcolor normal
		echo
	fi
	cnt_lp "$ODIR" $LP_OPTS $VERBOSE
	checkret  "Error: lp step failed, abort ..."
else
	echo
	chcolor bold blue
	echo "######################################"
	echo "#    PAGAI STEP SKIPED              #"
	echo "######################################"
	chcolor normal 
	echo
fi
