#!/bin/bash

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 /var/www or public_html? [v or p]";
read set_sitepath; 
##value is either V for var/www of P for public - default to P
echo $set_sitepath;

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

if [[ "${set_sitepath}" == "v" ]];then
	dirpath="/var/www/";
	echo $dirpath;
fi

echo "The dirpath = "${dirpath};

if [[ "${domain}" != "" && "${vhost}" != "" ]]; 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 www-data:www-data /var/www/${vhost};
	# 
	# 	if [[ ! -e /etc/apache2/backup ]]; then
	# 		mkdir /etc/apache2/backup;
	# 	fi
	
	if [[ ! -e ${dirpath}.${vhost} ]]; then
		mkdir ${dirpath}${vhost};
	fi
	
	# if [[ ! -e ${dirpath}.${vhost}/htdocs ]]; then
	# 	mkdir ${dirpath}.${vhost}/htdocs;
	# fi
	if [[ "${set_sitepath}" == "v" ]];then
		chown -R www-data:www-data ${dirpath}${vhost};
	else
		chown -R ${set_username}:${set_username} ${dirpath}${vhost};
	fi

	# if [[ ! -e /etc/apache2/backup ]]; then
	# 	mkdir /etc/apache2/backup;
	# fi
	
		# 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 "" >> ${vhostFile};
	echo "### ${vhost}.${domain}" >> ${vhostFile};
	echo "<Directory ${dirpath}${vhost}>" >> ${vhostFile};
	echo "	Options All MultiViews ExecCGI" >> ${vhostFile};
	echo "	AddHandler cgi-script cgi pl" >> ${vhostFile};
	echo "	AllowOverride All" >> ${vhostFile};
	echo "	Order allow,deny" >> ${vhostFile};
	echo "	Allow from all" >> ${vhostFile};
	echo "</Directory>" >> ${vhostFile};
	echo "<VirtualHost *:80>" >> ${vhostFile};
	echo "	ServerName ${vhost}.${domain}" >> ${vhostFile};
	echo "	DocumentRoot ${dirpath}${vhost}" >> ${vhostFile};
	echo "	LogLevel warn" >> ${vhostFile};
	echo "	CustomLog /var/log/apache2/access_${vhost}.log combined" >> ${vhostFile};
	echo "</VirtualHost>" >> ${vhostFile};
	echo "<VirtualHost *:443>" >> ${vhostFile};
	echo "	ServerName ${vhost}.${domain}" >> ${vhostFile};
	echo "	DocumentRoot ${dirpath}${vhost}" >> ${vhostFile};
	echo "	SSLEngine On" >> ${vhostFile};
	echo "	SSLCertificateFile /etc/apache2/ssl/apache.pem" >> ${vhostFile};
	echo "	LogLevel warn" >> ${vhostFile};
	echo "	CustomLog /var/log/apache2/access_${vhost}.log combined" >> ${vhostFile};
	echo "</VirtualHost>" >> ${vhostFile};

	echo "The following was added to ${vhostFile}";
	echo "";
	# tail -24 /etc/apache2/httpd.conf
	cat ${vhostFile};
	echo "";

	apache2ctl=`which apache2ctl`;
	a2ensite=`which a2ensite`;

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

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

	echo "Would you like to enable ${vhost}.${domain}? [y or n]";
	read is_enable_vhost;

	if [[ "${is_enable_vhost}" == "y" ]]; then
		$a2ensite ${vhost};
	fi
	
	echo "Would you like to restart apache now? [y or n]";
	read is_restart;

	if [[ "${is_restart}" == "y" ]]; then
		echo "Restarting...";
		$apache2ctl graceful; 
	fi;

	echo "All done!!!";
fi
