#! /bin/bash
# Tests whether the executable files listed below are in place.

# We test by actually running all the executables.

# Failure message
## See Netpbm Library Prerequisites
## http://netpbm.sourceforge.net/prereq.html
## if make succeeds but this test fails
##
## If you run the "make test-install" on an older version, some executables
## may be reported as missing because of missing features.

function testExitStatus () {

# This function takes 3 positional parameters:
#   $1: filename
#   $2: expected exit status   (In some cases we expect error.)
#   $3: actual exit status

    case $3 in
      126) if [ ! -x $1 ]
             then echo $1": NOT EXECUTABLE";
             else echo $1": ERROR: "$3;    echo $1": error: "$3 1>&2 ;
           fi ;;
      127) type -p $1 > /dev/null
           if [ $? -ne 0 ]
             then echo $1": NO SUCH FILE";
             else echo $1": ERROR: "$3;    echo $1": error: "$3 1>&2 ;
           fi ;;
      88)  echo $1": NO SUCH FILE" ;;
      $2)  echo $1": ok" ;;
      *)   echo $1": ERROR: "$3;    echo $1": error: "$3 1>&2 ;;
    esac

}


# Test programs which have the --version flag.
# See showVersion() in lib/libpm.c for the standard version announcement.

ordinary_testprogs="\
  411toppm \
  asciitopgm \
  atktopbm \
  avstopam \
  bioradtopgm \
  bmptopnm \
  brushtopbm \
  cameratopam \
  cistopbm \
  cmuwmtopbm \
  ddbugtopbm \
  escp2topbm \
  eyuvtoppm \
  fiascotopnm \
  fitstopnm \
  fstopgm \
  g3topbm \
  gemtopnm \
  giftopnm \
  gouldtoppm \
  hdifftopam \
  hipstopgm \
  ilbmtoppm \
  imgtoppm \
  infotopam \
  jbigtopnm \
  jpeg2ktopam \
  jpegtopnm \
  leaftoppm \
  lispmtopgm \
  macptopbm \
  mdatopbm \
  mgrtopbm \
  mrftopbm \
  mtvtoppm \
  neotoppm \
  palmtopnm \
  pamaddnoise \
  pamaltsat \
  pamarith \
  pambackground \
  pambayer \
  pambrighten \
  pamchannel \
  pamcomp \
  pamcrater \
  pamcut \
  pamdeinterlace \
  pamdepth \
  pamdice \
  pamditherbw \
  pamedge \
  pamendian \
  pamenlarge \
  pamexec \
  pamfile \
  pamfind \
  pamfix \
  pamflip \
  pamfunc \
  pamgauss \
  pamgetcolor \
  pamgradient \
  pamhue \
  pamlevels \
  pamlookup \
  pammasksharpen \
  pammixinterlace \
  pammixmulti \
  pammosaicknit \
  pamoil \
  pampaintspill \
  pamperspective \
  pampick \
  pampop9 \
  pamrecolor \
  pamrubber \
  pamscale \
  pamseq \
  pamshadedrelief \
  pamsharpmap \
  pamsharpness \
  pamsistoaglyph \
  pamslice \
  pamsplit \
  pamstack \
  pamstereogram \
  pamstretch \
  pamstretch-gen \
  pamsumm \
  pamsummcol \
  pamtable \
  pamthreshold \
  pamtilt \
  pamtoavs \
  pamtodjvurle \
  pamtofits \
  pamtogif \
  pamtohdiff \
  pamtohtmltbl \
  pamtojpeg2k \
  pamtompfont \
  pamtooctaveimg \
  pamtopam \
  pamtopdbimg \
  pamtopfm \
  pamtopng \
  pamtopnm \
  pamtosrf \
  pamtosvg \
  pamtotga \
  pamtotiff \
  pamtouil \
  pamtowinicon \
  pamtoxvmini \
  pamtris \
  pamundice \
  pamunlookup \
  pamvalidate \
  pamwipeout \
  pamx \
  pbmclean \
  pbmlife \
  pbmmake \
  pbmmask \
  pbmminkowski \
  pbmpage \
  pbmpscale \
  pbmreduce \
  pbmtext \
  pbmtextps \
  pbmto10x \
  pbmto4425 \
  pbmtoascii \
  pbmtoatk \
  pbmtobbnbg \
  pbmtocis \
  pbmtocmuwm \
  pbmtodjvurle \
  pbmtoepsi \
  pbmtoepson \
  pbmtoescp2 \
  pbmtog3 \
  pbmtogem \
  pbmtogo \
  pbmtoibm23xx \
  pbmtolj \
  pbmtoln03 \
  pbmtolps \
  pbmtomacp \
  pbmtomatrixorbital \
  pbmtomda \
  pbmtomgr \
  pbmtomrf \
  pbmtonokia \
  pbmtopgm \
  pbmtopi3 \
  pbmtopk \
  pbmtoplot \
  pbmtoppa \
  pbmtopsg3 \
  pbmtoptx \
  pbmtosunicon \
  pbmtowbmp \
  pbmtoxbm \
  pbmtoybm \
  pbmtozinc \
  pbmupc \
  pc1toppm \
  pcxtoppm \
  pdbimgtopam \
  pfmtopam \
  pgmabel \
  pgmbentley \
  pgmdeshadow \
  pgmenhance \
  pgmhist \
  pgmkernel \
  pgmmake \
  pgmmedian \
  pgmminkowski \
  pgmmorphconv \
  pgmnoise \
  pgmramp \
  pgmtexture \
  pgmtofs \
  pgmtolispm \
  pgmtopbm \
  pgmtopgm \
  pgmtoppm \
  pgmtosbig \
  pgmtost4 \
  pi1toppm \
  pi3topbm \
  picttoppm \
  pjtoppm \
  pktopbm \
  pngtopam \
  pnmalias \
  pnmcat \
  pnmcolormap \
  pnmconvol \
  pnmcrop \
  pnmgamma \
  pnmhisteq \
  pnmhistmap \
  pnmindex \
  pnminvert \
  pnmmargin \
  pnmmercator \
  pnmmontage \
  pnmnlfilt \
  pnmnorm \
  pnmpad \
  pnmpaste \
  pnmpsnr \
  pnmquant \
  pnmquantall \
  pnmremap \
  pnmrotate \
  pnmscalefixed \
  pnmshear \
  pnmsmooth \
  pnmstitch \
  pnmtile \
  pnmtoddif \
  pnmtofiasco \
  pnmtojbig \
  pnmtojpeg \
  pnmtopalm \
  pnmtopclxl \
  pnmtopng \
  pnmtops \
  pnmtorast \
  pnmtorle \
  pnmtosgi \
  pnmtosir \
  pnmtotiffcmyk \
  pnmtoxwd \
  ppm3d \
  ppmbrighten \
  ppmchange \
  ppmcie \
  ppmcolormask \
  ppmcolors \
  ppmdcfont \
  ppmddumpfont \
  ppmdim \
  ppmdist \
  ppmdither \
  ppmdmkfont \
  ppmdraw \
  ppmfade \
  ppmflash \
  ppmforge \
  ppmglobe \
  ppmhist \
  ppmlabel \
  ppmmake \
  ppmmix \
  ppmntsc \
  ppmpat \
  ppmrainbow \
  ppmrelief \
  ppmrough \
  ppmshadow \
  ppmshift \
  ppmspread \
  ppmtoacad \
  ppmtoapplevol \
  ppmtoarbtxt \
  ppmtoascii \
  ppmtobmp \
  ppmtoeyuv \
  ppmtoicr \
  ppmtoilbm \
  ppmtoleaf \
  ppmtolj \
  ppmtomitsu \
  ppmtoneo \
  ppmtopcx \
  ppmtopgm \
  ppmtopi1 \
  ppmtopict \
  ppmtopj \
  ppmtopjxl \
  ppmtoppm \
  ppmtopuzz \
  ppmtorgb3 \
  ppmtosixel \
  ppmtospu \
  ppmtoterm \
  ppmtowinicon \
  ppmtoxpm \
  ppmtoyuv \
  ppmtoyuvsplit \
  ppmtv \
  ppmwheel \
  psidtopgm \
  pstopnm \
  qrttoppm \
  rasttopnm \
  rawtopgm \
  rawtoppm \
  rgb3toppm \
  rlatopam \
  rletopnm \
  sbigtopgm \
  sgitopnm \
  sirtopnm \
  sldtoppm \
  spctoppm \
  spottopgm \
  sputoppm \
  srftopam \
  st4topgm \
  sunicontopnm \
  svgtopam \
  tgatoppm \
  thinkjettopbm \
  tifftopnm \
  wbmptopbm \
  winicontopam \
  winicontoppm \
  xbmtopbm \
  ximtoppm \
  xpmtoppm \
  xvminitoppm \
  xwdtopnm \
  ybmtopbm \
  yuvsplittoppm \
  yuvtoppm \
  yuy2topam \
  zeisstopnm \
"


