#!/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?
domain="$1"
vhuser="$2"

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

# Konfigurationsparameter ermitteln
myip=$(ifconfig eth0|grep "inet Adr"|tr -s " " | cut -d ":" -f2 | cut -d " " -f1)

# 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
docroot="$vhostdir/$domain/htdocs"
cgidir="$vhostdir/$domain/cgi-bin"

# Testen, ob der vhost bereits angelegt ist.
if [[ -d $docroot ]]; then
    echo "Der vhost $domain 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/$domain.conf

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

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

path="$vhostdir/$domain/webalizer"
file=".htaccess"
string="username%$vhuser"
changevalues $path $file "$string"
string="dummy-host.example.com%$domain"
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 $domain</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/$domain/wrapper
fi
    
# PHP-FCGIDWrapper-Script erzeugen
echo '#!/bin/bash' >> $vhostdir/$domain/wrapper/fcgid-wrapper
echo "# Kleines PHP-FCGI Wrapper-Script fuer die Kombination fcgid/suexec/php" >> $vhostdir/$domain/wrapper/fcgid-wrapper
echo "export PHPRC=\"$vhostdir/$domain/wrapper/\"" >> $vhostdir/$domain/wrapper/fcgid-wrapper
echo -e "exec /usr/bin/php-cgi5\n" >> $vhostdir/$domain/wrapper/fcgid-wrapper

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

# Bei Bedarf DNS-Eintrag erzeugen -- Wenn ein * Eintrag vorhanden ist, wird kein neuer A-Record erzeugt.
dnsentry=$(dig @localhost $domain +short)

if [[ -z $dnsentry ]]; then
    vhostname=$(echo $domain | cut -d "." -f 1)
    echo -e "$vhostname\t\tIN A $myip" >> /var/lib/named/master/$domain.zone
    /etc/init.d/named restart
fi
