#!/bin/bash

# Description:
#   This script is designed to modify the details for a band as contained in the 
#   page http://www.ella-associates.org/grapevine/grapevine.htm
# 
# Notes:
#   The web server must have 'write' permission to the file inorder
#   for this script to achieve anything. If the web-server does not
#   has 'write' permission the script does not complain but the 
#   target file is not updated.
#
# Ideas:
#   make the date a better format, check to see if the file
#   is writable, etc. 
# Dependencies:
#   plaintext2html-forum.sh
#
# See Also:
#   There are some cgi scripts which are similar to this
#   add-comment, check-hotmail
# Author:
#    mjb


# 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
# 
eval "`./procgi $*`"

  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.

  # echo "A script to add something to a text file"
  sBandName=$FORM_bandname
  sBandDescription=$FORM_banddescription
  sPassword=$FORM_password
  sUserName=$FORM_username
  sDebug=$FORM_debug 
  sTargetFileName=$(\
    echo $HTTP_REFERER | \
    sed "s@http://$HTTP_HOST/@$DOCUMENT_ROOT/@g")
  sDebug="x"

  sResultPageTemplate=$(echo "<html>
   <head><title>The Results of the Band Details Alteration Request</title></head> 
   <body> 
   </body>
   </html>")
    
  if [ "$sDebug" != "" ]
  then
    echo "
      <pre>
        The variables received by this cgi script were
	sBandName=$sBandName
	sBandDescription=$sBandDescription
	sUserName=$sUserName
	sPassword=$sPassword
	sTargetFileName=$sTargetFileName
      </pre>
         "
  fi
  
  if [ "$sBandName" = "" ] 
  then
    #-- Another 'gotcha'. because the string sScriptResultMessage is to be used in
    #-- the sed command, it cannot contain / characters. This may be obvious to some
    #-- Another gotcha: the following sed snippet -e "s#\\#\\\\#g" in my humble opinion
    #-- should actually work but doesn't because the sed engine (gnu I think) seem to
    #-- apply the escaping back-slash to the # character rather than to the other \
    #-- character. Actually this is a quoting problem not a sed problem.
    #-- I cant get this escaping of escape characters to work and frankly I dont care
      #sSafeFileName=$(echo $sFileName | sed -e 's#/#\\\/#g' -e 's#\\#\\\\#g')
      #sSafeFileName=$(echo $sFileName | sed -e 's#/#\\\/#g' )
      sScriptResultMessage="<strong><center> \
        No band-name was specified in the Form which you just filled in. \
	The details have not been altered.  <\/center><\/strong><hr> \
        <center><a href = \"$HTTP_REFERER\">Click here to try again</a> [or click the 'back' button]</center>" 
       
      #"/cgi-bin/update-band-details">
      echo $sResultPageTemplate | sed "s@<body>@<body>$sScriptResultMessage@g"
      #cat $sTargetFileName | \
      #  sed "s@\"/cgi-bin/update-band-details\">@\"/cgi-bin/update-band-details\">$sScriptResultMessage@g"

    exit 1;  
  fi
  
  if [ "$sBandDescription" = "" ] 
  then
      #sSafeFileName=$(echo $sFileName | sed -e 's#/#\\\/#g' -e 's#\\#\\\\#g')
      #sSafeFileName=$(echo $sFileName | sed -e 's#/#\\\/#g' )
      sScriptResultMessage="<strong><center> \
        No band-description was specified in the Form which you just filled in. \
	The details have not been altered.  <\/center><\/strong><hr> \
        <center><a href = \"$HTTP_REFERER\">Click here to try again</a> [or click the 'back' button]</center>" 
       
      echo $sResultPageTemplate | sed "s@<body>@<body>$sScriptResultMessage@g"
      #cat $sTargetFileName | \
      #  sed "s@\"/cgi-bin/update-band-details\">@\"/cgi-bin/update-band-details\">$sScriptResultMessage@g"

    exit 1;  
  fi

  #sTargetFileName=$(echo $sFileName | sed "s/\.[^\.]*$//gi")".html"
  if [ "$sPassword" != "ella" ] 
  then
    echo "<html>
     <head><title>The Results of the Band Details Alteration Request</title></head> 
      <strong><center> \
        The password which you entered in the Form is not correct. If you are a member 
	of one of the bands mentioned on this page please contact Josephine or Robert 
	Grundy in order to obtain a password. You can contact them at the following email  
	addresses 
	<br> josephine@findesign.org 
	<br> nick@findesign.org 
	<br> robert_grundy@ella-associates.com 
	The details have not been altered.  </center></strong><hr> 
        <center><a href = \"$HTTP_REFERER\">Click here to try again</a> [or click the 'back' button]</center>" 
     echo "</body></html>"  
     # echo $sResultPageTemplate | sed "s/\@/{:at:}/g" | sed "s@<body>@<body>$sScriptResultMessage@g" 2>&1

    exit 1;  
  else
    #-- We need to get rid of the MS Line Ending format, if the 
    #-- text was entered on an MS Windows computer.
    #-- finding the -a switch for fromdos took some time.
    #-- For some reason the 'fromdos' command gets rid of all
    #-- line breaks. It is possible that the 'proccgi' program is 
    #-- doing something unpleasant to the FORM values
    #-- Actually it appears that the line endings as interpreted by the proccgi
    #-- program are just \r characters.
    #-- Try tr '\r' '\n'
    sComment=$(echo $sComment | tr '\r' '\n')
    
    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.
 #-- The lines below turn a literal string into a regular expression which will match
 #-- against similar strings.

name="Vilna kabaret"; 
name=$(echo $name | \
  tr -s ' ' | \
  sed -e "s/ /\[\[:space:\]\]\*/g" -e "s/^/\[\[:space:\]\]\*/g" -e "s/$/\[\[:space:\]\]\*/g");
 (echo $name; \
 cat grapevine-new.htm | \
  sed -n "/<span[[:space:]]\+class=\"bands\">$name/I,/<\/td>/I p")
  
  cp $sFileName $sFileName.temp
  (sed -n "1,/^[ ]*COMMENTS/p" $sFileName.temp; \
  echo "
  Added by: $sUserName, on $(date '+%A, %d %B %Y, %I:%M %p')

$sComment "; \
  sed "1,/^[ ]*COMMENTS/d" $sFileName.temp; ) > $sFileName 
  rm -f $sFileName.temp

   sScriptResultMessage="<strong><center> \
        Your bands details have been altered, thankyou $sUserName. \
        <\/center><\/strong><hr>"
       
   cat $sTargetFileName | sed "s/<body>/<body>$sScriptResultMessage/g"
