#!/bin/bash

# RPI Control Hat - Data Cleanup Utility
# Clears all logs and gathered measurement data

# Package installation paths
VAR_DIR="/var/lib/rpi-power-hat"
MEASUREMENTS_DIR="$VAR_DIR/measurements"
HISTORY_DIR="$VAR_DIR/history"
IMAGES_DIR="$VAR_DIR/images"
KEYS_DIR="$VAR_DIR/keys"
WORKING_DIR="/tmp/rpi-power-hat"

echo "=================================================="
echo "  RPI Control Hat - Data Cleanup Utility"
echo "=================================================="
echo ""
echo "This script will permanently delete the following:"
echo ""

# Count and show what will be deleted
MEASUREMENT_COUNT=$(find "$MEASUREMENTS_DIR" -name "*.csv" 2>/dev/null | wc -l)
HISTORY_COUNT=$(find "$HISTORY_DIR" -name "*.log" 2>/dev/null | wc -l)
WORKING_COUNT=$(find "$WORKING_DIR" -type f 2>/dev/null | wc -l)

echo "MEASUREMENTS: $MEASUREMENT_COUNT CSV files in $MEASUREMENTS_DIR"
echo "HISTORY:     $HISTORY_COUNT log files in $HISTORY_DIR"
echo "WORKING:     $WORKING_COUNT temporary files in $WORKING_DIR"

# Single confirmation prompt
echo "Are you sure you want to permanently delete all logs and measurement data? (yes/no)"
read -r response

if [[ "$response" != "yes" ]]; then
	echo "Cleanup cancelled. No files were deleted."
	exit 0
fi

echo ""
echo "Starting cleanup..."

# Clear measurements
if [[ -d "$MEASUREMENTS_DIR" ]]; then
	rm -f "$MEASUREMENTS_DIR"/*.csv
	echo "Cleared $MEASUREMENT_COUNT measurement files"
fi

# Clear history
if [[ -d "$HISTORY_DIR" ]]; then
	rm -f "$HISTORY_DIR"/*.log
	echo "Cleared $HISTORY_COUNT history files"
fi

# Clear working directory
if [[ -d "$WORKING_DIR" ]]; then
	rm -rf "$WORKING_DIR"
	echo "Cleared $WORKING_COUNT temporary files"
fi

echo ""
echo "Note: SSH keys in $KEYS_DIR and images in $IMAGES_DIR are preserved."
echo "Cleanup completed"
exit 0