#!/bin/bash
# Create version-stamped locked backup using backup-central
set -e

# Get version from VERSION file
VERSION=$(cat /opt/media-downloader/VERSION | tr -d '[:space:]')

# Create timestamp
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
BACKUP_NAME="${VERSION}-${TIMESTAMP}"

# Configuration
PROFILE_ID="profile-media-downloader"

echo "╔════════════════════════════════════════════════╗"
echo "║   Media Downloader Version Backup              ║"
echo "╠════════════════════════════════════════════════╣"
echo "║ Version: ${VERSION}                            ║"
echo "║ Name:    ${BACKUP_NAME}                        ║"
echo "╚════════════════════════════════════════════════╝"
echo ""

echo "⏳ Starting backup using backup-central..."
echo ""

# Run backup using CLI with profile, custom name, and locked flag
backup-central backup -P "$PROFILE_ID" -n "$BACKUP_NAME" -l

if [ $? -eq 0 ]; then
    echo ""
    echo "╔════════════════════════════════════════════════╗"
    echo "║            Backup Complete                     ║"
    echo "╠════════════════════════════════════════════════╣"
    echo "║ Name:    ${BACKUP_NAME}                        ║"
    echo "║ Profile: Media Downloader                      ║"
    echo "║ Status:  Locked & Protected                    ║"
    echo "║ Type:    Incremental                           ║"
    echo "╚════════════════════════════════════════════════╝"
    echo ""
    echo "✓ Version backup created successfully!"
    echo ""
else
    echo ""
    echo "✗ Backup failed!"
    echo ""
    exit 1
fi
