#! /bin/sh

# 'pamnoise' is designed to be backward compatible with 'pgmnoise' except that
# it produces PAM output instead of PGM.  That means we can pass any valid
# 'pgmnoise' arguments to 'pamnoise', and just pipe the output through
# 'pgmtopgm' to make it PGM.
#
# The only things for which that does not work is the -plain option, which
# has to go on the 'pgmtopgm', not the 'pamnoise', invocation.  So we have
# a big hack to separate -plain from the rest of the arguments.
#
# -version is also a problem, because while it works on 'pamnoise', having
# it on 'pamnoise' causes 'pgmtopgm' to fail in an ugly way.

plainOpt='' # initial value
quietOpt='' # initial value

for arg in "$@"; do
    case "$arg" in
        -version|--version )
	pamnoise "$@"; exit $?;
	;;
	-plain|--plain )
	plainOpt='-plain' ;
	;;
	-quiet|--quiet )
	quietOpt='-quiet' ;
	;;
    esac	
    done

if [ $# -lt 2 ];
  then
  echo "pgmnoise: Wrong number of arguments: $#.  Arguments are width and height of image, in pixels" 1>&2 ; exit 1;
fi

pamnoise "$@" | pgmtopgm $plainOpt $quietOpt
