#!/bin/bash

#### v8.AUTO mod to explicitly cd to /usr/local/apache2213/htdocs/skyprod.net/pub/RSS/production-feed2 for use as a user cron job

now="$(date)"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Start of processing $0"
echo "$now"

pwd
cd /usr/local/apache2213/htdocs/skyprod.net/pub/RSS/production-feed2
pwd

# Clean up  
rm production-feed2.xml.tmp*
rm check-stdout.txt
rm check-stderr.txt

# Get the date and format it for RSS <pubdate> tag.
# Get the time in ms.
# Use sed to replace variables in template file. 
#
# Michael Cook mcook2@mymts.net 2011-11-16.

# Get the date
now="$(date)"

# Get the time in milliseconds - actually redundant here
nowms=$( perl -MTime::HiRes -e 'print int(1000 *
Time::HiRes::gettimeofday),"\n"' )

# Show both 
echo "$now"
echo "$nowms"

comma=","
echo "$comma"

# Set up pubdate substitution in the correct format "Wed, etc.", in four steps
#	get the weekday
datepart1=`echo $now | sed 's/\// /g' | awk '{print $1}'`
echo $datepart1
#	get a comma
datepart2=`echo $comma` 
echo $datepart2

#### mcook FIX 2011-12-22 - START
#	get the rest of the date
#
#	- x64 returns Thu 22 Dec 2011 05:35:26 CST which is correct 
#       - in the sequence required by RFC-822 x64:   $1  $2 $3  $4   $5       $6
#                                                    Thu 22 Dec 2011 05:35:26 CST
datepart3=`echo $now | sed 's/\// /g' | awk '{print " " $2 " " $3 " " $4 " " $5 " " $6}'`
#
#	- PPC returns Thu Dec 22 CST hh:mm:ss 2011 
#       - in the sequence required by RFC-822 PPC:   $1  $2  $3  $6   $4       $5
#                                                    Thu Dec 22 CST  05:35:26 2011
datepart3=`echo $now | sed 's/\// /g' | awk '{print " " $3 " " $2 " " $6 " " $4 " " $5}'`
### mcook FIX 2011-12-22 - END

echo $datepart3

#	concatenate the results
newRSSdate=`echo $datepart1$datepart2$datepart3`
echo "This is the computed RFC-822 pubDate->$newRSSdate"

# Template file name
#dateforRSS.production-feed2.template.xml

# Various work files

# Final output file name
#production-feed2.xml

# Template strings to replace ..
#
#      <pubDate>***RSSpubdate****</pubDate>
#      <guid isPermaLink="false">****RSSpermalink******</guid>
#
# Invoke sed for each template variable to be replaced creating .tmp file 
#
# First REPLACE ALL ====RSSpubdate==== with newRSSdate
#
#echo "run stat before the 1st sed"
#stat dateforRSS.production-feed2.template.xml
#stat production-feed2.xml.tmp
sed "s/====RSSpubdate====/${newRSSdate}/g" dateforRSS.production-feed2.template.xml > production-feed2.xml.tmp
#echo "run stat after the 1st sed"
#stat production-feed2.xml.tmp

# I spent ages trying to get this to work 'til I understood what sed is doing

    #
    # HOW TO CHANGE THE FIRST OCCURRENCE OF A STRING IN A FILE USING SED
    #
    # Code to insert a unique timestamp for each tag like this ..
    #
    #  <guid isPermaLink="false">====RSSpermalink====</guid>
    #
    #           ..  in a home grown RSS template file containing several items
    #

    # Now count instances of "RSSpermalink"
    count=`grep -o RSSpermalink production-feed2.xml.tmp | wc -w` 
 
    # Set up a counter to control the loop
    counter=0
    echo "$counter"

    # Make a working copy of the template file

    cp production-feed2.xml.tmp production-feed2.xml.tmp2
    stat production-feed2.xml.tmp2

    #
    # Now go through the file for each occurrence of "RSSpermalink" and replace it with
    #       the current time in milliseconds

    while [ $counter -lt $count ]; do

        let counter=counter+1
        echo "$counter"

        cp production-feed2.xml.tmp2 production-feed2.xml.tmp2.$counter

        # Get time in milliseconds to be used as the unique value for the RSS permalink
        # (it has to be unique for each item within an RSS feed) 

        nowms=$( perl -MTime::HiRes -e 'print int(1000 *
        Time::HiRes::gettimeofday),"\n"' )


        # THE FOLLOWING LINE WOULD REPLACE ALL OCCURRENCES 
        #sed "s/====RSSpermalink====/${nowms}/g" production-feed2.xml.tmp2 > \
	#	production-feed2.xml.tmp.$counter

        # THIS REPLACES THE FIRST OCCURRENCE THEN STOPS

        # At last - the working sed statement to replace only the FIRST occurrence
        #       of "RSSpermalink" in each new generation of the work file

        sed "1,/====RSSpermalink====/s/====RSSpermalink====/${nowms}/" \
		production-feed2.xml.tmp2 > production-feed2.xml.tmp.$counter

        echo "$nowms"
        cp production-feed2.xml.tmp.$counter production-feed2.xml.tmp2
#    	stat production-feed2.xml.tmp2
#	stat production-feed2.xml.tmp.$counter
        echo "$counter"
    done

# Copy the results 
cp production-feed2.xml.tmp2 production-feed2.xml
#echo "run stat after all processing"
#stat production-feed2.xml

now="$(date)"
echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<End of processing $0"
echo "$now"

# Clean up  
#rm production-feed2.xml.tmp*

