#!/bin/bash

if [ $# -eq 0 ]
then
  echo 'Usage: drupal-vhosts DRUPAL_ROOT_DIRECTORY'
  exit
fi

drupal_root=$1
vhost=/etc/apache2/vhosts.d/drupal.conf

if [ -d "$drupal_root" ]
then
  # clear vhost file
	echo "# Auto-generated by drupal-vhosts package" > $vhost

	# check which Drupal version exist and generate vhost config for them
	for version in 5 6 7 8;
	do
		directory="$drupal_root/drupal-$version"
		if [ -d "$directory" ]
		then
			echo "Found Drupal $version."
			echo "# Drupal $version
<VirtualHost *>
  ServerName d${version}x.loc
  ServerAlias *.d${version}x.loc
  ServerAdmin root@site.loc

  DocumentRoot $directory
  <Directory $directory>
    AllowOverride FileInfo AuthConfig Limit Indexes Options=All,MultiViews
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>
" >> $vhost
		fi
	done
else
  echo "'$drupal_root' is not a valid directory."
fi
