#!/bin/bash

# =============================================================================
# runExifTool - simple output GUI for exiftool
  Version=1.0.0
# author: Copyright (C)2018 step
# license: GNU GPL version 2
# depend: exiftool, yad
# =============================================================================

# Export RUNEXIFTOOL_OPT to pass options to exiftool, default -s2
# usage: runexiftool [exiftool-options] files...
SN=runExifTool

die_printf() # [--usage] $1-exit-code $2-format [$3%-arguments] {{{1
# $2% may be gettexted, i.e. "$(gettext "file %s is...%s\n")" "$file" "$(gettext "open")"
{
  [ "$1" = --usage ] && { usage; shift; }
  local status fmt
  status="$1" fmt="$2"; shift 2
  if [ "$fmt" ]; then
    if [ -t 0 ]; then
      printf "%s: $fmt" "$(basename "$0")" "$@" >&2
    else
      yad --title="$SN" --window-icon=exif --image=gtk-stop \
        --timeout=8 --buttons-layout=center --borders=4 \
        --center --on-top --text "$(printf "$fmt" "$@")"
    fi
  fi
  exit $status
}

# Main {{{1
declare -a Argv=( "$@" ) Option
for (( i = 0; i < ${#Argv}; i++ )); do # gather options
  [[ ${Argv[$i]} == -* ]] && Option+=( "${Argv[$i]}" ) || break
done
while [[ $1 == -* ]]; do shift; done # chop options
declare -a Path=( "$@" ); Filename=( ${Path[@]##*/} )
YAD_WINTITLE="${Filename[*]}${1:+ -} $SN"
[ $# = 0 ] && die_printf 1 "usage: ${0##*/} files...\n\nman: perldoc exiftool\n"

yad < <(exiftool ${RUNEXIFTOOL_OPT:--s2} "${Option[@]}" "${Path[@]}") \
  --title "$YAD_WINTITLE" --window-icon=exif \
  --buttons-layout=center --borders=4 \
  --center --width=600 --height=400 \
  --text-info \
  --button=gtk-quit

