#!/bin/bash

###
### DOCREVS -- Get number of Internet Draft revisions
###
### Usage
###
###   docrevs [options] draftname
###
###   where options is one of the following:
###
###   --dates
###
###   --debug
###
###   --prefer-family={IPv4,IPv6}
###

###
### Parse command line
###

DEBUG=0
DATES=0
TRACKREPLACES=0
PREFFAMILY=IPv6

while [ $# -gt 1 ]
do
  case $1 in
    --debug)
                shift;
                DEBUG=1;;
    --dates)
                shift;
                DATES=1;;
    --prefer-family=IPv4)
                shift;
                PREFFAMILY=IPv4;;
    --prefer-family=IPv6)
                shift;
                PREFFAMILY=IPv6;;
    *)          echo 'docrevs: Unrecognized argument ('$1') -- exit' >> /dev/stderr;
                exit 1;;
  esac
done
if [ $# -gt 0 ]
then
  initialdocname=`echo "$1" |
                  # sed 's/-[0-9][0-9]\$//' |
                  sed 's/-[0-9][0-9][.]txt\$//'`
  docname=$initialdocname
else
  echo "docrevs: usage docrevs [options] draftname" >> /dev/stderr
  exit 1
fi

###
### Set variables for fetching the information, temporary directories, etc.
###

#WGETOPTS="--prefer-family="$PREFFAMILY
WGETOPTS="--prefer-family="$PREFFAMILY" -q --timeout=20"
tmpdir=/tmp/docrevs.$docname
rm -rf $tmpdir 2>&1 > /dev/null
mkdir $tmpdir
cd $tmpdir

###
### Get configuration data from the docage.data file
###

datafile=`echo $0 | sed s/docrevs/docage.data/`
if [ -s $datafile ]
then
  nop=ok
else
  echo 'docage: Fatal error: No data file ('$datafile') exists -- exit'
  rm -rf $tmpdir 2>&1 > /dev/null
  exit 1
fi

###
### Fetch data from the tools system
###

#stateurl="http://merlot.tools.ietf.org/draft/$docname/state"
stateurl="http://tools.ietf.org/draft/$docname/state"
statefile=$tmpdir/$docname.state
if wget $WGETOPTS -O $statefile $stateurl
then
  currentrev=`grep "^Doc-rev: " $statefile | cut -f2 -d: | cut -f1 -d';' | tr -d ' '`
else
  echo "docrev: wget failure for $stateurl" >> /dev/stderr
  touch $statefile
  currentrev=""
fi

if [ $DEBUG = 1 ]
then
  echo "docrev: debug: currentrev is $currentrev after state grep"
fi

###
### If that fails, find out if this is one of the special case drafts
###

if [ x$currentrev = x ]
then
  wrongrev=`fgrep wrongversion:$docname: $datafile | cut -f3 -d:`
  if [ x$wrongrev = x ]
  then
    nop=nop
  else
    currentrev=$wrongrev
  fi
fi

###
### And if that fails, look up the document itself and find out from file
###

if [ x$currentrev = x ]
then
  baseurl="http://tools.ietf.org/id/$docname"
  if wget $WGETOPTS $baseurl
  then
    nop=nop
  else
    echo "docrev: wget failure for $baseurl -- exit" >> /dev/stderr
    rm -rf $tmpdir 2>&1 > /dev/null
    exit 1
  fi
  ls -l $tmpdir | grep draft- |
  currentrev=`awk '
/draft.*[.].*/ {
  printf("%d\n",substr($0,index($0,".") - 2,2));
}
'`
  if [ $DEBUG = 1 ]
  then
    echo "docrev: debug: currentrev is $currentrev after document retrieval"
  fi

fi

###
### Print result
###

echo `expr $currentrev + 1`

###
### Fetch dates
###

if [ $DATES = 1 ]
then

  ###
  ### Get the "events" file from tools, in case it has the metadata
  ###

  eventsurl="http://tools.ietf.org/draft/$docname/events"
  eventsfile="$tmpdir/$docname.events"
  if wget $WGETOPTS -O $eventsfile $eventsurl
  then
    nop=nop
  else
    touch $eventsfile
  fi
  
  ###
  ### Parse the events file
  ###

  pureeventsfile=$tmpdir/$docname.revevents;
  awk '
/ docrev=.([0-9][0-9]). docsubmitted=.([0-9]+-[0-9]+-[0-9]+). / {
  x = substr($0,index($0,"docrev="));
  split(x,comps,sprintf("%c",39));
  printf("%02d:%s\n", comps[2], comps[4]);
}' < $eventsfile | sort -u |
  # remove data from years deemed unreliable
  fgrep -v :19 |
  fgrep -v :2000 |
  fgrep -v :2001 > $pureeventsfile
  
  ###
  ### For each revision, see if we know the publication date
  ###

  rev=00
  while [ $rev -le $currentrev ]
  do

    ###
    ### Is the date known in the metadata "events" file?
    ###
 
    date=`grep "^${rev}:" $pureeventsfile | cut -f2 -d: | tail -1`

    ###
    ### If not, fetch the actual document and use docage to determine publication date
    ###

    if [ x$date = x ] 
    then
      date=`docage --prefer-family=$PREFFAMILY --version $rev $docname 2> /dev/null`
      if [ $DEBUG = 1 ]
      then
        echo "docage: debug: using docage to get rev $rev date, result = $date"
      fi
    fi

    if [ x$date = x ] 
    then
      date=UNKNOWN
    fi

    ###
    ### Output the date, if known
    ###

    echo $rev:$date

    ###
    ### Increment revision counter
    ###

    rev=`expr $rev + 1`
    if [ $rev -lt 10 ]
    then
      rev=0$rev
    fi

  done
fi

###
### Cleanup and exit
###

if [ $DEBUG = 0 ]
then
  rm -rf $tmpdir 2>&1 > /dev/null
fi
exit 0
