From 790f3e8039b0445d0df1c2a51272cca125673211 Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Wed, 27 Mar 2024 18:58:31 +0100
Subject: [PATCH] Add systemd file generation to autostart-manage

---
 autostart-manage/before.sh            | 11 +++++++++++
 autostart-manage/lib/systemd_files.sh | 26 ++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 autostart-manage/before.sh
 create mode 100644 autostart-manage/lib/systemd_files.sh

diff --git a/autostart-manage/before.sh b/autostart-manage/before.sh
new file mode 100644
index 0000000..b2e3c84
--- /dev/null
+++ b/autostart-manage/before.sh
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+
+service_file="$HOME/.config/systemd/user/autostart@.service"
+if [ ! -f "$service_file" ]; then
+  _deploy_service_file "$service_file"
+fi
+
+target_file="$HOME/.config/systemd/user/autostart.target"
+if [ ! -f "$target_file" ]; then
+  _deploy_target_file "$target_file"
+fi
diff --git a/autostart-manage/lib/systemd_files.sh b/autostart-manage/lib/systemd_files.sh
new file mode 100644
index 0000000..d5eb682
--- /dev/null
+++ b/autostart-manage/lib/systemd_files.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+
+_deploy_service_file() {
+  cat <<EOF > "$1"
+[Unit]
+Description=Autostart several tools and services
+StartLimitIntervalSec=120
+StartLimitBurst=10
+
+[Service]
+KillMode=process
+ExecStart=/bin/sh -c ". \$HOME/.profile && autostart-manage exec '%i'"
+Restart=on-failure
+RestartSec=5s
+EOF
+}
+
+_deploy_target_file() {
+  cat <<EOF > "$1"
+[Unit]
+Description=Current graphical user session
+Documentation=man:systemd.special(7)
+RefuseManualStart=no
+StopWhenUnneeded=no
+EOF
+}