#!/bin/bash
_HOSTSFILE=/etc/hosts;
GOOD="NO";
#path_zend_sites="docs/";
path_zend_apache="/usr/local/zend/apache2/";
path_zend_apache_logs="/usr/local/zend/apache2/logs/";
path_zend_vhosts="/usr/local/zend/apache2/conf/extra/";
path_zend_vhost_file="/usr/local/zend/apache2/conf/extra/httpd-vhosts.conf";
vhost_file_name="httpd-vhosts.conf";

echo "Enter a domain. e.g. example.com";
read domain;
echo "Enter a subdomain name. e.g. vhost as in vhost.example.com";
read vhost;
echo "Will the site live in /usr/local/zend/apache2/docs or Sites (default)? [s or z]";
read set_sitepath; 

case ${set_sitepath} in
	"s")
	GOOD="YES";
	;;
	"")
	GOOD="YES";
	;;
	"z")
	GOOD="YES";
	;;
	*)
	echo "::::: BEYATCH!!! ::::::";
	echo "You entered ${set_sitepath}."
	echo "The only valid options are s, z, and to leave it blank (which is the same as s)."
	echo "Try again please."
	# echo ${GOOD};
	exit 1;
	;;
esac

if [[ "${set_sitepath}" == "s" || "${set_sitepath}" == "" ]];then
	echo "You chose to create the site in the Sites directory.";
	echo "Please enter the username of the Sites directory owner.";
	read set_username;
	# dir_sitepath="/home/${set_username}/public_html/";
	dir_sitepath="/Users/${set_username}/Sites/";
	# echo $dir_sitepath;
fi

if [[ "${set_sitepath}" == "z" ]];then
	dir_sitepath="/usr/local/zend/apache2/docs/";
	# echo $dir_sitepath;
fi

# echo "The dir_sitepath = "${dir_sitepath};
echo "================================================";
echo "================================================";
echo "The dir_sitepath = "${dir_sitepath};

# echo "The path_zend_sites variable = ${path_zend_sites}";
# echo "The path_zend_apache variable = ${path_zend_apache}";
# echo "The path_zend_apache_logs variable = ${path_zend_apache_logs}";
# echo "The path_zend_vhosts variable  ${path_zend_vhosts}";
# echo "The path_zend_vhost_file variable = ${path_zend_vhost_file}";
# echo "The path_zend_vhost_logs variable = ${path_zend_vhost_logs}";
# echo "================================================";
# echo "================================================";

if [[ "${domain}" != "" && "${vhost}" != "" && ${GOOD} == "YES" ]]; then
	# if [[ ! -e /var/www/${vhost} ]]; then
	# 	mkdir /var/www/${vhost};
	# fi
	# if [[ ! -e /var/www/${vhost}/htdocs ]]; then
	# 	mkdir /var/www/${vhost}/htdocs;
	# fi
	# chown -R daemon:daemon ${path_zend_apache}${}${vhost};
	# 
	# if [[ ! -e /etc/apache2/backup ]]; then
	# 	mkdir /etc/apache2/backup;
	# fi
	# 
	# cp /etc/apache2/httpd.conf /etc/apache2/backup/httpd.conf.`date +%s`;
	
	# echo "${dir_sitepath}${vhost}";

## Check that the directory where the virtual host will live exists
## If no then make it - If yes then echo it and exit;
	if [[ ! -e ${dir_sitepath}${vhost} ]]; then
		mkdir ${dir_sitepath}${vhost};
	else
		echo "${dir_sitepath}${vhost} already exists.";
		exit;
	fi

## Create an index page just to test immediately
	if [[ ! -e ${dir_sitepath}${vhost}/index.html ]]; then
		sh /Users/${set_username}/Documents/Scripts/make_page.sh > ${dir_sitepath}${vhost}/index.html;
	else
		echo "${dir_sitepath}${vhost}/index.html already exists.";
		exit;
	fi
	
## Set the permissions properly since this script is run with sudo
	if [[ ! "${set_sitepath}" == "z" ]];then
		# chown -R root:wheel ${dir_sitepath}${vhost};
		# else
		`chown -R ${set_username}:staff ${dir_sitepath}${vhost}`;
	fi
# exit;
## Check that the backup directory exists so that we
## can create a backup of the httpd-vhosts.conf file.
	if [[ ! -e ${path_zend_vhosts}backup ]]; then
		mkdir ${path_zend_vhosts}backup;
	fi

	# echo `ls ${path_zend_vhosts}`;
	# echo "${path_zend_vhost_file}";
# exit;

## Make a backup of the httpd-vhosts.conf file in case we need to roll-back.
	cp ${path_zend_vhost_file} ${path_zend_vhosts}backup/${vhost_file_name}.`date +%s`;

	# cp /etc/apache2/httpd.conf /etc/apache2/backup/httpd.conf.`date +%s`;
	# Touch the file that will hold the vhost - name is the same as the vhost
		# touch /etc/apache2/sites-available/${vhost};
		# 
		# vhostFile="/etc/apache2/sites-available/${vhost}";
		# 
		# echo ${vhostFile};
