#!/bin/sh
# The installed Daywake executable owns the MCP protocol and permission checks.
set -eu

if [ "$#" -ne 0 ]; then
    printf '%s\n' 'Daywake MCP launcher accepts no arguments.' >&2
    exit 64
fi

verify_app() {
    app_path="$1"
    property_list="$app_path/Contents/Info.plist"
    if ! bundle_id=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$property_list" 2>/dev/null) ||
       ! bundle_executable=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' "$property_list" 2>/dev/null) ||
       ! app_version=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$property_list" 2>/dev/null); then
        printf 'Daywake Info.plist is missing or damaged: %s\n' "$property_list" >&2
        printf '%s\n' 'Reinstall Daywake for Mac 4.0.1 or later. No application was started.' >&2
        exit 69
    fi
    if [ "$bundle_id" != 'app.riji.mac' ] || [ "$bundle_executable" != 'Riji' ]; then
        printf 'This is not a supported Daywake for Mac application: %s\n' "$app_path" >&2
        printf '%s\n' 'Choose the installed Daywake app with bundle ID app.riji.mac and executable Riji.' >&2
        exit 69
    fi
    if LC_ALL=C /usr/bin/awk -v version="$app_version" 'BEGIN {
        if (version !~ /^[0-9]+\.[0-9]+(\.[0-9]+)?$/) exit 2
        split(version, parts, ".")
        if (parts[1] + 0 > 4 ||
            (parts[1] + 0 == 4 && parts[2] + 0 > 0) ||
            (parts[1] + 0 == 4 && parts[2] + 0 == 0 && parts[3] + 0 >= 1)) exit 0
        exit 1
    }'; then
        return 0
    else
        version_status="$?"
        if [ "$version_status" -eq 1 ]; then
            printf 'Daywake for Mac 4.0.1 or later is required; found %s at %s.\n' "$app_version" "$app_path" >&2
            printf '%s\n' 'Please update Daywake, then reopen it before connecting. No application was started.' >&2
        else
            printf 'Daywake Info.plist has an invalid application version: %s\n' "$property_list" >&2
            printf '%s\n' 'Reinstall Daywake for Mac 4.0.1 or later. No application was started.' >&2
        fi
        exit 69
    fi
}

if [ "${DAYWAKE_APP_PATH+x}" = x ]; then
    case "$DAYWAKE_APP_PATH" in
        /*) ;;
        *)
            printf '%s\n' 'DAYWAKE_APP_PATH must be an absolute path to the installed Daywake .app.' >&2
            exit 64
            ;;
    esac
    executable="$DAYWAKE_APP_PATH/Contents/MacOS/Riji"
    if [ ! -f "$executable" ] || [ ! -x "$executable" ]; then
        printf 'Daywake is unavailable at DAYWAKE_APP_PATH: %s\n' "$DAYWAKE_APP_PATH" >&2
        printf '%s\n' 'Install Daywake for Mac, or correct DAYWAKE_APP_PATH. The launcher does not open the app or change its settings.' >&2
        exit 69
    fi
    verify_app "$DAYWAKE_APP_PATH"
    exec "$executable" --mcp
fi

for app in "/Applications/日迹 Daywake.app" "${HOME:-}/Applications/日迹 Daywake.app" "/Applications/日迹.app" "${HOME:-}/Applications/日迹.app"; do
    executable="$app/Contents/MacOS/Riji"
    if [ -f "$executable" ] && [ -x "$executable" ]; then
        verify_app "$app"
        exec "$executable" --mcp
    fi
done

printf '%s\n' 'Daywake for Mac was not found in /Applications or ~/Applications.' >&2
printf '%s\n' 'Install 日迹 Daywake.app, or set DAYWAKE_APP_PATH to its absolute .app path in your MCP configuration.' >&2
printf '%s\n' 'Open Daywake yourself before using to-do tools; choose the AI permissions in Settings → To-dos.' >&2
exit 69
