wok diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/sdft/stuff/sdft Tue Jan 27 16:55:03 2015 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +#!/bin/sh 1.5 +# sdft - SliTaz Desktop Files Tools 1.6 +# - tools for edit and pretty print .desktop files for SliTaz GNU/Linux 1.7 +# Aleksej Bobylev <al.bobylev@gmail.com>, 2014 1.8 + 1.9 +VERSION="040717" 1.10 + 1.11 +### functions ### 1.12 +usage() { 1.13 + cat << "EOT" 1.14 +sdft - SliTaz Desktop Files Tools, v. $VERSION 1.15 +Tools for edit and pretty print .desktop files for SliTaz GNU/Linux 1.16 + 1.17 +Usage: 1.18 +sdft /path/to/file.desktop [COMMAND ...] 1.19 + 1.20 +Commands: 1.21 +-a "LINE" Add a LINE to .desktop file 1.22 +-r "LINE" Remove all lines with LINE 1.23 +-s "LINE" Substitute existing LINE (like '-r' then '-a') 1.24 +-g Remove GenericName lines (who uses it?) 1.25 +-x Remove X- lines 1.26 +-t Remove Terminal line 1.27 +-tf Remove Terminal=false line 1.28 +-te Remove TryExec line 1.29 +-o Remove sections other than '[Desktop Entry]' 1.30 +-i In-place edit (replace original) 1.31 + 1.32 +Examples: 1.33 +sdft $src/my.desktop -a "Name[en_GB]=Desktop" 1.34 +sdft $src/my.desktop -r "Name[en_GB]" 1.35 +sdft $src/my.desktop -s "Categories=Utility;Calculator;" 1.36 +sdft $src/my.desktop -r "X-GNOME-.*" 1.37 +sdft $src/my.desktop -a "Name[en_GB]=Desktop" -g -o 1.38 + 1.39 +EOT 1.40 +} 1.41 +extract() { 1.42 + local EX=${1//[/\[}; EX=${EX//]/\]} 1.43 + grep -e "^$EX=" $WORKING/section 1.44 + sed -i "/^$EX=/d" $WORKING/section 1.45 +} 1.46 +extract_no_repeat() { 1.47 + local IT_NAME="$1" IT_CONTENT 1.48 + IT_CONTENT=$(extract "$IT_NAME" | sed "s|$IT_NAME=\(.*\)|\1|") 1.49 + [ "x$IT_CONTENT" != x ] && echo "$IT_NAME=$IT_CONTENT" 1.50 + extract "$IT_NAME[.*]" | sort | sed -n "/$IT_NAME\[.*\]=$IT_CONTENT$/!p" 1.51 +} 1.52 +semicolon() { 1.53 + sed -e 's|.*|&;|' -e 's|;;|;|g' 1.54 +} 1.55 +### /functions ### 1.56 + 1.57 + 1.58 + 1.59 +case "$1" in 1.60 + -h|--help) usage; exit 0 ;; 1.61 + -v|-V|--version) echo "sdft v. $VERSION"; exit 0 ;; 1.62 +esac 1.63 + 1.64 +# working dir 1.65 +WORKING=$(mktemp -d) 1.66 +# original .desktop file to process it 1.67 +ORIGINAL="$WORKING/original.desktop" 1.68 +DESKTOP="$1"; cp "$DESKTOP" $ORIGINAL 1.69 + 1.70 +SECTION="Desktop Entry" 1.71 +if ! grep -qF "[$SECTION]" "$ORIGINAL"; then 1.72 + echo "Seems $1 is not a Desktop file. Abort" >&2 1.73 + exit 1 1.74 +fi 1.75 + 1.76 +# extract section content 1.77 +sed -n "/^\[$SECTION\]$/,/^\[.*\]$/{/^\[/!p}" $ORIGINAL > $WORKING/section 1.78 + 1.79 +# rest of the file 1.80 +sed "/^\[$SECTION\]$/,/^\[.*\]$/{/^[^\[]/d}" $ORIGINAL | sed "/^\[$SECTION\]$/d" > $WORKING/rest 1.81 + 1.82 +shift 1.83 +while [ "x$1" != "x" ]; do 1.84 + case "$1" in 1.85 + -a) shift; echo "$1" >> $WORKING/section; shift ;; 1.86 + -r) shift; extract "$1" > /dev/null; shift ;; 1.87 + -s) shift; extract "${1%%=*}" > /dev/null; echo "$1" >> $WORKING/section; shift ;; 1.88 + -g) shift; extract_no_repeat 'GenericName' > /dev/null ;; 1.89 + -x) shift; extract 'X-.*' > /dev/null ;; 1.90 + -t) shift; extract 'Terminal' > /dev/null ;; 1.91 + -tf) shift; sed -i '/^Terminal=false$/d' $WORKING/section ;; 1.92 + -te) shift; extract 'TryExec' > /dev/null ;; 1.93 + -o) shift; REMOVE_OTHER="yes" ;; 1.94 + -i) shift; IN_PLACE="yes" ;; 1.95 + *) echo "Unknown command '$1'" >&2; shift ;; 1.96 + esac 1.97 +done 1.98 + 1.99 +{ 1.100 + echo "[$SECTION]" 1.101 + extract 'Encoding' > /dev/null 1.102 + extract 'Version' > /dev/null 1.103 + extract 'Type' 1.104 + extract_no_repeat 'Name' 1.105 + extract_no_repeat 'GenericName' 1.106 + extract_no_repeat 'Comment' 1.107 + extract 'Terminal' 1.108 + extract 'StartupNotify' 1.109 + extract 'TryExec' 1.110 + extract 'Exec' 1.111 + extract 'Icon'; extract 'Icon[.*]' > /dev/null 1.112 + extract 'Categories' | sed 's|Application;||' | semicolon 1.113 + extract 'NoDisplay' 1.114 + extract 'MimeType' | semicolon 1.115 + 1.116 + cat $WORKING/section | sort | sed -n '/^$/!p' 1.117 + [ "x$REMOVE_OTHER" != "xyes" ] && cat $WORKING/rest | sed -n '/^$/!p' 1.118 +} > $WORKING/new 1.119 + 1.120 +if [ "x$IN_PLACE" == "xyes" ]; then 1.121 + cp -f $WORKING/new "$DESKTOP" 1.122 +else 1.123 + cat $WORKING/new 1.124 +fi 1.125 + 1.126 +# clean 1.127 +rm -rf $WORKING