#!/bin/bash

## Some variables you can change
# MySQL database name
WORDPRESSDB=wordpressdb
# MySQL user name
WORDPRESSUSER=wordpressuser
# Host to set up MySQL process on
UOHOST=shell.uoregon.edu


############
### Determine passwords
############
echo "*** Do you want to install MySQL using the 'mysql_install_db' command? You should do this ONE TIME ONLY"
read -p "Are you an advanced user who needs the MySQL passwords later? [y/n]: " VERIFY
if [ "$VERIFY" == "y" ]
then
	echo "// Installing MySQL!";
	echo "*** You now need to pick a password for you MySQL account. This is PASSWORD A. You will choose PASSWORD B in a moment."
	echo "*** If you know what a root password is, write this down, otherwise, just enter something secure and forget about it."
	read -p "Password A: " PASSWORDA
	echo
	
	echo "*** Please choose a password for your Wordpress Database User (as opposed to the root user)."
	echo "*** This is PASSWORD B. Write it down! You might need it later."
	read -p "Password B: " PASSWORDB
	echo
else
	# Determine passwords for the user and let them know what they are.
	# Random number between 80,000,000 and 100,000,000
	PASSWORDA=$[ $RANDOM % 10000000 + $RANDOM % 10000000 + 80000000 ]
	PASSWORDB=$[ $RANDOM % 10000000 + $RANDOM % 10000000 + 80000000 ]
fi






############
### .htaccess
############
echo "// Creating .htaccess in your public_html directory ($HOME/public_html) so all files ending in .php will execute"
sleep 1

mkdir ~/public_html > /dev/null 2>&1

cd ~/public_html

rm -f .htaccess

cat >> .htaccess << EOF
RemoveHandler .php
AddType application/my-httpd-php .php
Action application/my-httpd-php /~$USER/php.cgi
EOF

chmod 0755 .htaccess



############
### PHP.CGI
############
echo "// Creating php.cgi in your home directory ($HOME) so all files ending in .php will execute"
sleep 1

cd ~/public_html
rm -f php.cgi
cat >> php.cgi << EOF
#!/usr/local/bin/php5
<?php
$pwuid = posix_getpwuid(posix_geteuid());
if (is_file($_SERVER['PATH_TRANSLATED']) &&
      ($pwuid['name'] === 'nobody' ||
       $pwuid['name'] === 'apache' ||
       fileowner($_SERVER['PATH_TRANSLATED']) == posix_geteuid())) {
    chdir(dirname($_SERVER['PATH_TRANSLATED']));
    include(basename($_SERVER['PATH_TRANSLATED']));
}
?>
EOF

chmod 0755 php.cgi



############
### MySQL Prep
############
echo "// Generating a port number (between 5000 and 6000) to run MySQL from"
sleep 1

cd ~

## Get port number
PORT=0
PORTOUTPUT=1
while [ $PORTOUTPUT ]
do
	RANDNUM=$[ $RANDOM % 1000 + 5000 ] # generates a number between 5000 and 6000
	PORT=$RANDNUM
	PORTOUTPUT=`netstat -lt | grep $PORT`
done
echo "// For reference, the port number that MySQL is running on is: $PORT"


############
### MySQL Configuration File
############
echo "// Making a MySQL configuration file for your account"
sleep 1

cd ~

rm -f .htaccess

cat >> .my.cnf << EOF
[mysqld]
datadir=$HOME/mysql/
socket=$HOME/mysql/mysql.sock
port=$PORT
user=$USER

[mysql]
socket=$HOME/mysql/mysql.sock
port=$PORT
user=$USER

[mysql.server]
user=$PORT
basedir=$HOME/mysql/

[client]
host=127.0.0.1
socket=$HOME/mysql/mysql.sock
port=$PORT
user=$USER

[safe_mysqld]
pid-file=$HOME/mysql/mysql.pid
err-log=$HOME/mysql/safe.log
EOF



############
### MySQL - Install the database
############
echo "// Installing the MySQL database. This may take a few moments..."
sleep 1

# echo "*** Do you want to install MySQL using the 'mysql_install_db' command? You should do this ONE TIME ONLY"
# read -p "Install MySQL DB [y/n]: " VERIFY
# if [ "$VERIFY" == "y" ]
# then
# 	echo "// Installing MySQL!";
/usr/bin/mysql_install_db > /dev/null 2>&1
# fi

