#!/bin/bash
# -*- coding: utf-8 -*-
# Regression tests on z0lib
#
# Based on template 2.1.1
[ $BASH_VERSINFO -lt 4 ] && echo "This script $0 requires bash 4.0+!" && exit 4
READLINK=$(which readlink 2>/dev/null)
export READLINK
THIS=$(basename "$0")
TDIR=$(readlink -f $(dirname $0))
ME=$(readlink -e $0)
if [[ -d $HOME/devel || -z $HOME_DEVEL || ! -d $HOME_DEVEL ]]; then
  [[ -d $HOME/odoo/devel ]] && HOME_DEVEL="$HOME/odoo/devel" || HOME_DEVEL="$HOME/devel"
fi
PYPATH=""
[[ $(basename $PWD) == "tests" && $(basename $PWD/../..) == "build" ]] && PYPATH="$(dirname $PWD)"
[[ $(basename $PWD) == "tests" && $(basename $PWD/../..) == "build" && -d $PWD/../scripts ]] && PYPATH="$PYPATH $(readlink -f $PWD/../scripts)"
x=$ME; while [[ $x != $HOME && $x != "/" && ! -d $x/lib && ! -d $x/bin && ! -d $x/pypi ]]; do x=$(dirname $x); done
[[ -d $x/pypi ]] && PYPATH="$PYPATH $x/pypi"
[[ -d $x/pypi/z0lib ]] && PYPATH="$PYPATH $x/pypi/z0lib"
[[ -d $x/pypi/z0lib/z0lib ]] && PYPATH="$PYPATH $x/pypi/z0lib/z0lib"
[[ -d $x/tools ]] && PYPATH="$PYPATH $x/tools"
[[ -d $x/tools/z0lib ]] && PYPATH="$PYPATH $x/tools/z0lib"
[[ -d $x/bin ]] && PYPATH="$PYPATH $x/bin"
[[ -d $x/lib ]] && PYPATH="$PYPATH $x/lib"
[[ -d $HOME_DEVEL/venv/bin ]] && PYPATH="$PYPATH $HOME_DEVEL/venv/bin"
[[ -d $HOME_DEVEL/../tools ]] && PYPATH="$PYPATH $(readlink -f $HOME_DEVEL/../tools)"
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "PYPATH=$PYPATH"
for d in $TDIR $PYPATH /etc; do
  if [[ -e $d/z0librc ]]; then
    . $d/z0librc
    Z0LIBDIR=$(readlink -e $d)
    break
  fi
done
[[ -z "$Z0LIBDIR" ]] && echo "Library file z0librc not found in <$PYPATH>!" && exit 72
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "Z0LIBDIR=$Z0LIBDIR"
TESTDIR=$(findpkg "" "$TDIR . .." "tests")
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "TESTDIR=$TESTDIR"
RUNDIR=$(readlink -e $TESTDIR/..)
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "RUNDIR=$RUNDIR"
Z0TLIBDIR=$(findpkg z0testrc "$PYPATH" "zerobug")
[[ -z "$Z0TLIBDIR" ]] && echo "Library file z0testrc not found!" && exit 72
. $Z0TLIBDIR
Z0TLIBDIR=$(dirname $Z0TLIBDIR)
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "Z0TLIBDIR=$Z0TLIBDIR"

# DIST_CONF=$(findpkg ".z0tools.conf" "$PYPATH")
# TCONF="$HOME/.z0tools.conf"
CFG_init "ALL"
link_cfg_def
link_cfg $DIST_CONF $TCONF
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "DIST_CONF=$DIST_CONF" && echo "TCONF=$TCONF"
get_pypi_param ALL
RED="\e[1;31m"
CYAN="\e[1;36m"
GREEN="\e[1;32m"
CLR="\e[0m"

__version__=2.0.9


PASSED=0

store_cfg_param_value() {
#store_cfg_param_value(key value tid [-f|-d] [-D|1] [section] )
    if [[ $5 =~ (-D|1) ]] ; then
      PASSED=2
    else
      PASSED=1
    fi
    CFG_set "$1" "$2" "$3" "$4" "$5" "$6"
}

