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

# EC2 to VMware Migration Preflight Script
#
# Purpose:
#   Perform non-destructive checks before migrating an AWS EC2 instance to VMware.
#
# What this script does:
#   - Validates required tools are available
#   - Collects basic source-system information
#   - Checks filesystem, boot, network, and service readiness indicators
#   - Produces a timestamped report for migration planning
#
# What this script does NOT do:
#   - It does not export disks
#   - It does not modify boot configuration
#   - It does not change networking
#   - It does not shut down services
#   - It does not delete snapshots, files, or volumes
#
# Usage:
#   ./ec2-to-vmware-preflight.sh [--report-file /path/to/report.txt] [--quiet]
#
# Notes:
#   - Run on the source EC2 instance when possible.
#   - Review output with your virtualization and application teams.
#   - Adapt checks to your OS and application stack as needed.

REPORT_FILE=""
QUIET=0

usage() {
  cat <<'EOF'
Usage: ec2-to-vmware-preflight.sh [options]

Options:
  --report-file FILE   Write the report to FILE instead of stdout
  --quiet              Reduce informational output
  -h, --help           Show this help message
EOF
}

log() {
  if [[ "$QUIET" -eq 0 ]]; then
    printf '%s\n' "$*"
  fi
}

warn() {
  printf 'WARN: %s\n' "$*" >&2
}

have_cmd() {
  command -v "$1" >/dev/null 2>&1
}

append_report() {
  if [[ -n "$REPORT_FILE" ]]; then
    printf '%s\n' "$*" >> "$REPORT_FILE"
  else
    printf '%s\n' "$*"
  fi
}

run_cmd() {
  local label="$1"
  shift
  append_report "## ${label}"
  if "$@" >/tmp/ec2_vmware_preflight.out 2>/tmp/ec2_vmware_preflight.err; then
    sed 's/^/  /' /tmp/ec2_vmware_preflight.out | while IFS= read -r line; do append_report "$line"; done
  else
    append_report "  Command failed: $*"
    if [[ -s /tmp/ec2_vmware_preflight.out ]]; then
      sed 's/^/  /' /tmp/ec2_vmware_preflight.out | while IFS= read -r line; do append_report "$line"; done
    fi
    if [[ -s /tmp/ec2_vmware_preflight.err ]]; then
      sed 's/^/  /' /tmp/ec2_vmware_preflight.err | while IFS= read -r line; do append_report "$line"; done
    fi
  fi
  append_report ""
}

cleanup_temp() {
  rm -f /tmp/ec2_vmware_preflight.out /tmp/ec2_vmware_preflight.err
}
trap cleanup_temp EXIT

while [[ $# -gt 0 ]]; do
  case "$1" in
    --report-file)
      [[ $# -ge 2 ]] || { echo "ERROR: --report-file requires a value" >&2; exit 1; }
      REPORT_FILE="$2"
      shift 2
      ;;
    --quiet)
      QUIET=1
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "ERROR: Unknown argument: $1" >&2
      usage
      exit 1
      ;;
  esac
done

if [[ -n "$REPORT_FILE" ]]; then
  : > "$REPORT_FILE"
fi

append_report "# EC2 to VMware Migration Preflight Report"
append_report "Generated: $(date -u +'%Y-%m-%dT%H:%M:%SZ')"
append_report ""

append_report "## 1) Tooling check"
for cmd in uname hostname ip lsblk df awk sed grep; do
  if have_cmd "$cmd"; then
    append_report "- $cmd: available"
  else
    append_report "- $cmd: missing"
  fi
done
append_report ""

append_report "## 2) Host identity"
run_cmd "Kernel and OS basics" uname -a
run_cmd "Hostname" hostname -f
run_cmd "Uptime" uptime

append_report "## 3) CPU and memory snapshot"
if have_cmd free; then
  run_cmd "Memory" free -h
else
  append_report "- free: not available on this system"
fi
if have_cmd lscpu; then
  run_cmd "CPU" lscpu
fi

append_report "## 4) Block devices and filesystem overview"
run_cmd "Block devices" lsblk -f
run_cmd "Filesystem usage" df -hT

append_report "## 5) Network snapshot"
if have_cmd ip; then
  run_cmd "IP addresses" ip addr
  run_cmd "Routes" ip route
else
  append_report "- ip: not available"
fi

append_report "## 6) Boot mode hint"
if [[ -d /sys/firmware/efi ]]; then
  append_report "- Boot mode appears to be: UEFI"
else
  append_report "- Boot mode appears to be: BIOS/legacy or EFI not exposed"
fi
append_report ""

append_report "## 7) Service and application checks"
append_report "Review these manually based on your workload:"
append_report "- Confirm critical services and scheduled jobs"
append_report "- Confirm application logs are clean before migration"
append_report "- Confirm backups are recent and restorable"
append_report "- Confirm licensing and identity assumptions are documented"
append_report "- Confirm DNS, static IP, and routing expectations for VMware"
append_report ""

append_report "## 8) Migration readiness questions"
append_report "- Is the guest OS version supported on the target VMware platform?"
append_report "- Does the VM use the same boot mode expected by VMware?"
append_report "- Are disk controllers, partition style, and root volume layout documented?"
append_report "- Are encryption and transfer handling requirements approved?"
append_report "- Is there a validated rollback plan and isolated test network?"
append_report "- Have you verified application dependencies outside AWS-managed services?"
append_report ""

append_report "## 9) Suggested next actions"
append_report "1. Review this report with infrastructure and application owners."
append_report "2. Validate export or backup consistency requirements."
append_report "3. Prepare an isolated VMware test network or port group."
append_report "4. Perform a non-production boot test after conversion."
append_report "5. Sign off only after application-level validation succeeds."
append_report ""

log "Preflight report complete."
if [[ -n "$REPORT_FILE" ]]; then
  log "Report saved to: $REPORT_FILE"
fi
```