#!/bin/bash 

NOASK=
NOSECOND=
IFS=$'\012'

get_data()
{
   export PGPASSWORD=$origpass
   echo "$1" | sed -e "s@par1@$2@" -e "s@par2@$3@" -e "s@par3@$4@" | psql --tuples-only --no-align -E -h $orighost -p $origport -U $origuser $origdb
}

get_checkdata()
{
   export PGPASSWORD=$checkpass
   echo "$1" | sed -e "s@par1@$2@" -e "s@par2@$3@" -e "s@par3@$4@" | psql --tuples-only --no-align -E -h $checkhost -p$checkport -U $checkuser $checkdb
}


if [ "$(id -u)" = "0" ]; then
    ifconfig lo:mneconfig 127.0.156.1 netmask 255.255.255.0 up
fi

while [ $# -gt 0 ] ; do
  case $1 in
    -sdb)   origdb=$2; shift 2;;              # source db
    -shost) orighost=$2; shift 2;;            # source host
    -spass) origpass=$2; shift 2;;            # source password
    -ddb)   checkdb=$2; shift 2;;             # dest db
    -dhost) checkhost=$2; shift 2;;           # dest host
    -dpass) checkpass=$2; shift 2;;           # dest passwd
     *)     shift 1 ;;
  esac
done

while [ "$PGPASSWORD" = "" ]; do

    echo "For updating the database please give the password of the user $checkuser"
    echo -n "Password for $checkuser: "
    stty -echo < /dev/tty
    read pass
    stty echo < /dev/tty
    echo

    if [ "$pass" != "" ]; then
        export origpass=$pass;
        export checkpass=$pass;
    fi

    export PGPASSWORD=$origpass;
    echo "" | psql --set ON_ERROR_STOP=on -U $checkuser -h $orighost $origdb
    if [ $? != 0 ]; then
        echo "can't connect to orig $origdb" 1>&2
        export PGPASSWORD=
    fi

    if [ ! "$PGPASSWORD" = "" ]; then
        export PGPASSWORD=$checkpass;
        echo "" | psql --set ON_ERROR_STOP=on -U $checkuser -h $checkhost $checkdb
        if [ $? != 0 ]; then
            echo "can't connect to check $checkdb" 1>&2
            export PGPASSWORD=
        fi
    fi
    
done

checkversion=$(echo "select version from mne_application.update\g" | psql --tuples-only --no-align  -U $checkuser -h $checkhost -p $checkport -e $checkdb)
origversion=$(echo "select version from mne_application.update\g" | psql --tuples-only --no-align  -U $checkuser -h $orighost -p $origport -e $origdb)

if [ ! "$forceupdate" = "1" ] && [ "$checkversion" = "$origversion" ]; then
    echo "Database Version equal - no update"
    exit 0;
fi

( cd dbcheck; . ./dbcheck )
( cd dbcheck; export PGPASSWORD=$checkpass; psql -U $checkuser -h $checkhost -e $checkdb -p $checkport < modify.sql > modify.log 2>&1; )

if [ "$NOSECOND" = "" ]; then
    error_found=0
    fgrep ERROR dbcheck/modify.log 2>&1 > /dev/null
    error_found=$?
    
    if [ "$error_found" != "0" ]; then
        fgrep FEHLER dbcheck/modify.log 2>&1 > /dev/null
        error_found=$?
    fi

    if [ "$error_found" = "0" ]; then
        ( cd dbcheck; ./dbcheck -shost $orighost -dhost $checkhost -sdb $origdb -ddb $checkdb )
        ( cd dbcheck; export PGPASSWORD=$checkpass; psql -U $checkuser -h $checkhost -e $checkdb < modify.sql > modify.log 2>&1; )
    fi
fi

error_found=0
fgrep ERROR dbcheck/modify.log 1>&2 
error_found=$?
    
if [ "$error_found" != "0" ]; then
  fgrep FEHLER dbcheck/modify.log 1>&2 
  error_found=$?
fi

if [ "$error_found" != "1" ]; then
    if [ "$NOASK" = "" ]; then
    ok=
      while [ ! "$ok" = "y" ] &&[ ! "$ok" = "Y" ] &&[ ! "$ok" = "n" ] &&   [ ! "$ok" = "N" ] 
      do
          echo -n "error found - continue ?  (Y/N)"
          read ok
      done
    else
      ok=n
    fi
    
    if [ "$ok" = "n" ] || [ "$ok" = "N" ]; then
        echo "error found during updating database - please run it manual" 1>&2
        exit 1
    fi
fi

( cd dbcontent; . ./dbcontent )
( cd dbcontent; export PGPASSWORD=$checkpass; psql -U $checkuser -h $checkhost -e $checkdb < content.sql > content.log 2>&1 )
if [ -d dbadd ]; then
  ( cd dbadd;
    for file in *.sql
    do
      psql -U $checkuser -h $checkhost -e $checkdb < $file >> add.log 2>&1 
    done
  )
fi