# exit;

	echo "\n" >> ${path_zend_vhost_file};
	echo "### ${vhost}.${domain}" >> ${path_zend_vhost_file};
	echo "<VirtualHost *:80>" >> ${path_zend_vhost_file};
	echo "	ServerName ${vhost}.${domain}" >> ${path_zend_vhost_file};
	echo "  #ServerAlias www.${vhost}.${domain} *.${vhost}.${domain} ${vhost}.${domain}" >> ${path_zend_vhost_file};
	echo "	DocumentRoot ${dir_sitepath}${vhost}" >> ${path_zend_vhost_file};
	echo "	LogLevel warn" >> ${path_zend_vhost_file};
	echo "	CustomLog ${path_zend_apache_logs}access-error_${vhost}.log combined" >> ${path_zend_vhost_file};
	echo "\n";
	echo "	<Directory ${dir_sitepath}${vhost}>" >> ${path_zend_vhost_file};
	echo "		Options All MultiViews ExecCGI" >> ${path_zend_vhost_file};
	echo "		AddHandler cgi-script cgi pl" >> ${path_zend_vhost_file};
	echo "		AllowOverride All" >> ${path_zend_vhost_file};
	echo "		Order allow,deny" >> ${path_zend_vhost_file};
	echo "		Allow from all" >> ${path_zend_vhost_file};
	echo "	</Directory>" >> ${path_zend_vhost_file};
	echo "</VirtualHost>" >> ${path_zend_vhost_file};
	echo "#<VirtualHost *:443>" >> ${path_zend_vhost_file};
	echo "#	 ServerName ${vhost}.${domain}" >> ${path_zend_vhost_file};
	echo "#	 DocumentRoot ${dir_sitepath}${vhost}" >> ${path_zend_vhost_file};
	echo "#	 SSLEngine On" >> ${path_zend_vhost_file};
	echo "#	 SSLCertificateFile /etc/apache2/ssl/apache.pem" >> ${path_zend_vhost_file};
	echo "#	 LogLevel warn" >> ${path_zend_vhost_file};
	echo "#	 CustomLog ${path_zend_apache_logs}access-error_${vhost}.log combined" >> ${path_zend_vhost_file};
	echo "#</VirtualHost>" >> ${path_zend_vhost_file};

	echo "The following was added to ${path_zend_vhost_file}";
	echo "";
	tail -22 ${path_zend_vhost_file}
	echo "";

	zendapache2ctl="/usr/local/zend/apache2/bin/apachectl";

	echo "Would you like to run an apache config test? [y or n]";
	read is_config_test;

	if [[ "${is_config_test}" == "y" ]]; then
		$zendapache2ctl configtest;
	fi

	echo "Would you like to restart apache now? [y or n]";
	read is_restart;

	if [[ "${is_restart}" == "y" ]]; then
		echo "Restarting...";
		$zendapache2ctl graceful; 
	fi;
	
	## Need to add the hostname to the hosts file for local dev
	echo "Would you like to add the vhost ${vhost}.${domain} to the hosts file? [y or n]";
	read add_to_hosts;
	
	if [[ "${add_to_hosts}" == "y" ]]; then
		if [[ ! -e ${_HOSTSFILE}_backup_dir ]]; then
			mkdir ${_HOSTSFILE}_backup_dir;
		fi

		## Backup the 
		cp ${_HOSTSFILE} ${_HOSTSFILE}_backup_dir/hosts_bkup_`date +%s`;
		
		while read line   
		do   
		    if [[ "`echo "${line}" | grep '127.0.0.1'`" != "" ]]; then
				chgline="${line} ${vhost}.${domain}";
				`sed 's/'"${line}"'/'"${chgline}"'/g' ${_HOSTSFILE} > ${_HOSTSFILE}_tmp`;
			fi   
		done < ${_HOSTSFILE}
		
		if [[ "${chgline}" != "" ]]; then
			# cat ${_HOSTSFILE}_tmp;
			# exit 1;
			cp -i ${_HOSTSFILE}_tmp ${_HOSTSFILE};
			rm ${_HOSTSFILE}_tmp;
			# cat ${_HOSTSFILE};	
			echo "The following was written to ${_HOSTSFILE}:";
			echo "${chgline};"
			echo "Here is the line from the ${_HOSTSFILE} file:"
			cat ${_HOSTSFILE} | grep '127.0.0.1';
		else
			echo "There is no line with 127.0.0.1 in the /etc/hosts file.";
		fi
		
		## Run this command to flush the hostnames - this will allow you 
		## to browse to your new site right away even if it is a valid
		## hostname registered with DNS.
		`dscacheutil -flushcache`;
	fi

	echo "All done!!!";
fi
exit 1;