#!/bin/sh -e
#
# pdf_images - extract images from a pdf
# dependencies: pdfimages | poppler

pdf_images() {
  set \
    -o noclobber \
    -o noglob \
    -o verbose \
    -o xtrace
  for file in "${@-}"; do
    test -s "${file-}" && test ! -L "${file-}" \
      && case "${file-}" in
        *.[Pp][Dd][Ff])
          # -p: add page numbers to filenames
          # -print-filenames: to standard output
          command pdfimages \
            -all \
            -p \
            -print-filenames \
            "${file-}" \
            "${file%.*}"-
          ;;
        *) ;;
      esac
  done
  {
    set \
      +o noclobber \
      +o noglob \
      +o verbose \
      +o xtrace
  } 2>/dev/null
}

pdf_images "$@"
