#!/bin/bash

# @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
#
# @author Arthur Schiwon <blizzz@arthur-schiwon.de>
#
# @license GNU AGPL version 3 or any later version
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

NC_PERMDATADIR="/var/lib/univention-appcenter/apps/nextcloud/data"
NC_DATADIR="$NC_PERMDATADIR/nextcloud-data"

NC_PERMCONFDIR="/var/lib/univention-appcenter/apps/nextcloud/conf"
NC_UCR_FILE="$NC_PERMCONFDIR/ucr"

cd /var/www/html
if [ ! -x occ ]; then
	echo "occ missing or not executable"
	exit 1
fi

OCC="sudo -u www-data ./occ"

NC_IS_INSTALLED=`$OCC status | grep "installed: true" -c`
NC_IS_UPGRADE=1

if [ "$NC_IS_INSTALLED" -eq 0 ] ; then
    NC_IS_UPGRADE=0

    NC_ADMIN_PWD_FILE="$NC_PERMCONFDIR/admin.secret"
    NC_DB_TYPE="pgsql"
    NC_LOCAL_ADMIN="nc_admin"
    NC_LOCAL_ADMIN_PWD=`pwgen -y 30 1`
    echo "$NC_LOCAL_ADMIN_PWD" > "$NC_ADMIN_PWD_FILE"
    chmod 600 "$NC_ADMIN_PWD_FILE"

    mkdir -p "$NC_DATADIR"
    chown www-data:www-data -R "$NC_DATADIR"

    $OCC maintenance:install \
        --admin-user    "$NC_LOCAL_ADMIN" \
        --admin-pass    "$NC_LOCAL_ADMIN_PWD" \
        --database      "$NC_DB_TYPE" \
        --database-host "$DB_HOST" \
        --database-port "$DB_PORT" \
        --database-name "$DB_NAME" \
        --database-user "$DB_USER" \
        --database-pass "$DB_PASSWORD" \
        --data-dir      "$NC_DATADIR"

    STATE=$?
    if [[ $STATE != 0 ]]; then
        echo  "Error while installing Nextcloud"
        exit 1;
    fi
fi

UPGRADE_LOGFILE="/var/log/nextcloud-upgrade_"`date +%y_%m_%d`".log"
$OCC check
$OCC status
$OCC app:list
$OCC upgrade 2>&1>> "$UPGRADE_LOGFILE"

# basic Nextcloud configuration
eval "`cat \"$NC_UCR_FILE\"`"
if [ "$NC_IS_UPGRADE" -eq 0 ] ; then
    $OCC config:system:set updatechecker --value="false"    # this is handled via UCS AppCenter
    $OCC config:system:set --value "\OC\Memcache\APCu" memcache.local
    $OCC config:system:set overwriteprotocol --value="https"
    $OCC config:system:set overwritewbroot --value="/nextcloud"
    $OCC config:system:set overwrite.cli.url --value="https://$NC_UCR_DOMAIN/nextcloud"
    $OCC config:system:set htaccess.RewriteBase --value="/nextcloud"
    $OCC maintenance:update:htaccess
    $OCC background:cron
    $OCC app:enable user_ldap
    $OCC app:disable updatenotification

    if [ "$NC_APPLIANCE" = true ]; then
        # Heads up: in Nc 13 we will have occ app:install <appid>
        EXTRA_APP_URLS=(\
            https://github.com/nextcloud/zenodo/releases/download/v0.9.4/zenodo-0.9.4.tar.gz \
            https://download.nextcloud.com/server/apps/dashboard-4.0.5.tar.gz \
            https://github.com/nextcloud/circles/releases/download/v0.13.6/circles-0.13.6.tar.gz \
            https://download.nextcloud.com/server/apps/groupfolders-1.1.0.tar.gz \
            https://github.com/nextcloud/announcementcenter/releases/download/v3.1.1/announcementcenter-3.1.1.tar.gz \
            https://github.com/nextcloud/admin_notifications/releases/download/v1.0.1/admin_notifications-1.0.1.tar.gz \
            https://github.com/nextcloud/quota_warning/releases/download/v1.1.1/quota_warning-1.1.1.tar.gz \
            https://download.nextcloud.com/server/apps/orcid-0.9.1.tar.gz \
            https://download.nextcloud.com/server/apps/user_saml-1.4.0.tar.gz \
        )

        cd apps
        for APP_URL in "${EXTRA_APP_URLS[@]}" ; do
            wget "$APP_URL" -O app.tar.gz && \
            tar -xf app.tar.gz
            rm app.tar.gz
        done
        chown -R www-data:nogroup *
        cd ..

        # enable extra apps
        $OCC app:enable zenodo
        $OCC app:enable dashboard
        $OCC app:enable circles
        $OCC app:enable groupfolders
        $OCC app:enable announcementcenter
        $OCC app:enable admin_notifications
        $OCC app:enable quota_warning
        $OCC app:enable orcid
        $OCC app:enable user_saml
        # ensure they are the latest version
        $OCC upgrade 2>&1>> "$UPGRADE_LOGFILE"
    fi

else
    if [ "$NC_APPLIANCE" = true ]; then
        # fixes wrong permissions as set in first release github issue #39
        # TODO: remove this if block when 12 is phased out, should be more than enough time to upgrade
        cd apps
        chown -R www-data:nogroup *
        cd ..
    fi
    # attempt to re-enable disabled apps
    DISABLED_APPS=( $(cat "$UPGRADE_LOGFILE" | grep "Disabled 3rd-party app:" | cut -d ":" -f 2 | egrep -o "[a-z]+[a-z0-9_]*[a-z0-9]+") )
    for APPID in "${DISABLED_APPS[@]}" ; do
        $OCC app:enable "$APPID" || echo "Could not re-enable $APPID"
    done

    # clean IP-related settings
    $OCC config:system:delete trusted_domains
    $OCC config:system:delete trusted_proxies
fi

# set IP-related settings (refreshed on update)
$OCC config:system:set trusted_proxies 0 --value="$NC_TRUSTED_PROXY_IP"
$OCC config:system:set trusted_domains 0 --value="$NC_UCR_DOMAIN"
NC_TRUSTED_DOMAIN_NO=1
NC_HOST_IPS=($NC_HOST_IPS)
for HOST_IP in "${NC_HOST_IPS[@]}" ; do
    HOST_IP=$(echo "$HOST_IP" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
    $OCC config:system:set trusted_domains "$NC_TRUSTED_DOMAIN_NO" --value="$HOST_IP"
    NC_TRUSTED_DOMAIN_NO=$(($NC_TRUSTED_DOMAIN_NO+1))
done

# env var is set from the dockerfile
if [ "$NC_IS_PATCHED" = true ]; then
    $OCC config:system:set integrity.check.disabled --value="true" --type=boolean
    # (un)comment and adjust following line depending on the use case,
    # otherwise a warning is shown, still
    $OCC integrity:check-app user_ldap

    # integrity checks are done once on upgrade case, thus we can directly remove the flag again
    $OCC config:system:delete integrity.check.disabled
fi

echo "*/15 * * * * www-data    php -f /var/www/html/cron.php" > /etc/cron.d/nextcloud
