#!/bin/bash

################################################################################
# Acronis List Backups
################################################################################
# Purpose: List all backups and archives using acrocmd
# Features:
# - Show backup archives
# - Show backup versions
# - Display backup details (size, date, location)
################################################################################

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
source "$SCRIPT_DIR/lib/common-functions.sh"
source "$SCRIPT_DIR/lib/system-detect.sh"

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

clear
print_banner "List Backups & Archives"

echo ""
echo -e "${BOLD}Retrieving backup information...${NC}"
echo ""

# Check if acrocmd is available
if [ ! -f "/usr/sbin/acrocmd" ]; then
    print_error "acrocmd command-line tool not found"
    echo ""
    press_enter
    exit 1
fi

# List archives
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo -e "${BOLD}Backup Archives${NC}"
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""

if /usr/sbin/acrocmd list archives 2>/dev/null | grep -q .; then
    /usr/sbin/acrocmd list archives 2>/dev/null
else
    echo -e "${YELLOW}No backup archives found${NC}"
    echo ""
    echo "Possible reasons:"
    echo "  • No backups have been created yet"
    echo "  • Agent not registered with Acronis Cloud"
    echo "  • No backup plans configured"
fi

echo ""
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo -e "${BOLD}Backup Details${NC}"
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""

if /usr/sbin/acrocmd list backups 2>/dev/null | grep -q .; then
    /usr/sbin/acrocmd list backups 2>/dev/null
else
    echo -e "${YELLOW}No backup details available${NC}"
fi

echo ""
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""
echo -e "${BOLD}Options:${NC}"
echo ""
echo "  • Create backups via 'Trigger Manual Backup'"
echo "  • Configure plans in Acronis web console"
echo "  • Check backup status for active operations"
echo ""

press_enter