# sleep to make sure previous commands have finished
sleep 4

## Start Daemon
mysqld_safe --user=mysql < /dev/null > /dev/null 2> /dev/null &


# sleep to make sure previous commands have finished
sleep 4




############
### MySQL - Root Password
############
echo "// Setting a root password for wordpress"
sleep 1

/usr/bin/mysqladmin -u root password $PASSWORDA
/usr/bin/mysqladmin -u root -h shell password $PASSWORDA



############
### MySQL - Create Wordpress DB
############
echo "// Creating a username and password for wordpress"
sleep 1

mysql -uroot -p$PASSWORDA -e "CREATE DATABASE $WORDPRESSDB; USE $WORDPRESSDB; GRANT ALL PRIVILEGES ON *.* TO '$WORDPRESSUSER'@'localhost' IDENTIFIED BY '$PASSWORDB'; GRANT ALL PRIVILEGES ON *.* TO '$WORDPRESSUSER'@'webserv1' IDENTIFIED BY '$PASSWORDB'; GRANT ALL PRIVILEGES ON *.* TO '$WORDPRESSUSER'@'webserv2' IDENTIFIED BY '$PASSWORDB';FLUSH privileges;"
# mysql -uroot -p$PASSWORDA << MYSQLCOMMANDS
# CREATE DATABASE $WORDPRESSDB;
# USE $WORDPRESSDB;
# GRANT ALL PRIVILEGES ON *.* TO $WORDPRESSUSER@'localhost' IDENTIFIED BY '$PASSWORDB';
# GRANT ALL PRIVILEGES ON *.* TO $WORDPRESSUSER@'%' IDENTIFIED BY '$PASSWORDB';
# FLUSH privileges;
# QUIT
# MYSQLCOMMANDS


############
### MySQL - Cron for MySQL Daemon
############
echo "// Configuring a cron job to ensure the MySQL Daemon doesn't go down"
sleep 1

cd ~

# save the script so the cron job has something to call
cat >> mysqld.sh << EOF
if ! mysqladmin ping > /dev/null ; then
       mysqld_safe &
fi
EOF

# make the file executable
chmod 0755 mysqld.sh

## create a file to use as a local crontab
# 15,45 * * * * ./mysqld.sh  (runs cron job every half hour)
cat >> crontab_local << EOF
15,45 * * * * ./mysqld.sh
EOF

# change permissions just in case
chmod 0755 crontab_local

# set the crontab
crontab crontab_local



############
### Wordpress - Files
############
echo "// Downloading and placing wordpress files. This may take a moment...."
sleep 1

cd ~/public_html
wget http://wordpress.org/latest.tar.gz > /dev/null 2>&1
tar -xzf latest.tar.gz
cd wordpress
mv * ..
cd ..
rm latest.tar.gz
rmdir wordpress

# make the uploads dir
echo "// Creating public_html/wp-content/uploads for uploaded images/files"
mkdir ~/public_html/wp-content/uploads

## permissions (recursively)
cd ~/public_html
chmod -R 0755 wp-admin wp-content
touch .htaccess
chmod 0755 .htaccess


############
### Wordpress - Configuration
############
echo "// Configuring wordpress"
sleep 1

cd ~/public_html
rm -f wp-config.php
cp wp-config-sample.php wp-config.php
RAND2=$RANDOM
cat wp-config.php | \
 sed -e "s/putyourdbnamehere/$WORDPRESSDB/" \
     -e "s/usernamehere/$WORDPRESSUSER/" \
     -e "s/yourpasswordhere/$PASSWORDB/" \
     -e "s/localhost/$UOHOST\:$PORT/" \
     -e "s/unique/$RAND2/" \
  >  tmp$$ && mv tmp$$ wp-config.php

echo
echo "// Here is some stuff you can ignore if you don't know what it is:"
echo "    PORT: $PORT"
echo "    MySQL 'root' account password: $PASSWORDA"
echo "    MySQL '$WORDPRESSUSER' account password: $PASSWORDB"
echo "    MySQL database for wordpress: $WORDPRESSDB"
echo "    MySQL host for connections: $UOHOST:$PORT"
echo
echo
echo "SUCCESSS! Visit http://uoregon.edu/~$USER to finish the installation of your blog!"
echo " Note: Please remove index.html (NOT index.php) from your ~/public_html folder so that WordPress can actually work"
echo
echo



