#!/bin/bash
# Copyright (c) 2015 by Silvan Calarco <silvan.calarco@mambasoft.it>

usage() {
   echo "Script to create a virtual host for Apache.

Usage:
$0 sitename.domain.ext
"
}

[ "`id -u`" = "0" ] || {
   usage
   echo "
ERROR: this script must be run as root user; aborting."
exit 1
}

SERVERNAME=$1

[ ${SERVERNAME} ] || {
   usage
   exit 1
}

SERVERUSER=${SERVERNAME/.*}
SERVERPASSWORD=`mkpasswd -l 9 -s 0`

SERVERADMIN="webmaster@`hostname -d`"
SERVERROOT=/var/www/${SERVERNAME}
DOCUMENTROOT=${SERVERROOT}/www
LOGROOT=${SERVERROOT}/log
ACCESSLOG=${LOGROOT}/access_log
ERRORLOG=${LOGROOT}/error_log

SERVERHOSTNAME=`hostname -f`
SERVERIP=`host $SERVERHOSTNAME | sed "s|.* has address ||"`

echo "%--------------------------------------------------------------------------------%
Creating the following Apache virtual host (please take note of this information):

Server Name:   $SERVERNAME
Server Admin:  $SERVERADMIN

SFTP user:     $SERVERUSER
SFTP password: $SERVERPASSWORD

Document root: $DOCUMENTROOT
Access log:    $ACCESSLOG
Error log:     $ERRORLOG

NOTE: remember to add A or CNAME record so that it points to IP $SERVERIP.
%--------------------------------------------------------------------------------%

"

getent passwd $SERVERUSER >/dev/null && {
   echo "ERROR: user $SERVERUSER already exists; aborting."
   exit 1
}

[ -e /etc/httpd/httpd.d/${SERVERNAME}.conf ] && {
   echo "ERROR: a virtual host for ${SERVERNAME} is already configured; aborting."
   exit 1
}

[ -e $DOCUMENTROOT ] && {
   echo "ERROR: document root $DOCUMENTROOT already exists; aborting."
   exit 1
}

echo "If all the above is correct type 'yes' + ENTER to confirm or press CTRL-C to abort."
read ans

[ "$ans" = "yes" ] || exit 1

useradd ${SERVERUSER} -g sftponly -d ${SERVERROOT} -p ${SERVERPASSWORD} -c "${SERVERNAME} user" -s /bin/false || {
   echo "ERROR: unable to create ${SERVERUSER} user; aborting."
   exit 1
}

echo ${SERVERPASSWORD} | passwd ${SERVERUSER} --stdin || {
   echo "ERROR: unable to set password for ${SERVERUSER}; aborting."
   exit 1
}

mkdir -p ${DOCUMENTROOT} ${LOGROOT}
chown ${SERVERUSER}.sftponly ${DOCUMENTROOT}

cat > /etc/httpd/httpd.d/${SERVERNAME}.conf << _EOF
<VirtualHost *:80>
   ServerAdmin $SERVERADMIN
   DocumentRoot $DOCUMENTROOT
   ServerName $SERVERNAME
   LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %X" combinedom
   ErrorLog $ERRORLOG
   CustomLog $ACCESSLOG combinedom
</VirtualHost>

<Directory $DOCUMENTROOT>
    Options Indexes FollowSymLinks Includes
    AllowOverride All
    Require all granted
    Order allow,deny
    Allow from All
    DirectoryIndex index.php index.html
</Directory>
_EOF

cat > /etc/logrotate.d/${SERVERNAME} << _EOF
${ACCESSLOG} {
    monthly
    rotate 12
    copytruncate
    compress
    notifempty
    missingok
}

${ERRORLOG} {
    monthly
    rotate 12
    copytruncate
    compress
    notifempty
    missingok
}
_EOF

apachectl configtest >/dev/null || {
   echo "ERROR: there is a problem in Apache configuration, so I won't reload it; aborting."
   exit 1
}

apachectl graceful || {
   echo "ERROR: error reloading Apache configuration; please check for it because all your web services are now unavailable. Aborting."
   exit 1
}

echo "All done!"
exit 0
