#!/bin/sh
# @(#) $Id: exifgrep,v 1.5 2003/08/05 17:00:49 alex Exp $

# Run 'egrep' on the output of exifprobe -L, massaging the output a la
# 'list-photo'.  Accepts the -var, -export, -num, and '-' options of
# list_photo; all other options are passed to egrep.

usage()
{
    echo "Usage: exifgrep [-var|-export] [-num] [egrep-options] egrep-pattern [NOT egrep-pattern] imagefilename[s]" 1>&2
    exit 1
}


# Create the output format, choosing numerical or "interpreted" values
# from the exifprobe output lines, and stripping unwanted spaces for
# "variable" format (while quoting multi-word values).

format()
{
    if test 0$dovar -ne 0
    then
        while read var nequals nvalue iequals value remainder
        do
            case ${var} in
                "") continue ;;
            esac
            if test ${donum} -eq 1
            then
                case ${nvalue} in
                     [0-9]*) echo "${export} ${var}=${nvalue}" ;;
                          *) quote ${var} ${nvalue} ${iequals} ${value} ${remainder};;
                esac
            else
                case "${iequals}" in
                    "") quote ${var} ${nvalue}${value}${remainder} ;;
                     =) quote ${var} ${value} ${remainder} ;;
                  APEX) quote ${var} ${remainder} ;;
                     *) quote ${var} ${nvalue} ${iequals} ${value} ${remainder} ;;
                esac
            fi
        done
    else
        oifs=${IFS}
        IFS="="
        while read var nvalue value
        do
            case ${var} in
                "") continue ;;
            esac
            if test ${donum} -eq 1
            then
                case ${nvalue} in
                    " \""*) echo -n "${var}=${nvalue}"
                            case "${value}" in
                                "") echo ;;
                                 *) echo "=${value}" ;;
                            esac
                            ;;
                     *) echo "${var}=${nvalue}" ;;
                esac
            else
                case "${value}" in
                    "") echo "${var}=${nvalue}" ;;
                     *) case ${nvalue} in
                            " \""*) echo -n "${var}=${nvalue}"
                                    case "${value}" in
                                        "") echo ;;
                                         *) echo "=${value}" ;;
                                    esac
                                    ;;
                             *) echo "${var}=${value}" ;;
                        esac
                        ;;
                esac
            fi
        done
        IFS=$oifs
    fi
}

# On output, place double-quotes around multi-word values which
# are not already quoted. This is necessary for 'variable' format.

quote()
{
    case $# in
        0) ;;
        1) echo ${export} $1= ;;
        2) echo ${export} $1=$2 ;;
        *) case $1 in
            \#*) echo -n $1 ;;
              *) echo -n ${export} $1= ;;
           esac
           shift
           case $1 in
            \"*) echo $* ;;
              *) echo \"$*\" ;; #"
           esac
           ;;
    esac
}


# Gather options first.  This is kind of wierd.
dovar=0
donum=0
export=
egrep_options=
vgrep_options=
pattern=
notpattern=

while test $# -ge 1
do
    case $1 in
        -var) dovar=1; shift ;;
        -num) donum=1; shift ;;
        -export*) export=export; dovar=1; shift ;;
          -h) usage ;;
          -*) egrep_options="$egrep_options $1"; shift ;;
         NOT) case "$2" in
                -f) notpattern="-f $3"; shift; shift; shift; break ;;
                 *) notpattern=$2; shift; shift; break ;;
              esac
              ;;
           *) pattern=$1; shift
              case $1 in
                NOT) case "$2" in
                        -f) notpattern="-f $3"; shift; shift; shift; break ;;
                         *) notpattern=$2; shift; shift; break ;;
                     esac
                     ;;
                  *) break ;;
              esac
              ;;
    esac
done

if test $# -lt 1 -o "X${pattern}${notpattern}" = "X"
then
    usage
fi

# All variables are global.
# If there are no filenames, read from stdin, which is expected to be
# exifprobe -L output.

case $# in
     0) cat | egrep ${egrep_options} ${pattern} | format
        ;;
     *) case "${notpattern}" in
            "") exifprobe -L $* | egrep ${egrep_options} ${pattern} | format ;;
             *) case "${pattern}" in
                    "") exifprobe -L $* | egrep ${egrep_options} -v ${notpattern} | format ;;
                     *) exifprobe -L $* | egrep ${egrep_options} ${pattern} | egrep -v ${notpattern} | format ;;
                esac
                ;;
        esac
        ;;
esac
