#!/bin/sh

DA_MYSQL=/usr/local/directadmin/conf/mysql.conf
if [ ! -s ${DA_MYSQL} ]; then
        echo "Cannot find ${DA_MYSQL} so cannot create PMA tables";
        exit 1;
fi

MYSQLUSER=`grep -m1 "^user=" ${DA_MYSQL} | cut -d= -f2`
MYSQLPASSWORD=`grep -m1 "^passwd=" ${DA_MYSQL} | cut -d= -f2`

PHPMYADMIN_USER=da_phpmyadmin
PHPMYADMIN_PASS=`perl -le'print map+(A..Z,a..z,0..9)[rand 62],0..15'`;

if [ `grep -m1 -c -e "^host=" ${DA_MYSQL}` -gt "0" ]; then
        MYSQLHOST=`grep -m1 "^host=" ${DA_MYSQL} | cut -d= -f2`
else
        MYSQLHOST=localhost
fi

MYSQL_ACCESS_HOST=localhost
if [ "$MYSQLHOST" != "localhost" ]; then
        #its a remote databsae, so connections would come from the server IP.
        if [ -s ${WORKDIR}/scripts/setup.txt ]; then
                MYSQL_ACCESS_HOST=`cat ${WORKDIR}/scripts/setup.txt | grep -m1 -e '^ip=' | cut -d= -f2`
        fi

        #if we're on a LAN, then things change.
        if [ -s ${DA_MYSQL} ]; then
                if [ `grep -m1 -c -e "^access_host=" ${DA_MYSQL}` = "0" ]; then
                        MYSQL_ACCESS_HOST=`grep -m1 "^access_host=" ${DA_MYSQL} | cut -d= -f2`
                fi
        fi
fi

#end initMySQL()


SQL=/var/www/html/phpMyAdmin/examples/create_tables.sql
if [ ! -s ${SQL} ]; then
        SQL=/var/www/html/phpMyAdmin/sql/create_tables.sql
        if [ ! -s ${SQL} ]; then
                echo "Cannot find $SQL so cannot create PMA tables";
                exit 2;
        fi
fi

mysql -u${MYSQLUSER} -p${MYSQLPASSWORD} < ${SQL}
mysql -e "GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER,LOCK TABLES,INDEX ON phpmyadmin.* TO '${PHPMYADMIN_USER}'@'${MYSQL_ACCESS_HOST}' IDENTIFIED BY '${PHPMYADMIN_PASS}';" --host=${MYSQLHOST} --user=${MYSQLUSER} --password=${MYSQLPASSWORD}
mysql -e "SET PASSWORD FOR '${PHPMYADMIN_USER}'@'${MYSQL_ACCESS_HOST}' = PASSWORD('${PHPMYADMIN_PASS}');" --host=${MYSQLHOST} --user=${MYSQLUSER} --password=${MYSQLPASSWORD}
mysql -e "FLUSH PRIVILEGES;" --host=${MYSQLHOST} --user=${MYSQLUSER} --password=${MYSQLPASSWORD}

CONFIG=/usr/local/directadmin/custombuild/custom/phpmyadmin/config.inc.php

#if dir doesn't exist, create.. then if file doesn't exist, cp from regular pma location..

[[ -d /usr/local/directadmin/custombuild/custom/phpmyadmin/ ]] || mkdir -p /usr/local/directadmin/custombuild/custom/phpmyadmin/
[[ -f /usr/local/directadmin/custombuild/custom/phpmyadmin/config.inc.php  ]] ||  cp /var/www/html/phpMyAdmin/config.inc.php /usr/local/directadmin/custombuild/custom/phpmyadmin/config.inc.php

#heredoc to enable features by appending to the new custom config file

cat <<-'EOF' >> $CONFIG
$cfg['Servers'][$i]['controlhost'] = '';
$cfg['Servers'][$i]['controluser'] = '';
$cfg['Servers'][$i]['controlpass'] = '';
$cfg['Servers'][$i]['pmadb'] = '';
$cfg['Servers'][$i]['bookmarktable'] = '';
$cfg['Servers'][$i]['relation'] = '';
$cfg['Servers'][$i]['relation'] = '';
$cfg['Servers'][$i]['table_info'] = '';
$cfg['Servers'][$i]['table_coords'] = '';
$cfg['Servers'][$i]['pdf_pages'] = '';
$cfg['Servers'][$i]['column_info'] = '';
$cfg['Servers'][$i]['history'] = '';
$cfg['Servers'][$i]['recent'] = '';
$cfg['Servers'][$i]['favorite'] = '';
$cfg['Servers'][$i]['table_uiprefs'] = '';
$cfg['Servers'][$i]['tracking'] = '';
$cfg['Servers'][$i]['userconfig'] = '';
$cfg['Servers'][$i]['users'] = '';
$cfg['Servers'][$i]['usergroups'] = '';
$cfg['Servers'][$i]['navigationhiding'] = '';
$cfg['Servers'][$i]['savedsearches'] = '';
$cfg['Servers'][$i]['central_columns'] = '';
$cfg['Servers'][$i]['designer_settings'] = '';
$cfg['Servers'][$i]['export_templates'] = '';
$cfg['Servers'][$i]['tracking_version_auto_create'] = false;
$cfg['Servers'][$i]['tracking_default_statements']
    = 'CREATE TABLE,ALTER TABLE,DROP TABLE,RENAME TABLE,CREATE INDEX,' .
      'DROP INDEX,INSERT,UPDATE,DELETE,TRUNCATE,REPLACE,CREATE VIEW,' .
      'ALTER VIEW,DROP VIEW,CREATE DATABASE,ALTER DATABASE,DROP DATABASE';
$cfg['Servers'][$i]['tracking_add_drop_view'] = true;
$cfg['Servers'][$i]['tracking_add_drop_table'] = true;
$cfg['Servers'][$i]['tracking_add_drop_database'] = true;
$cfg['Export']['odt_relation'] = true;
$cfg['Export']['latex_relation'] = true;
$cfg['Export']['sql_relation'] = false;
EOF

#set the permissions for security:
chown webapps:apache ${CONFIG}
chmod 440 ${CONFIG}


#Build with CB
cd /usr/local/directadmin/custombuild/
./build phpmyadmin

exit 0;
