#!/bin/sh
# database creation script for gnuhealth-server

set -e

# Source the config file from dbconfig-common
. /etc/dbconfig-common/gnuhealth-server.conf

GNUHEALTH_USER="gnuhealth"
GNUHEALTH_HOMEDIR="/var/lib/gnuhealth"
GNUHEALTH_CONFFILE="/etc/gnuhealth/gnuhealth-server.conf"

# Start printing output on a new line
echo

# Create system user
adduser --home ${GNUHEALTH_HOMEDIR} --quiet --system --group ${GNUHEALTH_USER}

tf=`sudo -u ${GNUHEALTH_USER} tempfile 2>/dev/null`
# Use the same password for the Tryton admin as for the database
echo $dbc_dbpass > $tf
# Initialize the gnuhealth database
sudo -u ${GNUHEALTH_USER} TRYTONPASSFILE=$tf trytond --database=gnuhealth \
       --init=all --config=${GNUHEALTH_CONFFILE}
rm -f $tf

# Change database owner (needs to be run as postgres user, as PostgreSQL's
# default authentication is set to peer method)
sudo -u postgres psql -U postgres -c 'ALTER DATABASE gnuhealth OWNER TO gnuhealth'

exit 0

