#!/bin/sh

# Returns ranking of page, paying attention to joint rankings (equal hits).

# Usage:
#    $0 pagename < rankedhits
#        To get the ranking of pagename, or "" if none.
#
#        The ranking is that of the first line on which the hit count appears
#        that is achieved by the named page.
#
#    $0 -autosource pagename
#        Get ranking of pagename, or "" if none.
#    
#    $0 -autosource
#        Output selected source file on stdout, only, or "" if none.
#
#        Automatically select the live rank file, or a backup if absent.
#
#    $0 -tests
#        To execute all self-tests:
#    $0 -test pagename
#        To execute one test:

# Given on stdin sorted (descending by hits) ranking data of the form below:
#
#
# and in $1 a page name of the form XXX.html
#
# returns the rank number for $1 or an empty string if not found.
#
#   "nosuch.html" would return ""


case "$1" in
  *.html)
    # Extract the ranking of page $1.
    exec awk -v PG="$1" 'BEGIN { oldcount=-1; countrank=-1; }
        {
        if($1 != oldcount) { oldcount=$1; countrank=NR; }
        if($2 == PG) { print countrank; exit; }
        }'
    exit 0;;

  -autosource)
    shift
    # Popular articles file from live log data.
    POPARTSFILE=.work/tmp/populararts.txt
    SOURCE=$POPARTSFILE
    # Substitute archived popularity file if live one not available.
    if [ ! -s "$POPARTSFILE" ]; then
        SOURCE="`ls -1 .work/archive/poparts/20??????-populararts.txt | tail -1`"
    fi
    if [ "" = "$SOURCE" -o ! -s "$SOURCE" ]; then
        echo "ERROR: cannot find source" 1>&2; exit 1
        exit 1
    fi
    if [ "$#" -eq 0 ]; then
        echo $SOURCE
        exit 0
    fi
    exec $0 "$@" < $SOURCE;;

  -test) break;; # Fall through to below.
  -tests)
    # Run all self-tests.
    if [ "1" != "`$0 -test note-on-LIME.html`" ]; then
        echo "ERROR: test failed for note-on-LIME.html" 1>&2; exit 1
    fi
    if [ "3" != "`$0 -test note-on-A.html`" ]; then
        echo "ERROR: test failed for note-on-A.html" 1>&2; exit 1
    fi
    if [ "4" != "`$0 -test note-on-B.html`" ]; then
        echo "ERROR: test failed for note-on-B.html" 1>&2; exit 1
    fi
    if [ "4" != "`$0 -test note-on-C.html`" ]; then
        echo "ERROR: test failed for note-on-C.html" 1>&2; exit 1
    fi
    if [ "" != "`$0 -test nonesuch.html`" ]; then
        echo "ERROR: test failed for nonesuch.html" 1>&2; exit 1
    fi
    echo "INFO: tests passed."
    exit 0;;
  -help|*)
    echo "ERROR: usage $0 ((-autosource)|(<rankedhits.txt)) pagename.html" 1>&2; exit 1;;
esac


# Spit out test data into recursively-invoked instance.
exec $0 $2 << EOTESTDATA
     27 note-on-LIME.html
     21 note-on-USB-DC-power-meters-REVIEW.html
     17 note-on-A.html
     16 note-on-B.html
     16 note-on-C.html
     16 Enphase-AC-Battery-REVIEW.html
     15 triple-glazing-3G.html
EOTESTDATA

# In the above data
#   "note-on-LIME.html" would return "1"
#   "note-on-A.html" would return "3"
#   "note-on-B.html" would return "4"
#   "note-on-C.html" would return "4"
#   "nonesuch.html" would return ""
exit 0
