module main import os pub fn service_install(config_path string, quiet bool) ! { exe_path := os.executable() abs_config := os.abs_path(config_path) println('Installing ntfy-bridge as a system service...') println(' Config: ${abs_config}') $if windows { args := '--quiet --config "${abs_config}"' name := 'ntfy-bridge' cmd := 'sc create ${name} binPath= "${exe_path} ${args}" start= auto DisplayName= "ntfy-bridge Notification Hub"' r := os.execute(cmd) if r.exit_code != 0 { eprintln('sc create failed: ${r.output}') exit(1) } println(' Service created.') os.execute('sc failure ${name} reset= 86400 actions= restart/10000/restart/30000/restart/60000') os.execute('sc description ${name} "Homelab notification hub"') r2 := os.execute('sc start ${name}') if r2.exit_code != 0 { println(' Warning: sc start ${name} manually.') } println(' Service started. sc start|stop|query|delete ${name}') return } $if linux { // Detect init system is_systemd := os.exists('/run/systemd/system') || os.exists('/sys/fs/cgroup/systemd') is_openrc := os.exists('/etc/init.d') && (os.exists('/sbin/openrc') || os.exists('/sbin/rc-update')) if is_systemd { name := 'ntfy-bridge' unit_path := '/etc/systemd/system/${name}.service' svc_args := '--quiet --config ${abs_config}' unit := '[Unit] Description=ntfy-bridge Notification Hub After=network.target [Service] Type=simple ExecStart=${exe_path} ${svc_args} Restart=always RestartSec=10 StandardOutput=journal StandardError=journal SyslogIdentifier=ntfy-bridge [Install] WantedBy=multi-user.target ' println(' Writing ${unit_path}') os.write_file(unit_path, unit) or { return error('Failed to write ${unit_path} (sudo?).') } os.execute('systemctl daemon-reload') os.execute('systemctl enable ${name}') r := os.execute('systemctl start ${name}') println(' Service installed. systemctl status|stop|start ${name}') if r.exit_code != 0 { println(' Warning: start failed.') } return } if is_openrc { name := 'ntfy-bridge' init_path := '/etc/init.d/${name}' svc_args := '--quiet --config ${abs_config}' script := "#!/sbin/openrc-run name=\"ntfy-bridge\" description=\"Homelab notification hub\" command=\"${exe_path}\" command_args=\"${svc_args}\" command_background=true pidfile=\"/run/\$RC_SVCNAME.pid\" depend() { \tneed net \tafter firewall } " println(' Writing ${init_path}') os.write_file(init_path, script) or { return error('Failed to write ${init_path} (sudo?).') } os.execute('chmod +x ${init_path}') os.execute('rc-update add ${name} default') r := os.execute('rc-service ${name} start') println(' Service installed. rc-service ${name} status|stop|start') if r.exit_code != 0 { println(' Warning: start failed.') } return } return error('Unsupported init system (need systemd or openrc).') } return error('Unsupported OS.') } pub fn service_uninstall() ! { println('Uninstalling ntfy-bridge service...') $if windows { name := 'ntfy-bridge' os.execute('sc stop ${name}') r := os.execute('sc delete ${name}') if r.exit_code != 0 { return error('sc delete failed: ${r.output}') } println(' Service uninstalled.') return } $if linux { is_systemd := os.exists('/run/systemd/system') || os.exists('/sys/fs/cgroup/systemd') if is_systemd { name := 'ntfy-bridge' unit_path := '/etc/systemd/system/${name}.service' os.execute('systemctl stop ${name}') os.execute('systemctl disable ${name}') if os.exists(unit_path) { os.rm(unit_path) or { return error('Failed to remove ${unit_path} (sudo?).') } } os.execute('systemctl daemon-reload') println(' Service uninstalled.') return } if os.exists('/etc/init.d/ntfy-bridge') { os.execute('rc-service ntfy-bridge stop') os.execute('rc-update del ntfy-bridge') os.rm('/etc/init.d/ntfy-bridge') or { return error('Failed to remove /etc/init.d/ntfy-bridge (sudo?).') } println(' Service uninstalled.') return } return error('Service not found.') } return error('Unsupported OS.') } pub fn service_status() ! { $if windows { r := os.execute('sc query ntfy-bridge') if r.exit_code == 0 { println(r.output) } else { println('Not installed.') } return } $if linux { is_systemd := os.exists('/run/systemd/system') || os.exists('/sys/fs/cgroup/systemd') if is_systemd { r := os.execute('systemctl is-active ntfy-bridge') println('Status: ${r.output.trim_space()}') return } if os.exists('/etc/init.d/ntfy-bridge') { r := os.execute('rc-service ntfy-bridge status') println(r.output) return } println('Not installed.') return } println('Service status not supported on this OS.') }