#!/bin/sh
# Check if executable standalone binary exists
if [ -x /usr/share/notesapp/NotesAPP ]; then
    exec /usr/share/notesapp/NotesAPP "$@"
fi

# Fallback to Python 3 execution
if ! command -v python3 >/dev/null 2>&1; then
    MSG="Python 3 is required to run NotesAPP."
    if command -v zenity >/dev/null 2>&1; then
        zenity --error --text="$MSG" --title="NotesAPP Error"
    elif command -v kdialog >/dev/null 2>&1; then
        kdialog --error "$MSG" --title="NotesAPP Error"
    fi
    echo "$MSG" >&2
    exit 1
fi

# Verify Python modules
python3 -c "import webview, cryptography" >/dev/null 2>&1
if [ $? -ne 0 ]; then
    # Try auto-installing via pip if pip is available
    python3 -m pip install pywebview cryptography >/dev/null 2>&1
    python3 -c "import webview, cryptography" >/dev/null 2>&1
fi

python3 -c "import webview, cryptography" >/dev/null 2>&1
if [ $? -ne 0 ]; then
    MSG="NotesAPP requires 'python3-webview' and 'python3-cryptography'.\n\nPlease run in terminal:\nsudo apt update && sudo apt install -y python3-webview python3-cryptography"
    if command -v zenity >/dev/null 2>&1; then
        zenity --error --text="$MSG" --title="NotesAPP Dependency Required"
    elif command -v kdialog >/dev/null 2>&1; then
        kdialog --error "$MSG" --title="NotesAPP Dependency Required"
    elif command -v xmessage >/dev/null 2>&1; then
        xmessage -center "$MSG"
    fi
    echo "$MSG" >&2
    exit 1
fi

exec python3 /usr/share/notesapp/main.py "$@"
