#!/bin/sh
##
##  yourls-setup
##

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

#   determine MySQL root password
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 MySQL database for Wordpress
        @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name"
        ( echo "GRANT ALL 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
        ;;
    uninstall )
        #   remove the database
        ( echo "DROP DATABASE $db_name;"
        ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" mysql
        ;;
esac

