#!/bin/sh

# imdocselect utility format options.xml > file.xml

# Selects lines from options.html based on <utils ...> tags.
# Removes all blank lines.

# To select all utilities, use imdocselect . options.xml > all.xml

${SED} "
       /<gm>/d
       /<\/gm>/d
       s/    composite/    gm composite/
       s/    convert/    gm convert/
       s/    animate/    gm animate/
       s/    display/    gm display/
       s/    conjure/    gm conjure/
       s/    montage/    gm montage/
       s/    mogrify/    gm mogrify/
       s/    identify/    gm identify/
       s/    import/    gm import/
       " $3 | \
              \
              \
${SED} "
     : begin

       /<imdoc>/d
       /<\/imdoc>/d
       /<im>/d
       /<\/im>/d

       /^ *$/d

       /<.utils>/d
       s/<utils/<UTIL/
       t dummy

     : dummy
       s/<UTIL/<utils GraphicsMagick/
       t inutil

       s/<gm//
       t skipgm

       /<.format>/d
       s/<format/<FORMAT/
       t dummy1

     : dummy1

       s/<FORMAT/<format/
       t inform
       b

     : inutil
       s/<$1/<u/
       t
       s/$1/found/
       t select
       s/<utils/</
       t skip

     : skip
       s/<.utils>//
       t select
       s/<utils/<UTIL/
       t inutil
       s/.*/!DELETE!/
       n
       b skip

     : skipgm
       s/<.gm>//
       t select
       s/.*/!DELETE!/
       n
       b skipgm

     : inform
       s/$2/found/
       t select
       s/.*/!DELETE!/
       t skipf

     : skipf
       s/<.format>//
       t select
       s/<format/<FORMAT/
       t inform
       s/.*/!DELETE!/
       n
       b skipf

     : select
       d
     " |         \
                    \
${SED} "/!DELETE!/d"
