#!/bin/bash
#
# shellcheck disable=SC1091,SC2053,SC2086,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.0.1

export LC_ALL=C.UTF-8

declare -i count=0
declare -i index=0

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

filter="${1}"

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

xpath="/config/services/compose/files/file"
count=$(omv_config_get_count "${xpath}")
index=1
while [ ${index} -le ${count} ]; do
  pos="${xpath}[position()=${index}]"
  name=$(omv_config_get "${pos}/name")
  if [ -n "${filter}" ] && [[ "${name}" != ${filter} ]]; then
    index=$(( index + 1 ))
    continue
  fi
  echo -e "\nSyncing ${name} ..."
  uuid=$(omv_config_get "${pos}/uuid")
  /usr/sbin/omv-rpc -u admin "compose" "importChanges" "{\"uuid\":\"${uuid}\"}"
  index=$(( index + 1 ))
done;

exit 0
