#!/usr/bin/env bash

# Usage: ./script.sh uuids.txt
#
sudo -u archivematica bash -c " \
    set -a -e -x
    source /etc/default/archivematica-dashboard || \
        source /etc/sysconfig/archivematica-dashboard \
            || (echo 'Environment file not found'; exit 1)
    /usr/share/archivematica/virtualenvs/archivematica/bin/python -m archivematica.dashboard.manage \
        rebuild_aip_index_from_storage_service --delete --uuid ec5c14b5-a629-476a-be48-21d36dda7cc0
";

set -euo pipefail

if [ $# -ne 1 ]; then
  echo "Usage: $0 <file-with-uuids>"
  exit 1
fi

UUID_FILE="$1"

if [ ! -f "$UUID_FILE" ]; then
  echo "File not found: $UUID_FILE"
  exit 1
fi

while IFS= read -r uuid; do
  [[ -z "$uuid" ]] && continue

  echo "Processing UUID: $uuid"

  sudo -u archivematica bash -c "
    set -a -e -x
    source /etc/default/archivematica-dashboard 2>/dev/null ||
    source /etc/sysconfig/archivematica-dashboard 2>/dev/null ||
    { echo 'Environment file not found'; exit 1; }
    /usr/share/archivematica/virtualenvs/archivematica/bin/python \
      -m archivematica.dashboard.manage \
      rebuild_aip_index_from_storage_service --delete --uuid $uuid
  "

done < "$UUID_FILE"
