#!/bin/bash

# Check that a search string was provided
if [ -z "$1" ]; then
    echo "$(date '+%Y-%m-%d %H:%M:%S') Usage: $0 <search_string>"
    exit 1
fi

SEARCH="$1"
DB_PATH="/4server/data/contracts.db"

# Get all UUIDs where tags include the search string
uuids=$(sqlite3 "$DB_PATH" "SELECT UUID FROM containers WHERE tags LIKE '%$SEARCH%';")

# Loop through each UUID
for uuid in $uuids; do
    # Check if a container with this name is running
    if doas docker ps --format '{{.Names}}' | grep -qx "$uuid"; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') Restarting $uuid"
	/4server/sbin/startContainer $uuid
    fi
done
