#!/bin/bash
if [ -n $1 ]
then
	user=$1
fi
if [ -z $1 ]
then
	echo -n "Entrez le nom de l'utilisateur à ajouter: "
	read user
fi
echo $user

adduser $user
path="/home/${user}/public_html"
mkdir $path
mkdir "/home/${user}/cgi-bin"
chmod 711 "/home/${user}/cgi-bin"
chown -R $user /home/${user}
chgrp -R $user /home/${user}

echo "Creating mysql database"
echo -n "Enter mysql password : "
read dbpass

mysql -e "create database ${user}"
mysql -e "grant all privileges on ${user}.* to '${user}'@'localhost'
identified by '${dbpass}' with grant option;"


echo -n "Do you want to add a virtual host for this user? [y/n] "
read yes
if [ $yes == "y" ] 
then
echo "<VirtualHost *>" >> vhost.conf
echo -n "Enter domain name : "
read domain
echo "ServerName $domain" >> vhost.conf
echo "DocumentRoot $path" >> vhost.conf
echo -n "Enter aliases for this domain (separated by spaces) : "
read aliases
	if [ -n $aliases]  
	then
	echo "ServerAlias $aliases" >> vhost.conf
	fi
echo "CustomLog /var/log/apache/${user}/accesslog combined" >> vhost.conf
echo "ErrorLog /var/log/apache/${user}/errorlog" >> vhost.conf

echo "Configuring cgi-bin directory"

echo "<IfModule mod_alias.c>
    ScriptAlias /cgi-bin/ /home/${user}/cgi-bin/
    <Directory /home/${user}/cgi-bin/>
        AllowOverride None
        Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
</IfModule>" >> vhost.conf


echo "</VirtualHost>

" >> vhost.conf
fi

