#!/bin/bash
: <<END_COMMENT


The Relax NG schema 5.x is stricter than the 4.x DTD. In our documentation
- especially when describing commands and functions - there are some
constructions which violates the new syntax.

To address such situations this script modifies indiviual parts of many files.
It is the critical part of the DocBook 4.x --> 5.x conversion.

It uses Perl to have the possibility to use negative lookaheads, which
isn't supported by every sed implementation. Example: 's/some(?!xyz)/_some_/g'

Within regular expressions, we soleyly use the names of DocBook objects
like tags and attributes plus the names of PG functions and commands. As far
as such names are not modified in a translation to other languages like
Russian or Spanish, it shut be possible to use the script also on such
translated sgml files.


END_COMMENT

# ------------------------------------
# -------  parameter handling --------
# ------------------------------------
if [[ $# -ne 1 ]]; then
  echo
  echo Usage: $0 NewSgmlDirectory
  echo Abort.
  echo
  exit 2
fi

cd $1
if [[ $? -ne 0 ]]; then
  echo
  echo sgml directory not found
  echo Abort.
  echo
  exit 2
fi


  # -------------
  # <literal>
  # -------------

  # <literal>..<function> is not valid and doesn't have any effect. Remove <literal>
  for file in datatype.sgml func.sgml plperl.sgml plpython.sgml pltcl.sgml   \
              ref/create_table.sgml ; do
    perl -i -p -e 's/<literal>\s*(<function>.*<\/function>.*)<\/literal>/\1/' $file 
  done

  # plpython.sgml: function name is extend with 'plpy.'. Remove <literal>
  perl -i -p -e 's/<term><literal>plpy\.(<function>)(.*)(<\/function>.*)<\/literal>/<term>\1plpy.\2\3/' plpython.sgml

  # Remove <literal>
  perl -i -p -e 's/<literal>(<function>pg_notify)/\1/;s/<\/literal>(. The function)/\1/' ref/notify.sgml


  #  <literal>..<command> is not valid. remove <literal>
  perl -i -p -e 's/<literal>(<command>semodule)(<\/command>) -l<\/literal>/\1 -l\2/' sepgsql.sgml

  # <literal><option> is not valid. remove <literal>
  perl -i -p -e 's/<literal>(<option>.*<\/option>)<\/literal>/\1/' libpq.sgml

  # <literal><parameter>...</parameter>...</literal> is not valid. Separate <literal> and <parameter>
  perl -i -p -e 's/<literal>(<parameter>.*<\/parameter>)(.*<\/literal>)/\1<literal>\2/' func.sgml

  # Replace <literal> by <phrase>
  perl -i -p -e 's/<literal>(sum\(.*)<\/literal>/<phrase>\1<\/phrase>/' func.sgml
  perl -i -p -e 's/<literal>(\$\.<parameter>.*<\/parameter>)<\/literal>/<phrase>\1<\/phrase>/' func.sgml
  # same problem, but multiple lines
  perl -i -p -e 's/<literal>((substring|position)\(.*?<parameter>)/<phrase>\1/' func.sgml
  perl -i -p -e 's/(<\/parameter>\))<\/literal>/\1<\/phrase>/' func.sgml
  # same problem, but multiple lines
  perl -i -p -e 's/<literal>(<parameter>)/<phrase>\1/' func.sgml
  perl -i -p -e 's/(<\/parameter> <\/optional>)<\/literal>/\1<\/phrase>/' func.sgml


  # very special
  perl -i -p -e 's/<literal>(\*<parameter>n<\/parameter>\$)<\/literal>/<phrase>\1<\/phrase>/' func.sgml
  perl -i -p -e 's/<literal>(<function>timezone<\/function>)/<phrase>\1/' func.sgml
  perl -i -p -e 's/(timestamp<\/replaceable>\))<\/literal>/\1<\/phrase>/' func.sgml


  #  <literal>..<literal> is not valid   -->   remove inner <literal>
  perl -i -p -e 's/<literal>(from|to)<\/literal>/\1/' ref/psql-ref.sgml

  #  <lineannotation>..<literal> is not valid   -->   <lineannotation>..<phrase>
  for file in datatype.sgml func.sgml ltree.sgml; do
    perl -i -p -e 's/(lineannotation>.*)<literal>(.*)<\/literal>/\1<phrase>\2<\/phrase>/' $file
    if [[ $file =~ "func.sgml" ]]; then
      perl -i -p -e 's/(lineannotation>.*)<function>(.*)<\/function>/\1<phrase>\2<\/phrase>/' $file
    fi
  done

  #  <option>..<literal> is not valid   -->   <option>..<phrase>
  for file in ref/createdb.sgml ref/initdb.sgml; do
    perl -i -p -e 's/<literal>libc<\/literal>\|<literal>icu<\/literal>/<phrase>libc<\/phrase>\|<phrase>icu<\/phrase>/'  \
         $file
  done
  perl -i -p -e 's/<option><literal>(.*)<\/literal><\/option>/<option><phrase>\1<\/phrase><\/option>/' installation.sgml

  # <term><literal>..<parameter> is not valid   -->   remove <literal>
  perl -i -p -e 's/(<term>)<literal>(.*<parameter>.*<\/parameter>)<\/literal>(<\/term>)/\1\2\3/' spi.sgml
  perl -i -p -e 's/(<term>)<literal>(.*)<\/literal>(<\/term>)/\1\2\3/' textsearch.sgml

  # <term>\n<literal>..<type> is not valid   -->   change <literal> to <phrase>
  perl -i -p -e 's/<literal>((querytree|ts_rewrite|numnode|tsquery_phrase|strip|length|setweight|ts_rank).*)<\/literal>/<phrase>\1<\/phrase>/' textsearch.sgml
  perl -i -p -e 's/<literal>(<type>.*<\/type>)<\/literal>/<phrase>\1<\/phrase>/' textsearch.sgml
  perl -i -p -e 's/<literal>(\!\!.*)<\/literal>/<phrase>\1<\/phrase>/' textsearch.sgml


  # -------------
  # <optional>
  # -------------
  for file in unaccent.sgml tablefunc.sgml pgcrypto.sgml adminpack.sgml datetime.sgml bki.sgml protocol.sgml   \
              ltree.sgml logicaldecoding.sgml ecpg.sgml lobj.sgml installation.sgml textsearch.sgml func.sgml  \
              ref/alter_function.sgml ref/alter_procedure.sgml ref/clusterdb.sgml ref/create_function.sgml     \
              ref/create_procedure.sgml ref/create_table.sgml ref/pgbench.sgml ref/psql-ref.sgml               \
              ref/reindexdb.sgml ref/select.sgml ref/vacuumdb.sgml
  do
    # <optional>..(<type>|<literal>|<...and some more...) is not valid. Use same syntax as
    # in many files of our /ref directory: '[]'
    perl -i -p -e 's/<optional>/[/g;s/<\/optional>/]/g' $file
  done


  # -------------
  # <function>
  # -------------

  # <function>..(<type>|<parameter>) is not valid  -->  </function> before (<type>|<parameter>)
  for file in bgworker.sgml; do  # <function> and </function> are at different lines
    perl -i -p -e 's/(<function>.*?)(<type>|<parameter>)/\1<\/function>\2/' $file
    perl -i -p -e 's/(<\/type>.*?|<\/parameter>.*?)<\/function>/\1/' $file
  done

  for file in xaggr.sgml datatype.sgml; do  # <function> and </function> are in one line
    perl -i -p -e 's/(<function>.*?)((<type>|<parameter>).*(<\/type>|<\/parameter>).*)<\/function>/\1<\/function>\2/' $file
  done


  # ---------
  # others
  # ---------
  perl -i -p -e 's/(<literal>)(<varname remap="structname">pg_type<\/varname>)(.oid<\/literal>)/\2\1\3/' btree.sgml
  perl -i -p -e 's/<literal>(a@|foo\*)<\/literal>/<phrase>\1<\/phrase>/' ltree.sgml


