#!/bin/sh
##
##  punbb-setup.sh -- PunBB RDBMS Setup Utility
##

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

#   determine database details
db_dir="@l_prefix@/var/punbb/db"
db_dump="@l_prefix@/var/punbb/dump"
db_name="punbb"
db_user="punbb"
db_pass="punbb"
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;'`
db_host=`grep "^listen_addresses" @l_prefix@/var/postgresql/db/postgresql.conf |\
          sed -e "s;^listen_addresses *= *'\\(.*\\)'.*;\1;"`
db_port=`grep "^port" @l_prefix@/var/postgresql/db/postgresql.conf |\
          sed -e 's;^port *= *\([0-9][0-9]*\).*;\1;'`
if [ ".$db_host" = . ]; then db_host="localhost"; fi
if [ ".$db_port" = . ]; then db_port="5432"; fi

#   dispatch operation
cmd="${1:-"install"}"
shift
case "$cmd" in
    install )
        ##
        ##  create the 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;"
        ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql \
            -h "$db_host" -p "$db_port" -U "$db_suser" -d "$db_sname" -f-
        ;;

    uninstall )
        ##
        ##  remove the database
        ##

        ( echo "DROP DATABASE $db_name;"
          echo "DROP TABLESPACE $db_name;"
          echo "DROP ROLE $db_user;"
        ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql \
            -h "$db_host" -p "$db_port" -U "$db_suser" -d "$db_sname" -f-
        rm -rf @l_prefix@/var/punbb/db/* >/dev/null 2>&1 || true
        ;;

    backup )
        ##
        ##  backup the database
        ##

        #   determine dumpfile
        if [ $# -gt 0 ]; then
            #   manually managed dumpfile
            dumpfile="$1"
        else
            #   automatically managed (rotated) dumpfile
            rm -f $db_dump/punbb-dump-9.sql.gz >/dev/null 2>&1 || true
            i=8
            while [ $i -ge 0 ]; do
                if [ -f $db_dump/punbb-dump-$i.sql.bz2 ]; then
                    mv $db_dump/punbb-dump-$i.sql.bz2 $db_dump/punbb-dump-`expr $i + 1`.sql.bz2
                fi
                i=`expr $i - 1`
            done
            dumpfile="$db_dump/punbb-dump-0.sql.bz2"
        fi

        #   dump database content to dumpfile (compressed plain SQL format)
        umask 007
        PGPASSWORD="$db_spass" @l_prefix@/bin/pg_dump \
            -h "$db_host" -p "$db_port" -U "$db_suser" -S "$db_suser" "$db_name" | \
        @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
        chown @l_rusr@:@l_rgrp@ $dumpfile >/dev/null 2>&1 || true
        chmod 640 $dumpfile >/dev/null 2>&1 || true
        ;;

    restore )
        ##
        ##  restore the database
        ##

        #   determine dumpfile
        if [ $# -gt 0 ]; then
            dumpfile="$1"
        else
            dumpfile="0"
        fi
        case "$dumpfile" in
            [0-9] ) dumpfile="$db_dump/punbb-dump-$dumpfile.sql.bz2" ;;
        esac
        if [ ! -f $dumpfile ]; then
            echo "punbb-setup:restore:ERROR: no such dump file: $dumpfile" 1>&2
            exit 1
        fi

        #   optionally stop PunBB
        eval `@l_prefix@/bin/openpkg rc punbb status 2>/dev/null || true`
        if [ ".$punbb_active" = .yes ]; then
            @l_prefix@/bin/openpkg rc punbb stop >/dev/null 2>&1 || true
        fi

        #   drop old and initialize new database
        $prg uninstall || exit $?
        $prg install   || exit $?

        #   restore database content from dumpfile (compressed plain SQL format)
        @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
        PGPASSWORD="$db_spass" @l_prefix@/bin/psql \
            -q -h "$db_host" -p "$db_port" -U "$db_suser" -d "$db_name" -f- >/dev/null

        #   optionally restart PunBB
        if [ ".$punbb_active" = .yes ]; then
            @l_prefix@/bin/openpkg rc punbb start >/dev/null 2>&1 || true
        fi
        ;;

    edit )
        ##
        ##  interactively edit the database
        ##

        PGPASSWORD="$db_spass" @l_prefix@/bin/psql \
            -h "$db_host" -p "$db_port" -U "$db_suser" -d "$db_name"
        ;;
esac

