wok view sdft/stuff/sdft @ rev 18408

open-vm-tools: add wget and cacerts as deps
author Nathan Neulinger <nneul@neulinger.org>
date Sat Sep 19 14:21:42 2015 +0000 (2015-09-19)
parents
children c7196efa3c5d
line source
1 #!/bin/sh
2 # sdft - SliTaz Desktop Files Tools
3 # - tools for edit and pretty print .desktop files for SliTaz GNU/Linux
4 # Aleksej Bobylev <al.bobylev@gmail.com>, 2014
6 VERSION="040717"
8 ### functions ###
9 usage() {
10 cat << "EOT"
11 sdft - SliTaz Desktop Files Tools, v. $VERSION
12 Tools for edit and pretty print .desktop files for SliTaz GNU/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 -i In-place edit (replace original)
29 Examples:
30 sdft $src/my.desktop -a "Name[en_GB]=Desktop"
31 sdft $src/my.desktop -r "Name[en_GB]"
32 sdft $src/my.desktop -s "Categories=Utility;Calculator;"
33 sdft $src/my.desktop -r "X-GNOME-.*"
34 sdft $src/my.desktop -a "Name[en_GB]=Desktop" -g -o
36 EOT
37 }
38 extract() {
39 local EX=${1//[/\[}; EX=${EX//]/\]}
40 grep -e "^$EX=" $WORKING/section
41 sed -i "/^$EX=/d" $WORKING/section
42 }
43 extract_no_repeat() {
44 local IT_NAME="$1" IT_CONTENT
45 IT_CONTENT=$(extract "$IT_NAME" | sed "s|$IT_NAME=\(.*\)|\1|")
46 [ "x$IT_CONTENT" != x ] && echo "$IT_NAME=$IT_CONTENT"
47 extract "$IT_NAME[.*]" | sort | sed -n "/$IT_NAME\[.*\]=$IT_CONTENT$/!p"
48 }
49 semicolon() {
50 sed -e 's|.*|&;|' -e 's|;;|;|g'
51 }
52 ### /functions ###
56 case "$1" in
57 -h|--help) usage; exit 0 ;;
58 -v|-V|--version) echo "sdft v. $VERSION"; exit 0 ;;
59 esac
61 # working dir
62 WORKING=$(mktemp -d)
63 # original .desktop file to process it
64 ORIGINAL="$WORKING/original.desktop"
65 DESKTOP="$1"; cp "$DESKTOP" $ORIGINAL
67 SECTION="Desktop Entry"
68 if ! grep -qF "[$SECTION]" "$ORIGINAL"; then
69 echo "Seems $1 is not a Desktop file. Abort" >&2
70 exit 1
71 fi
73 # extract section content
74 sed -n "/^\[$SECTION\]$/,/^\[.*\]$/{/^\[/!p}" $ORIGINAL > $WORKING/section
76 # rest of the file
77 sed "/^\[$SECTION\]$/,/^\[.*\]$/{/^[^\[]/d}" $ORIGINAL | sed "/^\[$SECTION\]$/d" > $WORKING/rest
79 shift
80 while [ "x$1" != "x" ]; do
81 case "$1" in
82 -a) shift; echo "$1" >> $WORKING/section; shift ;;
83 -r) shift; extract "$1" > /dev/null; shift ;;
84 -s) shift; extract "${1%%=*}" > /dev/null; echo "$1" >> $WORKING/section; shift ;;
85 -g) shift; extract_no_repeat 'GenericName' > /dev/null ;;
86 -x) shift; extract 'X-.*' > /dev/null ;;
87 -t) shift; extract 'Terminal' > /dev/null ;;
88 -tf) shift; sed -i '/^Terminal=false$/d' $WORKING/section ;;
89 -te) shift; extract 'TryExec' > /dev/null ;;
90 -o) shift; REMOVE_OTHER="yes" ;;
91 -i) shift; IN_PLACE="yes" ;;
92 *) echo "Unknown command '$1'" >&2; shift ;;
93 esac
94 done
96 {
97 echo "[$SECTION]"
98 extract 'Encoding' > /dev/null
99 extract 'Version' > /dev/null
100 extract 'Type'
101 extract_no_repeat 'Name'
102 extract_no_repeat 'GenericName'
103 extract_no_repeat 'Comment'
104 extract 'Terminal'
105 extract 'StartupNotify'
106 extract 'TryExec'
107 extract 'Exec'
108 extract 'Icon'; extract 'Icon[.*]' > /dev/null
109 extract 'Categories' | sed 's|Application;||' | semicolon
110 extract 'NoDisplay'
111 extract 'MimeType' | semicolon
113 cat $WORKING/section | sort | sed -n '/^$/!p'
114 [ "x$REMOVE_OTHER" != "xyes" ] && cat $WORKING/rest | sed -n '/^$/!p'
115 } > $WORKING/new
117 if [ "x$IN_PLACE" == "xyes" ]; then
118 cp -f $WORKING/new "$DESKTOP"
119 else
120 cat $WORKING/new
121 fi
123 # clean
124 rm -rf $WORKING