#!/command/with-contenv sh
set -ex

cd /var/lib/nextcloud
chmod +x ./occ

if [ ! -f /var/lib/nextcloud/data/.installed ]; then
    /command/s6-setuidgid nginx ./occ maintenance:install \
        --database=pgsql \
        --database-host="$DB_HOST" \
        --database-name="$DB_NAME" \
        --database-user="$DB_USER" \
        --database-pass="$DB_PASSWORD" \
        --database-port="$DB_PORT" \
        --admin-user="$ADMIN_USER" \
        --admin-pass="$ADMIN_PASSWORD" \
        --data-dir=/var/lib/nextcloud/data/ \
        --no-interaction

    touch /var/lib/nextcloud/data/.installed
fi

/command/s6-setuidgid nginx ./occ config:app:set --value cron core backgroundjobs_mode
/command/s6-setuidgid nginx ./occ app:enable files_sharing
/command/s6-setuidgid nginx ./occ app:enable text
/command/s6-setuidgid nginx ./occ app:enable files_pdfviewer
/command/s6-setuidgid nginx ./occ app:enable files_videoplayer
/command/s6-setuidgid nginx ./occ app:enable theming
/command/s6-setuidgid nginx ./occ app:enable photos
/command/s6-setuidgid nginx ./occ app:enable twofactor_totp
/command/s6-setuidgid nginx ./occ app:enable twofactor_backupcodes
/command/s6-setuidgid nginx ./occ app:enable twofactor_u2f
/command/s6-setuidgid nginx ./occ app:enable polls
/command/s6-setuidgid nginx ./occ app:enable calendar
/command/s6-setuidgid nginx ./occ app:enable contacts
/command/s6-setuidgid nginx ./occ app:enable news
/command/s6-setuidgid nginx ./occ app:enable tasks
/command/s6-setuidgid nginx ./occ app:enable notes
/command/s6-setuidgid nginx ./occ app:enable sociallogin
/command/s6-setuidgid nginx ./occ app:enable integration_mastodon
/command/s6-setuidgid nginx ./occ app:enable integration_github
/command/s6-setuidgid nginx ./occ app:enable integration_gitlab
/command/s6-setuidgid nginx ./occ app:disable activity
/command/s6-setuidgid nginx ./occ app:disable comments
/command/s6-setuidgid nginx ./occ app:disable files_trashbin
/command/s6-setuidgid nginx ./occ app:disable firstrunwizard
/command/s6-setuidgid nginx ./occ app:disable logreader
/command/s6-setuidgid nginx ./occ app:disable nextcloud_announcements
/command/s6-setuidgid nginx ./occ app:disable password_policy
/command/s6-setuidgid nginx ./occ app:disable serverinfo
/command/s6-setuidgid nginx ./occ app:disable support
/command/s6-setuidgid nginx ./occ app:disable survey_client
/command/s6-setuidgid nginx ./occ app:disable systemtags
/command/s6-setuidgid nginx ./occ app:disable updatenotification

/command/s6-setuidgid nginx ./occ config:system:set --value '\OC\Memcache\APCu' memcache.local

/command/s6-setuidgid nginx ./occ config:system:set --value "https://$DOMAIN" overwrite.cli.url
/command/s6-setuidgid nginx ./occ config:system:set --value "localhost" trusted_domains 0
/command/s6-setuidgid nginx ./occ config:system:set --value "$DOMAIN" trusted_domains 1
/command/s6-setuidgid nginx ./occ config:system:set --value "syslog" log_type
/command/s6-setuidgid nginx ./occ config:system:set --value "" logfile
/command/s6-setuidgid nginx ./occ config:system:set --value "3" loglevel

/command/s6-setuidgid nginx ./occ config:system:set --value "smtp" mail_smtpmode
/command/s6-setuidgid nginx ./occ config:system:set --value "${MAIL_SMTP_AUTH_TYPE:-PLAIN}" mail_smtpauthtype
/command/s6-setuidgid nginx ./occ config:system:set --value "$MAIL_FROM" mail_from_address
/command/s6-setuidgid nginx ./occ config:system:set --value "$MAIL_DOMAIN" mail_domain
/command/s6-setuidgid nginx ./occ config:system:set --value "${MAIL_SMTP_SECURITY:-tls}" mail_smtpsecure
/command/s6-setuidgid nginx ./occ config:system:set --value "${MAIL_SMTP_AUTH:-1}" mail_smtpauth
/command/s6-setuidgid nginx ./occ config:system:set --value "$MAIL_SMTP_HOST" mail_smtphost
/command/s6-setuidgid nginx ./occ config:system:set --value "${MAIL_SMTP_PORT:-587}" mail_smtpport
/command/s6-setuidgid nginx ./occ config:system:set --value "$MAIL_SMTP_NAME" mail_smtpname
/command/s6-setuidgid nginx ./occ config:system:set --value "$MAIL_SMTP_PASSWORD" mail_smtppassword