Repository navigation
Expand file tree
/
Copy pathflatpak-shims
More file actions
executable file
·128 lines (111 loc) · 3.17 KB
/
Copy pathflatpak-shims
File metadata and controls
executable file
·128 lines (111 loc) · 3.17 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
#!/bin/sh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2024 Jeremy Brubaker <jbru362@gmail.com>
#
# abstract: generate shims to run flatpaks
#
# Based on https://github.com/ronoaldo/tools/blob/master/bin/flatpak-gen-shims
#
# Defaults {{{1
VERBOSE_DFLT=
SHIMDIR_DFLT=$HOME/bin/.flatpak
MAPFILE_DFLT=${XDG_CONFIG_HOME:-~/.config}/flatpak-shims/mapping.conf
# Documentation {{{1
#
VERSION='1.0'
PROGNAME=$(basename $0)
print_help() {
cat <<EOF
Usage: $PROGNAME [OPTION]
Generate shims to run flatpaks
-d DIRECTORY Place shims in DIRECTORY (Default = $SHIMDIR_DFLT)
NOTE: all files in this directory are deleted so
it should contain *only* flatpak shims
-f FILE Alternate appid to name mapping file
(Default = $MAPFILE_DFLT)
-v turn on verbose output
-V display version info and exit
-h display this help and exit
EOF
}
print_version() {
cat <<EOF
$PROGNAME $VERSION
Copyright (C) 2024 Orion Arts
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jeremy Brubaker.
EOF
}
# Process options {{{1
#
shimdir=$SHIMDIR_DFLT
verbose=$VERBOSE_DFLT
MAPFILE=$MAPFILE_DFLT
while getopts 'd:f:vVh' opt; do
case $opt in
d) shim_dir=$OPTARG ;;
f) MAPFILE=$OPTARG ;;
v) verbose=-v ;;
V) print_version; exit ;;
h|?) print_help; exit ;;
esac
done
shift $((OPTIND - 1))
OPTIND=1
[ -r "$MAPFILE" ] || printf 'Mapping file not found: %s\n' "$MAPFILE" >&2
# Functions {{{1
#
mkshim() {
[ $# = "2" ] || return
appid=$1
shim=$2
cat > "$shim" <<EOF
#!/bin/sh
#
# flatpak shim managed by $PROGNAME
#
app=$appid
if [ \$# -eq 0 ]; then
# Run as a GUI application
nohup flatpak run "$appid" >/dev/null 2>&1 &
else
# Run as a CLI application
flatpak run "$appid" "\$@"
fi
EOF
chmod +x "$shim"
}
# Main {{{1
#
mkdir -p "$shimdir"
# Removing all shims and recreating is the easy
# way to keep them from being stale
rm -f "$shimdir"/*
for appid in $(flatpak list --app --columns=application); do
cmd=
[ -r "$MAPFILE" ] &&
cmd=$(grep "^$appid:" < "$MAPFILE" | cut -d: -f2)
[ -z "$cmd" ] &&
cmd=$(flatpak list --app |
grep "$appid" |
cut -f1 |
tr [:upper:] [:lower:] |
sed -e 's/ */-/')
[ -n "$verbose" ] && printf 'Creating shim for %s => %s\n' "$appid" "$cmd"
mkshim "$appid" "$shimdir/$cmd"
done