#!/bin/sh 

#<TODO> 2009-02-24
# Update the contact name Chelton Evans to point to a web page.
# Problem:  
# ./p1/misc/proj/zpr/doc.html -> ../../../../doc02.html
# So replace between /xx/ with /../ and remove ./ at start and end html file reference.
# This is part of general path manipulation, so have generic functions.
#
# Hack:  strip start and end
#       p1/misc/proj/zpr
#        replace / with space
#      "p1 misc proj zpr"
#        iterate through list, appending ../ to a string initially blank 
#
# A little more generic, write a function given a path returns what it takes
#  to get back to the base directory. so ../../../.. returnned in prev example.




source ${base}/p1/comsci/sh/chel.sh

# This script updates the modified time field as displayed in the html code.
# It reads in the modified time from the file. The html file has the following
# syntax.
#
#<!--UPDATE_DATE_MODIFIED-->
#<!--UPDATE_DATE_BEGIN-->
#2007-01-01
#<br/>
#<a href="mailto:chelton.evans@yahoo.com">Chelton Evans</a>
#<!--UPDATE_DATE_END-->
# So that this program is not destructive to the files original time stamp
# the timestamp is directly overwritten back to its modified time before
# the script changed the file.

# Given an extension search for these files excluding the /html directory.
# If the file has the tags then the modified file time is written to the tags.
function updatemodifiedtimestamp
{
  local i
  local k="find . -name \*.${1} | grep -v /html"
  local s
  #echo ${k}  
  for i in `eval ${k}`; do
    #echo ${i}
    if grep -q "\-\-UPDATE_DATE_MODIFIED\-\-" ${i}; then
#echo i=${i}

      # Write the file's international date to a file.
      echo '<!--UPDATE_DATE_BEGIN-->' > /tmp/dateref.txt
      ls --full-time -G -g ${i} | awk '{print $4}' >> /tmp/dateref.txt

      # Add the mail href too.
      echo \<br/\>  >> /tmp/dateref.txt
      echo \<a href=\"mailto:chelton.evans@yahoo.com\"\>Chelton Evans\</a\>  >> /tmp/dateref.txt

      echo '<!--UPDATE_DATE_END-->' >> /tmp/dateref.txt

      # Delete the old date between and including the markers.
      # Insert the new date.
      sed -e '/--UPDATE_DATE_BEGIN--/,/--UPDATE_DATE_END--/d' -e '/--UPDATE_DATE_MODIFIED--/r /tmp/dateref.txt' ${i} > /tmp/tempfile.txt 

      # Extract the timestamp in a format suitable for the touch command.
      # The original modification time needs to remain the same as its message is when
      # was the file last edited by someone else and not this program.
      ls --full-time -G -g ${i} | awk '{print $4}' | awk -F- '{print $1$2$3"0000" }' > /tmp/dateref2.txt
      filetostring /tmp/dateref2.txt
      s="touch -m -t ${filetostringval} ${i}"
#echo i=${i}
#echo s=${s}

      # Commit
      mv ${i} /tmp/ 
      mv /tmp/tempfile.txt ${i}
      eval ${s}

    fi;
  done
}

updatemodifiedtimestamp xml
updatemodifiedtimestamp html




