#!/bin/bash
# Legacy launcher: bevorzugt native Tauri-Apps, sonst statische Web-UI im Browser.
set -euo pipefail

APP="${1:?usage: cyloneos-webapp <app-id>}"

case "${APP}" in
	files)
		if [ -x /usr/lib/cyloneos/files/cyloneos-files ]; then
			exec /usr/bin/cyloneos-files
		fi
		;;
	settings)
		if [ -x /usr/lib/cyloneos/settings/cyloneos-settings ]; then
			exec /usr/bin/cyloneos-settings
		fi
		;;
esac

ROOT="/usr/share/cyloneos/${APP}"
INDEX="${ROOT}/index.html"

if [ ! -f "${INDEX}" ]; then
	echo "cyloneos-webapp: '${APP}' nicht installiert (weder Tauri noch ${INDEX})." >&2
	exit 1
fi

URL="file://${INDEX}"

if command -v epiphany-browser >/dev/null 2>&1; then
	PROFILE="${XDG_CACHE_HOME:-$HOME/.cache}/cyloneos-epiphany-${APP}"
	exec epiphany-browser --profile="${PROFILE}" --application-mode "${URL}"
fi

if command -v chromium >/dev/null 2>&1; then
	exec chromium --app="${URL}"
fi

if command -v google-chrome >/dev/null 2>&1; then
	exec google-chrome --app="${URL}"
fi

exec xdg-open "${URL}"
