#!/bin/sh

for i in `cat /etc/img_ext`; do
  if [ -z "$EXTS" ]; then
    EXTS="$i"
  else
    EXTS="$EXTS\|$i"
  fi
done

if test "$1" = "-a"; then
  # display all images in the selected folder
  DIR=$2
  [ -d "$DIR" ] || DIR=${DIR%/[^/]*}/
  ls -1 "$DIR" | grep -i "\.\($EXTS\)\$" | sed "s%\(.*\)%$DIR\1%" > /tmp/view_img_files
elif test "$1" = "-r"; then
  # display all images in the selected folder and its subfolders (recursive)
  DIR=$2
  [ -d "$DIR" ] || DIR=${DIR%/[^/]*}/
  find "$DIR" | grep -i "\.\($EXTS\)\$" > /tmp/view_img_files
else
  # display a single image
  echo "$1" > /tmp/view_img_files
fi

if [ -s /tmp/view_img_files ]; then
  echo 'quit 165' > /var/mp_control
else
  exit 1
fi
