#!/bin/bash

# Set parameters

LOG_DIR="/var/log/facmgr"
LOG_FILE="$LOG_DIR/prox_ai_lite_reset.log"
SERVICE_FILE_PATH="/etc/systemd/system/prox_ai_docker.service"

# Check if script is run as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

# Create log directory if it doesn't exist
if [ ! -d "$LOG_DIR" ]; then
  echo "Directory $DIR does not exist. Creating..."
  mkdir -p "$LOG_DIR"
fi

function log {
    echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" | tee -a "$LOG_FILE"
}

# Start of script
log "Starting reset process"

# Stop and remove containers with label "prox_ai_lite"
docker-compose -f /opt/prox_ai_install/docker-compose.yml down --remove-orphans
if docker ps -q --filter "label=prox_ai_lite" > /dev/null; then
    docker stop $(docker ps -aq --filter "label=prox_ai_lite")
    docker rm $(docker ps -aq --filter "label=prox_ai_lite")
else
    log "No containers with label 'prox_ai_lite' found."
fi

# Remove images with label "prox_ai_lite"
if docker images -q --filter "label=prox_ai_lite" > /dev/null; then
    docker image prune -a --force --filter "label=prox_ai_lite"
    log "Images with label 'prox_ai_lite' removed."
else
    log "No images with label 'prox_ai_lite' found."
fi

# Remove service file
rm -rf "$SERVICE_FILE_PATH"
log "Service file removed."

# End of script
log "Reset prox_ai_lite successful"


exit 0