test_01() {
    for tid in 0 1 2 3; do
      # Test w/o debug
      CFG_init $tid
      FCONF="$TESTDIR/test.conf"
      if [ ${opt_dry_run:-0} -eq 0 ]; then
        link_cfg $FCONF "" $tid
      fi
      TRES=$(get_cfg_value $tid "param1")
      test_result "get_cfg_value $tid param1" "Right" "$TRES"
      test_result "ext function called ($tid)" "1" "$PASSED"
      PASSED=0
      TRES=$(get_cfg_value $tid "param2")
      test_result "get_cfg_value $tid param2" "NoDebug" "$TRES"
    done
}

test_02() {
    for tid in 0; do
      # Test with debug (1)
      CFG_init $tid
      FCONF="$TESTDIR/test.conf"
      if [ ${opt_dry_run:-0} -eq 0 ]; then
        link_cfg $FCONF "" $tid "" "1"
      fi
      TRES=$(get_cfg_value $tid "param1")
      test_result "get_cfg_value $tid param1 1" "Right" "$TRES"
      TRES=$(get_cfg_value $tid "param2")
      test_result "get_cfg_value $tid param2 1" "Debug" "$TRES"
      test_result "ext function called" "2" "$PASSED"
      PASSED=0
    done
    for tid in 0; do
      # Test with debug (-D)
      CFG_init $tid
      FCONF="$TESTDIR/test.conf"
      if [ ${opt_dry_run:-0} -eq 0 ]; then
        link_cfg $FCONF "" $tid "" "-D"
      fi
      TRES=$(get_cfg_value $tid "param1")
      test_result "get_cfg_value $tid param1 -D" "Right" "$TRES"
      TRES=$(get_cfg_value $tid "param2")
      test_result "get_cfg_value $tid param2 -D" "Debug" "$TRES"
      test_result "ext function called" "2" "$PASSED"
      PASSED=0
      TRES=$(get_cfg_value $tid "LOCAL_PKGS")
      test_result "get_cfg_value $tid LOCAL_PKGS" "arcangelo clodoo lisa odoo_score os0 oerplib3 python_plus travis_emulator wok_code z0bug_odoo z0lib zar zerobug" "$TRES"
    done
}

Z0BUG_setup() {
    FCONF="$TESTDIR/test.conf"
    if [ ${opt_dry_run:-0} -eq 0 ]; then
      if [ -f $FCONF ]; then rm -f $FCONF; fi
      echo "#">$FCONF.sample
      echo "param1=Invalid">>$FCONF
      local FH=$(xuname "-f")
      local x=$(xuname "-v")
      local v=$(echo $x|awk -F. '{print $1}')
      local DISTO=$(xuname "-d")$v
      echo "">>$FCONF
      echo "[_${DISTO}_]">>$FCONF
      echo "param1=Right">>$FCONF
      echo "param2=NoDebug">>$FCONF
      echo "[_${DISTO}_DEV_]">>$FCONF
      echo "param2=Debug">>$FCONF
      echo "">>$FCONF
      if [ "$DISTO" == "CentOS7" ]; then
        echo "[_Ubuntu14_]">>$FCONF
      else
        echo "[_CentOS7_]">>$FCONF
      fi
      echo "param1=Wrong">>$FCONF
      echo "param2=WrongDebug">>$FCONF
    fi
}


Z0BUG_init
parseoptest -l$TESTDIR/test_z0lib.log "$@"
sts=$?
[[ $sts -ne 127 ]] && exit $sts
for p in z0librc odoorc travisrc zarrc z0testrc; do
  if [[ -f $RUNDIR/$p ]]; then
    [[ $p == "z0librc" ]] && Z0LIBDIR="$RUNDIR" && source $RUNDIR/$p
    [[ $p == "odoorc" ]] && ODOOLIBDIR="$RUNDIR" && source $RUNDIR/$p
    [[ $p == "travisrc" ]] && TRAVISLIBDIR="$RUNDIR" && source $RUNDIR/$p
    [[ $p == "zarrc" ]] && ZARLIB="$RUNDIR" && source $RUNDIR/$p
    [[ $p == "z0testrc" ]] && Z0TLIBDIR="$RUNDIR" && source $RUNDIR/$p
  fi
done



UT1_LIST=
UT_LIST=
[[ "$(type -t Z0BUG_setup)" == "function" ]] && Z0BUG_setup
Z0BUG_main_file "$UT1_LIST" "$UT_LIST"
sts=$?
[[ "$(type -t Z0BUG_teardown)" == "function" ]] && Z0BUG_teardown
exit $sts
