#!/bin/sh
# asciiview - an ascii art image browser script. Front end for aview/aaflip
clear()
{
  kill $! 2>/dev/null
  rm -f /tmp/aview$$.pgm 2>/dev/null
}
myconvert()
{
   if anytopnm "$1" >/tmp/aview$$.pgm 2>/dev/null; then
     exit
   elif convert -colorspace gray "$1" pgm:- 2>/dev/null; then
     exit
   fi
   echo "Failed to convert file format to PNM by both convert and anytopnm" >&2
   while true; do
     echo "0 " 
   done
}
set -A filenames
options=""
if [ "$1" = "" ]; then
  echo "$0 - an ascii art image/animation browser.

  To run this script you need aview, aaflip and NetPBM or ImageMagick.
  You may browse any graphics format supported by NetPBM or ImageMagick
  and .fli/.flc files.

  Usage:
   $0 [options] [filenames]

  type aview --help [enter] for list of options.
  "
  exit 1
fi
while [ "$1" != "" ]; do
  case $1 in
    "-font" | "-driver" | "-kbddriver" | "-mousedriver" | "-*width" | "-*height" | "-bright" | "-contrast" | "-gamma" | "-random" | "-dimmul" | "-boldmul")
      options="$options $1 $2"
      shift 
      shift 
      ;;
    -*)
      options="$options $1"
      shift
      ;;
    *) 
      filenames[${#filenames[@]}]="$1"
      shift
      ;;
  esac
done
trap clear 0 2 3 9 15
mkfifo /tmp/aview$$.pgm
i=0
while [ $i -lt ${#filenames[@]} ]; do
	name=${filenames[$i]}
	if [ -r "$name" ]; then
		case "$name" in
*.fli | *.lfc | *.flic )
  PATH="$PATH:."
  aaflip $options "$name"
  ;;
*)
  myconvert "$name" > /tmp/aview$$.pgm &
  pid=$!
  aview  $options /tmp/aview$$.pgm
  kill $pid 2>/dev/null
esac
else
  echo "$name could not be opended"
fi
	i=$((i+1))
done
