Script to update Idempiere´s database with patches from a directory

This script ist to apply all unapplied patches from a directory to the local idempiere database. The original script from Carlos Ruiz downloads the scripts from the jenkins server. This one can be used to apply all the patches from a local build.

#
# Contributor: Carlos Ruiz - globalqss
# June 27, 2013
#
# Modified by Markus Bozem, January 2, 2016
#
# Script to synchronize your installation with patches in a given directory

set -e

DATABASE=idempiere
USER=adempiere
FOLDER=$1

if [ -n "$2" ]
   then
     export PGPASSWORD=$2
   fi


if [ -d "$1" -a -n "$1" ]
  then
    echo "Sync with $1"
  else
    echo "Usage syncAppliedDir directory [password]"
    exit 1
  fi

BASEDIR=`dirname $0`
cd $BASEDIR
> /tmp/lisFS.txt

psql -h localhost -d $DATABASE -U $USER -q -t -c "select name from ad_migrationscript" | sed -e 's:^ ::' | grep -v '^
 | sort > /tmp/lisDB.txt
if [ -d ${FOLDER}/postgresql ]
  then
     cd ${FOLDER}/postgresql
     ls *.sql | sort >> /tmp/lisFS.txt
     cd ../..
  fi
sort -o /tmp/lisFS.txt /tmp/lisFS.txt
sort -o /tmp/lisDB.txt /tmp/lisDB.txt
MSGERROR=""
APPLIED=N
for i in `comm -13 /tmp/lisDB.txt /tmp/lisFS.txt`
do
    SCRIPT="${FOLDER}/postgresql/$i"
    OUTFILE=/tmp/`basename "$i" .sql`.out
    echo "Start script $SCRIPT"
    psql -h localhost -d $DATABASE -U $USER -f "$SCRIPT" 2>&1 | tee "$OUTFILE"
    if fgrep "ERROR:
FATAL:" "$OUTFILE" > /dev/null 2>&1
    then
        MSGERROR="$MSGERROR\n**** ERROR ON FILE $OUTFILE - Please verify ****"
    fi
    APPLIED=Y
done
if [ x$APPLIED = xY ]
then
    for i in processes_post_migration/postgresql/*.sql
    do
        OUTFILE=/tmp/`basename "$i" .sql`.out
        psql -h localhost -d $DATABASE -U $USER -f "$i" 2>&1 | tee "$OUTFILE"
        if fgrep "ERROR:
FATAL:" "$OUTFILE" > /dev/null 2>&1
        then
            MSGERROR="$MSGERROR\n**** ERROR ON FILE $OUTFILE - Please verify ****"
        fi
    done
fi
if [ -n "$MSGERROR" ]
then
    echo "$MSGERROR"
fi