#!/bin/sh
# Copyright (c) 1997-now, NeoNova BV, The Netherlands. All rights Reserved.
#
# This software is the copyrighted work of NeoNova BV.
# Use of the software is subject to the general business terms and conditions of NeoNova BV
# and its related transaction documents like license sheets or other written notices.#
# Any reproduction or redistribution of the software not in accordance with the license terms
# is expressly prohibited by law, and may result in severe civil and criminally penalties.
# Violators will be prosecuted to the maximum extent possible.

# should be run as user postgres

if [ ! `id -un` = postgres ]
then
  echo please only run as user postgres
  exit 0
fi
 
CURRENT=`psql -t astium -c "select value from astglobals where name = 'dbversion'"`
echo $CURRENT

if [ -z "$CURRENT" ]
then
  logger "ERROR - database not upgraded"
  exit 0
fi

cd /usr/local/astium/setup/db/sql-changes
for i in `ls [0-9]*`
do
  if [ $i -gt $CURRENT ]
  then
    echo "Applying upgrade $i.."
    psql -a astium < $i
    psql astium -c "update astglobals set value = '$i' where name = 'dbversion'"  
  fi
done 2>&1 | tee /tmp/apply-changes.out

