#!/bin/bash

cliHandleBackupCommands()
{
    local backup_type="$initial_command2"
    local action="$initial_command3"
    local name="$initial_command4"
    local extra="$initial_command5"
    local extra2="$initial_command6"

    if [[ -z "$backup_type" ]]; then
        cliShowBackupHelp
        return
    fi

    case "$backup_type" in
        app)
            case "$action" in
                ""|help) cliShowBackupHelp ;;
                create)
                    [[ -z "$name" ]] && { isNotice "No app name provided."; cliShowBackupHelp; return; }
                    backupAppStart "$name"
                    ;;
                schedule)
                    [[ -z "$name" ]] && { isNotice "No app name provided."; cliShowBackupHelp; return; }
                    backupAppSchedule "$name"
                    ;;
                delete)
                    [[ -z "$name" ]] && { isNotice "No app name provided."; cliShowBackupHelp; return; }
                    [[ -z "$extra" ]] && { isNotice "No <location_idx>:<snapshot_id> provided (e.g. 1:abc123)."; cliShowBackupHelp; return; }
                    local idx="${extra%%:*}"
                    local snap="${extra##*:}"
                    backupAppDeleteSnapshot "$idx" "$snap"
                    ;;
                delete_all)
                    [[ -z "$name" ]] && { isNotice "No app name provided."; cliShowBackupHelp; return; }
                    backupAppDeleteAll "$name"
                    ;;
                list)
                    [[ -z "$name" ]] && { isNotice "No app name provided."; cliShowBackupHelp; return; }
                    local idx
                    while IFS= read -r idx; do
                        [[ -z "$idx" ]] && continue
                        echo "--- $(resticLocationName "$idx") (loc $idx) ---"
                        engineSnapshotsJson "$idx" "$name"
                    done < <(resticEnabledLocations)
                    ;;
                *)
                    isNotice "Invalid app backup action: $action"
                    cliShowBackupHelp
                    ;;
            esac
            ;;
        all)
            backupAllApps
            ;;
        system)
            backupSystemConfig
            ;;
        scheduled)
            backupScheduleEnabledApps
            ;;
        location)
            case "$action" in
                add)
                    [[ -z "$name" ]] && { isNotice "Usage: backup location add <name> [type]"; cliShowBackupHelp; return; }
                    locationAdd "$name" "$extra"
                    ;;
                remove)
                    [[ -z "$name" ]] && { isNotice "Usage: backup location remove <idx>"; cliShowBackupHelp; return; }
                    locationRemove "$name" "$extra"
                    ;;
                list)
                    local idx
                    while IFS= read -r idx; do
                        [[ -z "$idx" ]] && continue
                        local enabled="no"; resticLocationEnabled "$idx" && enabled="yes"
                        echo "[$idx] $(resticLocationName "$idx") — type=$(resticLocationType "$idx"), enabled=$enabled"
                    done < <(resticAllLocationIndices)
                    ;;
                init)
                    engineInitAllLocations
                    ;;
                check)
                    engineCheckAllLocations "${name:-5}"
                    ;;
                stats)
                    [[ -z "$name" ]] && name="1"
                    engineLocationStats "$name"
                    ;;
                ssh-key-set)
                    [[ -z "$name" || -z "$extra" ]] && { isNotice "Usage: backup location ssh-key-set <idx> <base64-key>"; return; }
                    backupSshKeySet "$name" "$extra"
                    ;;
                ssh-key-generate)
                    [[ -z "$name" ]] && { isNotice "Usage: backup location ssh-key-generate <idx>"; return; }
                    backupSshKeyGenerate "$name"
                    ;;
                ssh-key-public)
                    [[ -z "$name" ]] && { isNotice "Usage: backup location ssh-key-public <idx>"; return; }
                    backupSshKeyPublic "$name"
                    ;;
                ssh-key-delete)
                    [[ -z "$name" ]] && { isNotice "Usage: backup location ssh-key-delete <idx>"; return; }
                    backupSshKeyDelete "$name"
                    ;;
                *) isNotice "Invalid location action: $action"; cliShowBackupHelp ;;
            esac
            ;;
        verify)
            engineCheckAllLocations "${action:-5}"
            ;;
        *)
            isNotice "Invalid backup type: $backup_type"
            cliShowBackupHelp
            ;;
    esac
}
