c533331c05865ec4bf6a51a80630de18faf5540b
[geeqie.git] / scripts / keyword_merge.sh
1 #!/bin/sh
2
3 ## @file
4 ## @brief Merge the keyword tree of one Geeqie configuration file into another.  
5 ##
6 ## The keyword trees are simply concatenated. When Geeqie loads
7 ## the resulting configuration file, any duplicates are discarded.
8 ##
9 ## There is no error checking.
10 ##
11
12 merge_file()
13 {
14 flag=0
15 while read -r line_merge
16 do
17         if [ $flag -eq 0 ]
18         then
19                 if [ "$line_merge" != "${line_merge%<keyword_tree>*}" ]
20                 then
21                         flag=1
22                 fi
23         else
24                 if [ "$line_merge" != "${line_merge%<keyword_tree>*}" ]
25                 then
26                         flag=0
27                 else
28                         printf '%s\n' "$line_merge" >> "$2"
29                 fi
30         fi
31 done < "$1"
32 }
33
34 np=$#
35
36 zenity --info --text="This script will merge the keywords from one Geeqie\nconfiguration file into another.\n\n\The command format is:\nkeyword_merge.sh {config. file to merge into} {config. file to merge from}\n\nIf you do not supply parameters, you are prompted.\n\nYou are given the option to backup the main config. file before it is overwritten with the merged data.\n\nEnsure that Geeqie is not running." --title="Geeqie merge keywords" --width=400 --height=200
37 if [ $? -eq 1 ]
38 then
39         exit
40 fi
41
42
43 if [ $np -ge 3 ]
44 then
45         zenity --error --text "Too many parameters"
46         exit
47 elif [ $np -eq 0 ]
48 then
49         config_main=$(zenity --file-selection --file-filter="geeqierc.xml" --file-filter="*.xml" --file-filter="*" --title="Select main configuration file")
50         if [ $? -eq 1 ]
51         then
52                 exit
53         fi
54         config_merge=$(zenity --file-selection --file-filter="geeqierc.xml" --file-filter="*.xml" --file-filter="*"-- title="Select configuration file to merge from")
55         if [ $? -eq 1 ]
56         then
57                 exit
58         fi
59 elif [ $np -eq 1 ]
60 then
61         config_merge=$(zenity --file-selection --file-filter="geeqierc.xml" --file-filter="*.xml" --file-filter="*" --title="Select configuration file to merge from")
62         if [ $? -eq 1 ]
63         then
64                 exit
65         fi
66 fi
67
68 tmp_file=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
69
70 while read -r line_main
71 do
72         if [ "$line_main" != "${line_merge%<keyword_tree>*}" ]
73         then
74                 merge_file "$config_merge" "$tmp_file"
75         fi
76         printf '%s\n' "$line_main" >> "$tmp_file"
77 done < "$config_main"
78
79
80 if zenity --question --text="Backup configuration file before overwriting?"
81 then
82         cp "$config_main" "$config_main.$(date +%Y%m%d%H%M%S)"
83 fi
84
85 mv "$tmp_file" "$config_main"