#! /bin/bash
# -*- coding: utf-8 -*-

READLINK=$(which greadlink 2>/dev/null) || READLINK=$(which readlink 2>/dev/null)
export READLINK
# Based on template 2.0.0
THIS=$(basename "$0")
TDIR=$(readlink -f $(dirname $0))
[ $BASH_VERSINFO -lt 4 ] && echo "This script $0 requires bash 4.0+!" && exit 4
if [[ -z $HOME_DEVEL || ! -d $HOME_DEVEL ]]; then
  [[ -d $HOME/odoo/devel ]] && HOME_DEVEL="$HOME/odoo/devel" || HOME_DEVEL="$HOME/devel"
fi
[[ -x $TDIR/../bin/python3 ]] && PYTHON=$(readlink -f $TDIR/../bin/python3) || [[ -x $TDIR/python3 ]] && PYTHON="$TDIR/python3" || PYTHON=$(which python3 2>/dev/null) || PYTHON="python"
[[ -z $PYPATH ]] && PYPATH=$(echo -e "import os,sys\no=os.path\na=o.abspath\nj=o.join\nd=o.dirname\nb=o.basename\nf=o.isfile\np=o.isdir\nC=a('"$TDIR"')\nD='"$HOME_DEVEL"'\nif not p(D) and '/devel/' in C:\n D=C\n while b(D)!='devel':  D=d(D)\nN='venv_tools'\nU='setup.py'\nO='tools'\nH=o.expanduser('~')\nT=j(d(D),O)\nR=j(d(D),'pypi') if b(D)==N else j(D,'pypi')\nW=D if b(D)==N else j(D,'venv')\nS='site-packages'\nX='scripts'\ndef pt(P):\n P=a(P)\n if b(P) in (X,'tests','travis','_travis'):\n  P=d(P)\n if b(P)==b(d(P)) and f(j(P,'..',U)):\n  P=d(d(P))\n elif b(d(C))==O and f(j(P,U)):\n  P=d(P)\n return P\ndef ik(P):\n return P.startswith((H,D,K,W)) and p(P) and p(j(P,X)) and f(j(P,'__init__.py')) and f(j(P,'__main__.py'))\ndef ak(L,P):\n if P not in L:\n  L.append(P)\nL=[C]\nK=pt(C)\nfor B in ('z0lib','zerobug','odoo_score','clodoo','travis_emulator'):\n for P in [C]+sys.path+os.environ['PATH'].split(':')+[W,R,T]:\n  P=pt(P)\n  if B==b(P) and ik(P):\n   ak(L,P)\n   break\n  elif ik(j(P,B,B)):\n   ak(L,j(P,B,B))\n   break\n  elif ik(j(P,B)):\n   ak(L,j(P,B))\n   break\n  elif ik(j(P,S,B)):\n   ak(L,j(P,S,B))\n   break\nak(L,os.getcwd())\nprint(' '.join(L))\n"|$PYTHON)
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "PYPATH=$PYPATH"
for d in $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"
ODOOLIBDIR=$(findpkg odoorc "$PYPATH" "clodoo")
[[ -z "$ODOOLIBDIR" ]] && echo "Library file odoorc not found!" && exit 72
. $ODOOLIBDIR
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "ODOOLIBDIR=$ODOOLIBDIR"

# 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"
GREEN="\e[1;32m"
CLR="\e[0m"

__version__=2.0.1

run_traced_debug() {
    if [ $opt_verbose -gt 1 ]; then
        run_traced "$1"
    elif [ $opt_dry_run -eq 0 ]; then
        eval $1
    fi
}

