#!/bin/sh # # Build the DAViCal database # DBNAME="${1:-davical}" ADMINPW="${2}" DBADIR="`dirname \"$0\"`" INSTALL_NOTE_FN="`mktemp -t tmp.XXXXXXXXXX`" testawldir() { [ -f "${1}/dba/awl-tables.sql" ] } # # Attempt to locate the AWL directory AWLDIR="${DBADIR}/../../awl" if ! testawldir "${AWLDIR}"; then AWLDIR="/usr/share/awl" if ! testawldir "${AWLDIR}"; then AWLDIR="/usr/local/share/awl" if ! testawldir "${AWLDIR}"; then echo "Unable to find AWL libraries" exit 1 fi fi fi export AWL_DBAUSER=davical_dba export AWL_APPUSER=davical_app # Get the major version for PostgreSQL export DBVERSION="`psql -qXAt -c "SELECT version();" template1 | cut -f2 -d' ' | cut -f1-2 -d'.'`" install_note() { cat >>"${INSTALL_NOTE_FN}" } db_users() { psql -qXAt -c "SELECT usename FROM pg_user;" template1 } create_db_user() { if ! db_users | grep "^${1}$" >/dev/null ; then psql -qXAt -c "CREATE USER ${1} NOCREATEDB NOCREATEROLE;" template1 cat </dev/null; then createlang plpgsql "${DBNAME}" fi } try_db_user() { [ "XtestX`psql -U "${1}" -qXAt -c \"SELECT usename FROM pg_user;\" \"${DBNAME}\" 2>/dev/null`" != "XtestX" ] } # Hide all the annoying NOTICE... messages export PGOPTIONS='--client-min-messages=warning' create_db_user "${AWL_DBAUSER}" create_db_user "${AWL_APPUSER}" # FIXME: Need to check that the database was actually created. if ! createdb --encoding UTF8 --template template0 --owner "${AWL_DBAUSER}" "${DBNAME}" ; then echo "Unable to create database" exit 1 fi # # Try a few alternatives for a database user or give up... if try_db_user "${AWL_DBAUSER}" ; then export DBA="-U ${AWL_DBAUSER}" else if try_db_user "postgres" ; then export DBA="-U postgres" else if try_db_user "${USER}" ; then export DBA="" else if try_db_user "${PGUSER}" ; then export DBA="" else cat <&1 psql -qXAt ${DBA} -f "${AWLDIR}/dba/schema-management.sql" "${DBNAME}" 2>&1 # # Load the DAViCal tables psql -qXAt ${DBA} -f "${DBADIR}/davical.sql" "${DBNAME}" 2>&1 # # Set permissions for the application DB user on the database if ! ${DBADIR}/update-davical-database --dbname "${DBNAME}" --appuser "${AWL_APPUSER}" --nopatch --owner "${AWL_DBAUSER}" ; then cat <