924fb0eedb93dedd4eb7736273aaacd368064b9c
[geeqie.git] / plugins / ufraw / geeqie-ufraw
1 #!/bin/bash
2
3 # FIXME TODO:
4 # restore XMP in output files from input sidecars
5 # getopt, verbosity levels
6 # improve the default ufraw configuration
7 # localization?
8 # help
9
10
11 # matches raw file names, used as case insensitive
12 RAW_REGEX='.*\.\(arw\)\|\(srf\)\|\(sr2\)\|\(crw\)\|\(cr2\)\|\(kdc\)\|\(dcr\)\|\(k25\)\|\(raf\)\|\(mef\)\|\(mos\)\|\(mrw\)\|\(nef\)\|\(orf\)\|\(pef\)\|\(ptx\)\|\(dng\)\|\(x3f\)\|\(raw\)\|\(r3d\)\|\(3fr\)\|\(erf\)$'
13
14 # matches ufraw id file names, used as case sensitive
15 ID_REGEX='.*\.ufraw$'
16
17 get_output_from_id ()
18 {
19     grep "<OutputFilename>.*</OutputFilename>" "$1" |sed -e 's|.*<OutputFilename>\(.*\)</OutputFilename>.*|\1|'
20 }
21
22 # test if the id file has changed and the output needs to be refreshed
23 id_file_changed ()
24 {
25     idfile=$1
26     output=`get_output_from_id "$idfile"`
27     [ ! -f "$output" -o "$idfile" -nt "$output" ]
28 }
29
30 # refresh the output file specified by given id file, if necessary
31 process_ufraw_id_file ()
32 {
33     idfile=$1
34     if id_file_changed "$idfile" ; then
35         ufraw-batch --overwrite "$idfile"
36     fi
37 }
38
39 # test for newly added raw files that were never processed
40 raw_file_not_processed ()
41 {
42     rawfile=$1
43     basename=${rawfile%.*}
44     [ ! -f "$basename.ufraw" ]
45 }
46
47 # process raw file for the first time
48 process_raw_file_default ()
49 {
50     rawfile=$1
51     if raw_file_not_processed "$rawfile" ; then
52         ufraw-batch --create-id=also \
53                     --wb=camera \
54                     --exposure=auto \
55                     --out-type=jpeg \
56                     --compression=96 \
57                     "$rawfile"
58     fi
59 }
60
61 # process all files listed in file $1
62 # if $2 is not empty, produce output for zenity --progress
63 process_list ()
64 {
65     list=$1
66     use_zenity=$2
67     
68     count=`wc -l <$list`
69     n=0
70     [ -n "$use_zenity" ] && echo 0
71
72     if [ "$count" -gt 0 ] ; then
73         while read file; do
74             [ -f "$file" ] || continue
75             if echo "$file"|grep -q -i "$RAW_REGEX" ; then
76                 process_raw_file_default "$file" 
77             elif echo "$file"|grep -q "$ID_REGEX" ; then
78                 process_ufraw_id_file "$file"
79
80             fi
81
82             n=$((n + 1))
83             
84             # the function can end at the 'echo' command with broken pipe
85             # if it is cancelled via zenity
86             [ -n "$use_zenity" ] && echo $((n * 100 / count))
87
88         done <$list
89     fi
90     [ -n "$use_zenity" ] && echo 100
91 }
92
93 # process all files in directory $1, including subdirectories
94 # processing is controlled by zenity dialogs if $DISPLAY is set
95 process_tree ()
96 {
97     list=`mktemp /tmp/geeqie-ufraw-list.XXXXXXXXXX` || exit 1
98
99     find "$1" -iregex "$RAW_REGEX" -print | while read rawfile ; do
100         raw_file_not_processed "$rawfile" && echo "$rawfile" 
101     done >>$list
102     
103     #refresh output from changed id files
104     find "$1" -regex "$ID_REGEX" -print | while read idfile ; do
105         id_file_changed "$idfile" && echo "$idfile"
106     done >>$list
107
108     if [ -n "$DISPLAY" ] ; then
109         if [ -s $list ] && \
110            zenity --list --title "Files to proceed" --text "Files to proceed" --column "Files" <$list ; then
111             process_list $list with_zenity | zenity --progress --auto-close
112         fi
113     else 
114         # no DISPLAY
115         process_list $list
116     fi
117     rm $list
118 }
119
120
121
122 if [ -d "$1" ] ; then
123     # $1 is a directory
124     process_tree "$1"
125 else
126     list=`mktemp /tmp/geeqie-ufraw-list.XXXXXXXXXX` || exit 1
127     for file in "$@" ; do
128         echo $file
129     done >>$list
130     process_list $list
131     rm $list
132 fi