#!@l_bash@
##
##  drupal-setup.sh -- Drupal 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 [<database-directory>]" 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

#   database configuration
db_dir="@l_prefix@/var/drupal/db"
db_dump="@l_prefix@/var/drupal/dump"
db_type="@l_dbtype@"
db_name="drupal"
db_user="drupal"
db_pass="drupal"

#   determine RDBMS-specific details
if [ ".$db_type" = .mysql ]; then
    db_sname="mysql"
    db_suser=`grep "^user" @l_prefix@/etc/mysql/my.pwd |\
              sed -e 's;^user[^=]*= *;;' -e 's; *$;;'`
    db_spass=`grep "^password" @l_prefix@/etc/mysql/my.pwd |\
              sed -e 's;^password[^=]*= *;;' -e 's; *$;;'`
elif [ ".$db_type" = .pgsql ]; then
    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;'`
fi

#   dispatch operation
cmd="${1:-"install"}"
shift
case "$cmd" in
    install )
        ##
        ##  create the database
        ##

        if [ $# -gt 0 ]; then
            db_dir="$1"
            shift
        fi
        if [ ".$db_type" = .mysql ]; then
            #   FIXME: MySQL 5.0 still doesn't allow easy relocation of tablespaces
            @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name"
            ( echo "GRANT ALL PRIVILEGES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';"
              echo "FLUSH PRIVILEGES;"
            ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
        elif [ ".$db_type" = .pgsql ]; then
            ( 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' TEMPLATE template0;"
            ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
        fi

        #   activate configuration directory
        #   (is automatically deactivated by installer afterwards)
        if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then
            chmod 777 @l_prefix@/share/drupal/sites/default
        fi
        ;;

    uninstall )
        ##
        ##  remove the database
        ##

        if [ ".$db_type" = .mysql ]; then
            ( echo "DROP DATABASE $db_name;"
            ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
        elif [ ".$db_type" = .pgsql ]; then
            ( 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-
        fi

        #   remove the generated configuration
        if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then
            rm -f @l_prefix@/share/drupal/sites/default/settings.php
        fi
        ;;

    backup )
        ##
        ##  backup the database
        ##

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

        #   dump database content to dumpfile (compressed plain SQL format)
        umask 007
        if [ ".$db_type" = .mysql ]; then
            @l_prefix@/bin/mysqldump --user="$db_suser" --password="$db_spass" "$db_name" | \
            @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
        elif [ ".$db_type" = .pgsql ]; then
            PGPASSWORD="$db_spass" @l_prefix@/bin/pg_dump -U "$db_suser" -S "$db_suser" "$db_name" | \
            @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
        fi
        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/drupal-dump-$dumpfile.sql.bz2" ;;
        esac
        if [ ! -f $dumpfile ]; then
            echo "drupal-setup:restore:ERROR: no such dump file: $dumpfile" 1>&2
            exit 1
        fi

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

        #   drop old and initialize new database
        DRUPAL_SETUP_RESTORE=1
        export DRUPAL_SETUP_RESTORE
        $prg uninstall || exit $?
        $prg install   || exit $?

        #   restore database content from dumpfile (compressed plain SQL format)
        if [ ".$db_type" = .mysql ]; then
            @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
            @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name" >/dev/null
        elif [ ".$db_type" = .pgsql ]; then
            @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
            PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_name" -f- >/dev/null
        fi

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

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

        if [ ".$db_type" = .mysql ]; then
            @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name"
        elif [ ".$db_type" = .pgsql ]; then
            PGPASSWORD="$db_spass" @l_prefix@/bin/psql -U "$db_suser" -d "$db_name"
        fi
        ;;
esac

