#!/bin/bash
#
# shellcheck disable=SC1091,SC2086,SC2207,SC2162
#
# Copyright (c) 2023-2026 openmediavault plugin developers
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
#
# version: 0.9.2

export LC_ALL=C.UTF-8

. /usr/share/openmediavault/scripts/helper-functions

filter=${1}

if [ -z "${filter}" ]; then
  filter="*"
fi

if [[ "${filter}" == *","* ]]; then
  filter="@(${filter//,/|})"
fi

# Get the backup shared folder reference and path
sfref=$(omv_config_get "/config/services/compose/backupsharedfolderref")
if ! omv_isuuid "${sfref}"; then
  echo "No backup sharedfolder set."
  exit 13
fi
backuppath="$(omv_get_sharedfolder_path "${sfref}")"
if [ ! -d "${backuppath}" ]; then
  echo "Backup shared folder directory does not exist.  Exiting..."
  exit 14
fi
backuppath="${backuppath/%\/}"
echo "Backup path :: ${backuppath}"

find "${backuppath}" -mindepth 1 -maxdepth 1 -type d -iname "${filter}" | while read path; do
  echo "path :: ${path}"
  compose="$(basename "${path}")"
  echo "compose :: ${compose}"
  omv-compose-restore "${compose}"
done

exit 0
