#! /bin/csh -f
# Last edited on 2004-02-04 05:25:37 by stolfi

# A filter to be run on the main.raw to turn it into 
# the starting version of main.org

sed \
    -e '1,/# BEGIN TEXT/d' \
    -e '/# END TEXT/,$d' \
  | gawk \
     ' /[\#] *[=][=][=].*[.]htm/ { next; } \
       /[@](part|chapter)/ { print; next; } \
       /[@](fix)/ { gsub(/@/, "<ATSIGN>"); print; next; } \
       /[@]verse/ { \
         $0 = gensub(/[ ]+[&][\#]1508[;] *$/, "", "s", $0); \
         $0 = gensub(/{([0-9]+)}{([A-Z:0-9]+)} */, "{\\1}{\\2}\n    ", "g", $0); \
         print; next; \
       } \
       // { print; next; } \
     ' \
  | html-to-hexbytes \
  | hexbytes-to-jshb \
  | gawk \
     ' /./ { gsub(/[_ ]+$/, ""); } \
       /^ *<ATSIGN>/ { gsub(/<ATSIGN>/, "@"); } \
       /^ *([\#]|$)/ { print; next; } \
       /[@]fix / { gsub(/[@]fix +/, "#     "); print; next; } \
       /[@](part|chapter|[=])/ { print; next; } \
       /[@]verse/ { \
         $0 = gensub(/^ *@verse{([0-9]+)}{([A-Z:0-9]+)} */, \\
           "@unit V \\1 verse\n  \\2", \\
           "g", $0); \
         print; next; \
       } \
       /^ *([\#@]|$)/ { print; next; } \
       /./ { gsub(/[_ ]+/, " "); gsub(/^[ ]/, "    "); print; next; } \
     ' \
  | egrep -v '^ *$'