#!/bin/sh
#
# creates printfilters for all pbmtools on your system, which convert to p[bgnp]m
# and stores them in /usr/lib/rhs/rhs-printfilters
#
# If there is already such a filterfile, then the new one is named *.fpi.<random>.new
# Already existing filterfiles with a name *.fpi.<random>.new are overwritten [albeit
# this happens only with a probability of inverse cardinality of $RANDOM [2e-16?].] !!
#
# You will have to rename the files of those filename clashes by hand.
#

this_random=$RANDOM
rhs_printfilter_path=/usr/lib/rhs/rhs-printfilters/

for f in  $(find /bin /usr/bin /usr/local/bin -iname "*top?m")
  do
    filtername=$(basename $f | sed 's/p.m$/-&/;s/to-p.m$/-&/;s/p.m$/pnm/')
    comment=$(basename $f | sed 's/p.m$/ &/;s/to p.m$/ &/')
    filterfilename=${rhs_printfilter_path}${filtername}.fpi
    if  [ -f $filterfilename ]
      then
        filterfilename=${filterfilename}.${this_random}.new
    fi
    echo '#!/bin/sh' > ${filterfilename}
    echo '#' >> ${filterfilename}
    echo '# convert '$comment >> ${filterfilename}
    echo '#' >> ${filterfilename}
    echo 'source ${SPOOLDIR}/$(basename $0 .fpi).cfg' >> ${filterfilename}
    echo  >> ${filterfilename}
    echo $f >> ${filterfilename}
    chmod 755 ${filterfilename}
  done
