#!/bin/bash

# This file is a contribution by Karsten Hilbert
# <karsten.hilbert@gmx.net>.

# This script automates the upgrade the schema of the database of
# Orthanc. Check out the file /usr/share/doc/orthanc/README.Debian and
# Debian issue #829380 ("orthanc: please provide orthanc_upgrade
# script") for more information.

set -ex

if [ "$(id -u)" -ne 0 ]; then
    echo "This script must be run as root."
    exit -1
fi

systemctl stop orthanc.service

su orthanc -c "/usr/sbin/Orthanc --upgrade --verbose --logfile=/var/log/orthanc/upgrade.log /etc/orthanc/"
RESULT=$?
if [ $RESULT -ne 0 ] ; then
    echo "error upgrading"
    read -e
    less /var/log/orthanc/upgrade.log
    exit $RESULT
fi

systemctl start orthanc.service
