#!/bin/bash

# configure_host - Settings script configure_host run on Docker host

function check_apache_conf(){
  echo $(date +%c): configure_host - check_apache_conf
  APACHE_DEFAULT_PATH="/etc/apache2/sites-enabled/default-ssl.conf"
  CUSTOM_SEAFCONF_PATH="/etc/apache2/ucs-sites.conf.d/seafile.conf"

  # Wir holen uns den zweizeiler aus der Originaldatei und schreiben Ihn in die custom Datei
  cat $APACHE_DEFAULT_PATH | grep "seafile" > $CUSTOM_SEAFCONF_PATH
  sed -i -e 's|ProxyPass /seafile|ProxyPass /seafhttp|g' $CUSTOM_SEAFCONF_PATH
  sed -i -e 's|ProxyPassReverse /seafile|ProxyPassReverse /seafhttp|g' $CUSTOM_SEAFCONF_PATH
  sed -i -e 's|/seafile|/seafile/seafhttp|g' $CUSTOM_SEAFCONF_PATH
  
  if [[ $(apachectl configtest 2>&1) == "Syntax OK" ]]; then
    apachectl restart
  else
    rm /etc/apache2/ucs-sites.conf.d/seafile.conf
  fi
}

function remove_apache_conf(){
  if [[ -f /etc/apache2/ucs-sites.conf.d/seafile.conf ]]; then
    rm /etc/apache2/ucs-sites.conf.d/seafile.conf
  fi
}

function detect_online_editor() {
  echo $(date +%c): configure_host - detect_online_editor
  FQDN="$(ucr get hostname).$(ucr get domainname)"
  f="/var/lib/univention-appcenter/apps/seafile/data/seafile-data/seafile/conf/seahub_settings.py"

  if ! grep -q "OFFICE_" $f; then
    collabora=""				# (mögliche Werte sind: leer, co, co-on)
    onlyoffice=""				# (mögliche Werte sind: leer, onlyoffice-documentserver, onlyoffice-documentserver-ie)

    if [[ "$(ucr get appcenter/apps/collabora/status)" == "installed" ]]; then
      collabora="co"
    elif [[ "$(ucr get appcenter/apps/collabora-online/status)" == "installed" ]]; then
      collabora="co-on"
    fi

    if [[ "$(ucr get appcenter/apps/onlyoffice-ds/status)" == "installed" ]]; then
      onlyoffice="onlyoffice-documentserver"
    elif [[ "$(ucr get appcenter/apps/onlyoffice-ds-integration/status)" == "installed" ]]; then
      onlyoffice="onlyoffice-documentserver-ie"
    fi

    if [[ $collabora != "" && $onlyoffice != "" ]]; then
      echo "# Sowohl ONLYOFFICE als auch Collabora (OFFICE_SERVER) wurden gefunden. Bitte manuell die Konfiguration einfügen. Details zur Konfiguration finden Sie unter manual.seafile.com" >> ${f}
    elif [[ $collabora != "" ]]; then
      #Bei Collabora und Collabora-online ist die URL gleich.
    echo "
OFFICE_SERVER_TYPE = 'CollaboraOffice'
ENABLE_OFFICE_WEB_APP = True
OFFICE_WEB_APP_BASE_URL = 'https://"$FQDN"/hosting/discovery'
WOPI_ACCESS_TOKEN_EXPIRATION = 30 * 60
OFFICE_WEB_APP_FILE_EXTENSION = ('odp', 'ods', 'odt', 'xls', 'xlsb', 'xlsm', 'xlsx','ppsx', 'ppt', 'pptm', 'pptx', 'doc', 'docm', 'docx')
ENABLE_OFFICE_WEB_APP_EDIT = True
OFFICE_WEB_APP_EDIT_FILE_EXTENSION = ('odp', 'ods', 'odt', 'xls', 'xlsb', 'xlsm', 'xlsx','ppsx', 'ppt', 'pptm', 'pptx', 'doc', 'docm', 'docx')
" >> ${f}
	elif [[ $onlyoffice != "" ]]; then
	  #Ich brauch hier keine weitere Abfrage, weil der richtige Pfad schon in der Variable steht.
	  echo "
ENABLE_ONLYOFFICE = True
VERIFY_ONLYOFFICE_CERTIFICATE = False
ONLYOFFICE_APIJS_URL = 'https://"$FQDN"/"$onlyoffice"/web-apps/apps/api/documents/api.js'
ONLYOFFICE_FILE_EXTENSION = ('doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'odt', 'fodt', 'odp', 'fodp', 'ods', 'fods')
ONLYOFFICE_EDIT_FILE_EXTENSION = ('docx', 'pptx', 'xlsx')
" >> ${f}
    else
      echo "Kein Editor gefunden."
    fi
  fi 
}


###############################################
###############################################


if [[ $1 == "install" ]]; then
  echo $(date +%c): configure_host - install
  check_apache_conf
  detect_online_editor
fi

if [[ $1 == "remove" ]]; then
  echo $(date +%c): configure_host - remove
  remove_apache_conf
fi

if [[ $1 == "settings" ]]; then
  echo $(date +%c): configure_host - settings
  check_apache_conf
  detect_online_editor
fi

if [[ $1 == "upgrade" ]]; then
  echo $(date +%c): configure_host - upgrade
  check_apache_conf
  detect_online_editor
fi

exit 0