#! /bin/bash
#
# =======================
# New Nginix Site Script 0.1
# Written by PeeDub
# http://selfpwnt.com
# You may use, modify, and redistribute this script freely
# Released: April 2012
# =======================

# Function to make a php index file:
function make_pindex
{
cat <<- _EOF_
        <html>
        <head><title>$dname</title></head>
        <body><?php echo 'welcome to'; ?>  $dname</body>
        </html>
_EOF_
}

# =======================
#	     header
# =======================
clear
echo "***      Site Setup      ***"

# =======================
# set domain name variable
# =======================
echo -n "==> Enter new domain name (domain.com): "
read dname

echo -n "==> Enter new user for site: "
read nuser

echo -n "==> Enter password for ${nuser}: "
read npass

echo "Setting up files for $dname"

# =======================
# create needed directories
# =======================
mkdir -vp /var/sites/$dname/public_html
mkdir -vp /var/sites/$dname/logs

# =======================
# build index.php file unless we're setting up a wordpress site
# =======================
newwp=no

if [ $# -gt 0 ] && [ $1 == 'wp' ]
then
    newwp=yes
fi

if [ $# -eq 2 ]
then
    if [ $2 == 'i' ]
    then
        newwp=no
    fi
fi

if [ $# -gt 0 ] && [ $1 == 'wp' ] && [ $newwp == 'yes' ]
then
    wget http://wordpress.org/latest.tar.gz -P /var/sites/$dname/public_html
    tar --strip-components=1 -xzvf /var/sites/$dname/public_html/latest.tar.gz -C /var/sites/$dname/public_html
    rm -f /var/sites/$dname/public_html/latest.tar.gz
else
    make_pindex > /var/sites/$dname/public_html/index.php
    echo "created /var/sites/$dname/public_html/index.php"
fi

# =======================
# build vhost config file
# =======================
if [ $# -gt 0 ] && [ $1 == 'wp' ]
then
    cp /usr/local/share/nginx-vhost-templates/example-wp.com /etc/nginx/sites-available/$dname
else
    cp /usr/local/share/nginx-vhost-templates/example.com /etc/nginx/sites-available/$dname
fi

echo "created /etc/nginx/sites-available/$dname"

# =======================
# enable site
#========================
ln -s /etc/nginx/sites-available/$dname /etc/nginx/sites-enabled/$dname
echo "enabled $dname"

# Find and replace in vhost config
sed -i 's/example.com/'$dname'/g' /etc/nginx/sites-available/$dname

#========================
# build php pool file
#========================
cp /usr/local/share/php-fpm-pool-templates/example.com.conf /etc/php5/fpm/pool.d/${dname}.conf
echo "created /etc/php5/fpm/pool.d/${dname}.conf"

# Find and replace in php pool config
sed -i 's/example.com/'$dname'/g' /etc/php5/fpm/pool.d/${dname}.conf
sed -i 's/example/'$nuser'/g' /etc/php5/fpm/pool.d/${dname}.conf

# ======================
# Create User
# ======================
useradd -M -s /sbin/nologin $nuser -p `mkpasswd $npass`
echo "created user: $nuser"

# =======================
# Set owner of public_html directory
# =======================
chown -R ${nuser}:${nuser} /var/sites/$dname/public_html
echo "Set ownership of web directory to $nuser"

# =======================
# Restart nginx and php
# =======================
/etc/init.d/nginx reload
echo "Done!"
/etc/init.d/php5-fpm restart
echo "Done!"

# =======================
# Setting up SFTP
# =======================
cp /usr/local/share/sftp_user /tmp
sed -i 's/example.com/'$dname'/g' /tmp/sftp_user
sed -i 's/example/'$nuser'/g' /tmp/sftp_user
cat /tmp/sftp_user >> /etc/ssh/sshd_config
echo "SFTP configuration complete"
/etc/init.d/ssh restart

# =======================
#    exit
# =======================

echo "***      Finished setting up hosting for $dname. Goodbye!"
exit
