#!/bin/bash

if [ -f ../dbcfg ]; then
    . ../dbcfg
fi


while [ $# -gt 0 ] ; do
  case $1 in
    -c)       docopy="c"; shift 1;;
    -sdb)     origdb=$2; shift 2;;
    -shost)   orighost=$2; shift 2;;
    -sport)   origport=$2; shift 2;;
    -suser)   origuser=$2; shift 2;;
    -ddb)     checkdb=$2; shift 2;;
    -dhost)   checkhost=$2; shift 2;;
    -dport)   checkport=$2; shift 2;;
    -duser)   checkuser=$2; shift 2;;
    -pass )   origpass=$2; checkpass=$2; shift 2;;
    -r)       reverse=1; shift 1;;
    -cusschemas) cus_schemas=$2; shift 2;;
    -schemas) ovschemas=$2; shift 2;;
     *)       shift 1 ;;
  esac
done

if [ "$reverse" = "1" ];then
  tmp=$origdb;
  origdb=$checkdb;
  checkdb=$tmp;

  tmp=$orighost;
  orighost=$checkhost;
  checkhost=$tmp;
fi

if [ "$docopy" = "c" ]; then 
  echo Copy $orighost':'$origdb to $checkhost':'$checkdb 2>&1
else
  echo Do Update $orighost':'$origdb to $checkhost':'$checkdb 2>&1
fi

. do_readdb

rm -f modify.sql 2>/dev/null
rm -f drop.sql   2>/dev/null

echo "SET SESSION AUTHORIZATION admindb;" > modify.sql

rdschemas="SELECT schemata.schema_name FROM information_schema.schemata"
checkschemas=`get_checkdata "$rdschemas"`

for s in $schemas
do
    found=0
    for cs in $checkschemas 
    do
        if [ "$s" = "$cs" ]; then
          found=1
          break
        fi
    done
    
    if [ "$found" = "0" ]; then
        echo "CREATE SCHEMA $s;" >> modify.sql
    fi
done

awk 'BEGIN { stopprint=0; }
    /DROP TABLE/               { next; }
    /DROP SCHEMA/              { stopprint = 1; next; }
    /DROP SEQUENCE/            { next; }
    /Start Schema/             { stopprint = 0 }
                               { if ( stopprint == 0 ) print $0 }
    ' check.db >> modify.sql

. do_checktablecols    >> modify.sql
echo -ne "                                             \r" >&2

awk 'BEGIN             { stopprint=0; }
    /CREATE SCHEMA/    { next; }
    /CREATE TABLE/     { stopprint = 1; }
    /CREATE SEQUENCE/  { stopprint = 1; }
    /ALTER TABLE/      { stopprint = 0; }
    /^\);$/            { if ( stopprint == 1 ) { stopprint = 0; next; } }
                       { if ( stopprint == 0 ) print $0 }
    ' orig.db >> modify.sql

rm -f check.db  2> /dev/null
rm -f orig.db   2> /dev/null
