Files
2026-04-20 10:09:58 -07:00

107 lines
2.7 KiB
Bash

#!/usr/bin/env bash
nextcloud_apps_paths_from_config() {
local nextcloud_root="${1:-}"
local config_php=""
if [[ -z "${nextcloud_root}" ]]; then
return 1
fi
config_php="${nextcloud_root%/}/config/config.php"
if [[ ! -f "${config_php}" ]]; then
return 1
fi
php -r '
$configFile = $argv[1];
$CONFIG = [];
include $configFile;
$appsPaths = $CONFIG["apps_paths"] ?? [];
if (!is_array($appsPaths)) {
exit(0);
}
foreach ($appsPaths as $entry) {
if (!is_array($entry)) {
continue;
}
$path = isset($entry["path"]) ? trim((string) $entry["path"]) : "";
if ($path === "") {
continue;
}
$url = isset($entry["url"]) ? trim((string) $entry["url"]) : "";
$writable = !empty($entry["writable"]) ? "1" : "0";
echo $path, "\t", $url, "\t", $writable, "\n";
}
' "${config_php}"
}
nextcloud_apps_path_is_configured() {
local nextcloud_root="${1:-}"
local apps_path="${2:-}"
local configured_path=""
local configured_url=""
local configured_writable=""
if [[ -z "${nextcloud_root}" || -z "${apps_path}" ]]; then
return 1
fi
while IFS=$'\t' read -r configured_path configured_url configured_writable; do
if [[ -n "${configured_path}" && "${configured_path}" == "${apps_path}" ]]; then
return 0
fi
done < <(nextcloud_apps_paths_from_config "${nextcloud_root}" 2>/dev/null || true)
return 1
}
nextcloud_select_apps_path() {
local nextcloud_root="${1:-}"
local requested_path="${2:-}"
local selected_path=""
local default_apps_path=""
local default_selected_path=""
local configured_path=""
local configured_url=""
local configured_writable=""
if [[ -n "${nextcloud_root}" && -n "${requested_path}" ]]; then
if nextcloud_apps_path_is_configured "${nextcloud_root}" "${requested_path}"; then
printf '%s\n' "${requested_path}"
return 0
fi
fi
if [[ -n "${nextcloud_root}" ]]; then
default_apps_path="${nextcloud_root%/}/apps"
while IFS=$'\t' read -r configured_path configured_url configured_writable; do
if [[ -z "${configured_path}" ]]; then
continue
fi
if [[ "${configured_path}" == "${default_apps_path}" ]]; then
default_selected_path="${configured_path}"
fi
if [[ -z "${selected_path}" ]]; then
selected_path="${configured_path}"
fi
done < <(nextcloud_apps_paths_from_config "${nextcloud_root}" 2>/dev/null || true)
fi
if [[ -n "${default_selected_path}" ]]; then
printf '%s\n' "${default_selected_path}"
return 0
fi
if [[ -n "${selected_path}" ]]; then
printf '%s\n' "${selected_path}"
return 0
fi
if [[ -n "${nextcloud_root}" ]]; then
printf '%s\n' "${nextcloud_root%/}/apps"
return 0
fi
return 1
}