#!/bin/sh
# Kotoba desktop launcher installer (Linux). Not a rewrite of the app --
# installs a launcher + a .desktop entry that opens the real
# app.kotoba.cloud web app in a chromeless window of whatever
# Chromium-based browser is installed, or the default browser otherwise.
set -e

PREFIX="${KOTOBA_INSTALL_PREFIX:-$HOME/.local}"
BIN_DIR="$PREFIX/bin"
APP_DIR="$PREFIX/share/applications"
URL="https://app.kotoba.cloud/"

mkdir -p "$BIN_DIR" "$APP_DIR"

cat > "$BIN_DIR/kotoba-app" <<LAUNCHER
#!/bin/sh
URL="$URL"
for BROWSER in google-chrome google-chrome-stable chromium chromium-browser microsoft-edge brave-browser; do
  if command -v "\$BROWSER" >/dev/null 2>&1; then
    exec "\$BROWSER" --app="\$URL" --user-data-dir="\$HOME/.local/share/kotoba/chrome-profile"
  fi
done
exec xdg-open "\$URL"
LAUNCHER
chmod +x "$BIN_DIR/kotoba-app"

cat > "$APP_DIR/cloud.kotoba.app.desktop" <<DESKTOP
[Desktop Entry]
Type=Application
Name=Kotoba
Comment=Hermes-on-Modal sandbox (app.kotoba.cloud)
Exec=$BIN_DIR/kotoba-app
Terminal=false
Categories=Utility;Network;
DESKTOP

echo "Installed: $BIN_DIR/kotoba-app"
echo "Menu entry: $APP_DIR/cloud.kotoba.app.desktop"
case ":$PATH:" in
  *":$BIN_DIR:"*) : ;;
  *) echo "Note: $BIN_DIR is not on your PATH -- add it, or launch via the desktop menu entry." ;;
esac
