#!/bin/bash

# Set parameters

LOG_DIR="/var/log/facmgr"
LOG_FILE="$LOG_DIR/filecoin_station_reset.log"
SERVICE_FILE_PATH="/etc/systemd/system/filecoin_station_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 "filecoin_station"
docker-compose -f /opt/filecoin_station_install/docker-compose.yml down --remove-orphans
if docker ps -q --filter "label=filecoin_station" > /dev/null; then
    docker stop $(docker ps -aq --filter "label=filecoin_station")
    docker rm $(docker ps -aq --filter "label=filecoin_station")
else
    log "No containers with label 'filecoin_station' found."
fi

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

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

# End of script
log "Reset filecoin_station successful"


exit 0




