#! /bin/bash 
# Last edited on 2013-02-28 02:15:45 by stolfilocal

echo=1
tmp="/tmp/$$"

# Extract the watchlist buttons "start {NNNNNN} NEXT" and "start {NNNNNN} PREV" 

grep 'WatchList.load' \
  | egrep -v 'select name="type"' \
  | sed -e 's:.*start=:start :g' -e 's:[&].*[>][^A-Z<>]*\([NP][ER][XE][TV]\)[^A-Z<>]*[<].*$: \1:g' \
  > ${tmp}-all.htm
  
printf "all PREV/NEXT buttons:\n" 1>&2
cat ${tmp}-all.htm 1>&2
  
# Checking for completeness:

grep PREV ${tmp}-all.htm \
  | gawk '//{ print $2; }' \
  | sort | uniq \
  > ${tmp}-prev.htm
  
grep NEXT ${tmp}-all.htm \
  | gawk '//{ print $2; }' \
  | sort | uniq \
  > ${tmp}-next.htm
  
# Checking for completeness:

# printf "all pages (targets of PREV or NEXT): " 1>&2
# bool 1+2 ${tmp}-prev.htm ${tmp}-next.htm \
#   | sort -k1,1n \
#   | tr '\012' ' ' 1>&2
# printf "\n" 1>&2

printf "apparent ini pages (targets of PREV but not NEXT): " 1>&2
bool 1-2 ${tmp}-prev.htm ${tmp}-next.htm \
  | sort -k1,1n \
  | tr '\012' ' ' 1>&2
printf "\n" 1>&2
  
printf "apparent fin pages (targets of NEXT but not PREV): " 1>&2
bool 2-1 ${tmp}-prev.htm ${tmp}-next.htm \
  | sort -k1,1n \
  | tr '\012' ' ' 1>&2
printf "\n" 1>&2

# Write all page numbers to standard output:

bool 1+2 ${tmp}-prev.htm ${tmp}-next.htm \
  | sort -b -k1,1n

rm ${tmp}-*.htm

