#!/bin/sh
# Royal Bandits MCC Kali AI installer -- installer only acts after user runs it.
# Requires Kali x86_64 and HTTPS, installs within your normal user's home.
set -eu
BASE="https://kalilinuxal.royalbanditsmccsecurity.co.uk"
EXPECTED="51fc71f399c685d7dc33fd66a14fcf42a44e2f76de3a49894ff126902fd83649"
PKG="rbmcc-kali-ai-0.1.0.tar.gz"
[ "$(uname -s)" = "Linux" ] && [ "$(uname -m)" = "x86_64" ] || { echo "Linux x86_64 required"; exit 2; }
[ "$(id -u)" -ne 0 ] || { echo "Run as your normal user, not root"; exit 2; }
grep -qi 'kali' /etc/os-release || { echo "This first release is for Kali Linux only"; exit 2; }
for c in curl sha256sum tar python3; do command -v "$c" >/dev/null || { echo "Missing: $c"; exit 2; }; done
TARGET="$HOME/.local/share/rbmcc-kali-ai"
[ ! -e "$TARGET" ] || { echo "Existing installation preserved at $TARGET -- nothing overwritten"; exit 3; }
TMP="$(mktemp -d)"
trap 'rm -rf -- "$TMP"' EXIT
echo "Downloading verified Royal Bandits AI application (no model weights) ..."
curl --proto '=https' --tlsv1.2 -fLSs --retry 2 "$BASE/releases/$PKG" -o "$TMP/$PKG"
ACTUAL="$(sha256sum "$TMP/$PKG" | awk '{print $1}')"
[ "$ACTUAL" = "$EXPECTED" ] || { echo "Download hash mismatch. Aborting."; exit 4; }
mkdir -p "$HOME/.local/share" "$HOME/.local/share/applications" "$HOME/Desktop"
tar -xzf "$TMP/$PKG" -C "$TMP"
[ -f "$TMP/rbmcc-kali-ai/kali_ai_dashboard_server.py" ] || { echo "Invalid app package"; exit 4; }
mv "$TMP/rbmcc-kali-ai" "$TARGET"
chmod u+x "$TARGET/START_RBMCC_KALI.sh"
D="$HOME/.local/share/applications/rbmcc-kickass-ai.desktop"
cat > "$D" <<EOF
[Desktop Entry]
Type=Application
Name=Royal Bandits Kick-Ass AI
Comment=Local Kali AI with Gemma, robot and read-only tool catalogue
Exec=/bin/sh "$TARGET/START_RBMCC_KALI.sh"
Terminal=true
Icon=utilities-terminal
Categories=Utility;Development;
EOF
chmod 644 "$D"
cp "$D" "$HOME/Desktop/Royal Bandits Kick-Ass AI.desktop"
chmod u+x "$HOME/Desktop/Royal Bandits Kick-Ass AI.desktop"
echo "APP_INSTALLED: $TARGET"
echo "Desktop shortcut created. No Kali tools or system permissions changed."
if command -v ollama >/dev/null 2>&1; then
  echo "Ollama detected."
  printf "Download Gemma 3 1B into local model storage now? [y/N] "
  read yn || yn=N
  case "$yn" in y|Y|yes|YES)
    ollama pull gemma3:1b || echo "Model pull failed: check Ollama service/internet. App files remain installed."
    ;; *) echo "Model download skipped; run: ollama pull gemma3:1b";; esac
else
  echo "Ollama not yet installed. Use the OFFICIAL site https://ollama.com/download/linux"
  echo "Then run: ollama pull gemma3:1b"
fi
echo "Launch using your Royal Bandits Kick-Ass AI desktop shortcut."
