# convert-ppm-pbm Copyright 2012 DMM; GPL 3+ 
# 
# For each file in a directory which is either a .ppm or .pbm
# convert it to png

# requires package: imagemagick

shopt -s extglob 

FILES=`ls`

for f in $FILES; do

   # cut the shorted possible (%) string off the end which matches 
   # exactly one (@) of .jp2 or .tiff
   # In other words, strip the .jp2 or .tiff suffix
   BASE=${f%@(.ppm|.pbm)} 
   # Now get the suffix by stripping the BASE off the filename
   SUFFIX=${f#$BASE} 
   
   if [ "$SUFFIX" == ".ppm" ]; then
      echo $f
      convert $BASE.ppm $BASE.png
   elif [ "$SUFFIX" == ".pbm" ]; then
      echo $f
      convert $BASE.pbm $BASE.png
   else
      echo $f IS UNKNOWN, skipping
   fi

done
