#!/usr/bin/bash
#
# Installs latest wordpress in destination directory, and
# creates mysql user and database for wordpress.
# Requirements: Must have HTTP virtual host already setup.
# Copyright (c) 2016 Joyent Inc., All rights reserved.

usage() {
cat << EOF

Usage: $0 [options] DIRECTORY

Arguments:
    
    DIRECTORY        Directory to install wordpress.

Options:
    
    -u               Username to access database.  [use portal credentials]
    -p               Password to access database.  [use portal credentials]

EOF
exit 2;
}

while getopts "u:p:" OPTION; do
        case $OPTION in
                u)
                        DB_USER=$OPTARG;
                        ;;
                p)
                        DB_PASS=$OPTARG;
                        ;;
                *)
                        usage;
                        ;;
        esac
done

shift $(($OPTIND - 1))
if [[ $# = 1 ]]; then
        WP_DIR=$1;
else
        usage;
fi

export LANG=C
PATH="/opt/local/bin:/opt/local/gnu/bin:/opt/local/sbin:/usr/bin:/usr/sbin";
DB_USER=${DB_USER:-root};
DB_PASS=${DB_PASS:-`mdata-get mysql_pw 2>/dev/null`};
WP_USER=wp_$(zonename | awk -F\- '{ print $5 }');
WP_PASS=$(od -An -N8 -x /dev/random | head -1 | sed 's/^[ \t]*//' | tr -d ' ');
WP_DB=wp_$(od -An -N3 -x /dev/random | head -1 | sed 's/^[ \t]*//' | tr -d ' ');
WP_FILE="latest.tar.gz";
WP_URL="http://wordpress.org/${WP_FILE}";

# Check for tools in PATH
for i in wget gtar gsed sm-create-db sm-create-dbuser mysql; do
	hash $i &>/dev/null
	[[ $? -eq 1 ]] && echo "* ERROR - $i was not found in your PATH." && exit 2;
done

# Check if MySQL is running
[[ ! `pgrep mysqld` ]] && echo "* ERROR - mysql must be running to create the database for wordpress." && exit 2;

# Check that we can connect with DB_USER DB_PASS
mysql -u${DB_USER} -p${DB_PASS} -e "select version();" > /dev/null 2>&1
[[ $? -eq 1 ]] && echo "* ERROR - Can't connect to mysql with username password specified." && exit 2;

# Make destination directory if it doesn't exist
[[ ! -d ${WP_DIR} ]] && mkdir ${WP_DIR};

# Download and extract wordpress
echo "* Downloading latest wordpress.";

wget ${WP_URL} -O ${WP_DIR}/${WP_FILE} > /dev/null 2>&1
[[ $? > 0 ]] && echo "* ERROR - There was an error getting ${WP_URL}." && exit 2;

gtar zxvf ${WP_DIR}/${WP_FILE} -C ${WP_DIR} --strip=1 > /dev/null 2>&1
[[ $? > 0 ]] && echo "* ERROR - There was an error extracting ${WP_FILE} to ${WP_DIR}." && exit 2;

# Configure wordpress
echo "* Configuring wordpress.";

mv ${WP_DIR}/wp-config-sample.php ${WP_DIR}/wp-config.php

gsed -i "s/database_name_here/${WP_DB}/g" ${WP_DIR}/wp-config.php
gsed -i "s/username_here/${WP_USER}/g" ${WP_DIR}/wp-config.php
gsed -i "s/password_here/${WP_PASS}/g" ${WP_DIR}/wp-config.php

# Create database, user and pass
sm-create-db -u${DB_USER} -p${DB_PASS} mysql ${WP_DB}
sm-create-dbuser -u${DB_USER} -p${DB_PASS} mysql ${WP_USER} ${WP_PASS} ${WP_DB}

# Fix permissions on wp-content to allow uploads
chgrp www ${WP_DIR}/wp-content
chmod -R g+rwx ${WP_DIR}/wp-content

echo "* Visit http://x.x.x.x/wp-admin/install.php to finish the install.";
