Bash · 9833 bytes Raw Blame History
1 #!/bin/bash
2 # Install script for garfield-portal
3 #
4 # This script installs the garfield portal backend which allows
5 # applications to use garfield as the system file picker.
6 #
7 # Usage:
8 # ./install.sh - Install to system (requires sudo)
9 # ./install.sh --user - Install to user directory only
10 # ./install.sh --check - Check installation status
11
12 set -e
13
14 # Colors for output
15 RED='\033[0;31m'
16 GREEN='\033[0;32m'
17 YELLOW='\033[1;33m'
18 NC='\033[0m' # No Color
19
20 # Directories
21 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
22 BINARY_NAME="garfield-portal"
23 GARFIELD_BINARY="garfield"
24 PORTAL_FILE="garfield.portal"
25 SERVICE_FILE="garfield-portal.service"
26
27 # System paths
28 SYS_BIN_DIR="/usr/local/bin"
29 SYS_PORTAL_DIR="/usr/share/xdg-desktop-portal/portals"
30 SYS_PORTALS_CONF_DIR="/usr/share/xdg-desktop-portal"
31
32 # User paths
33 USER_BIN_DIR="$HOME/.local/bin"
34 USER_PORTAL_DIR="$HOME/.local/share/xdg-desktop-portal/portals"
35 USER_SERVICE_DIR="$HOME/.config/systemd/user"
36 USER_PORTALS_CONF_DIR="$HOME/.config/xdg-desktop-portal"
37
38 print_status() {
39 echo -e "${GREEN}[OK]${NC} $1"
40 }
41
42 print_warning() {
43 echo -e "${YELLOW}[WARN]${NC} $1"
44 }
45
46 print_error() {
47 echo -e "${RED}[ERROR]${NC} $1"
48 }
49
50 check_binary() {
51 local name="$1"
52 local binary_path="$SCRIPT_DIR/../target/release/$name"
53 if [[ ! -f "$binary_path" ]]; then
54 binary_path="$SCRIPT_DIR/target/release/$name"
55 fi
56 if [[ ! -f "$binary_path" ]]; then
57 # Try workspace root
58 binary_path="$(dirname "$SCRIPT_DIR")/target/release/$name"
59 fi
60 if [[ ! -f "$binary_path" ]]; then
61 print_error "Binary '$name' not found. Please run 'cargo build --release' first."
62 exit 1
63 fi
64 echo "$binary_path"
65 }
66
67 install_system() {
68 echo "Installing garfield-portal (system-wide)..."
69
70 local portal_binary garfield_binary
71 portal_binary=$(check_binary "$BINARY_NAME")
72 garfield_binary=$(check_binary "$GARFIELD_BINARY")
73
74 # Install portal daemon binary
75 sudo install -Dm755 "$portal_binary" "$SYS_BIN_DIR/$BINARY_NAME"
76 print_status "Installed binary to $SYS_BIN_DIR/$BINARY_NAME"
77
78 # Install main garfield binary (required for picker mode)
79 sudo install -Dm755 "$garfield_binary" "$SYS_BIN_DIR/$GARFIELD_BINARY"
80 print_status "Installed binary to $SYS_BIN_DIR/$GARFIELD_BINARY"
81
82 # Install portal file
83 sudo install -Dm644 "$SCRIPT_DIR/data/$PORTAL_FILE" "$SYS_PORTAL_DIR/$PORTAL_FILE"
84 print_status "Installed portal config to $SYS_PORTAL_DIR/$PORTAL_FILE"
85
86 # Install systemd service (user service, but to system location)
87 sudo install -Dm644 "$SCRIPT_DIR/data/$SERVICE_FILE" "/usr/lib/systemd/user/$SERVICE_FILE"
88 print_status "Installed systemd service to /usr/lib/systemd/user/$SERVICE_FILE"
89
90 # Install gar-portals.conf
91 sudo install -Dm644 "$SCRIPT_DIR/data/gar-portals.conf" "$SYS_PORTALS_CONF_DIR/gar-portals.conf"
92 print_status "Installed portal preferences to $SYS_PORTALS_CONF_DIR/gar-portals.conf"
93
94 echo ""
95 echo "Installation complete!"
96 echo ""
97 echo "To enable the portal service, run:"
98 echo " systemctl --user daemon-reload"
99 echo " systemctl --user enable --now garfield-portal"
100 echo ""
101 echo "To test, run a GTK app with:"
102 echo " GTK_USE_PORTAL=1 gedit"
103 }
104
105 install_user() {
106 echo "Installing garfield-portal (user only)..."
107
108 local portal_binary garfield_binary
109 portal_binary=$(check_binary "$BINARY_NAME")
110 garfield_binary=$(check_binary "$GARFIELD_BINARY")
111
112 # Create directories
113 mkdir -p "$USER_BIN_DIR"
114 mkdir -p "$USER_PORTAL_DIR"
115 mkdir -p "$USER_SERVICE_DIR"
116 mkdir -p "$USER_PORTALS_CONF_DIR"
117
118 # Install portal daemon binary
119 install -m755 "$portal_binary" "$USER_BIN_DIR/$BINARY_NAME"
120 print_status "Installed binary to $USER_BIN_DIR/$BINARY_NAME"
121
122 # Install main garfield binary (required for picker mode)
123 install -m755 "$garfield_binary" "$USER_BIN_DIR/$GARFIELD_BINARY"
124 print_status "Installed binary to $USER_BIN_DIR/$GARFIELD_BINARY"
125
126 # Install portal file
127 install -m644 "$SCRIPT_DIR/data/$PORTAL_FILE" "$USER_PORTAL_DIR/$PORTAL_FILE"
128 print_status "Installed portal config to $USER_PORTAL_DIR/$PORTAL_FILE"
129
130 # Install systemd service with modified path
131 sed "s|/usr/local/bin/garfield-portal|$USER_BIN_DIR/garfield-portal|g" \
132 "$SCRIPT_DIR/data/$SERVICE_FILE" > "$USER_SERVICE_DIR/$SERVICE_FILE"
133 print_status "Installed systemd service to $USER_SERVICE_DIR/$SERVICE_FILE"
134
135 # Install gar-portals.conf
136 install -m644 "$SCRIPT_DIR/data/gar-portals.conf" "$USER_PORTALS_CONF_DIR/gar-portals.conf"
137 print_status "Installed portal preferences to $USER_PORTALS_CONF_DIR/gar-portals.conf"
138
139 echo ""
140 echo "Installation complete!"
141 echo ""
142 echo "Make sure $USER_BIN_DIR is in your PATH."
143 echo ""
144 echo "To enable the portal service, run:"
145 echo " systemctl --user daemon-reload"
146 echo " systemctl --user enable --now garfield-portal"
147 echo ""
148 echo "To test, run a GTK app with:"
149 echo " GTK_USE_PORTAL=1 gedit"
150 }
151
152 uninstall_system() {
153 echo "Uninstalling garfield-portal (system-wide)..."
154
155 # Stop service first
156 systemctl --user stop garfield-portal 2>/dev/null || true
157 systemctl --user disable garfield-portal 2>/dev/null || true
158
159 sudo rm -f "$SYS_BIN_DIR/$BINARY_NAME"
160 sudo rm -f "$SYS_BIN_DIR/$GARFIELD_BINARY"
161 sudo rm -f "/usr/local/sbin/$GARFIELD_BINARY" # Clean up stray copy
162 sudo rm -f "$SYS_PORTAL_DIR/$PORTAL_FILE"
163 sudo rm -f "/usr/lib/systemd/user/$SERVICE_FILE"
164 sudo rm -f "$SYS_PORTALS_CONF_DIR/gar-portals.conf"
165
166 systemctl --user daemon-reload
167
168 print_status "Uninstalled garfield-portal"
169 }
170
171 uninstall_user() {
172 echo "Uninstalling garfield-portal (user only)..."
173
174 # Stop service first
175 systemctl --user stop garfield-portal 2>/dev/null || true
176 systemctl --user disable garfield-portal 2>/dev/null || true
177
178 rm -f "$USER_BIN_DIR/$BINARY_NAME"
179 rm -f "$USER_BIN_DIR/$GARFIELD_BINARY"
180 rm -f "$USER_PORTAL_DIR/$PORTAL_FILE"
181 rm -f "$USER_SERVICE_DIR/$SERVICE_FILE"
182 rm -f "$USER_PORTALS_CONF_DIR/gar-portals.conf"
183
184 systemctl --user daemon-reload
185
186 print_status "Uninstalled garfield-portal"
187 }
188
189 check_installation() {
190 echo "Checking garfield-portal installation..."
191 echo ""
192
193 # Check portal daemon binary
194 if command -v garfield-portal &> /dev/null; then
195 print_status "Portal binary found: $(command -v garfield-portal)"
196 elif [[ -f "$USER_BIN_DIR/$BINARY_NAME" ]]; then
197 print_warning "Portal binary found at $USER_BIN_DIR/$BINARY_NAME (not in PATH)"
198 elif [[ -f "$SYS_BIN_DIR/$BINARY_NAME" ]]; then
199 print_status "Portal binary found at $SYS_BIN_DIR/$BINARY_NAME"
200 else
201 print_error "Portal binary not found"
202 fi
203
204 # Check main garfield binary (required for picker mode)
205 local garfield_path
206 if [[ -f "$SYS_BIN_DIR/$GARFIELD_BINARY" ]]; then
207 garfield_path="$SYS_BIN_DIR/$GARFIELD_BINARY"
208 elif [[ -f "$USER_BIN_DIR/$GARFIELD_BINARY" ]]; then
209 garfield_path="$USER_BIN_DIR/$GARFIELD_BINARY"
210 elif command -v garfield &> /dev/null; then
211 garfield_path="$(command -v garfield)"
212 fi
213
214 if [[ -n "$garfield_path" ]]; then
215 # Check if garfield has picker mode support
216 if "$garfield_path" --help 2>&1 | grep -q "\-\-picker"; then
217 print_status "Garfield binary found with picker support: $garfield_path"
218 else
219 print_error "Garfield binary at $garfield_path does NOT have picker mode!"
220 print_error "The portal will not work. Please reinstall."
221 fi
222 else
223 print_error "Garfield binary not found (required for picker mode)"
224 fi
225
226 # Check portal config
227 if [[ -f "$SYS_PORTAL_DIR/$PORTAL_FILE" ]]; then
228 print_status "Portal config found: $SYS_PORTAL_DIR/$PORTAL_FILE"
229 elif [[ -f "$USER_PORTAL_DIR/$PORTAL_FILE" ]]; then
230 print_status "Portal config found: $USER_PORTAL_DIR/$PORTAL_FILE"
231 else
232 print_error "Portal config not found"
233 fi
234
235 # Check systemd service
236 if systemctl --user is-enabled garfield-portal &> /dev/null; then
237 print_status "Systemd service enabled"
238 if systemctl --user is-active garfield-portal &> /dev/null; then
239 print_status "Systemd service running"
240 else
241 print_warning "Systemd service not running"
242 fi
243 else
244 print_warning "Systemd service not enabled"
245 fi
246
247 # Check D-Bus name
248 if dbus-send --session --print-reply --dest=org.freedesktop.DBus \
249 /org/freedesktop/DBus org.freedesktop.DBus.NameHasOwner \
250 string:"org.freedesktop.impl.portal.desktop.garfield" 2>/dev/null | grep -q "true"; then
251 print_status "D-Bus service registered"
252 else
253 print_warning "D-Bus service not registered (portal may not be running)"
254 fi
255 }
256
257 show_help() {
258 echo "Usage: $0 [OPTION]"
259 echo ""
260 echo "Install garfield-portal as the system file picker."
261 echo ""
262 echo "Options:"
263 echo " --user Install to user directories only (no sudo required)"
264 echo " --uninstall Remove system installation"
265 echo " --uninstall-user Remove user installation"
266 echo " --check Check installation status"
267 echo " --help Show this help message"
268 echo ""
269 echo "Without options, installs system-wide (requires sudo)."
270 }
271
272 case "${1:-}" in
273 --user)
274 install_user
275 ;;
276 --uninstall)
277 uninstall_system
278 ;;
279 --uninstall-user)
280 uninstall_user
281 ;;
282 --check)
283 check_installation
284 ;;
285 --help|-h)
286 show_help
287 ;;
288 "")
289 install_system
290 ;;
291 *)
292 print_error "Unknown option: $1"
293 show_help
294 exit 1
295 ;;
296 esac