#! /bin/bash 
# 
# =======================
# Siteup Script 0.1
# Written by Command Line Idiot
# http://commandlineidiot.com
# You may use, modify, and redistribute this script freely
# Released: August 2007
# =======================

# =======================
#	set functions
# =======================
#	make_index is the function to create a basic index.html file
# 	within the documents directory of the new domain. The variable
# 	for domain name is passed into the file at $dname. You can alter
#	any of the code between the terms _EOF_ and it will be reflected
#	in the index.html file.

function make_index
{
cat <<- _EOF_
	<html>
	<head><title>$dname</title></head>
	<body>welcome to $dname</body> 
	</html>
_EOF_
} 

#	make_vhost is the function to create a config file that
#	Apache2 can interpret. The variable for the domain name is passed
#	into the file at $dname, and the system-wide variable for username
#	is passed into the file at $USER. You may wish to replace the
#	ServerAdmin email address with your own email address. You may alter
#	any of the code between the terms _EOF_ to build your own preferred 
#	standard config file.  

function make_vhost
{
cat <<- _EOF_
	<VirtualHost *:80>
	        ServerAdmin $USER@localhost
	        ServerName $dname
	        ServerAlias *.$dname

	        DirectoryIndex index.html index.htm index.php
	        DocumentRoot /home/$USER/$dname/

	        ScriptAlias /cgi-bin/ /home/$USER/$dname/cgi-bin/
	        <Location /cgi-bin>
	                Options +ExecCGI
	        </Location>

	        <Directory /home/$USER/$dname/>
	                Options Indexes FollowSymLinks MultiViews
	                AllowOverride All
	                Order allow,deny
	                Allow from all
	        </Directory>

	        ErrorLog /home/$USER/logs/$dname/error.log
	        CustomLog /home/$USER/logs/$dname/access.log combined
	</VirtualHost>
_EOF_
}

# =======================
#	     header
# =======================
echo "***      Site Setup      ***"
 
# =======================
# set domain name variable
# =======================
echo -n "==> Enter username: "
read USER
echo -n "==> Enter new domain name (domain.com): "
read dname
echo "Setting up files for $dname"



# =======================
# create needed directories
# =======================
mkdir -vp /home/$USER/$dname
mkdir -vp /home/$USER/$dname/cgi-bin
mkdir -vp /home/$USER/logs/$dname
touch /home/$USER/logs/$dname/access.log 
echo "created /home/$USER/logs/$dname/access.log"
touch /home/$USER/logs/$dname/error.log 
echo "created /home/$USER/logs/$dname/error.log"
chgrp -R apache /home/$USER/logs
chgrp -R apache /home/$USER/$dname
chmod 755 /home/$USER

# =======================
# build index.html file
# =======================
make_index > /home/$USER/$dname/index.html
echo "created /home/$USER/$dname/index.html"

# =======================
# build vhost config file
# =======================
make_vhost > /etc/httpd/conf/sites-enabled/$dname
echo "created /etc/httpd/conf/sites-enabled/$dname"

# =======================
#    exit
# =======================
/etc/init.d/httpd reload

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