#!/bin/bash


###
### DOCBYTES -- Get number of bytes in an Internet Draft
###
### Usage
###
###   docbytes [options] draftname
###
###   where options is one of the following:
###
###   --version nn
###
###   --debug
###
###   --prefer-family={IPv4,IPv6}
###

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

DEBUG=0
PREFFAMILY=IPv6
VERSION=any

while [ $# -gt 1 ]
do
  case $1 in
    --debug)
                shift;
                DEBUG=1;;
    --version)  shift;
                VERSION=$1;
                shift;;
    --prefer-family=IPv4)
                shift;
                PREFFAMILY=IPv4;;
    --prefer-family=IPv6)
                shift;
                PREFFAMILY=IPv6;;
    *)          echo 'docbytes: Unrecognized argument ('$1') -- exit' >> /dev/stderr;
                exit 1;;
  esac
done

###
### Settings
###

cachedir=$HOME/IETF/newdb/real/history-i-d
WGETOPTS="--prefer-family="$PREFFAMILY" -q --timeout=20"

###
### Function: fetch a draft
###

function draftget() {

  draftfiletostore=$1;
  drafturlbasetoget=$2;
  drafturltoget=$3;

  ###
  ### Check if we need to fetch the last version
  ###

  if [ x$VERSION = xlast ]
  then
    VERSION=`docrevs $drafturltoget`
    if [ $DEBUG = 1 ]
    then
      echo 'docbytes: debug: had to fetch what the last version is ('$VERSION')'
    fi
    if [ x$VERSION = x ]
    then
      echo 'docbytes: error: cannot find the last version'
      exit 1
    fi
    IDTYPE=`(cd $cachedir; getidtype $drafturltoget-$VERSION.txt) | cut -f2 -d:`
    if [ $DEBUG = 1 ]
    then
      echo 'docpages: debug: after expired/published check, the last version doctype is ('$IDTYPE')'
    fi
    if [ $IDTYPE = published ]
    then
      VERSION=`awk -v value=$VERSION 'BEGIN { printf("%02u\n", value - 1); }' < /dev/null`
    fi
    if [ $IDTYPE = empty ]
    then
      VERSION=`awk -v value=$VERSION 'BEGIN { printf("%02u\n", value - 1); }' < /dev/null`
    fi
    if [ $IDTYPE = expired ]
    then
      VERSION=`awk -v value=$VERSION 'BEGIN { printf("%02u\n", value - 1); }' < /dev/null`
    fi
    if [ $DEBUG = 1 ]
    then
      echo 'docpages: debug: after expired/published check, the version is ('$VERSION')'
    fi
  fi

  ###
  ### Pick the version
  ###

  if [ $VERSION = any ]
  then
    cachefile=$cachedir/$drafturltoget-00.txt
  else
    cachefile=$cachedir/$drafturltoget-$VERSION.txt
  fi

  ###
  ### Is it in cache?
  ###

  if [ $DEBUG = 1 ]
  then
    echo 'docbytes: debug: checking if the file '$cachefile' is in the cache'
  fi

  if [ -f $cachefile -a -s $cachefile ]
  then
    if [ $DEBUG = 1 ]
    then
      echo 'docage: debug: no need to fetch '$drafturltoget', already in cache '$cachefile >> /dev/stderr
    fi

    if [ $VERSION = any ]
    then

      lastrev=`(cd $cachedir; ls $drafturltoget-*.txt | tail -1)`
      prevrev=`(cd $cachedir; ls $drafturltoget-*.txt | tail -2 | head -1)`
      if [ `getidtype $cachedir/$lastrev | cut -f2 -d:` = exists ]
      then
        ln -s $cachedir/$lastrev $draftfiletostore
      else
        ln -s $cachedir/$prevrev $draftfiletostore
      fi

    else

      ln -s $cachefile $draftfiletostore

    fi

  else

    if [ $DEBUG = 1 ]
    then
      echo 'docage: debug: Must fetch '$drafturltoget', not in cache '$cachedir >> /dev/stderr
    fi
    wget $WGETOPTS -O $draftfiletostore $drafturlbasetoget$drafturltoget

  fi
}

###
### Find the document
###

for i in $*
do

  docname=`echo "$1" |
           sed 's/-[0-9][0-9][.]txt\$//'`
  
  draftfile=/tmp/$docname.$$.bytes
  drafturlbase=http://tools.ietf.org/id/
  drafturl=$docname

  if draftget $draftfile $drafturlbase $drafturl
  then
    nop=nop
  else
    echo "docbytes: wget failure in draft $docname fetch -- exit" >> /dev/stderr
    exit 1;
  fi

  #
  # Count the bytes
  #

  bc=`wc -c $draftfile | cut -f1 -d" "`
  echo $i:$bc

  #
  # Clean-up
  #

  rm -f $draftfile

done

