-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathdistrobox-systemd-service
More file actions
147 lines (127 loc) · 3.34 KB
/
distrobox-systemd-service
File metadata and controls
147 lines (127 loc) · 3.34 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
# https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.
# POSIX
# Optional env variables:
# DBX_CONTAINER_MANAGER
# DBX_VERBOSE
# Ensure we have our env variables correctly set
[ -z "${USER}" ] && USER="$(id -run)"
[ -z "${HOME}" ] && HOME="$(getent passwd "${USER}" | cut -d':' -f6)"
[ -z "${SHELL}" ] && SHELL="$(getent passwd "${USER}" | cut -d':' -f7)"
# Defaults
container_name="${1:-}"
action="${2:-start}"
verbose=0
version="1.8.2.4"
show_help()
{
cat << EOF
distrobox version: ${version}
Usage:
distrobox-systemd-service <container_name> [start|stop] [--verbose]
Description:
Manage background app registration and system tray integration for distrobox containers.
This enables visibility in GNOME Background Apps and system tray areas.
Options:
-h, --help Show this message
-v, --verbose Show more verbosity
-V, --version Show version
Examples:
distrobox-systemd-service my-container start
distrobox-systemd-service my-container stop
EOF
}
# Parse arguments
while [ $# -gt 0 ]; do
case "${1}" in
-h | --help)
show_help
exit 0
;;
-v | --verbose)
verbose=1
shift
;;
-V | --version)
printf "distrobox version: %s\n" "${version}"
exit 0
;;
*)
if [ -z "${container_name}" ]; then
container_name="${1}"
elif [ -z "${action}" ]; then
action="${1}"
fi
shift
;;
esac
done
if [ -z "${container_name}" ]; then
printf >&2 "Error: container name required\n"
show_help
exit 1
fi
# Register background app with freedesktop portal
register_background_app()
{
local container="${1}"
local app_id="distrobox.${container}"
local xdg_data_home="${XDG_DATA_HOME:-${HOME}/.local/share}"
local service_dir="${xdg_data_home}/dbus-1/services"
mkdir -p "${service_dir}"
# Create dbus service file for background app
cat > "${service_dir}/${app_id}.service" << EOF
[D-BUS Service]
Name=${app_id}
Exec=distrobox-enter ${container} -- true
EOF
if [ "${verbose}" -eq 1 ]; then
printf "Registered background app: %s\n" "${app_id}"
fi
}
unregister_background_app()
{
local container="${1}"
local app_id="distrobox.${container}"
local xdg_data_home="${XDG_DATA_HOME:-${HOME}/.local/share}"
local service_dir="${xdg_data_home}/dbus-1/services"
local service_file="${service_dir}/${app_id}.service"
if [ -f "${service_file}" ]; then
rm -f "${service_file}"
if [ "${verbose}" -eq 1 ]; then
printf "Unregistered background app: %s\n" "${app_id}"
fi
fi
}
# Main execution
case "${action}" in
start)
register_background_app "${container_name}"
;;
stop)
unregister_background_app "${container_name}"
;;
*)
printf >&2 "Invalid action: %s\n" "${action}"
show_help
exit 1
;;
esac
exit 0