#!/bin/sh
#
# Copy Exif tags (APP1 marker) from one JPEG file to another.
# Requires my modified rdjpgcom and wrjpgcom.
#
# Usage:
#
USAGE='Usage: copyexif source-jpeg dest-jpeg'

if [ $# -ne 2 ]
then
    echo "$USAGE" 1>&2
    exit 2
fi

TMP=/tmp/cpex$$
trap "rm -f $TMP.*; exit 1" 1 2 15
trap "rm -f $TMP.*; exit 0" 13

if ! rdjpgcom -raw app1 "$1" > $TMP.a
then
    rm -f $TMP.*
    echo "copyexif: rdjpgcom failed; exiting for safety" 1>&2
    exit 1
fi

if ! wrjpgcom -app1 $TMP.a "$2" > $TMP.b
then
    rm -f $TMP.*
    echo "copyexif: wrjpgcom failed; exiting for safety" 1>&2
    exit 1
fi

# No interrupts allowed after this point
trap 1 2 13 15
cp $TMP.b "$2"
rm -f $TMP.*
