djekl
6/3/2013 - 12:28 PM

addvhost.sh

#!/bin/sh

## clear the output
#clear
echo "\n"

## auth the sudo command
sudo -p "Please enter you admin password: " whoami 1>/dev/null
if [ "$(sudo whoami)" != 'root' ]; then
    echo "You entered an invalid password or you are not an admin/sudoer user!\n\nScript aborted"
    exit 1;
fi

## get the current username
username=$(whoami)

## get parameters for hostname and document root
echo "\nHost name (e.g. dev.domain.com):"
read hostname

echo "\nDocument root (project directory '~/projects/'):"
read project

## print a nice spacer
echo "\n==================================\n"

## add entry to hosts file
sudo bash -c "echo '127.0.0.1       $hostname www.$hostname' >> /etc/hosts"

## add entry to vhosts file
sudo bash -c "echo \"<VirtualHost *:80>
    ServerName  $hostname
    ServerAlias www.$hostname

    DocumentRoot '/Users/$username/projects/$project/'

    ErrorLog  '/Users/$username/Documents/www_logs/$hostname/error.log'
    CustomLog '/Users/$username/Documents/www_logs/$hostname/access.log' combined

    SetEnv APP_ENVIRONMENT development

</VirtualHost>
\" >> /etc/apache2/extra/httpd-vhosts.conf"

## create the log folder as $username
mkdir -p /Users/$username/Documents/www_logs/$hostname

## set correct permissions
find "/Users/$username/projects/$project" -type d -exec chmod 757 {} \;
find "/Users/$username/projects/$project" -type f -exec chmod 644 {} \;
#chmod 755 "/Users/$username/projects/$project"
chmod 755 "/Users/$username/Documents/www_logs/$hostname"

## restart apache
sudo bash -c "apachectl restart"

## kill the sudo session
sudo -k

## inform user we are finished
echo "\n\nVirtualHost added for $hostname.\n\n"