#!/bin/bash

# Set parameters

LOG_DIR="/var/log/facmgr"
LOG_FILE="$LOG_DIR/smbt_reset.log"
SERVICE_FILE_PATH="/etc/systemd/system/smbt_docker.service"
SAMBA_DIR="/etc/samba"
SAMBA_DATA="/data/vfs/bucket_baeqaemjq/samba"

# 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/smbt_install/docker-compose.yml down --remove-orphans
if docker ps -q --filter "label=smbt" > /dev/null; then
    docker stop $(docker ps -aq --filter "label=smbt")
    docker rm $(docker ps -aq --filter "label=smbt")
else
    log "No containers with label 'smbt' found."
fi

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

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

# End of script
log "Reset smbt successful"


exit 0




