#!/bin/bash
### MODUL-NR. 020 ###
# (c) August 2008 Stefan Schäfer / FSP Computer & Netzwerke
# (c) 2009-2026 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

# openCloud Installation

# openCloud setup

# 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="openCloud"
pgsubtitle="openCloud Setup"

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

# Weitermachen?
if [[ $cont == "0" ]]; then
    pgsubtitle="Software wird installiert"
    ## Nur benoetigt für opencloud-server Installation via RPM
	#zypper refresh 2>&1 | tee -a $LOGFILE | pgbox
    #zypper -n install opencloud-server 2>&1 | tee -a $LOGFILE | pgbox
    #zyppercheck ${PIPESTATUS[0]}

    # Gruppe "opencloud" anlegen
    samba-tool group add --description="Gruppe zur Beschränkung der openCloud Nutzer" opencloud 2>&1| tee -a $LOGFILE | pgbox

    # lokale IP ermitteln
    hn=`hostname -f`
    ip=`dig @localhost $hn +short`
    # Erforderliche DNS Eintraege erstellen
    moddnsrecords a A opencloud $ip
    moddnsrecords a A weboffice $ip
    moddnsrecords a A wopi $ip

	# openCloud initialisieren
    ## Nur benoetigt für opencloud-server Installation via RPM
	# sudo -u opencloud-server opencloud-server init --config-path /etc/opencloud-server/

	## Installation via git und docker
	path="/var/lib/opencloud/"
	mkdir /etc/opencloud 2>&1 | tee -a $LOGFILE | pgbox
	mkdir -p /srv/opencloud/data 2>&1 | tee -a $LOGFILE | pgbox
	chown -R 1000:1000 /srv/opencloud/data 2>&1 | tee -a $LOGFILE | pgbox
	mkdir $path 2>&1 | tee -a $LOGFILE | pgbox
	cd $path
	git clone https://github.com/opencloud-eu/opencloud-compose.git
	path=$path/opencloud-compose
	cd $path

	# setting up opencloud-compose
	cp $TEMPLATEDIR/$modulename/env.invis $path/.env 
	file=".env"
	domain=`getconfdata "Domain" "2"`
	string="invis-net.loc%$domain"
	changevalues $path $file "$string"

	docker compose up -d
	
    # openCloud im Portal aktivieren
    # Wenn eine Active-Sync unterstuetzende Groupware installiert ist, wird ownCloud ueber den z-push vHost angeboten
    usedgroupware=`getconfdata "Groupware" "2"`
    if [[ $usedgroupware == "roundcube" ]]; then
		#swpestat ownCloud TRUE 2>&1 | tee -a $LOGFILE | pgbox
		echo "OpenCloud Implementation noch in Arbeit" 2>&1 |tee -a $LOGFILE | pgbox
    else
		## Apache vorbereiten
		# activate apache configuration
		#cp $TEMPLATEDIR/$modulename/owncloud.conf /etc/apache2/conf.d
		#swpestat ownCloudZP TRUE 2>&1 | tee -a $LOGFILE | pgbox
		echo "OpenCloud Implementation noch in Arbeit" 2>&1 |tee -a $LOGFILE | pgbox
    fi

    # Flag OPENCLOUD setzen
    a2enflag OPENCLOUD
    chkservice apache2 r

else
    msgbox "openCloud Installation übersprungen" "$windowtitle"
fi
