|
| 1 | +#!/bin/sh |
| 2 | +set -eu |
| 3 | + |
| 4 | +service_name="dev.ensan.inputmethod.azooKeyMac.ConverterServer" |
| 5 | +app_path="/Library/Input Methods/azooKeyMac.app" |
| 6 | +server_path="${app_path}/Contents/MacOS/ConverterServer" |
| 7 | +agent_dir="/Library/LaunchAgents" |
| 8 | +agent_path="${agent_dir}/${service_name}.plist" |
| 9 | + |
| 10 | +if [ ! -x "${server_path}" ]; then |
| 11 | + echo "ConverterServer not found: ${server_path}" >&2 |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +mkdir -p "${agent_dir}" |
| 16 | +cat > "${agent_path}" <<PLIST |
| 17 | +<?xml version="1.0" encoding="UTF-8"?> |
| 18 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 19 | +<plist version="1.0"> |
| 20 | +<dict> |
| 21 | + <key>Label</key> |
| 22 | + <string>${service_name}</string> |
| 23 | + <key>ProgramArguments</key> |
| 24 | + <array> |
| 25 | + <string>${server_path}</string> |
| 26 | + </array> |
| 27 | + <key>MachServices</key> |
| 28 | + <dict> |
| 29 | + <key>${service_name}</key> |
| 30 | + <true/> |
| 31 | + </dict> |
| 32 | + <key>KeepAlive</key> |
| 33 | + <true/> |
| 34 | + <key>RunAtLoad</key> |
| 35 | + <true/> |
| 36 | + <key>StandardOutPath</key> |
| 37 | + <string>/tmp/${service_name}.stdout.log</string> |
| 38 | + <key>StandardErrorPath</key> |
| 39 | + <string>/tmp/${service_name}.stderr.log</string> |
| 40 | +</dict> |
| 41 | +</plist> |
| 42 | +PLIST |
| 43 | + |
| 44 | +chmod 644 "${agent_path}" |
| 45 | +chown root:wheel "${agent_path}" |
| 46 | + |
| 47 | +console_user="$(stat -f %Su /dev/console)" |
| 48 | +if [ -z "${console_user}" ] || [ "${console_user}" = "root" ] || [ "${console_user}" = "_mbsetupuser" ]; then |
| 49 | + echo "No active console user; ${service_name} will start on next login." |
| 50 | + exit 0 |
| 51 | +fi |
| 52 | + |
| 53 | +console_uid="$(id -u "${console_user}")" |
| 54 | +gui_domain="gui/${console_uid}" |
| 55 | +legacy_agent_path="$(eval echo "~${console_user}")/Library/LaunchAgents/${service_name}.plist" |
| 56 | + |
| 57 | +launchctl bootout "${gui_domain}/${service_name}" >/dev/null 2>&1 || true |
| 58 | +if [ -f "${legacy_agent_path}" ]; then |
| 59 | + launchctl bootout "${gui_domain}" "${legacy_agent_path}" >/dev/null 2>&1 || true |
| 60 | + rm -f "${legacy_agent_path}" |
| 61 | +fi |
| 62 | +launchctl bootstrap "${gui_domain}" "${agent_path}" |
| 63 | +launchctl kickstart -k "${gui_domain}/${service_name}" |
| 64 | +launchctl print "${gui_domain}/${service_name}" >/dev/null |
| 65 | + |
| 66 | +echo "Installed and started ${service_name}" |
0 commit comments