#!/bin/bash
#
# Smoke tests for #9727: -? syntax help and clean exits on early fatal errors in
# ConversionDriver, ProgressTransformDriver and ReportDriver.
#
# Usage: test_9727.sh [fwd_branch_dir] [project_dir]
#   fwd_branch_dir  FWD branch with a completed build (default ~/projects/fwd/9727a)
#   project_dir     converted project with cfg/p2j.cfg.xml (default ~/projects/hotel_gui_dev)
#
# A full conversion and a report run are not done here; run those separately.

FWD=${1:-~/projects/fwd/9727a}
PROJ=${2:-~/projects/hotel_gui_dev}
CP="$FWD/build/lib/*"

CD=com.goldencode.p2j.convert.ConversionDriver
PTD=com.goldencode.p2j.convert.ProgressTransformDriver
RD=com.goldencode.p2j.report.ReportDriver

WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT

PASS=0
FAIL=0

# check <description> <expected rc> <help stream: out|err|none> <expected text or ""> <dir> <class> [args...]
check()
{
   local desc=$1 exp_rc=$2 help=$3 text=$4 dir=$5
   shift 5

   (cd "$dir" && timeout 120 java -cp "$CP" -DP2J_HOME=. "$@" > "$WORK/out" 2> "$WORK/err")
   local rc=$?
   local why=""

   [ "$rc" -eq "$exp_rc" ] || why="rc=$rc (expected $exp_rc)"
   cat "$WORK/out" "$WORK/err" > "$WORK/all"

   case $help in
      out) grep -q "^Syntax:" "$WORK/out" || why="$why; no help on stdout" ;;
      err) grep -q "^Syntax:" "$WORK/err" || why="$why; no help on stderr" ;;
   esac

   if [ -n "$text" ] && ! grep -qF -- "$text" "$WORK/all"; then
      why="$why; missing \"$text\""
   fi
   if grep -q "Exception" "$WORK/all"; then
      why="$why; exception in output"
   fi
   if grep -q "Syntax:_" "$WORK/all"; then
      why="$why; help text flattened by the logger"
   fi

   if [ -z "$why" ]; then
      PASS=$((PASS + 1))
      echo "PASS  $desc"
   else
      FAIL=$((FAIL + 1))
      echo "FAIL  $desc: ${why#; }"
      sed 's/^/        | /' "$WORK/all" | head -8
   fi
}

if ! ls $CP > /dev/null 2>&1; then
   echo "No jars found in $FWD/build/lib; build the branch first."
   exit 2
fi

# 1. no project at all (no cfg/ directory)
NOPROJ="$WORK/noproj"
mkdir -p "$NOPROJ"
echo "--- no project ($NOPROJ)"
check "CD -?"                    0   out  ""                                  "$NOPROJ" $CD  '-?'
check "PTD -?"                   0   out  ""                                  "$NOPROJ" $PTD '-?'
check "RD -?"                    0   out  ""                                  "$NOPROJ" $RD  '-?'
check "CD -D2 -?"                0   out  ""                                  "$NOPROJ" $CD  -D2 '-?'
check "PTD -D2 -?"               0   out  ""                                  "$NOPROJ" $PTD -D2 '-?'
check "RD -D2 -?"                0   out  ""                                  "$NOPROJ" $RD  -D2 '-?'
check "CD no args"               255 err  ""                                  "$NOPROJ" $CD
check "PTD no args"              255 err  ""                                  "$NOPROJ" $PTD
check "CD options only"          252 err  "Missing mode parameter."           "$NOPROJ" $CD  -D2
check "PTD options only"         252 err  "Missing mode parameter."           "$NOPROJ" $PTD -D2
check "CD no config"             249 err  "Unable to load the project configuration" "$NOPROJ" $CD  F2 x.p
check "PTD no config"            249 err  "Unable to load the project configuration" "$NOPROJ" $PTD F2 x.p
check "RD no args, no config"    248 err  "Unable to load the project configuration" "$NOPROJ" $RD
check "RD -D2, no config"        248 err  "Unable to load the project configuration" "$NOPROJ" $RD  -D2

# 2. cfg/ directory without p2j.cfg.xml
NOCFG="$WORK/nocfg"
mkdir -p "$NOCFG/cfg"
echo "--- cfg/ without p2j.cfg.xml ($NOCFG)"
check "CD missing p2j.cfg.xml"   249 err  "No data file found"                "$NOCFG"  $CD  F2 x.p
check "RD missing p2j.cfg.xml"   248 err  "No data file found"                "$NOCFG"  $RD

# 3. real project (regression)
if [ -f "$PROJ/cfg/p2j.cfg.xml" ]; then
   echo "--- real project ($PROJ)"
   check "CD -? with config"     0   out  ""                                  "$PROJ"   $CD  '-?'
   check "CD dry run (-SL F0)"   0   none ""                                  "$PROJ"   $CD  -SL F0 abl "*.p"
   check "CD bad mode"           251 err  "Unrecognized mode 'XX'."           "$PROJ"   $CD  XX abl/some.p
else
   echo "--- skipping real project checks, no $PROJ/cfg/p2j.cfg.xml"
fi

echo
echo "passed: $PASS  failed: $FAIL"
[ "$FAIL" -eq 0 ]
