#!/bin/bash
### MODUL-NR. 020 ###
# (c) August 2008 Stefan Schäfer / FSP Computer & Netzwerke
# (c) 2009-2018 Stefan Schäfer / invis-server.org / stefan@invis-server.org
# (c) 2013,2014 Dimitri Asarowski / invis-server.org / dimitri@invis-server.org
# (c) 2013-2017 Ingo Göppert / invis-server.org / ingo@invis-server.org

# License: GPLv3
# Questions: info@invis-server.org

# Dokuwiki Installation inkl. Plugins

# Dokuwiki setup including plugins

# Dieses Programm ist freie Software. Sie können es unter den Bedingungen der 
# GNU General Public License, wie von der Free Software Foundation veröffentlicht,
# weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder
# (nach Ihrer Option) jeder späteren Version.

# Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen
# von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite 
# Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. 
# Details finden Sie in der GNU General Public License.

# Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem 
# Programm erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>. 

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# include functions
source $SINEINCLUDEDIR/functions

modulename=`basename $0`

clear
windowtitle="Dokuwiki"
pgsubtitle="Dokuwiki Setup"

dialog --backtitle "$DIALOGTITLE" --title "$windowtitle" --exit-label "Weiter" --textbox "$INFODIR/dokuwiki.txt" 38 90 --and-widget --backtitle "$DIALOGTITLE" --title "$windowtitle"  --defaultno --yesno "Möchten Sie Dokuwiki installieren?" 0 0
cont=${?}

if [[ $cont == "0" ]];then

    # install dokuwiki package
    pgsubtitle="Dokuwiki wird installiert" 
    zypper refresh 2>&1 | tee -a $LOGFILE  |pgbox
    zypper -n install -n dokuwiki 2>&1 | tee -a $LOGFILE | pgbox
    zyppercheck ${PIPESTATUS[0]}

    # Dokuwiki Template Bootstrap3 herunterladen und installieren
    # Create download-url
    url=`curl -s https://api.github.com/repos/LotarProject/dokuwiki-template-bootstrap3/releases/latest |grep tarball | tr -s " "| cut -d " " -f3 |cut -d "," -f1 |tr -d '"'`
    wget -O $SINEWORKINGDIR/bootstrap3.tar.gz $url 2>&1 | tee -a $LOGFILE | pgbox
    getpath=`find $SINEWORKINGDIR -name "bootstrap3.tar.gz"`
    dwtemplatedir="/srv/www/htdocs/dokuwiki/lib/tpl/"
    tar -xzf $getpath -C $dwtemplatedir 2>&1 | tee -a $LOGFILE | pgbox
    bootstrapdirname=`ls $dwtemplatedir|grep "Lotar"`
    mv $dwtemplatedir/$bootstrapdirname $dwtemplatedir/bootstrap3

    # Display Wiki Page Plugin herunterladen
    # Vorübergehender Link fuer Displaywikipage
    #pgsubtitle="Dokuwiki-Erweiterungen werden heruntergeladen und installiert"
    #wget -P $SINEWORKINGDIR http://cloud.github.com/downloads/tatewake/dokuwiki-plugin-displaywikipage/displaywikipage-stable.tar.gz 2>&1 | tee -a $LOGFILE | pgbox
    #getpath=`find $SINEWORKINGDIR -name "display*.tar.gz"`
    #tar -xzf $getpath -C /srv/www/htdocs/dokuwiki/lib/plugins/ 2>&1 | tee -a $LOGFILE | pgbox

    # Display Wiki Page Plugin herunterladen
    # Voruebergehender Link fuer Monobook Template
    #wget -O $SINEWORKINGDIR/monobook-stable.tar.gz https://andreashaerter.com/downloads/dokuwiki-template-monobook/latest 2>&1 | tee -a $LOGFILE | pgbox
    #getpath=`find $SINEWORKINGDIR -name "monobook-stable.tar.gz"`
    #tar -xzf $getpath -C /srv/www/htdocs/dokuwiki/lib/tpl/ 2>&1 | tee -a $LOGFILE | pgbox

    # Encrypt Password Plugin
    wget -P $SINEWORKINGDIR https://github.com/ssahara/dw-plugin-encryptedpasswords/archive/master.zip 2>&1 | tee -a $LOGFILE | pgbox
    getpath=`find $SINEWORKINGDIR -name "master.zip"`
    unzip $getpath -d /srv/www/htdocs/dokuwiki/lib/plugins/ 2>&1 | tee -a $LOGFILE | pgbox
    mv /srv/www/htdocs/dokuwiki/lib/plugins/dw-plugin-encryptedpasswords-master /srv/www/htdocs/dokuwiki/lib/plugins/encryptedpasswords 2>&1 | tee -a $LOGFILE | pgbox
    
    # Konfigurieren
    path="/srv/www/htdocs/dokuwiki/conf"
    file="local.php"
    cp $TEMPLATEDIR/$modulename/$file $path/$file
    
    organisation=`getconfdata "Organisation" "2"`
    string="organization%$organisation"
    changevalues $path $file "$string"
    
    adminpw=`getconfdata "LDAPAdminPW" "2"`
    string="admin-secret%$adminpw"
    changevalues $path $file "$string"
    
    basedn=`getconfdata "baseDN" "2"`
    string="dc=invis-net,dc=loc%$basedn"
    changevalues $path $file "$string"

    domain=`getconfdata "Domain" "2"`
    string="invis-net.loc%$domain"
    changevalues $path $file "$string"
    
    # Gruppen fuer Dokuwiki anlegen
    samba-tool group add --description="Mitglieder dürfen im Wiki lesen" wiki-nutzer 2>&1| tee -a $LOGFILE  |pgbox
    samba-tool group add --description="Mitglieder dürfen im Wiki schreiben" wiki-redakteure 2>&1| tee -a $LOGFILE  |pgbox
    samba-tool group add --description="Mitglieder dürfen im Wiki löschen" wiki-chefredakteure 2>&1| tee -a $LOGFILE  |pgbox
    
    # Install dokuwiki apache2 configuration
    cp $TEMPLATEDIR/$modulename/dokuwiki.conf /etc/apache2/conf.d

    # ACL-Datei kopieren
    cp $TEMPLATEDIR/$modulename/acl.auth.php /srv/www/htdocs/dokuwiki/conf/
    chown wwwrun:www /srv/www/htdocs/dokuwiki/conf/acl.auth.php

    # Monobook Konfiguration installieren
    #cp $TEMPLATEDIR/$modulename/monobook.conf /etc/apache2/conf.d
    #find /srv/www/htdocs/dokuwiki/lib/tpl/monobook -name ".htaccess" -exec rm {} \;

    # insert default sidebar and startpage
    cp $TEMPLATEDIR/$modulename/*.txt /srv/www/htdocs/dokuwiki/data/pages/

    # change owner for the whole dw directory
    chown -R wwwrun:www /srv/www/htdocs/dokuwiki

    # set DOKUWIKI flag and restart apache webserver
    a2enflag DOKUWIKI
    chkservice apache2

    # Dokuwiki im Portal aktivieren
    swpestat Dokuwiki TRUE 2>&1 | tee -a $LOGFILE | pgbox

else
	msgbox "Wenn Sie es sich zu einem späteren Zeitpunkt anders überlegen, können Sie diesen Schritt durch erneutes Aufrufen des Setup-Scripts mit dem Parameter \"dokuwiki\" wiederholen." "Dokuwiki Installation übersprungen"
fi

