#!/bin/bash
# Script zum schnellen Anlegen eines vhost für Apache2
# Version: 0.5
# (C) 2010 invis-server.org
# Author: Stefan Schaefer <stefan@invis-server.org>
# Questions: http://forum.invis-server.org
# License: GPLv3

# Globale Einstellungen
rootpackconfig="/etc/rootpack/rootpack.cfg"

usage() {
    echo "----------------------------------------------------------------"
    echo "Übergeben Sie diesem Script den FQDN und den Besitzer des vhosts"
    echo
    echo "Beispiel: \$> mkvhost www.example.com username" 
    echo "----------------------------------------------------------------"
}

# Konfigurationsparameter tauschen
changevalues() {
    # Arg1 = Pfad, Arg2 = Datei, Arg3 = sed String
    cat $1/$2|sed "s%$3%g" > $1/$2.new
    mv $1/$2.new $1/$2
}

getconfvalue() {
    cat $rootpackconfig | grep "$1:" | cut -d ":" -f 2
}

clear

# Domain angegeben?
fqdn="$1"
vhuser="$2"

if [[ -z $fqdn || -z $vhuser ]]; then
    usage
    exit
fi

# Konfigurationsparameter ermitteln
myip=$(ip address show eth0|grep "inet\s"|tr -s " "|cut -d "/" -f1|cut -d " " -f3)

# Konfigurationsvorlage für Apache vhost-Konfiguration
vhtemplate=$(getconfvalue VhTemplate)

# Apache Konfigurationsverzeichnis fuer vhosts
vhconfdir=$(getconfvalue VhostConfDir)

# Hoster Informationen
hoster=$(getconfvalue Hoster)
hosterurl=$(getconfvalue HosterUrl)

# Verzeichnis der vhosts
vhostdir=$(getconfvalue VhostDir)/$vhuser/sites
docroot="$vhostdir/$fqdn/htdocs"
cgidir="$vhostdir/$fqdn/cgi-bin"

# Testen, ob der vhost bereits angelegt ist.
if [[ -d $docroot ]]; then
    echo "Der vhost $fqdn existiert bereits."
    exit
fi

# vhost Verzeichnis anlegen, wenn es nich nicht existiert
if [[ ! -d $vhostdir ]]; then
    mkdir -p $vhostdir
fi

# Erzeugen eine Apache Konfigurationsdatei
cp $vhtemplate $vhconfdir/$fqdn.conf

# Anpassen der VHost Konfiguration
path="$vhconfdir"
file="$fqdn.conf"
string="username%$vhuser"
changevalues $path $file "$string"
string="dummy-host.example.com%$fqdn"
changevalues $path $file "$string"
    
# Serververzeichnisse anlegen
mkdir -p $docroot
mkdir -p $vhostdir/$fqdn/webalizer
mkdir -p $cgidir
mkdir -p $vhostdir/$fqdn/wrapper
cp /etc/rootpack/templates/apache2/favicon.ico $docroot/

# .htaccess-Datei fuer Webalizer kopieren und anpassen
cp /etc/rootpack/templates/webalizer/.htaccess $vhostdir/$fqdn/webalizer/
#cp -r /etc/rootpack/templates/webalizer/flags/ $vhostdir/$fqdn/webalizer/

path="$vhostdir/$fqdn/webalizer"
file=".htaccess"
string="username%$vhuser"
changevalues $path $file "$string"
string="dummy-host.example.com%$fqdn"
changevalues $path $file "$string"

# Anlegen einer index.Datei zu Testzwecken
echo "<head>" >> $docroot/index.html
echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">" >> $docroot/index.html
echo "<!-- ... andere Angaben im Dateikopf ... -->" >> $docroot/index.html
echo "</head>" >> $docroot/index.html
echo "<html><body>" >> $docroot/index.html
echo "<hr>" >> $docroot/index.html
echo "<h2><div align=center>Hier entsteht die Internetseite $fqdn</div></h2>" >> $docroot/index.html
echo "<hr>" >> $docroot/index.html
echo "<font size=-1>Powered by <a href="http://$hosterurl">$hoster<a></font>" >> $docroot/index.html
echo "</body></html>" >> $docroot/index.html
    
# Wenn vorhanden php-cgi5 php.ini ins wrapper Verzeichnis kopieren
fphpini="/etc/php5/fastcgi/php.ini"
if [[ -f $fphpini ]];then
    cp $fphpini $vhostdir/$fqdn/wrapper
fi
    
# PHP-FCGIDWrapper-Script erzeugen
echo '#!/bin/bash' >> $vhostdir/$fqdn/wrapper/fcgid-wrapper
echo "# Kleines PHP-FCGI Wrapper-Script fuer die Kombination fcgid/suexec/php" >> $vhostdir/$fqdn/wrapper/fcgid-wrapper
echo "export PHPRC=\"$vhostdir/$fqdn/wrapper/\"" >> $vhostdir/$fqdn/wrapper/fcgid-wrapper
echo -e "exec /usr/bin/php-cgi5\n" >> $vhostdir/$fqdn/wrapper/fcgid-wrapper

# Script ausführbar machen
chmod ug+x $vhostdir/$fqdn/wrapper/fcgid-wrapper
    
# Anpassen der Besitzrechte an den Serververzeichnissen
if [[ -n `a2enmod -l | grep suexec` ]]; then
    chown -R $vhuser.users $vhostdir/$fqdn 
    # Anpassen der VHost Konfiguration
    path="$vhconfdir"
    file="$fqdn.conf"
    strings="wwwrun www%$vhuser users"
    changevalues $path $file "$strings"
else
    chown -R $vhuser.www $vhostdir/$fqdn 
    find $vhostdir/$fqdn/ -type d -exec chmod 4775 '{}' \;
fi
    
# Apache neu laden
systemctl reload apache2.service

# Pruefen, ob fuer den Hostnamen bereits ein DNS A Record besteht
dnsentry=$(dig $fqdn +short)

if [[ -z $dnsentry ]]; then
    echo "Der Name dieses vHosts ist noch nicht per DNS auflösbar. Es muss ein DNS A-Record auf unserem Nameserver \"dnsmain.fspisp.de\" angelegt werden."
fi