check_for_modules() {
    local mods r xi xu XXX
    OPTI=
    xi=-i
    OPTU=
    xu=-u
    XXX=
    if [[ $opt_modules == "all" ]]; then
        OPTU="-uall"
    else
        mods=${opt_modules//,/ }
        for m in $mods; do
            r=$(psql -U$DB_USER $opt_db -tc "select state from ir_module_module where name='$m'" 2>/dev/null)
            if [[ $r =~ uninstallable ]]; then
                XXX="$XXX $m"
            elif [[ $r =~ uninstalled ]]; then
                OPTI="$OPTI$xi$m"
                xi=,
            elif [[ $r =~ installed ]]; then
                OPTU="$OPTU$xu$m"
                xu=,
            elif [[ $opt_force -ne 0 ]]; then
                OPTI="$OPTI$xi$m"
                OPTU="$OPTU$xu$m"
                xu=,
            else
                XXX="$XXX $m"
            fi
        done
    fi
    [[ -n $XXX ]] && echo "Modules $XXX not found!" && exit 1
}

coverage_set() {
    if [[ $opt_dry_run -eq 0 && $opt_test -ne 0 && $opt_nocov -eq 0 ]]; then
      [[ ! -d $HOME/coverage ]] && mkdir $HOME/coverage
      COVERAGE_DATA_FILE="$HOME/coverage/${UMLI}"
      COVERAGE_PROCESS_START="$HOME/coverage/${UMLI}rc"
      # [[ -s $COVERAGE_PROCESS_START ]] || coverage_tmpl=$(find $PYPATH -name coveragerc|head -n 1)
      coverage_tmpl=$(find $PYPATH -name coveragerc|head -n 1)
      # [[ -n "$coverage_tmpl" ]] && cp $coverage_tmpl $COVERAGE_PROCESS_START
      cp $coverage_tmpl $COVERAGE_PROCESS_START
      grep -Eq "^data_file *=" $COVERAGE_PROCESS_START || sed -E "/^\[run\]/a\\\ndata_file=$COVERAGE_DATA_FILE\n" -i $COVERAGE_PROCESS_START
    fi
}

coverage_report() {
     if [[ -n $COVERAGE_PROCESS_START ]]; then
      opts=""
      for m in ${opt_modules//,/ }; do
        opts="$opts --include=$(find $odoo_root -type d -not -path "*/setup/*" -name $m)/*"
      done
      v=$(coverage --version|grep --color=never -Eo "[0-9]+"|head -n1)
      [[ $v -ge 6 ]] && coverage report --rcfile=$COVERAGE_PROCESS_START --data-file=$COVERAGE_DATA_FILE $opts -m || coverage report --rcfile=$COVERAGE_PROCESS_START $opts -m
    fi
}


OPTOPTS=(h        B       b          c        C           d        e       f         k        i       I       l        L        m           M         n           o         O      p        P         q           S        s        T        t         U          u       V           v           w       W        x)
OPTLONG=(help     debug   branch     conf     no-coverage database export  force     keep     import  install lang     lint-lev modules     multi     dry-run     ""        ""     path     psql-port quiet       stat     stop     test     ""        db-user    update  version     verbose     web     ""       xmlrpc-port)
OPTDEST=(opt_help opt_dbg opt_branch opt_conf opt_nocov   opt_db   opt_exp opt_force opt_keep opt_imp opt_xtl opt_lang opt_llvl opt_modules opt_multi opt_dry_run opt_ofile opt_ou opt_odir opt_qport opt_verbose opt_stat opt_stop opt_test opt_touch opt_dbuser opt_upd opt_version opt_verbose opt_web opt_venv opt_rport)
OPTACTI=("+"      "+"     "=>"       "=>"     1           "="      1       1         1        1       1       1        "="      "="         1         1           "="       1      "="      "="       0           1        1        1        1         "="        1       "*>"        "+"         1       1        "=")
OPTDEFL=(1        0       ""         ""       0           ""       0       0         0        0       0       0        ""       ""          -1        0           ""        0      ""       ""        0           0        0        0        0         ""         0       ""          -1          0       0        "")
OPTMETA=("help"   ""      "version"  "fname"  ""          "name"   ""      ""        ""       ""      ""      ""       "level"  "modules"   ""        "no op"     "file"    ""     "dir"    "port"    ""          ""       ""       ""       "touch"   "user"     ""      "version"   "verbose"   0       ""       "port")
OPTHELP=("this help"
    "debug mode (-BB debug via pycharm)"
    "odoo branch (version id)"
    "odoo configuration file"
    "no use coverage to run test"
    "db name to test,translate o upgrade (require -m switch)"
    "export translation (conflict with -i -u -I)"
    "force update or install modules or default parameters or create db template"
    "do not create new DB and keep it after run"
    "import translation (conflict with -e -u -I)"
    "install module (conflict with -e -i -u)"
    "load language"
    "set log level: may be info or debug"
    "modules to test, translate or upgrade"
    "multi-version odoo environment"
    "do nothing (dry-run)"
    "output file (if export multiple modules)"
    "use openupgrade, if avaiable"
    "odoo root path"
    "psql port"
    "silent mode"
    "show coverage stats (do not run odoo)"
    "stop after init"
    "execute odoo test on module"
    "touch config file, do not run odoo"
    "db username"
    "upgrade module (conflict with -e -i -I)"
    "show version"
    "verbose mode"
    "run as web server"
    "run virtualenv if avaiable"
    "set odoo xmlrpc port")
OPTARGS=()

parseoptargs "$@"
if [[ "$opt_version" ]]; then
    echo "$__version__"
    exit 0
fi
if [[ $opt_help -gt 0 ]]; then
    print_help "Run odoo for debug mode" \
        "(C) 2015-2022 by zeroincombenze(R)\nhttps://zeroincombenze-tools.readthedocs.io/\nAuthor: antoniomaria.vigliotti@gmail.com"
    exit 0
fi

discover_multi
CONFN=""
opaths=""
odoo_root=""
odoo_fver=""
[[ -z $MQT_TEMPLATE_DB ]] && MQT_TEMPLATE_DB="template_openerp"
[[ -z $MQT_TEST_DB ]] && MQT_TEST_DB="test_openerp"
if [[ -n $opt_conf ]]; then
    CONFN=$opt_conf
    [[ ! -f $CONFN ]] && echo "File $CONFN not found!" && exit 1
    opaths="$(grep ^addons_path $CONFN | awk -F= '{print $2}')"
    [[ -z $opaths ]] && echo "No path list found in $CONFN!" && exit 1
    for p in ${opaths//,/ }; do
        [[ -x $p/../odoo-bin || -x $p/../openerp-server ]] && odoo_root=$(readlink -f $p/..) && break
    done
    [[ -n $odoo_root ]] && odoo_fver=$(build_odoo_param FULLVER $odoo_root)
    [[ -n $opt_branch ]] && x=$(build_odoo_param FULLVER $odoo_fver) || x=$odoo_fver
    [[ $odoo_fver != $x ]] && echo "Version mismatch -b $opt_branch <> -c $opt_conf" && exit 1
    [[ -n $opt_odir && $opt_odir != $odoo_root ]] && echo "Path mismatch $odoo_root (-c $opt_conf) <> -p $opt_odir" && exit 1
elif [[ -n $opt_branch ]]; then
    odoo_fver=$(build_odoo_param FULLVER $opt_branch)
    [[ -n "$opt_odir" ]] && odoo_root=$(readlink -f $opt_odir) || odoo_root=$(build_odoo_param ROOT $opt_branch search)
    CONFN=$(build_odoo_param CONFN $odoo_fver search)
    [[ -f $CONFN ]] && opaths="$(grep ^addons_path $CONFN | awk -F= '{print $2}')"
else
    odoo_fver=$(build_odoo_param FULLVER $PWD)
    odoo_root=$(build_odoo_param ROOT $opt_branch search)
    CONFN=$(build_odoo_param CONFN $odoo_fver search)
    [[ -f $CONFN ]] && opaths="$(grep ^addons_path $CONFN | awk -F= '{print $2}')"
fi
[[ -z $odoo_root || ! -d $odoo_root ]] && echo "Odoo path $odoo_root not found!" && exit 1
odoo_ver=$(build_odoo_param MAJVER $odoo_fver)
LCONFN=$(build_odoo_param LCONFN $odoo_fver)
script=$(build_odoo_param BIN $odoo_root search)
[[ -z "$script" ]] && echo "No odoo script found!!" && exit 1
ODOO_RUNDIR=$(dirname $script)
VDIR=$(build_odoo_param VDIR $odoo_root)
[[ $opt_verbose -gt 0 && -n "$VDIR" ]] && echo "# Found $VDIR virtual directory"
GIT_ORGID=$(build_odoo_param GIT_ORGID $odoo_root)
# Unique module log identifier & Unique log identifier
ULI="${GIT_ORGID}${odoo_ver}"
UMLI="${opt_modules//,/+}.${ULI}"
manifest=$(build_odoo_param MANIFEST $odoo_fver)
if [[ -n $opt_rport ]]; then
    RPCPORT=$opt_rport
elif [[ -z $opt_conf && $opt_web -ne 0 ]]; then
    RPCPORT=$(build_odoo_param RPCPORT $odoo_fver $GIT_ORGID)
elif [[ $opt_test -ne 0 ]]; then
    [[ opt_dbg -gt 1 ]] && RPCPORT=$(build_odoo_param RPCPORT $odoo_fver DEBUG) || RPCPORT=$((($(date +%s) % 46000) + 19000))
elif [[ -z $opt_conf || ! -f $CONFN ]]; then
    RPCPORT=$(build_odoo_param RPCPORT $odoo_fver)
elif [[ -f $CONFN ]]; then
    RPCPORT=$(grep ^xmlrpc_port $CONFN | awk -F= '{print $2}' | tr -d " ")
    [[ -z "$RPCPORT" ]] && RPCPORT=$(grep ^http_port $CONFN | awk -F= '{print $2}' | tr -d " ")
else
    RPCPORT=$(build_odoo_param RPCPORT $odoo_fver)
fi
[[ -z "$RPCPORT" || $RPCPORT -eq 0 ]] && RPCPORT=$(build_odoo_param RPCPORT $odoo_fver)
if [[ -n $opt_dbuser ]]; then
    DB_USER=$opt_dbuser
elif [[ -z $opt_conf || ! -f $CONFN ]]; then
    DB_USER=$(build_odoo_param DB_USER $odoo_fver $GIT_ORGID)
elif [[ -f $CONFN ]]; then
    DB_USER=$(grep ^db_user $CONFN | awk -F= '{print $2}' | tr -d " ")
else
    DB_USER=$(build_odoo_param DB_USER $odoo_fver)
fi
if [[ -n "$opt_qport" ]]; then
    DB_PORT=$opt_qport
elif [[ -f $CONFN ]]; then
    DB_PORT=$(grep ^db_port $CONFN | awk -F= '{print $2}' | tr -d " ")
    [[ $DB_PORT == "False" ]] && unset DB_PORT
fi

create_db=0
drop_db=0
depmods=""

if [[ $opt_test -ne 0 ]]; then
    opt_web=0
    opt_lang=0 opt_exp=0 opt_imp=0
    opt_upd=0 opt_stop=1
    opt_xtl=1
    [[ $opt_dbg -ne 0 ]] && opt_nocov=1 || opt_nocov=0
    [[ -z $opt_db && $opt_keep -eq 0 && $opt_dbg -gt 1 ]] && opt_db="${MQT_TEST_DB}_${ULI}"
    [[ -z $opt_db && $opt_keep -eq 0 && $opt_dbg -lt 1 ]] && opt_db="${MQT_TEST_DB}$$"
    [[ -z $opt_db && $opt_keep -ne 0 ]] && opt_db="${MQT_TEST_DB}_${odoo_ver}"
    create_db=1
    [[ $opt_keep -eq 0 ]] && drop_db=1 || drop_db=0
    [[ ! -d $HOME_DEVEL/travis_log ]] && run_traced "mkdir $HOME_DEVEL/travis_log"
    LOGFILE="$HOME_DEVEL/travis_log/${UMLI}.log"
elif [[ $opt_lang -ne 0 ]]; then
    opt_keep=1
    opt_stop=1
    [[ -n "$opt_modules" ]] && opt_modules=
elif [[ $opt_exp -ne 0 || $opt_imp -ne 0 ]]; then
    opt_keep=1
    opt_stop=1
    [[ -z "$opt_modules" ]] && echo "Missing -m switch!!" && exit 1
    [[ -z "$opt_db" ]] && echo "Missing -d switch !!" && exit 1
elif [[ $opt_upd -ne 0 ]]; then
    opt_keep=1
    [[ -z "$opt_modules" ]] && echo "Missing -m switch!!" && exit 1
    [[ -z "$opt_db" ]] && echo "Missing -d switch !!" && exit 1
elif [[ $opt_xtl -ne 0 ]]; then
    [[ -z "$opt_modules" ]] && echo "Missing -m switch!!" && exit 1
    [[ -z "$opt_db" ]] && echo "Missing -d switch !!" && exit 1
fi

if [[ -n "$opt_modules" ]]; then
    if [[ $create_db -ne 0 ]]; then
        [[ -z $opaths ]] && echo "No path list found in $CONFN!" && exit 1
        if [[ -z "$($which odoo_dependencies.py 2>/dev/null)" ]]; then
            echo "Test incomplete!"
            echo "File odoo_dependencies.py not found!"
        else
            [[ $opt_verbose -ne 0 ]] && echo "Searching for modules path ..."
            if [[ "$opt_modules" == "all" ]]; then
                depmods=$(odoo_dependencies.py -RA mod "$opaths")
            else
                depmods=$(odoo_dependencies.py -RA mod "$opaths" -PM $opt_modules)
            fi
            [[ -z "$depmods" ]] && echo "Modules $opt_modules not found!" && exit 1
            if [[ "$opt_modules" != "all" ]]; then
                depmods=$(odoo_dependencies.py -RA dep $opaths -PM $opt_modules)
            fi
            [[ -n "$depmods" && $opt_test -eq 0 ]] && opt_modules="$opt_modules,$depmods"
        fi
        OPTS="-i $opt_modules"
        OPTDB=""
        [[ $opt_test -ne 0 && $odoo_ver -gt 6 ]] && OPTS="$OPTS --test-enable"
        [[ $opt_test -eq 0 && $odoo_ver -eq 6 ]] && OPTS="$OPTS --test-disable"
    else
        check_for_modules
        OPTSIU="$OPTI $OPTU"
        alt=
        if [[ $opt_exp -ne 0 && -n "$opt_ofile" ]]; then
            src=$(readlink -f $opt_ofile)
            OPTS="--modules=$opt_modules --i18n-export=$src -lit_IT"
        elif [[ $opt_exp -ne 0 || $opt_imp -ne 0 ]]; then
            [[ -z $opaths ]] && echo "No path list found in $CONFN!" && exit 1
            src=$(find ${opaths//,/ } -maxdepth 1 -type d -name $opt_modules 2>/dev/null|head -n1)
            if [[ -z $src ]]; then
                echo "Translation file not found!!"
                exit 1
            fi
            src=$(readlink -f $src)
            [[ ! -d $src/i18n ]] && echo "No directory $src/i18n found!!" && exit 1
            src="$src/i18n/it.po"
            makepo_it.py -f -b$odoo_fver -m$opt_modules $src
            if [[ $opt_imp -ne 0 ]]; then
                OPTS="--modules=$opt_modules --i18n-import=$src -lit_IT --i18n-overwrite"
            else
                OPTS="--modules=$opt_modules --i18n-export=$src -lit_IT"
            fi
        elif [[ $opt_upd -ne 0 && $opt_xtl -ne 0 ]]; then
            OPTS="$OPTSIU"
            [[ $opt_test -ne 0 ]] && OPTS="$OPTS --test-enable"
        elif [[ $opt_upd -ne 0 ]]; then
            OPTS="$OPTU"
            [[ $opt_test -ne 0 ]] && OPTS="$OPTS --test-enable"
            [[ -n $OPTI && $opt_verbose -ne 0 ]] && echo -e "Warning: some modules must be installed before\n$OPTI"
            [[ -z $OPTU ]] && echo "No module found to update" && exit 1
        elif [[ $opt_xtl -ne 0 ]]; then
            OPTS="$OPTI"
            [[ $opt_test -ne 0 ]] && OPTS="$OPTS --test-enable"
            [[ -n $OPTU && $opt_verbose -ne 0 ]] && echo -e "Warning: some modules must be updated\n$OPTU"
            [[ -z $OPTI ]] && echo "No module found to install" && exit 1
        else
            OPTS="$OPTSIU"
        fi
    fi
else
    if [[ $opt_lang -ne 0 ]]; then
        OPTS=--load-language=it_IT
    else
        OPTS=""
        OPTDB=""
    fi
fi

if [[ -n "$opt_modules" || $opt_upd -ne 0 || $opt_xtl -ne 0 || $opt_exp -ne 0 || $opt_imp -ne 0 || $opt_lang -ne 0 ]]; then
    if [[ -z "$opt_db" ]]; then
        opt_db="$MQT_TEST_DB"
        [[ $opt_stop -gt 0 && $opt_keep -eq 0 ]] && drop_db=1
    fi
fi
OPTDB="$OPTDB --stop-after-init"
if [[ $opt_stop -gt 0 ]]; then
    OPTS="$OPTS --stop-after-init"
    if [[ $opt_exp -eq 0 && $opt_imp -eq 0 && $opt_lang -eq 0 ]]; then
        [[ $opt_test -ne 0 && $odoo_ver -lt 12 ]] && OPTS="$OPTS --test-commit"
    fi
fi
if [[ -n "$opt_db" ]]; then
    OPTS="$OPTS -d $opt_db"
    OPTDB="$OPTDB -d $opt_db"
fi
if [[ $opt_stat -ne 0 ]]; then
    if [[ -n "$VDIR" ]]; then
        coverage_set
        coverage_report
    fi
    exit 0
fi
if [[ $opt_touch -eq 0 ]]; then
    [[ -n "$DB_PORT" ]] && opts="-U$DB_USER -p$DB_PORT" || opts="-U$DB_USER"
    [[ $create_db -gt 0 ]] && run_traced "pg_db_active -wa $opt_db; dropdb $opts --if-exists $opt_db"
    [[ $create_db -gt 0 && $odoo_ver -lt 10 ]] && run_traced "createdb $opts $opt_db"
    [[ -n "$VDIR" ]] && ve_root=$VDIR || ve_root=$HOME
    OPT_LLEV=
    [[ $opt_test -ne 0 && ! -d $HOME/tmp ]] && mkdir $HOME/tmp
    [[ $opt_test -ne 0 ]] && FULL_LCONFN="$HOME/tmp/$LCONFN$$" || FULL_LCONFN="$ve_root/$LCONFN"
    OPT_CONF="--config=$FULL_LCONFN"
    if [[ $opt_dry_run -eq 0 ]]; then
        for f in .openerp_serverrc .odoorc; do
            for d in $HOME $ve_root; do
                [[ -f $d/$f ]] && rm -f $d/$f
            done
        done
    fi
    [[ -f "$CONFN" ]] && run_traced "cp $CONFN $FULL_LCONFN"
    [[ $opt_verbose -gt 0 ]] && echo "===================================================================="
    if [[ -n "$VDIR" ]]; then
      coverage_set
      x=$(date +"%Y-%m-%d %H:%M:%S,000")
      [[ $opt_verbose -gt 0 ]] && echo "$x $$ DAEMON ? $(basename $0): cd $VDIR; source ./bin/activate"
      if [[ $opt_dry_run -eq 0 ]]; then
        cd $VDIR
        source ./bin/activate
        if [[ $opt_test -ne 0 && $opt_nocov -eq 0 ]]; then
          cov=$(which coverage 2>/dev/null)
          [[ -z $cov || ! $cov =~ $HOME ]] && run_traced "pip install coverage"
          cov=$(which coverage 2>/dev/null)
          if [[ -n $cov ]]; then
            v=$(coverage --version|grep --color=never -Eo "[0-9]+"|head -n1)
            [[ $v -lt 5 ]] && run_traced "pip install coverage -U"
          fi
        fi
      fi
    fi
    [[ ! -f "$CONFN" ]] && run_traced "$script -s --stop-after-init"
    tty -s
    if [[ $? == 0 ]]; then
        run_traced_debug "sed -e \"s|^logfile *=.*|logfile = False|\" -i $FULL_LCONFN"
    else
        run_traced_debug "sed -e \"s|^logfile *=.*|logfile = $ve_root/$$.log|\" -i $FULL_LCONFN"
    fi
    if [[ $opt_dbg -ne 0 || $opt_test -ne 0 ]]; then
        run_traced_debug "sed -e \"s|^limit_time_cpu *=.*|limit_time_cpu = 0|\" -i $FULL_LCONFN"
        run_traced_debug "sed -e \"s|^limit_time_real *=.*|limit_time_real = 0|\" -i $FULL_LCONFN"
    fi
    if [[ $odoo_ver -le 10 ]]; then
        run_traced_debug "sed -e \"s|^xmlrpc_port *=.*|xmlrpc_port = $RPCPORT|\" -i $FULL_LCONFN"
        OPT_CONF="$OPT_CONF --xmlrpc-port=$RPCPORT"
    else
        run_traced_debug "sed -e \"s|^http_port *=.*|http_port = $RPCPORT|\" -i $FULL_LCONFN"
        OPT_CONF="$OPT_CONF --http-port=$RPCPORT"
    fi
    if [[ -n "$DB_USER" ]]; then
        run_traced_debug "sed -e \"s|^db_user *=.*|db_user = $DB_USER|\" -i $FULL_LCONFN"
        [[ $opt_force -ne 0 ]] && OPT_CONF="$OPT_CONF --db_user=$DB_USER"
    fi
    if [[ -n "$opt_llvl" ]]; then
        run_traced_debug "sed -e \"s|^log_level *=.*|log_level = $opt_llvl|\" -i $FULL_LCONFN"
        OPT_LLEV="--log-level=$opt_llvl"
    fi
    run_traced_debug "sed -e \"s|^workers *=.*|workers = 0|\" -i $FULL_LCONFN"
    if [[ $create_db -gt 0 ]]; then
        if [[ -n "$depmods" && $opt_test -ne 0 ]]; then
            cmd="cd $ODOO_RUNDIR; $script $OPTDB $OPT_CONF --log-level=error -i $depmods"
            [[ $opt_opt_keep -ne 0 ]] && TEMPLATE="${MQT_TEMPLATE_DB}_${odoo_ver}" || [[ opt_dbg -gt 1 ]] && TEMPLATE="${MQT_TEMPLATE_DB}_${UMI}" || TEMPLATE="${opt_db/test/template}"
            cmd="${cmd/$opt_db/$TEMPLATE}"
            fnparam="${LOGFILE/.log/.tmp}"
            if [[ $opt_force -eq 0 && ! -f $fnparam ]] || ! echo $cmd|diff -qw $fnparam - || ! psql -Atl -l|grep -q $TEMPLATE; then
              # Create DB for test
              run_traced "pg_db_active -wa $TEMPLATE; dropdb $opts --if-exists $TEMPLATE"
              run_traced "$cmd"
            fi
            if psql -Atl -l|grep -q $TEMPLATE; then
              echo $cmd > $fnparam
              run_traced "psql -U$DB_USER template1 -c \"create database $opt_db owner $DB_USER template $TEMPLATE\""
            else
              echo "Template $TEMPLATE not found!"
              cmd="${cmd/$TEMPLATE$opt_db/}"
              run_traced "$cmd"
            fi
       else
            run_traced "cd $ODOO_RUNDIR; $script $OPTDB $OPT_CONF --log-level=error"
        fi
    fi
    # if [[ $odoo_ver -lt 10 && $opt_dry_run -eq 0 && $opt_exp -eq 0 && $opt_imp -eq 0 && $opt_lang -eq 0 ]]; then
    #     OPTS="--debug $OPTS"
    # fi

    if [[ $opt_test -ne 0 && $opt_dbg -eq 0 ]]; then
      [[ -f $LOGFILE ]] && rm -f $LOGFILE
      if [[ -n $COVERAGE_PROCESS_START ]]; then
        v=$(coverage --version|grep --color=never -Eo "[0-9]+"|head -n1)
        if [[ $v -ge 6 ]]; then
          run_traced "cd $ODOO_RUNDIR; coverage run --rcfile=$COVERAGE_PROCESS_START --data-file=$COVERAGE_DATA_FILE $script $OPT_CONF $OPT_LLEV $OPTS"
        else
          run_traced "cd $ODOO_RUNDIR; coverage run --rcfile=$COVERAGE_PROCESS_START $script $OPT_CONF $OPT_LLEV $OPTS"
        fi
      else
        run_traced "cd $ODOO_RUNDIR; $script $OPT_CONF $OPT_LLEV $OPTS"
      fi
    elif [[ opt_dbg -gt 1 ]]; then
      echo ""
      echo "Now you can test module $opt_module on pycharm"
      echo ""
      echo "Debug Odoo by pycharm after set configuration \"Debug Odoo $odoo_fver\""
      echo -e "parameters=\"\e[33m$script\e[0m \e[31m$OPT_CONF $OPT_LLEV $OPTS\e[0m\""
      echo ""
      echo "If your test code contains the follow statements"
      echo ""
      echo -e "    \e[33mdef tearDown(self):\e[0m"
      echo -e "        \e[33mself.env.cr.commit()\e[0m  # pylint: disable=invalid-commit"
      echo ""
      echo "you can browse test database from:"
      echo -e "\e[33mhttp://localhost:8069\e[0m or \e[33mhttp://localhost:$(build_odoo_param RPCPORT $odoo_fver)\e[0m"
      echo -e "DB=\e[31m$opt_db\e[0m login: admin/admin"
      echo ""
    else
      run_traced "cd $ODOO_RUNDIR; $script $OPT_CONF $OPT_LLEV $OPTS"
    fi

    coverage_report

    if [[ -n "$VDIR" ]]; then
        x=$(date +"%Y-%m-%d %H:%M:%S,000")
        [[ $opt_verbose -gt 0 && opt_dbg -le 1 ]] && echo "$x $$ DAEMON ? $(basename $0): deactivate"
        [[ $opt_dry_run -eq 0 ]] && deactivate
    fi
    [[ $opt_test -ne 0 && -f $FULL_LCONFN ]] && rm -f FULL_LCONFN
    if [[ $drop_db -gt 0 ]]; then
        if [[ -z "$opt_modules" || $opt_stop -eq 0 ]]; then
            [[ -n "$DB_PORT" ]] && opts="-U$DB_USER -p$DB_PORT" || opts="-U$DB_USER"
            run_traced "pg_db_active -wa $opt_db; dropdb $opts --if-exists $opt_db"
            [[ opt_dbg -ne 1 ]] && run_traced "pg_db_active -wa $TEMPLATE; dropdb $opts --if-exists $TEMPLATE"
        fi
    fi
    if [[ $opt_exp -ne 0 ]]; then
        makepo_it.py -b$odoo_fver -m$opt_modules $src
        echo "# Translation exported to '$src' file"
    elif [[ $opt_imp -ne 0 ]]; then
        echo "# Translation imported from '$src' file"
    fi
fi
