#!/bin/bash

######################################################################
# Check Root Permission                                              #
# Quit if Current User Without Root Permission.                      #
######################################################################
if [ $(id -u) != "0" ]; then
    echo "Error: To Run This, You Must Have Root Permission."
	echo "TIPS: Please Login as Root or Use 'sudo'."
    exit 1
fi

######################################################################
# Declear Function pak()                                             #
# echo -n "Press Any Key to Continue"                                #
# character=`pak`                                                    #
######################################################################
pak() { 
SAVEDSTTY=`stty -g` 
stty cbreak 
dd if=/dev/tty bs=1 count=1 2>/dev/null 
stty -cbreak 
stty $SAVEDSTTY 
} 

######################################################################
# Clear Screen                                                       #
######################################################################
clear

######################################################################
# Add Virtual Host                                                   #
######################################################################
echo "-------------------------------------------------------------------------"
echo "  Add Virtual Host for CNMP"
echo "-------------------------------------------------------------------------"
echo "  The script is going to add a Virtual Host on this server"
echo "-------------------------------------------------------------------------"
echo ""
subornot="no"
echo ""
echo "Do you wanna add a Virtual Host With Sub-Domain Name? [No/yes]"
read -p "If no, left blank : " subornot
echo ""
if [ "$subornot" == "" ]; then
subornot="no"
fi

subornot_tmp=$(echo $subornot | tr [A-Z] [a-z])

if [ "$subornot_tmp" = "no" ]; then
	vhostdom="zeddicus.com"
	echo ""
	echo "Please enter a domain name without 'www': "
	read -p "(Default domain: zeddicus.com): " vhostdom
	echo ""
	if [ "$vhostdom" = "" ]; then
		vhostdom="zeddicus.com"
	fi

elif [ "$subornot_tmp" = "yes" ]; then

vhostdom="sub.zeddicus.com"
echo ""
echo "Please enter a sub-domain name: "
read -p "(Default sub-domain name: sub.zeddicus.com)" vhostdom
echo ""
if [ "$vhostdom" = "" ]; then
		vhostdom="sub.zeddicus.com"

fi

else
clear
echo ""
echo ""
echo "  You can only type 'Yes' or 'No' !!! "
echo ""
echo ""
exit 0
fi

	vhostdir="/home/vhost"
	echo ""
	echo "Please input the directory for the domain : "
	read -p "(Default directory: /home/vhost/$vhostdom): " vhostdir
	echo ""
	if [ "$vhostdir" = "" ]; then
		vhostdir="/home/vhost/$vhostdom"
	fi

	echo ""
	echo "Press any key to start..."
	character=`pak`


mkdir /usr/local/nginx/conf/vhost
mkdir /home/vhost
mkdir $vhostdir
if [ "$subornot_tmp" = "no" ]; then
vhostdomfinal="$vhostdom www.$vhostdom"
else
vhostdomfinal="$vhostdom"
fi

cat >/usr/local/nginx/conf/vhost/$vhostdom.conf<<eof
server
	{
		listen       80;
		server_name $vhostdomfinal;
		index index.html index.htm index.php default.html default.htm default.php;
		root $vhostdir;

		location ~ .*\.(php|php5)?$
			{
				fastcgi_pass  unix:/tmp/php-cgi.sock;
				#fastcgi_pass  127.0.0.1:9000;
				fastcgi_index index.php;
				include fcgi.conf;
			}

		location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
			{
				expires      30d;
			}

		location ~ .*\.(js|css)?$
			{
				expires      12h;
			}

		access_log   off;
	}
eof


chmod -R 755 $vhostdir
chown -R www $vhostdir
/usr/local/nginx/sbin/nginx -t
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

clear
echo "-------------------------------------------------------------------------"
echo "Project BIGCOOL - CNMP, Written By Zeddicus Lau"
echo "-------------------------------------------------------------------------"
echo ""
echo "For more information please visit http://Zeddicus.com"
echo ""
echo "  Virtual Host Root Directory:"
echo "  $vhostdir"
echo ""
echo "  Virtual Host .conf File Directory"
echo "  $vhostdom.conf"
echo ""
echo "-------------------------------------------------------------------------"

