#!/bin/sh

###
### LASTCALLRETRIEVERWEB - Make a web page of drafts in IETF Last Call
###
### Version 0.0.1 May 17, 2007
###
### Written in 2007 by Jari Arkko
### Donated to the public domain.
###
###
### Usage:
###
###   lastcallretrieverweb
###
###   Output will be in current directory, index.html and lastcalldrafts.tgz
###

###
### Initialize
###

copy=0
quiet=0
db=/tmp/$$.lcrdb
dbtmp=/tmp/$$-tmp.lcrdb
drafttmp=/tmp/$$-drafts

rm -rf $db $dbtmp $drafttmp
mkdir $drafttmp

while [ $# -gt 0 ]
do
  case $1 in
    --copy)     copy=1;
                shift;;
    --quiet)    quiet=1;
                shift;;
    --timestamp) echo '';
                 echo 'Running lastcallretriverweb';
                 date;
                 echo '';
                 shift;;
    *)          echo 'Unrecognized argument ('$1') -- exit';
                exit 1;;
  esac
done

###
### Fetch the drafts in last call
###

if [ $quiet = 0 ]
then
  echo 'Fetching last call data from the tracker...'
fi

lastcallretriever > $dbtmp

###
### Sort
###

if [ $quiet = 0 ]
then
  echo 'Sorting...'
fi

(grep -v unknown $dbtmp |
 sort --field-separator=';' --key=3;
 grep unknown $dbtmp) > $db

###
### Start the HTML page
###

rm -f index.html
echo '<html>' > index.html
echo '<head>' >> index.html
echo '<title>Internet Drafts Currently in IETF Last Call</title>' >> index.html
echo '</head>' >> index.html
echo '<body>' >> index.html
echo '<h1>Internet Drafts Currently in IETF Last Call</h1>' >> index.html
echo '<p>The below drafts are currently in IETF Last Call:</p>' >> index.html

###
### Make a table of drafts in last call
###

echo '<TABLE BORDER=1>' >> index.html
echo '<TR><TH>Draft</TH><TH>Intended Status</TH><th>End Date</th></TR>' >> index.html

awk '
BEGIN {
  FS = ";";
}

/.*/ {
  draft = $1;
  status = $2;
  enddate = $3;
  drafturl = $4;
  lastcallurl = $5;
  printf("<TR><TD><a href=%c%s%c>%s</a></TD><TD>%s</TD>",
         34, drafturl, 34, draft,
         status);
  if (lastcallurl != "unknown") {
    printf("<TD><a href=%c%s%c>%s</a></TD>",
           34, lastcallurl, 34, enddate);
  } else {
    printf("<TD>%s</TD>",
           enddate);
  }
  printf("</TR>\n");
}

' < $db >> index.html

echo '</TABLE>' >> index.html

###
### Collect the drafts into a tarball
###

if [ $quiet = 0 ]
then
  echo 'Fetching drafts...'
fi

echo '<p>A gzipped tar file of all these drafts is available <a href="lastcalldrafts.tgz">here</a>.</p>' >> index.html

cp index.html $drafttmp
(cd $drafttmp;
 for i in `cut -f4 -d';' $db | sort`
 do
   if [ $quiet = 0 ]
   then
     echo '  '`basename $i`...
   fi
   wget -q $i
 done)
if [ $quiet = 0 ]
then
  echo 'Creating tarball...'
fi
(cd $drafttmp; tar czf - index.html draft*) > lastcalldrafts.tgz

###
### Finish the page
###

echo '<hr/>' >> index.html
echo '<p><font size="-2">Generated' >> index.html
date >> index.html
echo 'by <a href="http://www.arkko.com/tools/lastcallretrieverweb.html">lastcallretriverweb</a></font></p>' >> index.html
echo '</body>' >> index.html
echo '</html>' >> index.html

###
### Copy to web server
###

if [ $copy -eq 1 ]
then
  if [ $quiet = 0 ]
  then
    echo 'Copying...'
  fi
  scp -q index.html lastcalldrafts.tgz jarkko@users.piuha.net:public_html/tools/lastcalls
fi

###
### Cleanup
###

if [ $quiet = 0 ]
then
  echo 'Done'
fi

#rm -rf $db $dbtmp $drafttmp

