#!/usr/bin/with-contenv sh
set -ex

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

if [ ! -f /var/lib/nextcloud/data/.installed ]; then
    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-table-prefix="$DB_TABLE_PREFIX" \
        --admin-user="$ADMIN_USER" \
        --admin-pass="$ADMIN_PASSWORD" \
        --data-dir=/var/lib/nextcloud/data/ \
        --no-interaction

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

s6-setuidgid nginx ./occ config:app:set --value cron core backgroundjobs_mode
s6-setuidgid nginx ./occ app:enable user_ldap
s6-setuidgid nginx ./occ app:enable files_sharing
s6-setuidgid nginx ./occ app:enable files_texteditor
s6-setuidgid nginx ./occ app:enable files_pdfviewer
s6-setuidgid nginx ./occ app:enable files_videoplayer
s6-setuidgid nginx ./occ app:enable theming
s6-setuidgid nginx ./occ app:enable gallery
s6-setuidgid nginx ./occ app:enable twofactor_totp
s6-setuidgid nginx ./occ app:enable twofactor_backupcodes
s6-setuidgid nginx ./occ app:enable twofactor_u2f
s6-setuidgid nginx ./occ app:enable polls
s6-setuidgid nginx ./occ app:disable activity
s6-setuidgid nginx ./occ app:disable comments
s6-setuidgid nginx ./occ app:disable files_trashbin
s6-setuidgid nginx ./occ app:disable firstrunwizard
s6-setuidgid nginx ./occ app:disable logreader
s6-setuidgid nginx ./occ app:disable password_policy
s6-setuidgid nginx ./occ app:disable support
s6-setuidgid nginx ./occ app:disable survey_client
s6-setuidgid nginx ./occ app:disable updatenotification

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

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

s6-setuidgid nginx ./occ config:app:set --value "$LDAP_BASE_DN" user_ldap ldap_base
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_BASE_GROUP_DN" user_ldap ldap_base_groups
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_EMAIL_ATTRIBUTE" user_ldap ldap_email_attr
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_USER_FILTER" user_ldap ldap_userlist_filter
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_LOGIN_FILTER" user_ldap ldap_login_filter
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_TLS" user_ldap ldap_tls
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_HOST" user_ldap ldap_host
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_BIND_USER" user_ldap ldap_dn
s6-setuidgid nginx ./occ config:app:set --value "$(php -r 'echo base64_encode(getenv("LDAP_BIND_PASSWORD"));')" user_ldap ldap_agent_password
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_GROUP_FILTER" user_ldap ldap_group_filter
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_USER_DISPLAY_NAME" user_ldap ldap_display_name
s6-setuidgid nginx ./occ config:app:set --value "${LDAP_PORT:-389}" user_ldap ldap_port
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_BASE_USER_DN" user_ldap ldap_base_users
s6-setuidgid nginx ./occ config:app:set --value "$LDAP_UID_ATTRIBUTE" user_ldap ldap_expert_username_attr
s6-setuidgid nginx ./occ config:app:set --value "yes" user_ldap enabled

s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_backup_host
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_backup_port
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_backup_port
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_backup_port
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_backup_port
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_backup_port
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_user_display_name_2
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_gid_number
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_userfilter_objectclass
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_userfilter_groups
s6-setuidgid nginx ./occ config:app:set --value "authentication" user_ldap types
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap ldap_user_filter_mode
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap ldap_group_filter_mode
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_groupfilter_objectclass
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap ldap_turn_off_cert_check
s6-setuidgid nginx ./occ config:app:set --value "uniqueMember" user_ldap ldap_group_member_assoc_attribute
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap ldap_login_filter_mode
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap ldap_loginfilter_email
s6-setuidgid nginx ./occ config:app:set --value "1" user_ldap ldap_loginfilter_username
s6-setuidgid nginx ./occ config:app:set --value "1" user_ldap ldap_loginfilter_attributes
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_quota_attr
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_quota_def
s6-setuidgid nginx ./occ config:app:set --value "600" user_ldap ldap_cache_ttl
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_override_main_server
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_attributes_for_user_search
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_attributes_for_group_search
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap ldap_experienced_admin
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap home_folder_naming_rule
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap has_memberof_filter_support
s6-setuidgid nginx ./occ config:app:set --value "1" user_ldap use_memberof_to_detect_membership
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_expert_uuid_user_attr
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_expert_uuid_group_attr
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap last_jpegPhoto_lookup
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap ldap_nested_groups
s6-setuidgid nginx ./occ config:app:set --value "500" user_ldap ldap_paging_size
s6-setuidgid nginx ./occ config:app:set --value "0" user_ldap ldap_turn_on_pwd_change
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_dynamic_group_member_url
s6-setuidgid nginx ./occ config:app:set --value "" user_ldap ldap_default_ppolicy_dn
s6-setuidgid nginx ./occ config:app:set --value "1" user_ldap ldap_configuration_active
