wok annotate sdft/stuff/sdft @ rev 17543

Up busybox (1.23.1)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Jan 27 16:55:03 2015 +0100 (2015-01-27)
parents
children c7196efa3c5d
rev   line source
al@16921 1 #!/bin/sh
al@16921 2 # sdft - SliTaz Desktop Files Tools
al@16921 3 # - tools for edit and pretty print .desktop files for SliTaz GNU/Linux
al@16921 4 # Aleksej Bobylev <al.bobylev@gmail.com>, 2014
al@16921 5
al@16921 6 VERSION="040717"
al@16921 7
al@16921 8 ### functions ###
al@16921 9 usage() {
al@16921 10 cat << "EOT"
al@16921 11 sdft - SliTaz Desktop Files Tools, v. $VERSION
al@16921 12 Tools for edit and pretty print .desktop files for SliTaz GNU/Linux
al@16921 13
al@16921 14 Usage:
al@16921 15 sdft /path/to/file.desktop [COMMAND ...]
al@16921 16
al@16921 17 Commands:
al@16921 18 -a "LINE" Add a LINE to .desktop file
al@16921 19 -r "LINE" Remove all lines with LINE
al@16921 20 -s "LINE" Substitute existing LINE (like '-r' then '-a')
al@16921 21 -g Remove GenericName lines (who uses it?)
al@16921 22 -x Remove X- lines
al@16921 23 -t Remove Terminal line
al@16921 24 -tf Remove Terminal=false line
al@16921 25 -te Remove TryExec line
al@16921 26 -o Remove sections other than '[Desktop Entry]'
al@16921 27 -i In-place edit (replace original)
al@16921 28
al@16921 29 Examples:
al@16921 30 sdft $src/my.desktop -a "Name[en_GB]=Desktop"
al@16921 31 sdft $src/my.desktop -r "Name[en_GB]"
al@16921 32 sdft $src/my.desktop -s "Categories=Utility;Calculator;"
al@16921 33 sdft $src/my.desktop -r "X-GNOME-.*"
al@16921 34 sdft $src/my.desktop -a "Name[en_GB]=Desktop" -g -o
al@16921 35
al@16921 36 EOT
al@16921 37 }
al@16921 38 extract() {
al@16921 39 local EX=${1//[/\[}; EX=${EX//]/\]}
al@16921 40 grep -e "^$EX=" $WORKING/section
al@16921 41 sed -i "/^$EX=/d" $WORKING/section
al@16921 42 }
al@16921 43 extract_no_repeat() {
al@16921 44 local IT_NAME="$1" IT_CONTENT
al@16921 45 IT_CONTENT=$(extract "$IT_NAME" | sed "s|$IT_NAME=\(.*\)|\1|")
al@16921 46 [ "x$IT_CONTENT" != x ] && echo "$IT_NAME=$IT_CONTENT"
al@16921 47 extract "$IT_NAME[.*]" | sort | sed -n "/$IT_NAME\[.*\]=$IT_CONTENT$/!p"
al@16921 48 }
al@16921 49 semicolon() {
al@16921 50 sed -e 's|.*|&;|' -e 's|;;|;|g'
al@16921 51 }
al@16921 52 ### /functions ###
al@16921 53
al@16921 54
al@16921 55
al@16921 56 case "$1" in
al@16921 57 -h|--help) usage; exit 0 ;;
al@16921 58 -v|-V|--version) echo "sdft v. $VERSION"; exit 0 ;;
al@16921 59 esac
al@16921 60
al@16921 61 # working dir
al@16921 62 WORKING=$(mktemp -d)
al@16921 63 # original .desktop file to process it
al@16921 64 ORIGINAL="$WORKING/original.desktop"
al@16921 65 DESKTOP="$1"; cp "$DESKTOP" $ORIGINAL
al@16921 66
al@16921 67 SECTION="Desktop Entry"
al@16921 68 if ! grep -qF "[$SECTION]" "$ORIGINAL"; then
al@16921 69 echo "Seems $1 is not a Desktop file. Abort" >&2
al@16921 70 exit 1
al@16921 71 fi
al@16921 72
al@16921 73 # extract section content
al@16921 74 sed -n "/^\[$SECTION\]$/,/^\[.*\]$/{/^\[/!p}" $ORIGINAL > $WORKING/section
al@16921 75
al@16921 76 # rest of the file
al@16921 77 sed "/^\[$SECTION\]$/,/^\[.*\]$/{/^[^\[]/d}" $ORIGINAL | sed "/^\[$SECTION\]$/d" > $WORKING/rest
al@16921 78
al@16921 79 shift
al@16921 80 while [ "x$1" != "x" ]; do
al@16921 81 case "$1" in
al@16921 82 -a) shift; echo "$1" >> $WORKING/section; shift ;;
al@16921 83 -r) shift; extract "$1" > /dev/null; shift ;;
al@16921 84 -s) shift; extract "${1%%=*}" > /dev/null; echo "$1" >> $WORKING/section; shift ;;
al@16921 85 -g) shift; extract_no_repeat 'GenericName' > /dev/null ;;
al@16921 86 -x) shift; extract 'X-.*' > /dev/null ;;
al@16921 87 -t) shift; extract 'Terminal' > /dev/null ;;
al@16921 88 -tf) shift; sed -i '/^Terminal=false$/d' $WORKING/section ;;
al@16921 89 -te) shift; extract 'TryExec' > /dev/null ;;
al@16921 90 -o) shift; REMOVE_OTHER="yes" ;;
al@16921 91 -i) shift; IN_PLACE="yes" ;;
al@16921 92 *) echo "Unknown command '$1'" >&2; shift ;;
al@16921 93 esac
al@16921 94 done
al@16921 95
al@16921 96 {
al@16921 97 echo "[$SECTION]"
al@16921 98 extract 'Encoding' > /dev/null
al@16921 99 extract 'Version' > /dev/null
al@16921 100 extract 'Type'
al@16921 101 extract_no_repeat 'Name'
al@16921 102 extract_no_repeat 'GenericName'
al@16921 103 extract_no_repeat 'Comment'
al@16921 104 extract 'Terminal'
al@16921 105 extract 'StartupNotify'
al@16921 106 extract 'TryExec'
al@16921 107 extract 'Exec'
al@16921 108 extract 'Icon'; extract 'Icon[.*]' > /dev/null
al@16921 109 extract 'Categories' | sed 's|Application;||' | semicolon
al@16921 110 extract 'NoDisplay'
al@16921 111 extract 'MimeType' | semicolon
al@16921 112
al@16921 113 cat $WORKING/section | sort | sed -n '/^$/!p'
al@16921 114 [ "x$REMOVE_OTHER" != "xyes" ] && cat $WORKING/rest | sed -n '/^$/!p'
al@16921 115 } > $WORKING/new
al@16921 116
al@16921 117 if [ "x$IN_PLACE" == "xyes" ]; then
al@16921 118 cp -f $WORKING/new "$DESKTOP"
al@16921 119 else
al@16921 120 cat $WORKING/new
al@16921 121 fi
al@16921 122
al@16921 123 # clean
al@16921 124 rm -rf $WORKING