#!/usr/bin/env bash
# ==============================================================================
# HOWTOCTF.COM — Interactive Kali Linux Cheatsheet Browser
# Aligned with ITSO 2026 Competition Standards
# ==============================================================================

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TXT_FILE="$DIR/howtoctf_cheatsheet.txt"

if [ ! -f "$TXT_FILE" ]; then
  # Fallback to local file in current dir
  TXT_FILE="howtoctf_cheatsheet.txt"
fi

show_menu() {
  clear
  echo -e "\e[1;36m╔══════════════════════════════════════════════════════════════════════════════╗\e[0m"
  echo -e "\e[1;36m║   \e[1;37mHOWTOCTF.COM — KALI LINUX CTF COMPETITION CHEATSHEET & OPERATIONS GUIDE   \e[1;36m║\e[0m"
  echo -e "\e[1;36m║   \e[0;90mAligned with ITSO 2026 Competition Standards · Select a Topic Below        \e[1;36m║\e[0m"
  echo -e "\e[1;36m╚══════════════════════════════════════════════════════════════════════════════╝\e[0m"
  echo ""
  echo -e "  \e[1;32m[0]\e[0m Module 00: Core Orientation, CIA Triad, Standard Ports, SUID & CVE"
  echo -e "  \e[1;32m[1]\e[0m Module 01: Crypto & Forensics (Base64, John the Ripper, MD5, strings)"
  echo -e "  \e[1;32m[2]\e[0m Module 02: Network Setup & Wireshark (Subnetting, Cleartext FTP, POST)"
  echo -e "  \e[1;32m[3]\e[0m Module 03: Endpoint Security & Logs (auth.log Triage, crontab, Shells)"
  echo -e "  \e[1;32m[4]\e[0m Module 04: Vulnerability Auditing & Web (HTML Comments, SQLi, LFI)"
  echo -e "  \e[1;33m[E]\e[0m Top 10 Emergency One-Liners (Quick Recall)"
  echo -e "  \e[1;36m[S]\e[0m Kali Built-In Offline Manual Tricks (100% Legal)"
  echo -e "  \e[1;37m[A]\e[0m View Entire Cheatsheet (Scrollable with less)"
  echo -e "  \e[1;31m[Q]\e[0m Exit"
  echo ""
  echo -n -e "\e[1;37mEnter your choice [0-4, E, S, A, Q]: \e[0m"
}

view_section() {
  local pattern="$1"
  clear
  if [ -f "$TXT_FILE" ]; then
    grep -A 35 "\[$pattern\]" "$TXT_FILE" | head -n 35
  else
    echo "Cheatsheet data file not found."
  fi
  echo ""
  read -p "Press Enter to return to menu..."
}

while true; do
  show_menu
  read choice
  case "$choice" in
    0) view_section "MODULE 00" ;;
    1) view_section "MODULE 01" ;;
    2) view_section "MODULE 02" ;;
    3) view_section "MODULE 03" ;;
    4) view_section "MODULE 04" ;;
    e|E) view_section "EMERGENCY" ;;
    s|S) view_section "SURVIVAL" ;;
    a|A)
      if command -v less >/dev/null 2>&1; then
        less -R "$TXT_FILE"
      else
        cat "$TXT_FILE"
      fi
      ;;
    q|Q)
      clear
      echo -e "\e[1;32mGood luck in the competition! Train hard, win clean.\e[0m"
      exit 0
      ;;
    *)
      echo "Invalid selection."
      sleep 1
      ;;
  esac
done
