#!/bin/sh
##
##  davical-setup
##

#   command line argument sanity check
prg="$0"
if [ $# -eq 0 ]; then
    echo "$prg:ERROR: invalid command line" 1>&2
    echo "$prg:USAGE: $prg install [<admin-password>]" 1>&2
    echo "$prg:USAGE: $prg uninstall" 1>&2
    echo "$prg:USAGE: $prg edit" 1>&2
    exit 1
fi

#   database configuration
db_dir="@l_prefix@/var/davical/db"
db_name="davical"
db_user="davical"
db_pass="davical"
db_sname=`grep "^superuser_database" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
          sed -e 's;^ *superuser_database="\(.*\)".*;\1;'`
db_suser=`grep "^superuser_username" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
          sed -e 's;^ *superuser_username="\(.*\)".*;\1;'`
db_spass=`grep "^superuser_password" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
          sed -e 's;^ *superuser_password="\(.*\)".*;\1;'`

#   dispatch operation
cmd="${1:-"install"}"
shift
case "$cmd" in
    install )
        #   determine admin password
        admin_password="$1"
        if [ ".$admin_password" = . ]; then
            admin_password="`@l_prefix@/bin/apg -Mncl -a1 -m12 -n1`"
            echo "ATTENTION: using generated \"admin\" password: \"$admin_password\"" 1>&2
        fi

        #   create database
        ( echo "CREATE ROLE $db_user LOGIN ENCRYPTED PASSWORD '$db_pass' NOCREATEDB NOCREATEUSER;"
          echo "CREATE TABLESPACE $db_name OWNER $db_user LOCATION '$db_dir';"
          echo "CREATE DATABASE $db_name OWNER $db_user TABLESPACE $db_name ENCODING 'UTF8';"
        ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" \
            -c "CREATE LANGUAGE plpgsql;"

        #   create schema 1/2 (from original "create_database" script)
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" -A -t \
            -f "@l_prefix@/lib/davical/awl/dba/awl-tables.sql" 2>&1 | grep -v NOTICE
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" -A -t \
            -f "@l_prefix@/lib/davical/awl/dba/schema-management.sql" 2>&1 | grep -v NOTICE
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" -A -t \
            -f "@l_prefix@/lib/davical/davical/dba/davical.sql" 2>&1 | grep -v NOTICE
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" -A -t \
            -f "@l_prefix@/lib/davical/davical/dba/base-data.sql" 2>&1 | grep -v NOTICE

        #   create schema 2/2 (from original "update-davical-database" script)
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" -A -t \
            -f "@l_prefix@/lib/davical/davical/dba/supported_locales.sql" 2>&1 | grep -v NOTICE
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" -A -t \
            -f "@l_prefix@/lib/davical/davical/dba/caldav_functions.sql" 2>&1 | egrep -v '(NOTICE|ERROR)'
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" -A -t \
            -f "@l_prefix@/lib/davical/davical/dba/rrule_functions.sql" 2>&1 | egrep -v '(NOTICE|ERROR)'
        PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_name" -A -t \
            -f "/opmi/lib/davical/davical/dba/views/dav_principal.sql"
        PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_name" -A -t \
            -f "@l_prefix@/lib/davical/davical/dba/appuser_permissions.txt" 2>&1 | grep -v NOTICE

        #   set admin password
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" \
            -c "UPDATE usr SET password = '**$admin_password' WHERE user_no = 1;"
        ;;

    uninstall )
        PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -d "$db_name" \
            -c "DROP LANGUAGE plpgsql CASCADE;" 2>&1 | grep -v NOTICE
        ( echo "DROP DATABASE $db_name;"
          echo "DROP TABLESPACE $db_name;"
          echo "DROP ROLE $db_user;"
        ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
        ;;

    edit )
        PGPASSWORD="$db_spass" @l_prefix@/bin/psql -U "$db_suser" -d "$db_name"
        ;;
esac