# The string "fiascotopnm" has to be filtered out by fgrep for fiascotopnm
# has a slightly different version report format.

# Switch to control output from "program --version"
#
grepSwitch=1        # Initial value
                    # Always 1 for check-install,
                    # otherwise changes to 0 once a command succeeds.
grepOption[0]="-v"  # Suppress output
grepOption[1]=""    # Output --version message

for i in $ordinary_testprogs
  do
    # Stub out programs that aren't built.
    Available-Testprog "$i"
      if [ $? = 1 ]; then
      echo "$i: program was not built" 1>&2
      echo "$i: ok"
      continue
      fi


    $i --version < /dev/null 2>&1 | \
    # Older versions of pamstretch-gen and ppmshadow (encountered when
    # one does "make check-install") get stuck when there is no input.
    # Supply /dev/null as input to placate these programs.
    
    egrep -v -e "fiascotopnm" -e \
    ": ((BSD|SYSV|MSDOS|AMIGA) defined|RGB_?ENV=)" | \
    egrep ${grepOption[$grepSwitch]} -e \
    ": (Using lib(net)?pbm|(Built (by|at)|Compiled ))" 1>&2;
    # See showVersion() in lib/libpm.c for the above regular expressions.
  
    exitStatus=${PIPESTATUS[0]}
    testExitStatus $i 0 ${exitStatus}

    if [ ${CHECK_TYPE} != "install" ]
      then grepSwitch=$((${grepSwitch} * ${exitStatus}==0 ? 0 : 1)) ;
    fi
  done


# Anytopnm now supports --version but it delegates the report to pamtopnm.
# It fails if pamtopnm is absent.
# We consider anytopnm too important to test in an indirect manner.

anytopnm testgrid.pbm > /dev/null 2> /dev/null;
testExitStatus anytopnm 0 $?


# Test manweb which has --help.

manweb --help > /dev/null
    testExitStatus manweb 0 $?
