#! /bin/bash
# -*- coding: utf-8 -*-
#
# Copy table between 2 Dbs
#
# This free software is released under GNU Affero GPL3
# author: Antonio M. Vigliotti - antoniomaria.vigliotti@gmail.com
# (C) 2015-2019 by SHS-AV s.r.l. - http://www.shs-av.com - info@shs-av.com
#
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"
ZARLIB=$(findpkg zarrc "$PYPATH")
[[ -z "$ZARLIB" ]] && echo "Library file zarrc not found!" && exit 72
. $ZARLIB
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "ZARLIB=$ZARLIB"
TESTDIR=$(findpkg "" "$TDIR . .." "tests")
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "TESTDIR=$TESTDIR"
RUNDIR=$(readlink -e $TESTDIR/..)
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "RUNDIR=$RUNDIR"

# 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.4


OPTOPTS=(h        d        f      l       n            s        t        U          V           v           w)
OPTDEST=(opt_help opt_dst  opt_f  opt_log opt_dry_run  opt_src  opt_tbl  opt_user   opt_version opt_verbose opt_wep)
OPTACTI=("+"      "="      "="    "="     1            "="      "="      "="        "*>"        1           1)
OPTDEFL=(0        ""       "id"   ""      0            ""       ""       "postgres" ""          0           0)
OPTMETA=("help"   "DBname" "name" "file"  "do nothing" "DBname" "table"  "username" "version"   "verbose"   "")
OPTHELP=("this help"\
 "destination DB name"\
 "field name to count (def id)"\
 "log filename (def /var/log/bckdb.log)"\
 "do nothing (dry-run)"\
 "source DB name"\
 "table to copy"\
 "username (def postgres)"\
 "show version"\
 "verbose mode"\
 "wep destination table before import")
OPTARGS=()


parseoptargs "$@"
if [ -z "$opt_src" -o -z "$opt_dst" -o -z "$opt_tbl" ]; then
  opt_help=1
fi
if [ "$opt_version" ]
then
  echo "$__version__"
  exit 0
fi
if [ $opt_help -gt 0 ]
then
  print_help "Copy table between DBs"\
  "(C) 2015-2020 by zeroincombenze®\nhttp://wiki.zeroincombenze.org/en/Linux/dev\nAuthor: antoniomaria.vigliotti@gmail.com"
  exit 0
fi
if [ -z "$opt_log" ]; then
  publog="/var/log/zar/cptbl.log"
else
  publog=$opt_log
fi
FCONF=$TDIR/zar.conf
CFG_init
set_tlog_file "$publog"
link_cfg $FCONF
wlog "Starting copy $__version__ by $opt_user"

PGDIR=$(findpkg "" "/var/lib" "postgresql  pgsql" "8.2 8.3 9.0 9.1 9.2 9.3 9.4")
if [ -z "$PGDIR" ]; then
  wlog "No postgres directory found!"
  exit 1
fi
wlog "Found $PGDIR directory"
DBCKDIR="$PGDIR/backups"
if [ ! -d $DBCKDIR ]; then
  mkdir -p $DBCKDIR
fi
wlog "Backup directory is $DBCKDIR"
HOST_PRD=$(get_cfg_value 0 production_host)
HOST_DEV=$(get_cfg_value 0 development_host)
wlog "PRD=$HOST_PRD"
wlog "DEV=$HOST_DEV"
opt_tbl=${opt_tbl//./_}
PID=$$
tmpfn=./tmp$PID
echo "Stats before copy ..."
[[ "$opt_f" == "id" ]] && psql -U$opt_user -Atec "select last_value from ${opt_tbl}_id_seq;" $opt_dst
[[ "$opt_f" == "id" ]] && OLDNXT=$(psql -U$opt_user -Atc "select last_value from ${opt_tbl}_id_seq;" $opt_dst)
psql -U$opt_user -Atec "select max($opt_f) from $opt_tbl;" $opt_dst
psql -U$opt_user -Atec "select count($opt_f) from $opt_tbl;" $opt_src
CTRBEF=$(psql -U$opt_user -Atc "select count($opt_f) from $opt_tbl;" $opt_src)
echo "Copy data from source DB $opt_src"
run_traced "pg_dump -U$opt_user -a --inserts --disable-triggers -Fp -f $tmpfn.sql -t $opt_tbl $opt_src"
if [ $opt_wep -ne 0 ]; then
  echo "Deleting table $opt_tbl from DB $opt_dst"
  run_traced "psql -U$opt_user -Atec \"delete from $opt_tbl;\" $opt_dst"
fi
echo "Paste data into destination DB $opt_dst"
run_traced "psql -U$opt_user -d $opt_dst -f $tmpfn.sql -o $tmpfn.log 2>$tmpfn.err"
echo "Resulting stats ..."
psql -U$opt_user -Atec "select max($opt_f) from $opt_tbl;" $opt_dst
psql -U$opt_user -Atec "select count($opt_f) from $opt_tbl;" $opt_dst
NXTID=$(psql -U$opt_user -Atc "select max($opt_f) from $opt_tbl;" $opt_dst)
# ((NXTID++))
if [ $OLDNXT -gt $NXTID ]; then
  NXTID=$OLDNXT
fi
[[ "$opt_f" == "id" ]] && psql -U$opt_user -Atec "select last_value from ${opt_tbl}_id_seq;" $opt_dst
[[ "$opt_f" == "id" ]] && run_traced "psql -U$opt_user -d $opt_dst -c \"alter sequence ${opt_tbl}_id_seq restart $NXTID;\""
echo "Currently stats after copy ..."
[[ "$opt_f" == "id" ]] && psql -U$opt_user -Atec "select last_value from ${opt_tbl}_id_seq;" $opt_dst
psql -U$opt_user -Atec "select max($opt_f) from $opt_tbl;" $opt_dst
psql -U$opt_user -Atec "select count($opt_f) from $opt_tbl;" $opt_src
CTRAFT=$(psql -U$opt_user -Atc "select count($opt_f) from $opt_tbl;" $opt_src)
((DIF=CTRAFT-CTRBEF))
if [ $opt_wep -ne 0 ]; then
  echo "$CTRAFT records replace $CTRBEF records"
elif [ $DIF -eq 0 ]; then
  echo "No new record inserted"
else
  echo "$DIF new records inserted"
fi
rm -f $tmpfn.*
exit 0

