wok rev 19496

Up sdft (161116)
author Aleksej Bobylev <al.bobylev@gmail.com>
date Wed Nov 16 19:03:59 2016 +0200 (2016-11-16)
parents f2cf0de52bd3
children 1c0d98648375
files sdft/receipt sdft/stuff/sdft
line diff
     1.1 --- a/sdft/receipt	Tue Nov 15 15:25:39 2016 +0100
     1.2 +++ b/sdft/receipt	Wed Nov 16 19:03:59 2016 +0200
     1.3 @@ -1,7 +1,7 @@
     1.4  # SliTaz package receipt.
     1.5  
     1.6  PACKAGE="sdft"
     1.7 -VERSION="151108"
     1.8 +VERSION="161116"
     1.9  CATEGORY="development"
    1.10  SHORT_DESC="SliTaz Desktop Files Tools"
    1.11  MAINTAINER="al.bobylev@gmail.com"
     2.1 --- a/sdft/stuff/sdft	Tue Nov 15 15:25:39 2016 +0100
     2.2 +++ b/sdft/stuff/sdft	Wed Nov 16 19:03:59 2016 +0200
     2.3 @@ -1,15 +1,15 @@
     2.4  #!/bin/sh
     2.5  # sdft - SliTaz Desktop Files Tools
     2.6 -#      - tools for edit and pretty print .desktop files for SliTaz GNU/Linux
     2.7 -# Aleksej Bobylev <al.bobylev@gmail.com>, 2014-2015
     2.8 +#      - tools for editing and pretty printing .desktop files for SliTaz Linux
     2.9 +# Aleksej Bobylev <al.bobylev@gmail.com>, 2014-2016
    2.10  
    2.11 -VERSION="151108"
    2.12 +VERSION="161116"
    2.13  
    2.14  ### functions ###
    2.15  usage() {
    2.16 -	cat << "EOT"
    2.17 +	cat <<EOT
    2.18  sdft - SliTaz Desktop Files Tools, v. $VERSION
    2.19 -Tools for edit and pretty print .desktop files for SliTaz GNU/Linux
    2.20 +Tools for editing and pretty printing .desktop files for SliTaz Linux
    2.21  
    2.22  Usage:
    2.23  sdft /path/to/file.desktop [COMMAND ...]
    2.24 @@ -24,40 +24,49 @@
    2.25  -tf        Remove Terminal=false line
    2.26  -te        Remove TryExec line
    2.27  -o         Remove sections other than '[Desktop Entry]'
    2.28 +-k "LIST"  Keep only specified locales and remove the rest
    2.29  -i         In-place edit (replace original)
    2.30  
    2.31  Examples:
    2.32 -sdft $src/my.desktop -a "Name[en_GB]=Desktop"
    2.33 -sdft $src/my.desktop -r "Name[en_GB]"
    2.34 -sdft $src/my.desktop -s "Categories=Utility;Calculator;"
    2.35 -sdft $src/my.desktop -r "X-GNOME-.*"
    2.36 -sdft $src/my.desktop -a "Name[en_GB]=Desktop" -g -o
    2.37 +sdft \$src/my.desktop -a "Name[en_GB]=Desktop"
    2.38 +sdft \$src/my.desktop -r "Name[en_GB]"
    2.39 +sdft \$src/my.desktop -s "Categories=Utility;Calculator;"
    2.40 +sdft \$src/my.desktop -r "X-GNOME-.*"
    2.41 +sdft \$src/my.desktop -a "Name[en_GB]=Desktop" -g -o
    2.42 +sdft \$src/my.desktop -k "en pt ru"  # keeps en en_GB en_US pt pt_BR ru ru_UA...
    2.43  
    2.44  EOT
    2.45  }
    2.46 +
    2.47  extract() {
    2.48  	local EX=${1//[/\[}; EX=${EX//]/\]}
    2.49 -	grep -e "^$EX=" $WORKING/section
    2.50 +	grep -e "^$EX="   $WORKING/section
    2.51  	sed -i "/^$EX=/d" $WORKING/section
    2.52  }
    2.53 +
    2.54  extract_no_repeat() {
    2.55  	local IT_NAME="$1" IT_CONTENT
    2.56  	IT_CONTENT=$(extract "$IT_NAME" | sed "s|$IT_NAME=\(.*\)|\1|")
    2.57 -	[ "x$IT_CONTENT" != x ] && echo "$IT_NAME=$IT_CONTENT"
    2.58 +	[ -n "$IT_CONTENT" ] && echo "$IT_NAME=$IT_CONTENT"
    2.59  	extract "$IT_NAME[.*]" | sort #| sed -n "/$IT_NAME\[.*\]=$IT_CONTENT$/!p"
    2.60  }
    2.61 +
    2.62  semicolon() {
    2.63  	sed -e 's|.*|&;|' -e 's|;;|;|g'
    2.64  }
    2.65 +
    2.66  ### /functions ###
    2.67  
    2.68  
    2.69  
    2.70  case "$1" in
    2.71 -	-h|--help) usage; exit 0 ;;
    2.72 +	''|-h|--help)    usage;                   exit 0 ;;
    2.73  	-v|-V|--version) echo "sdft v. $VERSION"; exit 0 ;;
    2.74  esac
    2.75  
    2.76 +[ ! -s "$1" ] && exit 1
    2.77 +
    2.78 +
    2.79  # working dir
    2.80  WORKING=$(mktemp -d)
    2.81  # original .desktop file to process it
    2.82 @@ -77,26 +86,27 @@
    2.83  sed "/^\[$SECTION\]$/,/^\[.*\]$/{/^[^\[]/d}" $ORIGINAL | sed "/^\[$SECTION\]$/d" > $WORKING/rest
    2.84  
    2.85  shift
    2.86 -while [ "x$1" != "x" ]; do
    2.87 +while [ -n "$1" ]; do
    2.88  	case "$1" in
    2.89 -		-a) shift; echo "$1" >> $WORKING/section; shift ;;
    2.90 -		-r) shift; extract "$1" > /dev/null; shift ;;
    2.91 -		-s) shift; extract "${1%%=*}" > /dev/null; echo "$1" >> $WORKING/section; shift ;;
    2.92 -		-g) shift; extract_no_repeat 'GenericName' > /dev/null ;;
    2.93 -		-x) shift; extract 'X-.*' > /dev/null ;;
    2.94 -		-t) shift; extract 'Terminal' > /dev/null ;;
    2.95 -		-tf) shift; sed -i '/^Terminal=false$/d' $WORKING/section ;;
    2.96 -		-te) shift; extract 'TryExec' > /dev/null ;;
    2.97 -		-o) shift; REMOVE_OTHER="yes" ;;
    2.98 -		-i) shift; IN_PLACE="yes" ;;
    2.99 -		*) echo "Unknown command '$1'" >&2; shift ;;
   2.100 +		-a)  shift;                                             echo "$1" >> $WORKING/section; shift ;;
   2.101 +		-r)  shift; extract           "$1"          >/dev/null; shift ;;
   2.102 +		-s)  shift; extract           "${1%%=*}"    >/dev/null; echo "$1" >> $WORKING/section; shift ;;
   2.103 +		-g)  shift; extract_no_repeat 'GenericName' >/dev/null ;;
   2.104 +		-x)  shift; extract           'X-.*'        >/dev/null ;;
   2.105 +		-t)  shift; extract           'Terminal'    >/dev/null ;;
   2.106 +		-te) shift; extract           'TryExec'     >/dev/null ;;
   2.107 +		-tf) shift; sed -i            '/^Terminal=false$/d'                  $WORKING/section ;;
   2.108 +		-o)  shift; REMOVE_OTHER='yes' ;;
   2.109 +		-k)  shift; klocales="$1"; shift ;;
   2.110 +		-i)  shift; IN_PLACE='yes' ;;
   2.111 +		*)   echo "Unknown command '$1'" >&2; shift ;;
   2.112  	esac
   2.113  done
   2.114  
   2.115  {
   2.116  	echo "[$SECTION]"
   2.117 -	extract 'Encoding' > /dev/null
   2.118 -	extract 'Version' > /dev/null
   2.119 +	extract 'Encoding' >/dev/null
   2.120 +	extract 'Version'  >/dev/null
   2.121  	extract 'Type'
   2.122  	extract_no_repeat 'Name'
   2.123  	extract_no_repeat 'GenericName'
   2.124 @@ -105,16 +115,30 @@
   2.125  	extract 'StartupNotify'
   2.126  	extract 'TryExec'
   2.127  	extract 'Exec'
   2.128 -	extract 'Icon'; extract 'Icon[.*]' > /dev/null
   2.129 +	extract 'Icon'
   2.130 +	extract 'Icon[.*]' >/dev/null
   2.131  	extract 'Categories' | sed 's|Application;||' | semicolon
   2.132  	extract 'NoDisplay'
   2.133  	extract 'MimeType' | semicolon
   2.134  
   2.135  	cat $WORKING/section | sort | sed -n '/^$/!p'
   2.136 -	[ "x$REMOVE_OTHER" != "xyes" ] && cat $WORKING/rest | sed -n '/^$/!p'
   2.137 +	[ "$REMOVE_OTHER" != 'yes' ] && cat $WORKING/rest | sed -n '/^$/!p'
   2.138  } > $WORKING/new
   2.139  
   2.140 -if [ "x$IN_PLACE" == "xyes" ]; then
   2.141 +
   2.142 +if [ -n "$klocales" ]; then
   2.143 +	# Existing locales from Name, GenericName and Comment
   2.144 +	elocales=" $(sed -n 's%\(Name\|Comment\)\[\([a-zA-Z@_]*\)\].*%\2%p' $WORKING/new | sort -u | tr '\n' ' ') "
   2.145 +	for klocale in $klocales; do
   2.146 +		elocales=${elocales//$klocale}
   2.147 +	done
   2.148 +	for elocale in $elocales; do
   2.149 +		sed -i "/\[$elocale\]/d" $WORKING/new
   2.150 +	done
   2.151 +fi
   2.152 +
   2.153 +
   2.154 +if [ "$IN_PLACE" == 'yes' ]; then
   2.155  	cp -f $WORKING/new "$DESKTOP"
   2.156  else
   2.157  	cat $WORKING/new