#!/bin/bash

set -e

COMPOSITOR=""
COMPOSITOR_CONFIG=""
DMS_PATH="dms-greeter"
CACHE_DIR="/var/cache/dms-greeter"

show_help() {
    cat << EOF
dms-greeter - DankMaterialShell greeter launcher

Usage: dms-greeter --command COMPOSITOR [OPTIONS]

Required:
    --command COMPOSITOR    Compositor to use (niri, hyprland, or sway)

Options:
    -C, --config PATH       Custom compositor config file
    -p, --path PATH         DMS path (config name or absolute path)
                           (default: dms-greeter)
    --cache-dir PATH        Cache directory for greeter data
                           (default: /var/cache/dms-greeter)
    -h, --help             Show this help message

Examples:
    dms-greeter --command niri
    dms-greeter --command hyprland -C /etc/greetd/custom-hypr.conf
    dms-greeter --command sway -p /home/user/.config/quickshell/custom-dms
    dms-greeter --command niri --cache-dir /tmp/dmsgreeter
EOF
}

while [[ $# -gt 0 ]]; do
    case $1 in
        --command)
            COMPOSITOR="$2"
            shift 2
            ;;
        -C|--config)
            COMPOSITOR_CONFIG="$2"
            shift 2
            ;;
        -p|--path)
            DMS_PATH="$2"
            shift 2
            ;;
        --cache-dir)
            CACHE_DIR="$2"
            shift 2
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo "Unknown option: $1" >&2
            show_help
            exit 1
            ;;
    esac
done

if [[ -z "$COMPOSITOR" ]]; then
    echo "Error: --command COMPOSITOR is required" >&2
    show_help
    exit 1
fi

export XDG_SESSION_TYPE=wayland
export QT_QPA_PLATFORM=wayland
export QT_WAYLAND_DISABLE_WINDOWDECORATION=1
export EGL_PLATFORM=gbm
export DMS_RUN_GREETER=1
export DMS_GREET_CFG_DIR="$CACHE_DIR"

mkdir -p "$CACHE_DIR"

QS_CMD="qs"
if [[ "$DMS_PATH" == /* ]]; then
    QS_CMD="qs -p $DMS_PATH"
else
    QS_CMD="qs -c $DMS_PATH"
fi

case "$COMPOSITOR" in
    niri)
        if [[ -z "$COMPOSITOR_CONFIG" ]]; then
            TEMP_CONFIG=$(mktemp)
            cat > "$TEMP_CONFIG" << NIRI_EOF
hotkey-overlay {
    skip-at-startup
}

environment {
    DMS_RUN_GREETER "1"
}

spawn-at-startup "sh" "-c" "$QS_CMD; niri msg action quit --skip-confirmation"

debug {
  keep-max-bpc-unchanged
}

gestures {
   hot-corners {
     off
   }
}

layout {
  background-color "#000000"
}
NIRI_EOF
            COMPOSITOR_CONFIG="$TEMP_CONFIG"
        else
            TEMP_CONFIG=$(mktemp)
            cat "$COMPOSITOR_CONFIG" > "$TEMP_CONFIG"
            cat >> "$TEMP_CONFIG" << NIRI_EOF

spawn-at-startup "sh" "-c" "$QS_CMD; niri msg action quit --skip-confirmation"
NIRI_EOF
            COMPOSITOR_CONFIG="$TEMP_CONFIG"
        fi
        exec niri -c "$COMPOSITOR_CONFIG"
        ;;

    hyprland)
        if [[ -z "$COMPOSITOR_CONFIG" ]]; then
            TEMP_CONFIG=$(mktemp)
            cat > "$TEMP_CONFIG" << HYPRLAND_EOF
env = DMS_RUN_GREETER,1

exec = sh -c "$QS_CMD; hyprctl dispatch exit"
HYPRLAND_EOF
            COMPOSITOR_CONFIG="$TEMP_CONFIG"
        else
            TEMP_CONFIG=$(mktemp)
            cat "$COMPOSITOR_CONFIG" > "$TEMP_CONFIG"
            cat >> "$TEMP_CONFIG" << HYPRLAND_EOF

exec = sh -c "$QS_CMD; hyprctl dispatch exit"
HYPRLAND_EOF
            COMPOSITOR_CONFIG="$TEMP_CONFIG"
        fi
        exec Hyprland -c "$COMPOSITOR_CONFIG"
        ;;

    sway)
        if [[ -z "$COMPOSITOR_CONFIG" ]]; then
            TEMP_CONFIG=$(mktemp)
            cat > "$TEMP_CONFIG" << SWAY_EOF
exec "$QS_CMD; swaymsg exit"
SWAY_EOF
            COMPOSITOR_CONFIG="$TEMP_CONFIG"
        else
            TEMP_CONFIG=$(mktemp)
            cat "$COMPOSITOR_CONFIG" > "$TEMP_CONFIG"
            cat >> "$TEMP_CONFIG" << SWAY_EOF

exec "$QS_CMD; swaymsg exit"
SWAY_EOF
            COMPOSITOR_CONFIG="$TEMP_CONFIG"
        fi
        exec sway -c "$COMPOSITOR_CONFIG"
        ;;

    *)
        echo "Error: Unsupported compositor: $COMPOSITOR" >&2
        echo "Supported compositors: niri, hyprland, sway" >&2
        exit 1
        ;;
esac
