#!/usr/bin/env bash
# SaQura Crypto Inventory - Linux-Schnellstart. Mehrsprachig (DE/EN/JA): erkennt die
# System-Sprache, lokalisiert die Anzeige und gibt --lang an das Tool weiter. Waehlt die
# passende Single-Exe (arm64/x64) oder faellt auf Node zurueck. Keine Signatur noetig.
cd "$(dirname "$0")" || exit 1

LC="$(printf '%s' "${LANG:-${LC_ALL:-}}" | cut -c1-2 | tr 'A-Z' 'a-z')"
case "$LC" in ja) L=ja;; en) L=en;; *) L=de;; esac
S() { case "$L" in en) printf '%s' "$2";; ja) printf '%s' "$3";; *) printf '%s' "$1";; esac; }

echo
echo "  ============================================"
echo "   SaQura Crypto Inventory"
echo "  ============================================"
echo

ARCH="$(uname -m)"
case "$ARCH" in aarch64|arm64) BIN="./saqura-inventory-linux-arm64";; x86_64|amd64) BIN="./saqura-inventory-linux-x64";; *) BIN="";; esac
RUNNER=""
if [ -n "$BIN" ] && [ -f "$BIN" ]; then chmod +x "$BIN" 2>/dev/null; RUNNER="$BIN"
elif command -v node >/dev/null 2>&1 && [ -f "saqura-inventory.cjs" ]; then RUNNER="node saqura-inventory.cjs"; fi
if [ -z "$RUNNER" ]; then
  echo "  $(S "Kein passendes Programm fuer '$ARCH' gefunden." "No matching binary for '$ARCH' found." "'$ARCH' に対応するプログラムが見つかりません。")"; exit 1
fi

LIC="$(ls -1 *.lic 2>/dev/null | head -1)"
if [ -z "$LIC" ]; then
  echo "  $(S "Keine Lizenzdatei (*.lic) gefunden." "No license file (*.lic) found." "ライセンスファイル(*.lic)が見つかりません。")"; exit 1
fi

read -r -p "  $(S "Pfad zu Quellcode-Ordner (Enter = ueberspringen): " "Path to a source-code folder (Enter = skip): " "ソースコードのフォルダパス（Enter でスキップ）: ")" CODE
CODEARG=""; [ -n "$CODE" ] && [ -d "$CODE" ] && CODEARG="--code $CODE"
read -r -p "  $(S "Gefuehrten Fragebogen ausfuellen? (j/N): " "Fill in the guided questionnaire? (y/N): " "ガイド付きアンケートに回答しますか？(y/N): ")" A
QARG=""; case "$A" in j|J|y|Y) QARG="--interactive";; esac

echo
echo "  $(S "Starte Inventur" "Starting inventory" "インベントリを開始") (License: $LIC) ..."
echo
$RUNNER scan --lang "$L" --license "$LIC" $CODEARG $QARG --out report --diagnostics
RC=$?
if [ -f "report/report.html" ]; then
  command -v xdg-open >/dev/null 2>&1 && xdg-open "report/report.html" >/dev/null 2>&1 || true
  echo; echo "  $(S "Bericht" "Report" "レポート"): $(pwd)/report/report.html"
fi
echo
[ "$RC" = "0" ] && echo "  $(S "Fertig." "Done." "完了。")" || echo "  $(S "Mit Hinweisen beendet — saqura-diagnostics.jsonl." "Finished with notes — saqura-diagnostics.jsonl." "注意あり — saqura-diagnostics.jsonl。")"
