+# extract input file from ufraw id file
+get_input_from_id ()
+{
+ grep "<InputFilename>.*</InputFilename>" "$1" |sed -e 's|.*<InputFilename>\(.*\)</InputFilename>.*|\1|'
+}
+
+add_xmp_from_sidecar ()
+{
+ idfile=$1
+ input=`get_input_from_id "$idfile"`
+ [ -f "$input" ] || return 1
+
+ basename=${input%.*}
+ dirname=${basename%/*}
+ xmp=`find "$dirname" -maxdepth 1 -path "$basename.*" -regextype posix-egrep -iregex "$XMP_REGEX" -print | head -n 1`
+ [ -f "$xmp" ] || return 1
+
+ output=`get_output_from_id "$idfile"`
+
+ [ -f "$output" ] || return 1
+ xmpext=.${xmp##*.}
+
+ # passing the source file to exiv2 is unnecessary complicated
+ # do not change the orientation, ufraw resets it to 1
+ exiv2 insert -ixX -l "$dirname" -S "$xmpext" "$output"
+ exiv2 -M "set Xmp.tiff.Orientation 1" "$output"
+}
+