#!/usr/pkg/bin/bash
############################################################
# Read pfs frames from stdin and write them in the
# format determined by the extension of the file name
############################################################

if test -z "$1" || test "$1" = "--help"; then
cat <<EOF
Read pfs frames from stdin and write them in the format determined by
the extension of the file name.

Usage: Usage: pfsout <file> [<file>...]

Recognized file formats and extensions:
 Radiance RGBE - .pic, .hdr
 TIFF (incl. LogLuv) - .tiff, .tif
 PNM, PPM - .ppm, .pnm
 JPEG - .jpeg, .jpg
 PNG - .png
 PFS - .pfs
 OpenEXR - .exr
 PFM - .pfm
 DPX - .dpx
 GIF - .gif
 BMP - .bmp
 EPS - .eps 

See the man page for more information.
EOF
    exit 1
fi

#Arguments used for all images passed to pfsout
global_arguments=""
if test -n "$1"; then
    while test "${1:0:1}" = "-"; do
        
        #Handle options that require a parameter
        for par_opt in "--frames" "-f" "--absolute" "-a"; do
            if test "$1" = $par_opt; then
                if test -z "$2"; then
                    echo >&2 "Required parameter missing after $par_opt"
                    exit 1;
                fi
                global_arguments="$global_arguments $1"
                shift
                break;
            fi
        done
        
        global_arguments="$global_arguments $1"              
        shift             
    done
fi

while test "$1"; do
      extension="${1##*.}"

      file_pattern=$1
      
      # Get --frames and --skip-frames arguments 
      extra_arguments="";
      if test -n "$2"; then
          while test "${2:0:1}" = "-"; do

              #Handle options that require a parameter
              for par_opt in "--frames" "-f" "--absolute" "-a"; do
                  if test "$2" = $par_opt; then
                      if test -z "$3"; then
                          echo >&2 "Required parameter missing after $par_opt"
                          exit 1;
                      fi
                      extra_arguments="$extra_arguments $2"
                      shift
                      break;
                  fi
              done
                            
              extra_arguments="$extra_arguments $2"              
              shift             
          done
      fi
            
      case "$extension" in
          ("hdr"|"HDR"|"pic"|"PIC")
          pfsoutrgbe "$file_pattern" $global_arguments $extra_arguments
             ;;
          ("ppm"|"PPM"|"pnm"|"PNM")
          pfsoutppm "$file_pattern" $global_arguments $extra_arguments
             ;;
          ("tif"|"TIF"|"tiff"|"TIFF")
          if which pfsoutimgmagick >/dev/null; then
              pfsoutimgmagick "$file_pattern" $global_arguments $extra_arguments          
          else
              pfsouttiff "$file_pattern" $global_arguments $extra_arguments
          fi
             ;;
          ("exr"|"EXR")
          pfsoutexr "$file_pattern" $global_arguments $extra_arguments
             ;;
          ("pfm"|"PFM")
          pfsoutpfm "$file_pattern" $global_arguments $extra_arguments
             ;;
          ("jpg"|"JPG"|"jpeg"|"JPEG")
          if which pfsoutimgmagick >/dev/null; then
              pfsoutimgmagick "$file_pattern" $global_arguments $extra_arguments          
          else
              pfsoutppm - | pnmtojpeg >$1
          fi
             ;;
          ("png"|"PNG")
          if which pfsoutimgmagick >/dev/null; then
              pfsoutimgmagick "$file_pattern" $global_arguments $extra_arguments          
          else
              pfsoutppm - | pnmtopng >$1
          fi
             ;;
          ("dpx"|"DPX"|"gif"|"GIF"|"bmp"|"BMP"|"eps"|"EPS")
          pfsoutimgmagick "$file_pattern" $global_arguments $extra_arguments
             ;;
          ("pfs"|"PFS")
          cat >$1
          ;;
          ("yuv"|"YUV")
              pfsoutyuv "$file_pattern" $global_arguments $extra_arguments
              ;;        
          (*)
             echo 1>&2 "Unknown extension: $extension" 
             exit 1
             ;;          
      esac
      
      shift
done
