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

READLINK=$(which greadlink 2>/dev/null) || READLINK=$(which readlink 2>/dev/null)
export READLINK
# Based on template 1.0.9
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 ]]; then
  [[ -d $HOME/odoo/devel ]] && HOME_DEVEL="$HOME/odoo/devel" || HOME_DEVEL="$HOME/devel"
fi
[[ -x $TDIR/../bin/python ]] && PYTHON=$(readlink -f $TDIR/../bin/python) || [[ -x $TDIR/python ]] && PYTHON="$TDIR/python" || PYTHON="python"
[[ -z $PYPATH ]] && PYPATH=$(echo -e "C='"$TDIR"'\nD='"$HOME_DEVEL"'\nimport os,sys\no=os.path\na=o.abspath\nj=o.join\nd=o.dirname\nb=o.basename\nf=o.isfile\np=o.isdir\nH=o.expanduser('~')\nT=j(d(D), 'tools')\nR=j(d(D),'pypi') if o.basename(D)=='venv_tools' else j(D,'pypi')\nW=D if o.basename(D)=='venv_tools' else j(D,'venv')\ndef apl(L,P,B):\n if P:\n  if p(j(P,B,B)) and p(j(P,B,B,'script')) and f(j(P,B,B,'__init__')):\n   L.append(j(P,B,B))\n   return 1\n  elif j(P,B):\n   L.append(j(P,B))\n   return 1\n return 0\nL=[C]\nif b(C) in ('scripts','tests','travis','_travis'):\n C=a(j(C,'..'))\n L.append(C)\nif b(C)==b(d(C)) and f(j(C,'..','setup.py')):\n C=a(j(C,'..','..'))\nelif b(d(C))=='tools' and f(j(C,'setup.py')):\n C=a(j(C,'..'))\nP=os.environ['PATH'].split(':')\nV= ''\nfor X in sys.path:\n if not p(T) and p(j(X,'tools')):\n  T=j(X,'tools')\n if not V and b(X)=='site-packages':\n  V=X\nfor B in ('z0lib','zerobug','odoo_score','clodoo','travis_emulator'):\n if p(j(C,B)) or p(j(C,b(C),B)):\n  F=apl(L,C,B)\n else:\n  F=0\n  for X in P:\n   if p(j(X,B)):\n    F=apl(L,X,B)\n    break\n  if not F:\n   F=apl(L,V,B)\n  if not F:\n   apl(L,T,B)\nL=L+[os.getcwd()]+P\np=set()\npa=p.add\np=[x for x in L if x and x.startswith((H,D,C)) and not (x in p or pa(x))]\nprint(' '.join(p))\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
if [[ -z "$Z0LIBDIR" ]]; then
  echo "Library file z0librc not found in <$PYPATH>!"
  exit 72
fi
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "Z0LIBDIR=$Z0LIBDIR"
ODOOLIBDIR=$(findpkg odoorc "$PYPATH" "clodoo")
if [[ -z "$ODOOLIBDIR" ]]; then
  echo "Library file odoorc not found!"
  exit 72
fi
. $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__=1.0.7

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
}


