#!/bin/bash

function OCC
{
    params=$@;
    source /opt/remi/php74/enable
    cd /usr/share/nextcloud/
    TERM=dumb /usr/bin/occ $params
}

# does /usr/share/nextcloud exists ? no: we create it from sources
if [ ! -d /usr/share/nextcloud ]; then
  cp -a /usr/share/nextcloud-src /usr/share/nextcloud
fi

# important configuration variables
password=`/sbin/e-smith/db configuration getprop nextcloud DbPassword || echo "missing"`
dbname=`/sbin/e-smith/db configuration getprop nextcloud DbName || echo "nextcloud"`
dbuser=`/sbin/e-smith/db configuration getprop nextcloud DbUser || echo "nextcloud"`
adminuser=`/sbin/e-smith/db configuration getprop nextcloud AdminUser || echo "admin"`
adminpass=`/sbin/e-smith/db configuration getprop nextcloud AdminPassword ||/sbin/e-smith/db configuration getprop sysconfig SystemID  || echo "password;109"`
#occ config:system:get dbhost
host="localhost"
socket="--socket=/var/lib/mysql/mysql.sock"
# need to check what db we are supposed to use. starting NC 21 mariadb >= 102 is needed  core is 55
# are we fresh install or update ?
installed=$(/usr/bin/occ status --output json |jq -r '.installed')
# what version
majversion=$(/usr/bin/occ status --output json |jq -r '.version'|cut -d'.' -f1)
# is there a nextcloud db in core mariadb
if [ "$installed" != "true" ]; then  host="localhost:/var/lib/mysql/mariadb105.sock"; socket="--socket=/var/lib/mysql/mariadb105.sock"; fi
if [ "$installed" == "true" ]; then  host=$(occ config:system:get dbhost); socket="--socket=$(echo $host|awk -F'[:]' '{print $2}')" ; fi
if [ "$socket" == "--socket=" ]; then  socket=""; fi

# initialize grants mysql nextcloud database
/usr/bin/mysql --defaults-file=/root/.my.cnf $socket -e "CREATE DATABASE IF NOT EXISTS $dbname;"
/usr/bin/mysql --defaults-file=/root/.my.cnf $socket -e "grant all on $dbname.* to '$dbuser'@'localhost' identified by '$password';"
/usr/bin/mysql --defaults-file=/root/.my.cnf $socket -e "FLUSH PRIVILEGES"

# check if we are migrating from release 10 to 11
if [ -f /var/www/html/nextcloud/config/config.php ]; then
    mv /var/www/html/nextcloud/config/config.php /usr/share/nextcloud/config/config.php
    rm -rf /var/www/html/nextcloud/
fi

res=`/usr/bin/mysql --defaults-file=/root/.my.cnf $socket -e "select count(*) from information_schema.tables where table_type = 'BASE TABLE' and table_schema = '$dbname'" | tail -n1`;

if [[ $res == '0' ]]; then
    OCC "maintenance:install --database mysql --database-host $host  --database-name $dbname --database-user $dbuser --database-pass $password --admin-user $adminuser --admin-pass  $adminpass --data-dir /home/e-smith/files/nextcloud/data/"

    OCC "app:enable user_ldap"
    OCC "ldap:create-empty-config" # create config with empty id
    OCC "ldap:create-empty-config" # create config with id s01
    OCC "ldap:delete-config ''" # delte config with empty id
    mkdir -p /home/e-smith/files/nextcloud/skeleton/ibays
    /usr/bin/occ config:system:set  skeletondirectory --value="/home/e-smith/files/nextcloud/skeleton"
    #/usr/bin/occ config:system:set  templatedirectory --value=""
else
    mkdir -p /home/e-smith/files/nextcloud/skeleton/ibays
    /usr/bin/occ config:system:set  skeletondirectory --value="/home/e-smith/files/nextcloud/skeleton"
    # to satisfy code integrity check
    if [ -f /usr/share/nextcloud/.htaccess.rpmsave ]; then
        rm -f /usr/share/nextcloud/.htaccess.rpmsave
    fi
    if [ -f /usr/share/nextcloud/.htaccess.rpmnew ]; then
        rm -f /usr/share/nextcloud/.htaccess.rpmnew
    fi
    OCC "maintenance:mode --on"
    OCC "upgrade"
    OCC "maintenance:mode --off"
    OCC "integrity:check-core"
    # Catch 'Nextcloud is already latest version' message
    if [ $? -eq 3 ]; then
        exit 0
    fi
fi

#samba needed folder to put gencache.tdb
if [ ! -d /var/www/.cache/samba ]; then
  mkdir -p /var/www/.cache/samba
  chown www:shared /var/www/.cache/samba
fi
if [ ! -d /home/e-smith/.cache/samba ]; then
  mkdir -p /home/e-smith/.cache/samba
  chown www:admin /home/e-smith/.cache/samba
fi


