summaryrefslogtreecommitdiff
path: root/dot/local/bin/quickserve
blob: a07156d37124bda8adf3025f3d512acdaa8de577 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh
service_template()
{
    cat <<EOF
[Unit]
Description=${service_name}

[Service]
# Type=forking

Environment=DISPLAY=$DISPLAY
Environment=XAUTHORITY=$XAUTHORITY
Environment=SSH_AUTH_SOCK=%t/keyring/ssh

ExecStart=${exe} ${args}
Restart=always

[Install]
WantedBy=default.target
EOF
}

set -e
exe=$1
shift
args=$*

case "$exe" in
    sudo) exit 1;;
    /*) ;;
    */*) exe=$(realpath -e "$exe") ;;
    *) exe=$(which "$exe") ;;
esac

if [ "$(id -u)" = 0 ]; then
    service_dir=/etc/systemd/system
    user=
else
    service_dir=$HOME/.config/systemd/user
    user=y
fi

service_name=${exe##*/}
service_file=${service_dir}/${service_name}.service

[ -d "$service_dir" ] || mkdir -p "$service_dir"

systemctl="systemctl ${user:+--user}"

if [ -e "$service_file" ]; then
    "${EDITOR:-/usr/bin/vi}" "$service_file"
    $systemctl daemon-reload
    $systemctl status "$service_name"
else
    service_template > "$service_file"
    $systemctl daemon-reload
    $systemctl enable "$service_name"
    $systemctl start "$service_name"
fi