OPTOPTS=(h        B       b          c        C           d        e       f         k        i       I       l        L        m           M         n           o         O      p        P         q           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       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_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           "="       1      "="      "="       0           1        1        1         "="        1       "*>"        "+"         1       1        "=")
OPTDEFL=(1        0       ""         ""       0           ""       0       0         0        0       0       0        ""       ""          -1        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"
    "odoo branch (version id)"
    "odoo configuration file"
    "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"
    "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"
    "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=""
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}')"
fi
# [[ -z $CONFN || ! -f $CONFN ]] &&  && echo "Configuration file $CONFN not found!" && exit 1
[[ -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_ORGNM=$(build_odoo_param GIT_ORGNM $odoo_root)
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_ORGNM)
elif [[ -z $opt_conf && $opt_test -ne 0 ]]; then
    RPCPORT=$(build_odoo_param RPCPORT $odoo_fver DEBUG)
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_ORGNM)
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=1
    [[ -z "$opt_db" ]] && opt_db="test_openerp_$odoo_ver"
    create_db=1 drop_db=1
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
            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
            if [[ -n "$depmods" && $opt_test -eq 0 ]]; then
                opt_modules="$opt_modules,$depmods"
            fi
        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//,/ } -type d -name $opt_modules 2>/dev/null)
            if [[ -z $src ]]; then
                echo "Translation file not found!!"
                exit 1
            fi
            src=$($READLINK -f $src)
            [[ ! -d $src/i18n ]] && echo "Non directory $src/i18n found!!" && exit 1
            src="$src/1i8n/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="test_openerp"
        [[ $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_touch -eq 0 ]]; then
    [[ -n "$DB_PORT" ]] && opts="-U$DB_USER -p$DB_PORT" || opts="-U$DB_USER"
    [[ $drop_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=
    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"
    echo "===================================================================="
    if [[ -n "$VDIR" ]]; then
        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 ]] && run_traced "pip install coverage"
            cov=$(which coverage 2>/dev/null)
            if [[ -n $cov ]]; then
              v=$(coverage --version|grep -Eo "[0-9]+"|head -n1)
              [[ $v -lt 5 ]] && run_traced "pip install coverage -U"
              [[ ! -d $ODOO_RUNDIR/coverage ]] && mkdir $ODOO_RUNDIR/coverage
              [[ -n $opt_modules ]] && export COVERAGE_FILE="${opt_modules}_${opt_branch}"
              if [[ -n $COVERAGE_FILE ]]; then
                export COVERAGE_FILE="$ODOO_RUNDIR/coverage/$COVERAGE_FILE"
                COVERAGE_RCFILE="$ODOO_RUNDIR/coverage/${opt_modules}rc"
                [[ -s $COVERAGE_RCFILE ]] || coverage_tmpl=$(find $PYPATH -name coveragerc|head -n 1)
                [[ -n "$coverage_tmpl" ]] && cp $coverage_tmpl $COVERAGE_RCFILE
                sed -E "s|^ *\*.py|    */$opt_modules/*|" -i $COVERAGE_RCFILE
              fi
            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 ]]; 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
            run_traced "cd $ODOO_RUNDIR; $script $OPTDB $OPT_CONF --log-level=error -i $depmods"
        else
            run_traced "cd $ODOO_RUNDIR; $script $OPTDB $OPT_CONF --log-level=error"
        fi
    fi
    if [ $odoo_ver -lt 10 -a $opt_dry_run -eq 0 -a $opt_exp -eq 0 -a $opt_imp -eq 0 -a $opt_lang -eq 0 ]; then
        OPTS="--debug $OPTS"
    fi

    if [[ -n $COVERAGE_RCFILE ]]; then
      v=$(coverage --version|grep -Eo "[0-9]+"|head -n1)
      [[ $v -ge 6 ]] && run_traced "cd $ODOO_RUNDIR; coverage run --rcfile=$COVERAGE_RCFILE --data-file=$COVERAGE_FILE $script $OPT_CONF $OPT_LLEV $OPTS" || run_traced "cd $ODOO_RUNDIR; coverage run --rcfile=$COVERAGE_RCFILE $script $OPT_CONF $OPT_LLEV $OPTS"
    else
      run_traced "cd $ODOO_RUNDIR; $script $OPT_CONF $OPT_LLEV $OPTS"
    fi

    if [[ -n "$VDIR" ]]; then
        x=$(date +"%Y-%m-%d %H:%M:%S,000")
        [ $opt_verbose -gt 0 ] && echo "$x $$ DAEMON ? $(basename $0): deactivate"
        [ $opt_dry_run -eq 0 ] && deactivate
    fi
    if [ $drop_db -gt 0 ]; then
        if [ -z "$opt_modules" -o $opt_stop -eq 0 ]; then
            [[ -n "$DB_PORT" ]] && opts="-U$DB_USER -p$DB_PORT" || opts="-U$DB_USER"
            run_traced "dropdb $opts --if-exists $opt_db"
        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
    if [[ -n $COVERAGE_RCFILE ]]; then
      v=$(coverage --version|grep -Eo "[0-9]+"|head -n1)
      [[ $v -ge 6 ]] && coverage report --rcfile=$COVERAGE_RCFILE --data-file=$COVERAGE_FILE -m || coverage report --rcfile=$COVERAGE_RCFILE -m
    fi
fi
