#!/bin/bash
# Usage: ./start_by_uuid.sh <uuid>
# Example: ./start_by_uuid.sh abc-001-xxxx-xxxx-xxxx
exec > /4server/data/log/nukeContainer.log 2>&1
echo "$(date '+%Y-%m-%d %H:%M') Nuke container $1"

source /4server/sbin/helpers

BIN_PATH="/4server/sbin"

UUID="$1"

if [[ -z "$UUID" ]]; then
    echo "Usage: $0 <uuid>"
    exit 1
fi


get_container_status() {
    local uuid="$1"
    # Get the container ID or name matching the UUID
    CONTAINER_ID=$(docker ps -a --filter "name=$uuid" --format "{{.ID}}")
    if [[ -z "$CONTAINER_ID" ]]; then
        echo "not_found"
        return
    fi

    STATUS=$(docker inspect -f '{{.State.Status}}' "$CONTAINER_ID")
    echo "$STATUS"
}

# Check if container exists
STATUS=$(get_container_status "$UUID")
if [[ "$STATUS" == "running" ]]; then
    echo "Container $UUID is still running. Aborting deletion."
    exit 2
fi




get_contract_info

# Extract the second part of UUID (split by "-")
SECOND_PART=$(echo "$UUID" | cut -d'-' -f2)

# Decide which script to run
case "$SECOND_PART" in
    001)
        "$BIN_PATH/nuke/n8n"
        ;;
    002)
        "$BIN_PATH/nuke/ODOO_19"
        ;;
    *)
        echo "Unknown UUID type: $SECOND_PART"
        exit 2
        ;;
esac

#sqlite3 "/4server/data/contracts.db" <<SQL
#DELETE FROM containers WHERE UUID='$UUID';
#SQL



echo "Container $UUID successfully nuked."

