#!/bin/bash
# Strumenti per la gara Cyber Escape (26/11/2026).
# Va lanciato nella VM di CyberChallenge.IT (Ubuntu 24.04) oppure su qualsiasi Ubuntu 24.04, anche ARM (Mac M1-M4).
# Uso: bash 03_cyberescape.sh
set -e
export DEBIAN_FRONTEND=noninteractive

echo "wireshark-common wireshark-common/install-setuid boolean false" | sudo debconf-set-selections

sudo apt-get update
sudo apt-get install -y \
    wireshark \
    tshark \
    libimage-exiftool-perl \
    binwalk \
    xxd \
    file \
    zip \
    unzip \
    curl \
    python3 \
    python3-pil \
    openssl

STRUMENTI="$HOME/Strumenti"
mkdir -p "$STRUMENTI"
if [ ! -f "$STRUMENTI/CyberChef/index.html" ]; then
    echo "Scarico CyberChef (versione offline)..."
    URL=$(curl -fsSL https://api.github.com/repos/gchq/CyberChef/releases/latest \
        | python3 -c "import json,sys; print(next(a['browser_download_url'] for a in json.load(sys.stdin)['assets'] if a['name'].endswith('.zip')))") \
        && curl -fsSL "$URL" -o /tmp/cyberchef.zip \
        && rm -rf "$STRUMENTI/CyberChef" && mkdir -p "$STRUMENTI/CyberChef" \
        && unzip -q /tmp/cyberchef.zip -d "$STRUMENTI/CyberChef" \
        && mv "$STRUMENTI"/CyberChef/CyberChef_*.html "$STRUMENTI/CyberChef/index.html" \
        && rm -f /tmp/cyberchef.zip \
        || echo "CyberChef non scaricato: si puo' usare anche senza, non e' obbligatorio."
fi

if [ -d "$HOME/Desktop" ] && [ -f "$STRUMENTI/CyberChef/index.html" ]; then
    cat > "$HOME/Desktop/CyberChef.desktop" <<EOF
[Desktop Entry]
Type=Application
Name=CyberChef
Exec=xdg-open $STRUMENTI/CyberChef/index.html
Icon=accessories-text-editor
EOF
    chmod +x "$HOME/Desktop/CyberChef.desktop"
fi

echo
echo "Controllo:"
for p in wireshark tshark exiftool binwalk xxd file unzip python3 openssl; do
    if command -v "$p" >/dev/null; then echo "  ok      $p"; else echo "  MANCA   $p"; fi
done
[ -f "$STRUMENTI/CyberChef/index.html" ] && echo "  ok      CyberChef in $STRUMENTI/CyberChef" || echo "  (facoltativo) CyberChef non presente"
echo "Pronto per la gara."
