#!/bin/bash

PG_VERSION=`psql -At -c "show server_version" postgres`

if [[ "x$PG_VERSION" > "x9.5" && "x$PG_VERSION" < "x9.5.5" ]]
then
    echo "PG $PG_VERSION detected, upgrade required"
else
    echo "PG $PG_VERSION detected, NO upgrade required"
    exit 0;
fi
echo -n "Updating package list..."
apt-get update -qq
echo  Done
BRANCHES=`sudo -u odoo /home/odoo/bin/oe-meta list-branches`

for branch in $BRANCHES
do
    echo "shutting down $branch"
    service openerp-$branch stop
done

echo Update in progress...
DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confold" --force-yes postgresql-9.5


for branch in $BRANCHES
do
    echo "restarting $branch"
    service openerp-$branch start
done

