#!/bin/bash # # stardics-dict-config.sh 0.1 # Copyright 2009, Nikita Melnichenko # License: GPL-2 (http://opensource.org/licenses/gpl-license.php) # # Utility for management of stardict dictionary groups. # Run without arguments to display usage information. # settings stardict_cfg_path="$HOME/.stardict/stardict.cfg" dict_config_xml_path="$HOME/.stardict/dict_config.xml" # print usage function usage () { echo "$0 COMMAND" echo "COMMAND is one of 'extract', 'merge', 'remove'" echo " extract: extract dictionary XML config from $stardict_cfg_path to $dict_config_xml_path" echo " merge: merge dictionary XML config back to $stardict_cfg_path" echo " remove: remove some options from $stardict_cfg_path to force generation of default dictionary group during the next start of stardict" echo echo "This script creates backups of altered files before modification." echo "Please, close stardict before running this script." } # extract dict config from stardict configuration $stardict_cfg_path to $dict_config_xml_path function stardict_extract_dics () { cp "$dict_config_xml_path" "$dict_config_xml_path.old" >/dev/null 2>&1 grep '^dict_config_xml=' "$stardict_cfg_path" \ | LANG=C sed -e 's/^dict_config_xml=//' -e 's/>\n "$dict_config_xml_path" } # merge extracted dict config with stardict configuration $stardict_cfg_path function stardict_merge_dics () { local config_line=$(sed -e 's/^[ \t]*//' "$HOME/.stardict/dict_config.xml" \ | sed -e :a -e 'N; s/\n//; ta' \ | sed -e 's/^/dict_config_xml=/') local line_num=`grep -n 'dict_config_xml' "$stardict_cfg_path" | sed -e 's/^\([0-9]*\):.*$/\1/'` sed -e "1,"$((line_num-1))"!d" "$stardict_cfg_path" > "$stardict_cfg_path".new echo "$config_line" >> "$stardict_cfg_path".new sed -e $((line_num+1))",\$!d" "$stardict_cfg_path" >> "$stardict_cfg_path".new mv "$stardict_cfg_path" "$stardict_cfg_path".old mv "$stardict_cfg_path".new "$stardict_cfg_path" } # remove dict_config_xml and dict_order_list to force generation of default dictionary group function stardict_remove_dics () { cp "$stardict_cfg_path" "$stardict_cfg_path".old >/dev/null 2>&1 sed -e '/^dict_config_xml=/d' -e '/^dict_order_list=/d' -i "$stardict_cfg_path" } if [ "$1" == "extract" ] then stardict_extract_dics exit fi if [ "$1" == "merge" ] then stardict_merge_dics exit fi if [ "$1" == "remove" ] then stardict_remove_dics exit fi usage