#!/usr/bin/env bash set -euo pipefail if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then echo "Run this installer as root (sudo)." exit 1 fi script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" repo_root="$(cd "${script_dir}/.." && pwd)" default_nc_path="/var/www/nextcloud" read -r -p "Nextcloud path [${default_nc_path}]: " nc_path nc_path="${nc_path:-$default_nc_path}" if [[ ! -f "${nc_path}/occ" ]]; then echo "Could not find occ at ${nc_path}/occ. Please check the Nextcloud path." exit 1 fi occ() { if command -v sudo >/dev/null 2>&1; then sudo -u www-data php "${nc_path}/occ" "$@" else runuser -u www-data -- php "${nc_path}/occ" "$@" fi } detect_nc_url() { local url set +e url="$(occ config:system:get overwrite.cli.url 2>/dev/null)" if [[ -z "$url" ]]; then url="$(occ config:system:get trusted_domains 0 2>/dev/null)" if [[ -n "$url" && "$url" != http* ]]; then url="https://${url}" fi fi set -e echo "$url" } nc_url="$(detect_nc_url)" read -r -p "Nextcloud URL [${nc_url:-https://cloud.example.com}]: " nc_url_input nc_url="${nc_url_input:-$nc_url}" if [[ -z "$nc_url" ]]; then echo "Nextcloud URL is required." exit 1 fi nc_host="${nc_url#*://}" nc_host="${nc_host%%/*}" base_domain="$nc_host" if [[ "$nc_host" == *.* ]]; then base_domain="${nc_host#*.}" fi default_broker_host="qortalbroker.${base_domain}" read -r -p "Broker hostname [${default_broker_host}]: " broker_host broker_host="${broker_host:-$default_broker_host}" read -r -p "Broker external URL [https://${broker_host}]: " broker_url broker_url="${broker_url:-https://${broker_host}}" read -r -p "Nextcloud service user [admin]: " nc_service_user nc_service_user="${nc_service_user:-admin}" read -r -p "Nextcloud service password [admin123]: " nc_service_password nc_service_password="${nc_service_password:-admin123}" read -r -p "External Auth base URL [http://127.0.0.1:3191]: " ext_auth_url ext_auth_url="${ext_auth_url:-http://127.0.0.1:3191}" read -r -p "External Auth app ID: " ext_auth_app_id read -r -p "External Auth app secret: " ext_auth_app_secret if [[ ! "${ext_auth_url}" =~ ^https?://(127\.0\.0\.1|localhost) ]]; then echo "Warning: External Auth is not local. Ensure secondary wallet encryption is enabled." fi gen_secret() { tr -dc 'a-zA-Z0-9' "${broker_dir}/.env" </dev/null 2>&1; then rsync -a "${repo_root}/nextcloud/custom_apps/qortal_integration/" "${nc_path}/custom_apps/qortal_integration/" else rm -rf "${nc_path}/custom_apps/qortal_integration" cp -R "${repo_root}/nextcloud/custom_apps/qortal_integration" "${nc_path}/custom_apps/qortal_integration" fi echo "Enabling Nextcloud apps..." occ app:enable qortal_integration || true occ app:enable user_oidc || true occ config:app:set qortal_integration broker_internal_api_token --value="${broker_internal_token}" || true echo "Configuring user_oidc provider..." set +e occ user_oidc:provider qortal \ -c nextcloud-local \ -s "${oidc_secret}" \ -d "${broker_url}/.well-known/openid-configuration" \ --scope="openid profile email" \ --mapping-uid=sub \ --mapping-display-name=name \ --mapping-email=email set -e if ! command -v docker >/dev/null 2>&1; then echo "Docker is required but not installed. Install Docker and re-run docker compose manually." exit 1 fi if docker compose version >/dev/null 2>&1; then docker_compose="docker compose" elif command -v docker-compose >/dev/null 2>&1; then docker_compose="docker-compose" else echo "Docker Compose not found." exit 1 fi echo "Starting broker containers..." ${docker_compose} -f "${compose_dst}" --env-file "${broker_dir}/.env" up -d echo "Configuring Apache reverse proxy..." if ! a2enmod proxy proxy_http headers ssl >/dev/null 2>&1; then echo "Warning: failed to enable Apache modules (proxy/proxy_http/headers/ssl)." fi apache_site="/etc/apache2/sites-available/qortalbroker.conf" cat > "${apache_site}" < ServerName ${broker_host} ProxyPreserveHost On ProxyPass / http://127.0.0.1:3000/ ProxyPassReverse / http://127.0.0.1:3000/ RequestHeader set X-Forwarded-Proto "https" ErrorLog \${APACHE_LOG_DIR}/qortalbroker_error.log CustomLog \${APACHE_LOG_DIR}/qortalbroker_access.log combined EOF a2ensite qortalbroker.conf >/dev/null 2>&1 || true systemctl reload apache2 || true read -r -p "Run certbot for ${broker_host}? (y/N): " run_certbot if [[ "${run_certbot}" == "y" || "${run_certbot}" == "Y" ]]; then read -r -p "Let's Encrypt email: " le_email if command -v certbot >/dev/null 2>&1; then certbot --apache -d "${broker_host}" -m "${le_email}" --agree-tos --redirect --non-interactive else echo "certbot is not installed. Install it and rerun manually." fi fi echo "Install complete." echo "Broker URL: ${broker_url}" echo "Nextcloud URL: ${nc_url}" echo "OIDC provider configured with client_id=nextcloud-local" echo "If login fails, verify DNS for ${broker_host} and Apache proxy."