wok view sdft/stuff/sdft @ rev 25669

Up lapack (3.12.0), less (633), libarchive (3.7.2), liblouis (3.28.0), libmicrohttpd (1.0.1), libpng (1.6.43), libssh (0.10.6), libtasn1 (4.19.0), libtirpc (1.3.4), libvpx (1.14.0), libwebp (1.3.2), logrotate (3.21.0), lua (5.4.6)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Feb 25 16:11:20 2024 +0000 (4 months ago)
parents f226094b72d6
children
line source
1 #!/bin/sh
2 # sdft - SliTaz Desktop Files Tools
3 # - tools for editing and pretty printing .desktop files for SliTaz Linux
4 # Aleksej Bobylev <al.bobylev@gmail.com>, 2014-2017
6 VERSION="170314"
8 ### functions ###
9 usage() {
10 cat <<EOT
11 sdft - SliTaz Desktop Files Tools, v. $VERSION
12 Tools for editing and pretty printing .desktop files for SliTaz Linux
14 Usage:
15 sdft /path/to/file.desktop [COMMAND ...]
17 Commands:
18 -a "LINE" Add a LINE to .desktop file
19 -r "LINE" Remove all lines with LINE
20 -s "LINE" Substitute existing LINE (like '-r' then '-a')
21 -g Remove GenericName lines (who uses it?)
22 -x Remove X- lines
23 -t Remove Terminal line
24 -tf Remove Terminal=false line
25 -te Remove TryExec line
26 -o Remove sections other than '[Desktop Entry]'
27 -k "LIST" Keep only specified locales and remove the rest
28 -i In-place edit (replace original)
30 Examples:
31 sdft \$src/my.desktop -a "Name[en_GB]=Desktop"
32 sdft \$src/my.desktop -r "Name[en_GB]"
33 sdft \$src/my.desktop -s "Categories=Utility;Calculator;"
34 sdft \$src/my.desktop -r "X-GNOME-.*"
35 sdft \$src/my.desktop -a "Name[en_GB]=Desktop" -g -o
36 sdft \$src/my.desktop -k "en pt ru" # keeps en en_GB en_US pt pt_BR ru ru_UA...
38 EOT
39 }
41 extract() {
42 local EX="${1//[/\[}"; EX="${EX//]/\]}"
43 grep -e "^$EX=" $WORKING/section
44 sed -i "/^$EX=/d" $WORKING/section
45 }
47 extract_no_repeat() {
48 local IT_NAME="$1" IT_CONTENT
49 IT_CONTENT=$(extract "$IT_NAME" | sed "s|$IT_NAME=\(.*\)|\1|")
50 [ -n "$IT_CONTENT" ] && echo "$IT_NAME=$IT_CONTENT"
51 extract "$IT_NAME[.*]" | sort #| sed -n "/$IT_NAME\[.*\]=$IT_CONTENT$/!p"
52 }
54 semicolon() {
55 sed -e 's|.*|&;|' -e 's|;;|;|g'
56 }
58 ### /functions ###
62 case "$1" in
63 ''|-h|--help) usage; exit 0 ;;
64 -v|-V|--version) echo "sdft v. $VERSION"; exit 0 ;;
65 esac
67 [ ! -s "$1" ] && exit 1
70 # working dir
71 WORKING=$(mktemp -d)
72 # original .desktop file to process it
73 ORIGINAL="$WORKING/original.desktop"
74 DESKTOP="$1"; cp "$DESKTOP" $ORIGINAL
76 SECTION="Desktop Entry"
77 if ! grep -qF "[$SECTION]" "$ORIGINAL"; then
78 echo "Seems $1 is not a Desktop file. Abort" >&2
79 exit 1
80 fi
82 # extract section content
83 sed -n "/^\[$SECTION\]$/,/^\[.*\]$/{/^\[/!p}" $ORIGINAL > $WORKING/section
85 # rest of the file
86 sed "/^\[$SECTION\]$/,/^\[.*\]$/{/^[^\[]/d}" $ORIGINAL | sed "/^\[$SECTION\]$/d" > $WORKING/rest
88 shift
89 while [ -n "$1" ]; do
90 case "$1" in
91 -a) shift; echo "$1" >> $WORKING/section; shift ;;
92 -r) shift; extract "$1" >/dev/null; shift ;;
93 -s) shift; extract "${1%%=*}" >/dev/null; echo "$1" >> $WORKING/section; shift ;;
94 -g) shift; extract_no_repeat 'GenericName' >/dev/null ;;
95 -x) shift; extract 'X-.*' >/dev/null ;;
96 -t) shift; extract 'Terminal' >/dev/null ;;
97 -te) shift; extract 'TryExec' >/dev/null ;;
98 -tf) shift; sed -i '/^Terminal=false$/d' $WORKING/section ;;
99 -o) shift; REMOVE_OTHER='yes' ;;
100 -k) shift; klocales="$1"; shift ;;
101 -i) shift; IN_PLACE='yes' ;;
102 *) echo "Unknown command '$1'" >&2; shift ;;
103 esac
104 done
106 {
107 echo "[$SECTION]"
108 extract 'Encoding' >/dev/null
109 extract 'Version' >/dev/null
110 extract 'Type'
111 extract_no_repeat 'Name'
112 extract_no_repeat 'GenericName'
113 extract_no_repeat 'Comment'
114 extract 'Terminal'
115 extract 'StartupNotify'
116 extract 'TryExec'
117 extract 'Exec'
118 extract 'Icon'
119 extract 'Icon[.*]' >/dev/null
120 extract 'Categories' | sed 's|Application;||' | semicolon
121 extract 'NoDisplay'
122 extract 'MimeType' | semicolon
124 sort < $WORKING/section | sed -n '/^$/!p'
125 [ "$REMOVE_OTHER" != 'yes' ] && sed -n '/^$/!p' < $WORKING/rest
126 } > $WORKING/new
129 if [ -n "$klocales" ]; then
130 # Existing locales from Name, GenericName and Comment
131 elocales=" $(sed -n 's%\(Name\|Comment\)\[\([a-zA-Z@_]*\)\].*%\2%p' $WORKING/new | sort -u | tr '\n' ' ') "
132 for klocale in $klocales; do
133 elocales=${elocales//$klocale}
134 done
135 for elocale in $elocales; do
136 sed -i "/\[$elocale\]/d" $WORKING/new
137 done
138 fi
141 if [ "$IN_PLACE" == 'yes' ]; then
142 cp -f $WORKING/new "$DESKTOP"
143 else
144 cat $WORKING/new
145 fi
147 # clean
148 rm -rf $WORKING