#!/bin/bash

# Description:
#   This script is designed to update a text file based 
#   apon text entered in an HTML form. It can be used in conjuction
#   with the script 'text2html-collab.sh' This script creates the
#   necessary HTML forms and links which will point to the current cgi-script
#   
#   This script has been based directly on the 'add-comment' cgi script
#   and the 'poseidon-text2html-forum.sh'
#
# Notes:
#   The web server must have 'write' permission to the file inorder
#   for this script to achieve anything. If the web-server does not
#   have 'write' permission the script does not complain but the 
#   target file is not updated.
#
#   Some HTML textareas, notably Netscape, return strings with neither
#   unix nor DOS line endings, but just \r characters. For this we will just
#   use a simple sed conversion.
#
# Bugs and Ideas:
#
#   Many browsers do not refresh the web page when they should
# Dependencies:
#   text2html-collab.sh
#   iso2html.sed (used by the above script)
#   procgi
#      This is a c program which extracts submitted HTML form values from the
#      posted or 'getted' data sent by the web-visitors browser. This program
#      was found somewhere on the Internet.
# See Also:
#   plaintext2html.sh,
#     Converts a plain text style document into HTML with links and a table of contents
#   plaintext2pdf.sh, 
#     Converts the same type of document into PDF using the 'htmldoc' program
#   plaintext2html-forum.sh
#     Converts a document as above but allows the web-visitor to make contributions
#     via a CGI script called 'add-comment'
#   linkdoc2html.sh
#     Converts a text document which consists of a list of URLs and descriptions into
#     a hyperlinked HTML document
#   diary2html.sh
#     Converts a 'diary style' plain text document into HTML. A diary is just a
#     series of dates and description for those dates
#   linkdoc2html-index.sh, 
#   linkdoc2html-forum.sh
#     Similar to the 'plaintext2html-forum.sh' script but operates on 'linkdocs' as
#     described above.
#   add-comment
#     A CGI script which adds a user supplied comment to a text file and regenerates an
#     HTML file using one of the '2html' scripts described here. The script uses a
#     utility called 'procgi' or 'proccgi' which extracts submitted HTML form values.
# Author:
#    mjb
# Bugs:
#    This script will probably currently fail for strangely named text files. In particular
#    text files which follow the HTML internationalizing naming convention (for use with
#    the 'language negotiation' feature, or 'content negotiation' feature of web-servers
#    and web-browsers) such as 'index.html.en'. This script will probably mangle the
#    name of the generated HTML file.
#
#    If at any stage the form values are passed around 'unprotected', that is, unenclosed
#    in double quotes, then bash shell special characters will get 'expanded'. For example
#    the * character will be 'globbed', which means expanded to the listing of the current
#    directory. This means that the files will suddenly have directory listings inserted in
#    them, which is not at all good
#
# The line below parses the querystring or posted environment 
# variables. It uses a program which was found at.
#   http://www.fpx.de/fp/Software/ProcCGIsh.html
# 

