#!/bin/sh
##
##  mediawiki-setup.sh -- Mediawiki RDBMS Setup Utility
##

#   database configuration
db_name="wikimedia"
db_user="wikimedia"
db_pass="wikimedia"

#   determine RDBMS-specific details
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; *$;;'`

#   dispatch operation
cmd="${1:-"install"}"
case "$cmd" in
    install )
        #   create the database
        @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name"
        ( echo "GRANT CREATE, SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';"
          echo "FLUSH PRIVILEGES;"
        ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" mysql

        #   create configure web page
        rm -rf @l_prefix@/lib/mediawiki/config >/dev/null 2>&1 || true
        mkdir  @l_prefix@/lib/mediawiki/config >/dev/null 2>&1 || true
        chmod 775 @l_prefix@/lib/mediawiki/config 2>&1 || true
        chown @l_rusr@:@l_rgrp@ @l_prefix@/lib/mediawiki/config 2>&1 || true
        sed -e "s;\@l_prefix\@;@l_prefix@;g" \
            -e "s;\@l_db_name\@;$db_name;g" \
            -e "s;\@l_db_user\@;$db_user;g" \
            -e "s;\@l_db_pass\@;$db_pass;g" \
            -e "s;\@l_db_suser\@;$db_suser;g" \
            -e "s;\@l_db_spass\@;$db_spass;g" \
            <@l_prefix@/lib/mediawiki/config_index.php.in \
            >@l_prefix@/lib/mediawiki/config/index.php
        chown @l_rusr@:@l_rgrp@ @l_prefix@/lib/mediawiki/config/index.php

        #   open the installation page
        @l_prefix@/bin/w3m http://127.0.0.1:8080/mediawiki/config/

        #   install the resulting configuration and cleanup
        mv  @l_prefix@/lib/mediawiki/config/LocalSettings.php \
            @l_prefix@/lib/mediawiki/LocalSettings.php
        rm -rf @l_prefix@/lib/mediawiki/config >/dev/null 2>&1 || true
        ;;
    uninstall )
        #   remove the database
        ( echo "DROP DATABASE $db_name;"
        ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" mysql

        #   remove the generated configuration
        rm -f @l_prefix@/lib/mediawiki/LocalSettings.php
        ;;
esac

