#!/bin/csh -f
#
# Generate the tv.html web page from the tv file.
# This is a manually-updated replacement for the
# replaytv_contents.html page that was generated
# from the My.ReplayTV.com website's information.
#
# Brent Chivers 2008/Apr/19-20
##! /bin/csh -fb # for setuid/setgid scripts (tsk)
# set verbose # see commands before variable substitution
# set echo # see commands after variable substitution
unset histchars # get other users' history characters out of the way
unalias * # get other users' .cshrc stuff out of the way
## set nonomatch # don't require wildcard matches
# $0:t construction doesn't work!
set cmd_name = $0
set cmd_name = $cmd_name:t # the name of this command
cd $home/personal/tv # where the files are kept
set ts = `/bin/ls -lT tv` # when was source file last updated?
if (! -f tv.prev) touch tv.prev # create an empty previous file
/usr/bin/diff tv.prev tv | /usr/bin/egrep '<|>' > tv.diff.$ts[9].$ts[6].$ts[7].$ts[8]
if ($status) then # the tv file has changed
/bin/cat << END_1 # first part of web page
ReplayTV Contents
Our ReplayTV Contents
END_1
# add source file's timestamp to web page
echo -n 'Web Page Generated '"`/bin/date +' %R %A %e %B %Y'`"''
echo -n ' Last Data Update: '"$ts[8] $ts[7] $ts[6] $ts[9]"''
/bin/cat << END_2 # next static part of web page: headings for program table
Notes:
P: Preserved
S: Seen
D: Delete
K: Keep
Recorded Programs
Program
Episode
Date, Time
Notes
END_2
# turn tv-file lines into table rows
# if show doesn't list episode, span the program name over 2 colums
/usr/bin/awk -F ' ' '\
$2 ~ /[12][0-9][0-9][0-9]\/[01][0-9]\/[0-3][0-9]/ { printf ("
%s
%s, %s
%s
\n", $1, $2, $3, $4) ; next } \
{ printf ("
%s
%s
%s, %s
%s
\n", $1, $2, $3, $4, $5) ; next }' tv | /usr/bin/sed -e 's=/01/= Jan =' -e 's=/02/= Feb =' -e 's=/03/= Mar =' -e 's=/04/= Apr =' -e 's=/05/= May =' -e 's=/06/= Jun =' -e 's=/07/= Jul =' -e 's=/08/= Aug =' -e 's=/09/= Sep =' -e 's=/10/= Oct =' -e 's=/11/= Nov =' -e 's=/12/= Dec ='
/bin/cat << END_3 # next static part of web page: headings for date/time-sorted table
Programs by Date Recorded
Program
Episode
Date, Time
Notes
END_3
# add a blank (space) field for shows that don't list episodes (e.g. movies)
# sort by date and time; then report as above
# note: all lines have $2==episode now, but for some it is a single space char
# note: date and time were combined into on field before the sort
/usr/bin/awk -F ' ' '\
$2 ~ /[12][0-9][0-9][0-9]\/[01][0-9]\/[0-3][0-9]/ { printf ("%s\t \t%s, %s\t%s\n", $1, $2, $3, $4) ; next } \
{ printf ("%s\t%s\t%s, %s\t%s\n", $1, $2, $3, $4, $5) ; next }' tv | /usr/bin/sort -t ' ' +2 -3 > /tmp/$cmd_name.timesort.$$
/usr/bin/awk -F ' ' '\
$2 == " " { printf ("
%s
%s
%s
\n", $1, $3, $4) ; next } \
{ printf ("
%s
%s
%s
%s
\n", $1, $2, $3, $4) ; next }' /tmp/$cmd_name.timesort.$$ | /usr/bin/sed -e 's=/01/= Jan =' -e 's=/02/= Feb =' -e 's=/03/= Mar =' -e 's=/04/= Apr =' -e 's=/05/= May =' -e 's=/06/= Jun =' -e 's=/07/= Jul =' -e 's=/08/= Aug =' -e 's=/09/= Sep =' -e 's=/10/= Oct =' -e 's=/11/= Nov =' -e 's=/12/= Dec ='
/bin/cat << END_4 # next static part of web page: headings for deleted programs
Programs Deleted Recently
END_4
set diff_files = `/usr/bin/find . -mtime -7 -name tv.diff.\*`
if ($#diff_files) then # there have been recent updates
# prepend diff_file name to grep output if there's only 1 diff_file
# convert diff_file name "tv.diff.YYYY.Mon.DD.HH:MM:SS:" to separate field "YYYY Mon DD, HH:MM:SS "
# combine recording date and time into one field
# sort by recorded date+time (should be unique) for "join" test
# keep only the last item for any duplicate date+time
if ($#diff_files == 1) then # if only 1 set of changes found, grep will not add file name to putput
/usr/bin/grep '^<' $diff_files | /usr/bin/sed -e 's/^/'"$diff_files"':/' -e 's=\./tv\.diff\.==' -e 's/\./ /' -e 's/\./ /' -e 's/\./, /' -e 's/:< / /' | /usr/bin/awk -F ' ' '\
$3 ~ /[12][0-9][0-9][0-9]\/[01][0-9]\/[0-3][0-9]/ { printf ("%s\t%s\t \t%s, %s\t%s\n", $1, $2, $3, $4, $5) ; next } \
{ printf ("%s\t%s\t%s\t%s, %s\t%s\n", $1, $2, $3, $4, $5, $6) ; next }' | /usr/bin/sort -t ' ' +3 -4 | /usr/bin/awk -F ' ' '\
NR == 1 { prev_time = $4 ; prev_line = $0 ; next } \
prev_time != $4 { print prev_line } \
{ prev_time = $4 ; prev_line = $0 } \
END { print prev_line }' > /tmp/$cmd_name.less.$$
else # convert diff_file names to date+time; sort by program name and diff_file timestamp
/usr/bin/grep '^<' $diff_files | /usr/bin/sed -e 's=\./tv\.diff\.==' -e 's/\./ /' -e 's/\./ /' -e 's/\./, /' -e 's/:< / /' | /usr/bin/awk -F ' ' '\
$3 ~ /[12][0-9][0-9][0-9]\/[01][0-9]\/[0-3][0-9]/ { printf ("%s\t%s\t \t%s, %s\t%s\n", $1, $2, $3, $4, $5) ; next } \
{ printf ("%s\t%s\t%s\t%s, %s\t%s\n", $1, $2, $3, $4, $5, $6) ; next }' | /usr/bin/sort -t ' ' +3 -4 | /usr/bin/awk -F ' ' '\
NR == 1 { prev_time = $4 ; prev_line = $0 ; next } \
prev_time != $4 { print prev_line } \
{ prev_time = $4 ; prev_line = $0 } \
END { print prev_line }' > /tmp/$cmd_name.less.$$
endif
if (! -z /tmp/$cmd_name.less.$$) then # some lines were removed (or maybe just changed)
/usr/bin/join -1 4 -2 3 -v 1 -t ' ' /tmp/$cmd_name.less.$$ /tmp/$cmd_name.timesort.$$ > /tmp/$cmd_name.gone.$$
if (! -z /tmp/$cmd_name.gone.$$) then # the lines diff said were gone are really gone
/bin/cat << END_5 # next static part of web page
Program
Episode
Date, Time
Gone By
Notes
END_5
# list deleted programs sorted by program and recorded time
/usr/bin/sort -t ' ' +1 -2 +3 -4 /tmp/$cmd_name.gone.$$ | /usr/bin/awk -F ' ' '\
$3 == " " { printf ("
%s
%s
%s
%s
\n", $2, $4, $1, $5) ; next } \
{ printf ("
%s
%s
%s
%s
%s
\n", $2, $3, $4, $1, $5) ; next }' | /usr/bin/sed -e 's=/01/= Jan =' -e 's=/02/= Feb =' -e 's=/03/= Mar =' -e 's=/04/= Apr =' -e 's=/05/= May =' -e 's=/06/= Jun =' -e 's=/07/= Jul =' -e 's=/08/= Aug =' -e 's=/09/= Sep =' -e 's=/10/= Oct =' -e 's=/11/= Nov =' -e 's=/12/= Dec ='
/bin/cat << END_6 # next static part of web page
END_6
else # lines diff reported gone still match program timestamps in the tv file
echo "No programs deleted."
endif
/bin/rm /tmp/$cmd_name.gone.$$
else # nothing was removed in the recent update(s)
echo "No programs deleted."
endif
/bin/rm /tmp/$cmd_name.less.$$
else # there have not been recent updates
echo "No recent updates."
endif
/bin/cat << END_9 # last static part of web page
The MyReplayTV web site is defunct, so there is no longer any way to retrieve data from our ReplayTV unit.
This page is updated manually.
END_9
cp -p tv tv.prev # for comparison next time....
else # the tv file has not changed
echo 'The "tv" file has not changed.'
/bin/rm tv.diff.$ts[9].$ts[6].$ts[7].$ts[8] # don't keep empty file of changes
exit
endif
/bin/rm /tmp/$cmd_name.timesort.$$