Show shortcut keys in pop-up menus
[geeqie.git] / plugins / ufraw / geeqie-ufraw
1 #!/bin/sh
2
3 # FIXME TODO:
4 # improve the default ufraw configuration
5 # localization?
6
7
8 # matches raw file names, used as case insensitive
9 RAW_REGEX='.*\.(arw|srf|sr2|crw|cr2|kdc|dcr|k25|raf|mef|mos|mrw|nef|orf|pef|ptx|dng|x3f|raw|r3d|3fr|erf)$'
10
11 # matches output file names, used as case insensitive
12 OUT_REGEX='.*\.(jpg|jpeg|png|tif|tiff|ppm)$'
13
14 # matches ufraw id file names, used as case sensitive
15 ID_REGEX='.*\.ufraw$'
16
17 # matches xmp sidecar file names, used as case insensitive
18 XMP_REGEX='.*\.xmp$'
19
20 # extract output file from ufraw id file
21 get_output_from_id ()
22 {
23     grep "<OutputFilename>.*</OutputFilename>" "$1" |sed -e 's|.*<OutputFilename>\(.*\)</OutputFilename>.*|\1|'
24 }
25
26 # extract input file from ufraw id file
27 get_input_from_id ()
28 {
29     grep "<InputFilename>.*</InputFilename>" "$1" |sed -e 's|.*<InputFilename>\(.*\)</InputFilename>.*|\1|'
30 }
31
32 add_xmp_from_sidecar ()
33 {
34     idfile=$1
35     input=`get_input_from_id "$idfile"`
36     [ -f "$input" ] || return 1
37
38     basename=${input%.*}
39     dirname=${basename%/*}
40     xmp=`find "$dirname" -maxdepth 1 -path "$basename.*" -regextype posix-egrep -iregex "$XMP_REGEX" -print | head -n 1`
41     [ -f "$xmp" ] || return 1
42
43     output=`get_output_from_id "$idfile"`
44
45     [ -f "$output" ] || return 1
46     xmpext=.${xmp##*.}
47
48     # passing the source file to exiv2 is unnecessary complicated
49     # do not change the orientation, ufraw resets it to 1
50     exiv2 insert -ixX -l "$dirname" -S "$xmpext" "$output"
51     exiv2 -M "set Xmp.tiff.Orientation 1" "$output"
52 }
53
54 # test if the id file has changed and the output needs to be refreshed
55 id_file_changed ()
56 {
57     idfile=$1
58     output=`get_output_from_id "$idfile"`
59     [ ! -f "$output" -o "$idfile" -nt "$output" ]
60 }
61
62 # refresh the output file specified by given id file, if necessary
63 process_ufraw_id_file ()
64 {
65     idfile=$1
66     if id_file_changed "$idfile" ; then
67         ufraw-batch --overwrite "$idfile"
68         add_xmp_from_sidecar "$idfile"
69     fi
70 }
71
72 # test for newly added raw files that were never processed
73 raw_file_not_processed ()
74 {
75     rawfile=$1
76     basename=${rawfile%.*}
77     dirname=${basename%/*}
78     outfiles=`find "$dirname" -maxdepth 1 -path "$basename.*" -regextype posix-egrep \( -iregex "$OUT_REGEX" -o -regex "$ID_REGEX" \) -print `
79     [ -z "$outfiles" ] # return true if no possible output file exists
80
81     # raw+jpeg pair created by the camera is considered processed, 
82     # - this function returns false, the jpeg from camera is preserved and id file is not created
83 }
84
85 # process raw file for the first time
86 process_raw_file_default ()
87 {
88     rawfile=$1
89     if raw_file_not_processed "$rawfile" ; then
90         ufraw-batch --create-id=also \
91                     --wb=camera \
92                     --exposure=auto \
93                     --out-type=jpeg \
94                     --compression=96 \
95                     "$rawfile"
96         idfile=${rawfile%.*}.ufraw
97         add_xmp_from_sidecar "$idfile"
98     fi
99 }
100
101 # process all files listed in file $1
102 # if $2 is not empty, produce output for zenity --progress
103 process_list ()
104 {
105     list=$1
106     use_zenity=$2
107
108     count=`wc -l <$list`
109     n=0
110     [ -n "$use_zenity" ] && echo 0
111
112     if [ "$count" -gt 0 ] ; then
113         while read file; do
114             [ -f "$file" ] || continue
115             if echo "$file"|grep -E -q -i "$RAW_REGEX" ; then
116                 process_raw_file_default "$file" 
117             elif echo "$file"|grep -E -q "$ID_REGEX" ; then
118                 process_ufraw_id_file "$file"
119
120             fi
121
122             n=$((n + 1))
123
124             # the function can end at the 'echo' command with broken pipe
125             # if it is cancelled via zenity
126             [ -n "$use_zenity" ] && echo $((n * 100 / count))
127
128         done <$list
129     fi
130     [ -n "$use_zenity" ] && echo 100
131 }
132
133 # process all files in directories $@, including subdirectories
134 # processing is controlled by zenity dialogs if $DISPLAY is set
135 process_tree ()
136 {
137     list=`mktemp /tmp/geeqie-ufraw-list.XXXXXXXXXX` || exit 1
138
139     find "$@" -regextype posix-egrep -iregex "$RAW_REGEX" -print | while read rawfile ; do
140         raw_file_not_processed "$rawfile" && echo "$rawfile" 
141     done >>$list
142     
143     #refresh output from changed id files
144     find "$@" -regextype posix-egrep -regex "$ID_REGEX" -print | while read idfile ; do
145         id_file_changed "$idfile" && echo "$idfile"
146     done >>$list
147
148     if [ -n "$DISPLAY" ] ; then
149         if [ -s $list ] && \
150            zenity --list --title "Files to proceed" --text "Files to proceed" --column "Files" <$list ; then
151             process_list $list with_zenity | zenity --progress --auto-close
152         else
153            zenity --info --title "All files are up-to-date" --text "All files are up-to-date"
154         fi
155     else 
156         # no DISPLAY
157         process_list $list
158     fi
159     rm $list
160 }
161
162 print_help ()
163 {
164     cat << EOT
165
166     RAW file collection maintenance tool
167
168     Usage:
169
170     geeqie-ufraw [raw file | id file] ...
171     geeqie-ufraw --recursive [dir] ...
172     geeqie-ufraw -h | --help
173
174     This script searches for new RAW files or for modified UFRaw 
175     ID files and process them with ufraw-batch. It can work either 
176     on individual files or on whole directory.
177     The functions are designed to be usable from Geeqie menu.
178
179 EOT
180 }
181
182 #parse commandline
183
184 while true ; do
185     case "$1" in
186         -v|--verbose)
187             verbose=yes #fixme: not used yet
188             shift ;;
189         -R|--recursive)
190             recursive=yes
191             shift ;;
192         -h|-help|--help|-*) 
193             print_help
194             exit  ;;
195         *)
196             break ;;
197     esac
198 done
199
200 if [ $# -lt 1 ] ; then
201     print_help
202     exit
203 fi 
204
205 if [ -n "$recursive" ] ; then
206     # recursive processing of directories
207     process_tree "$@"
208 else
209     list=`mktemp /tmp/geeqie-ufraw-list.XXXXXXXXXX` || exit 1
210     for file in "$@" ; do
211         echo "$file" |sed -e "s|^\([^/]\)|$PWD/\1|"
212     done >>$list
213     process_list $list
214     rm $list
215 fi