#!/bin/bash

###############################################################################
# Glacier Archive Status Checker
#
# Shows progress of the archive-split process:
# - Total data size and folder count from source
# - Archives completed vs remaining per transit disk
# - Disk usage and free space on transit disks
# - Recent activity from archive logs
###############################################################################

# Configuration - must match glacier_archive_split.sh
SOURCE_DIR="/mnt/source_hdd/PhotosVideos"
TRANSIT_DISK_1="/mnt/sdd_transit"
TRANSIT_DISK_2="/mnt/sdc_transit"
STATE_DIR_NAME=".archive_state"
MASTER_MANIFEST_NAME="archive_master_manifest.txt"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Glacier Archive Status${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Source data summary
if [ -d "$SOURCE_DIR" ]; then
    total_folders=$(find "$SOURCE_DIR" -mindepth 1 -maxdepth 1 -type d ! -name ".*" | wc -l | tr -d ' ')
    total_size=$(du -sh "$SOURCE_DIR" 2>/dev/null | cut -f1)
    echo -e "${CYAN}Source:${NC} $SOURCE_DIR"
    echo -e "${CYAN}Total:${NC} $total_folders folders, $total_size"
else
    echo -e "${RED}Source directory not found: $SOURCE_DIR${NC}"
    total_folders=0
fi
echo ""

# Per-disk status
for disk in "$TRANSIT_DISK_1" "$TRANSIT_DISK_2"; do
    disk_name=$(basename "$disk")
    echo -e "${BLUE}--- $disk_name ($disk) ---${NC}"

    if [ ! -d "$disk" ]; then
        echo -e "  ${RED}Disk not mounted${NC}"
        echo ""
        continue
    fi

    # Disk space
    disk_total=$(df -h "$disk" 2>/dev/null | tail -1 | awk '{print $2}')
    disk_used=$(df -h "$disk" 2>/dev/null | tail -1 | awk '{print $3}')
    disk_free=$(df -h "$disk" 2>/dev/null | tail -1 | awk '{print $4}')
    disk_pct=$(df -h "$disk" 2>/dev/null | tail -1 | awk '{print $5}')
    echo -e "  Disk: ${disk_used} used / ${disk_total} total (${disk_free} free, ${disk_pct} used)"

    # Archive count and sizes
    archive_count=$(find "$disk" -maxdepth 1 -name "archive_*.tar.gz" 2>/dev/null | wc -l | tr -d ' ')
    if (( archive_count > 0 )); then
        archive_total_size=$(du -shc "$disk"/archive_*.tar.gz 2>/dev/null | tail -1 | cut -f1)
        echo -e "  ${GREEN}Archives: $archive_count files, $archive_total_size total${NC}"
    else
        echo -e "  ${YELLOW}Archives: none${NC}"
    fi

    # Completed count from state
    completed_file="$disk/$STATE_DIR_NAME/completed.txt"
    if [ -f "$completed_file" ]; then
        completed_count=$(wc -l < "$completed_file" | tr -d ' ')
        echo -e "  ${GREEN}Completed: $completed_count archives${NC}"

        # Show completed archives
        if (( completed_count > 0 )); then
            echo "  Completed list:"
            while IFS= read -r name; do
                # Show size if archive file exists
                if [ -f "$disk/$name" ]; then
                    size=$(du -sh "$disk/$name" 2>/dev/null | cut -f1)
                    echo "    $name  ($size)"
                else
                    echo "    $name  (uploaded/removed)"
                fi
            done < "$completed_file"
        fi
    else
        echo -e "  ${YELLOW}No state file (archiving not started)${NC}"
    fi

    # Show manifest count
    manifest_count=$(find "$disk" -maxdepth 1 -name "archive_*.manifest.txt" 2>/dev/null | wc -l | tr -d ' ')
    if (( manifest_count > 0 )); then
        echo "  Manifests: $manifest_count"
    fi

    # Recent log activity
    latest_log=$(ls -t "$disk/$STATE_DIR_NAME"/archive_*.log 2>/dev/null | head -1)
    if [ -n "$latest_log" ]; then
        echo ""
        echo "  Latest log ($(basename "$latest_log")):"
        tail -3 "$latest_log" | while IFS= read -r line; do
            echo "    $line"
        done
    fi

    echo ""
done

# Master manifest summary
echo -e "${BLUE}--- Master Manifests ---${NC}"
for disk in "$TRANSIT_DISK_1" "$TRANSIT_DISK_2"; do
    disk_name=$(basename "$disk")
    master="$disk/$MASTER_MANIFEST_NAME"
    if [ -f "$master" ]; then
        # Count non-header lines (entries start with a timestamp like 2026-)
        entry_count=$(grep -c '^[0-9]\{4\}-' "$master" 2>/dev/null || echo 0)
        echo "  $disk_name: $entry_count entries in $MASTER_MANIFEST_NAME"
    fi
done

echo ""
echo -e "${BLUE}========================================${NC}"