#-- The eval is expanding * characters as directory listings, according to the bash
#-- shell. This needs to be stopped somehow
#eval "`./procgi $* | sed 's/\*/{{:star:}}/g'`"
sPostedString=$(/home/cgi-bin/procgi $* | sed 's/\*/{{:star:}}/g')
  echo "Content-type: text/html"
  echo ""
 
  # This second empty echo line IS necessary. A 'server error' will
  # be generated by the web-server if it is not present.

  #-- To see exactly what the procgi program is passing to this script you can
  #-- use the following line
  #/home/cgi-bin/procgi $*
  #echo $sPostedString
  #echo "<hr>"
  eval "$sPostedString"

  # echo "A script to edit a text file"
  sFileName="$FORM_filename"
  sDocumentText=$(echo "$FORM_DocumentText" | sed 's/{{:star:}}/\*/g')
  #sUserName=$FORM_username
  sSourceDocumentType=$FORM_documenttype
  sDebug=$FORM_debug 

  #-- used for debugging
  if [ "y" = "x" ]
  then
    echo "
      <pre>
	REQUEST_METHOD=$REQUEST_METHOD
	HTTP_REFERER=$HTTP_REFERER
	SERVER_NAME=$SERVER_NAME
	sFileName=$sFileName
	sDocumentText=$sDocumentText
	sUserName=$sUserName
	sDebug=$sDebug
      </pre>
         "
     exit 1;
  fi
  
  if [ "$sFileName" = "" ] 
  then
      echo "<html>"
      echo "
       <strong><center>
	No file name was specified for the script $0. 
	This is an error. For more information please contact matthew@ella-associates.org  
	The document has not been altered.
       </center></strong>"
      echo "<hr>"
      echo "</html>"
    exit 1;   
  fi

  sHtmlFileName=$(echo $sFileName | sed "s/\.[^\.]*$//gi")".html"

  #-- Check that the directory is writable. If not the text file will be 'truncated'
  #-- by this script and will only contain the comment just added and nothing else.
  #-- This is a very bad outcome and must be avoided at all costs.
  if [ -w $(dirname $sFileName) ]
  then 
    :
  else
    sScriptResultMessage="<strong><center> \
      The directory in which the source file resides is not 'writable' by. \
      the Web Server. You will have to ask the system administrator to make this \
      directory writable in order for this edit script to work \
      The document has not been altered.  </center></strong><hr>"

    sScriptResultMessage="<strong><center> \
      El directorio en que el archivo est\&aacute; situado no puede ser escrito \
      por el servidor Web. Necesitas pedir del administrador del sistema para que \
      pueda arreglar esta situaci\&oacute;n \
      </center></strong><hr>"
     
    cat $sHtmlFileName | sed "s|<body>|<body>$sScriptResultMessage|g"
    exit 1;  
  fi
  
  #-- Check that the file is writable.
  if [ -w $sFileName ]
  then 
    :
  else
    sScriptResultMessage="<strong> \
      El archivo no puede ser escrito por el servidor Web. \
      Es preciso pedir del administrador del sistema que arregle esta \
      situaci\&oacute;n. El documento no ha sido cambiado <\/strong><hr>"
     
    #sScriptResultMessage="<strong> \
    #  The source file is not 'writable' by the Web Server. \
    #  You will have to ask the system administrator to make this \
    #  file writable in order for this document edit script to work \
    #  The document has not been altered.  <\/strong><hr>"
     
    cat $sHtmlFileName | sed "s|<body>|<body>$sScriptResultMessage|g"
    exit 1;  
  fi
  
  #-- Check that the html file is writable.
  if [ -w $sHtmlFileName ]
  then 
    :
  else
    sScriptResultMessage="<strong> \
      El archivo HTML no puede ser escrito por el servidor Web. \
      Es preciso pedir del administrador del sistema que arregla esta \
      situaci\&oacute;n. El documento no ha sido cambiado</strong><hr>"

      #The HTML file is not 'writable' by the Web Server.  
      #You will have to ask the system administrator to make this \
      #file writable in order for this document edit script to work \
      #The document has not been altered. </strong><hr>"
     
      cat $sHtmlFileName | sed "s|<body>|<body>$sScriptResultMessage|g"
    exit 1;  
  fi
  
  if [ "$sDocumentText" = "" ] 
  then
      sScriptResultMessage="<strong> \
        El texto del documento ha sido borrado. Tal vez, no es un buen idea. El documento \
	no ha sido cambiado </strong><hr>"
	
        #The text of the document has been obliterated. This is probably not a good idea \
	#The document has not been altered.  </strong><hr>"
       
      cat $sHtmlFileName | sed "s|<body>|<body>$sScriptResultMessage|g"
    exit 1;  
  else
    #-- We need to get rid of the MS Line Ending format, if the 
    #-- text was entered on an MS Windows computer.
    #echo "sDocumentText=$sDocumentText"
    #sDocumentText=$(echo $sDocumentText | tr '\r' '\n')
    #-- The date string below gives a full text date, with weekday and AM/PM time
    #-- The code below attempts to add the last comment immediately under the 
    #-- comment tag.
    
    cp $sFileName $sFileName.previous
    #-- Preserve the old version of the file, just in case something goes terribly
    #-- wrong. For example, if the web-server does not have permission to write to 
    #-- the directory in the which the text file resides, then the 'cp' command wont 
    #-- work, and nor will the lines below. Back to the drawing board.

    datetag=$(date "+%d%B%Y-%I-%M%p")
    cp $sFileName $sFileName.$datetag

    # We need to convert from iso whatever into UTF-8 
    # echo "last modified on $(date '+%A, %d %B %Y, %I:%M %p')"
      #-- We are going to need an actual text file for the transformation script, so...
    #echo "$sDocumentText" | tr '\r' '\n' | \
    #-- The dodgy line below removes \r characters which are at the ends of lines
    #-- This seems different from the situation on Debian. If I use the line above
    #-- the file gets double spaced. Sed doesn't seem to be able to do s/\r$//g at
    #-- least my version of sed doesnt do it.
    #-- sed "l" is useful because it displays all invisible characters
    #-- However the very last line of the file does not have this \r character
    #-- which means that the last character of the file in getting truncated. The 
    #-- $! below I thought would have solved this but it didn't. Possible because
    #-- the file contains blank lines at the end
    echo "$sDocumentText" |  \
      iconv --from-code=ISO-8859-1 --to-code=UTF-8 | sed "$!s/.$//g"  > $sFileName

    #-- For debugging 
    if [ "x" = "y" ]
    then
      echo "<pre>"
      echo "$sDocumentText" | sed "l" 
      echo "</pre>"
    fi
    echo
  fi

  if [ "$sUserName" = "" ]
  then
    sUserName="[anonymous]"
  fi
 
 
  #-- The date string below gives a full text date, with weekday and AM/PM time
  #-- The code below attempts to add the last comment immediately under the 
  #-- comment tag.
  
  cp $sFileName $sFileName.previous
  #-- Preserve the old version of the file, just in case something goes terribly
  #-- wrong. For example, if the web-server does not have permission to write to 
  #-- the directory in the which the text file resides, then the 'cp' command wont 
  #-- work, and nor will the lines below. Back to the drawing board.

  datetag=$(date "+%d%B%Y-%I-%M%p")
  cp $sFileName $sFileName.$datetag

  # echo "last modified on $(date '+%A, %d %B %Y, %I:%M %p')"

  # The web server needs write permissions on the directory. 
  # echo "sHtmlFileName=$sHtmlFileName"
  if [ "$sSourceDocumentType" = "wiki" ]
  then
    /usr/local/bin/text2html-collab.sh $sFileName notran > $sHtmlFileName
  elif [ "$sSourceDocumentType" = "plaintext" ]
  then
    /usr/local/bin/plaintext2html-forum.sh $sFileName > $sHtmlFileName
  else
    /usr/local/bin/plaintext2html-forum.sh $sFileName > $sHtmlFileName
  fi
  
  #-- Watch out for special 'sed' characters in the sScriptResultMessage variable 
  #-- The translation link wont work from with the cgi directory

  #cat $sHtmlFileName
  sScriptResultMessage=" \
   <strong> El documento ha sido puesto al d&iacute;a. Dentro de 15 segundos \
   vas a ser traido automaticamente atr&aacute;s. Tal vez, los cambios tu \
   has hecho no van a aparecer en el p&aacute;gina web hasta que tu \
   pulses el boton 'actualizar' en tu hojeador. Incluso, en algunos hojeadores \
   tu necesitas cerrar y abrir tu hojeador para que tu puedas ver los cambios \
   (perdon por este rollo) \
   Gracias </strong><br><br> \
   <em>  Your changes have been included in this document, thankyou. You will be \
   taken back to the page in 15 seconds. In some cases you will not see the changes which \
   you have just made until you press the 'refresh' button in your browser. With \
   some browsers (notably Microsoft Internet Explorer) you actually have to \
   close and open the browser in order to see the changes (sorry)</em><hr>"

  echo "
   <html>
   <head>
   <meta http-equiv="Refresh"
   content=\"15;url=$HTTP_REFERER\">
   </head>
   <body>
   $sScriptResultMessage
   </body></html>"
  #sed "s|<body>|<body>$sScriptResultMessage|g" $sHtmlFileName 
  #sed "s|Vea este pagina en (aproximado):||g" | \ 
  #sed "s|>Ingl&eacute\;s<|><|g"