#!/usr/bin/tcsh -f

# See 0README

# code block activation
#   0 to disable, 1 to enable

set VPN = 0           # Turn on/off VPN
set PA =  0           # Pause between WP API calls

# _________________________________________________________________________________

set TD = "temp"       # name of temp directory 

set gutenberg = 0 # add new templates to virgin articles

set internetarchive1 = 0 # add new templates to virgin articles (original version - deprecated don't use)
set internetarchive5 = 0 # add new templates to virgin articles (new version). 
set internetarchive2 = 0 # change {{IAA}} -> {{IAA |sname=name}} - via AWB
set internetarchive3 = 0 # optimize articles which already have templates - via AWB
set internetarchive6 = 0 # add new template to Category:1840 death (full manual). Same as #7 but full manual mode.

set internetarchive7 = 0 # add new template to Category:1840 death (unattended CLI download of temp dir data and create ia7out / ia7db)
#set ia7out           = "/home/stbalbach/wi-awb/test.ia7out" # list of names that found a match. For loading into AWB.
#set ia7db            = "/home/stbalbach/wi-awb/test.ia7db"  # list matching names <-> temp dir name. 
#set ia7out           = "/home/stbalbach/wi-awb/births1860.2001-2227.ia7out" # list of names that found a match. For loading into AWB.
#set ia7db            = "/home/stbalbach/wi-awb/births1860.2001-2227.ia7db"  # list matching names <-> temp dir name. 
set internetarchive8 = 0 # add new template to Category:1840 death (AWB mode pulling data from ia7out and db)

set internetarchive9    = 1 # standalone to generate ia9out-index temp data directories
set internetarchive9e   = 0 # standalone to generate ia9out-index temp data directories. For backtesting.
set internetarchive9e2  = 0 # standalone to generate ia9out-index temp data directories. For backtesting. No ia9out log files generated.
set internetarchive9r   = 0 # standalone to generate ia9out-index temp data directories. No ia9out log files generated.
set internetarchive10   = 0 # AWB mode pull existing data from ia9out-index temp directories
                            # Run in combo with demon-lin-ia9-manual.awk

set internetarchive4 = 0 # unattended pre-check articles with existing templates to see if they need optimizing (#3). No AWB.
set ia4out           = "/home/stbalbach/rewi/master-7814.7001-7814.post.auth"

set librivox  = 0 # add new templates to virgin articles
set librivox2 = 0 # fix some problems introduced by a buggy lv.csh

set gbooks    = 0    # to make the conversion - via AWB
set gbooks2   = 0    # to search for and log conversions. No AWB. Logs to ~/googlebookhits

set worldcat  = 0
set worldcat2 = 0    # original version now deprecated

set bandw = 0  # Books and Writers kirjasto.sci.fi

set curr = 0   # Template:Currency check for errors and issue a nulledit
#set TD = "currtemp"  # Use a different temp directory

# _________________________________________________________________________________

set WI_PATH="/home/pepper/wi-awb/"   # customize this path (w/trailing slash)

# No arguments?
if ($#argv == 0) then
  echo "No arguments." >> "$WI_PATH"wi-awb.err
  echo "No arguments." > /dev/stderr
  exit
endif

set AWK="/usr/local/bin/gawk"
set LUA="/usr/bin/lua"
set SED="/bin/sed"
set WGET="/usr/bin/wget"
set GETOPT="/usr/bin/getopt"
set CP="/bin/cp"
set RM="/bin/rm"
set MV="/bin/mv"
set TOUCH="/usr/bin/touch"
set MKDIR="/bin/mkdir"
set CHMOD="/bin/chmod"
set DATE="/bin/date"
set GREP="/bin/grep"
set TAIL="/usr/bin/tail"
set SLEEP="/bin/sleep"
set SORT="/usr/bin/sort"
set UNIQ="/usr/bin/uniq"
set CAT="/bin/cat"
set FFPLAY="/usr/sbin/ffplay"
set KMELEON="/t/Users/stbalbach/K-Meleon/k-meleon.exe"
set XTERM="/usr/bin/xterm" # This will startup faster if X is running in a separate window: 
                         # setenv DISPLAY :0.0
                         # X -multiwindow

set WIKIGET="$WI_PATH""wikiget"
set AWKLIB="$WI_PATH""libshell.awk"

set RAS="/cygdrive/c/Windows/system32/rasdial"
set VPN_NAME="Ipredator"
set VPN_LOGIN="Goriot"
set VPN_P="L5zBsp9AmApYpMdR7W9d"

set AGENT="WI.CSH: Wikipedia<->IA Project (iacs2015@nym.hush.com)"


#Initial VPN connection
if($VPN == 1) $RAS "$VPN_NAME" "$VPN_LOGIN" "$VPN_P"

set DATA_LV = "$WI_PATH""librivox.cv"    # LibriVox database. See ........
set DATA_PG = "$WI_PATH""gutenberg.dat"  # Gutenberg database. See ~/pg

alias LSF $AWK" -lreaddir 'BEGIN { FS = "'"'"/"'"'" } { if("'$3'"=="'"'"f"'"'") {print "'$2'"} }'"
alias LSD $AWK" -lreaddir 'BEGIN { FS = "'"'"/"'"'" } { if("'$3'"=="'"'"d"'"'") {print "'$2'"} }'"
alias WC  $AWK' -v i=0 '"'"'{i++} END {print i}'"'"' \!:1'

# GSUB "from" "to" "$string" (Directions: include "" or // around from and to. Statements can be regex if using //. Spaces are "[ ]")
alias GSUB $AWK' -v s=\!:3 '"'"'BEGIN{gsub(\!:1,\!:2,s);print s}'"'"

setenv DISPLAY :0.0
setenv LANG en_US.UTF-8
setenv LC_COLLATE en_US.UTF-8

#If cntrl-c interrupt, clean up temporary files - go to the myend: tag
onintr myend

# Check for a ~/temp directory
set SHAREDIR="$WI_PATH""$TD""/"  # Shared directory for passing files to AWB 
if( ! -e "$SHAREDIR" ) then
  $MKDIR "$SHAREDIR"
  echo "Making directory " "$SHAREDIR"
  if ( ! -e "$SHAREDIR" ) then 
    echo "Unable to create temp directory ""$SHAREDIR" >> "$WI_PATH"wi-awb.err
    echo "Unable to create temp directory ""$SHAREDIR" > /dev/stderr
    exit
  endif
endif

# ia9 etc.. use command-line option switches eg. wi.csh -n "Charles Dickens" -p births1870.0001-0100
#  -p is optional. If none defaults to default.id set in project.cfg

if( $internetarchive9 || $internetarchive9e || $internetarchive9r || $internetarchive9e2 || $internetarchive10 ) then

  # Parse command line options (-n name and -p project)
  source "$WI_PATH""getoptwi.csh"

  if("$getoptname" == "") then
    echo "No -n <name> argument." >> "$WI_PATH"wi-awb.err
    echo "No -n <name> argument." > /dev/stderr
    exit
  endif

  # Wikipedia article name
  set namewiki = "$getoptname" # From getoptwi.csh

  if("$getoptproj" == "") then # Use project.cfg -> default.id 
    set project = `$AWK '{if($0 ~ /^default[.]id/) {split($0,a,/[ ]{0,10}[=][ ]{0,10}/); print a[2]; i++; exit} }END{if(i == 0) print "unknown"}' "$WI_PATH""project.cfg" `
    if("$getoptproj" == "unknown") then
      echo "Unknown default project. Set default.id in project.cfg" >> "$WI_PATH"wi-awb.err
      echo "Unknown default project. Set default.id in project.cfg" > /dev/stderr
      exit
    endif
  else 
    set project = "$getoptproj"
  endif

  set projectdata = `$AWK -v id="$getoptproj" '{re = "^" id "[.]data"; if($0 ~ re) {split($0,a,/[ ]{0,10}[=][ ]{0,10}/); print a[2]; i++; exit} }END{if(i == 0 || i > 1 ) print "unknown"}' "$WI_PATH""project.cfg" `
  set projectmeta = `$AWK -v id="$getoptproj" '{re = "^" id "[.]meta"; if($0 ~ re) {split($0,a,/[ ]{0,10}[=][ ]{0,10}/); print a[2]; i++; exit} }END{if(i == 0 || i > 1 ) print "unknown"}' "$WI_PATH""project.cfg" `

  if( "$projectdata" != "unknown" && "$projectmeta" != "unknown" ) then
    set stamp=`$DATE +"%m%d%H%M%S"`
    set TDIRNAME="wi-awb-""$stamp"
    set TEMP_DIRECTORY="$projectdata"
    set DATA_DIRECTORY="$projectdata"
    set META_DIRECTORY="$projectmeta"
    set WI_TEMP="$TEMP_DIRECTORY""$TDIRNAME""/"
  else
    echo "Unknown project." >> "$WI_PATH"wi-awb.err
    echo "Unknown project." > /dev/stderr
    exit
  endif

else # No command-line option switches eg. wi.csh "Charles Dickens"

  set namewiki = "$argv[1]"
  set stamp=`$DATE +"%m%d%H%M%S"`
  set TDIRNAME="wi-awb-""$stamp"
  set TEMP_DIRECTORY="$WI_PATH""$TD"
  set WI_TEMP="$TEMP_DIRECTORY""/""$TDIRNAME""/"

endif

$MKDIR "$WI_TEMP"
if ( ! -e "$WI_TEMP" ) then 
  echo "Unable to create temp directory ""$WI_TEMP" >> "$WI_PATH"wi-awb.err
  echo "Unable to create temp directory ""$WI_TEMP" > /dev/stderr
  exit
endif

# Remove old files
if(-e "$SHAREDIR""article.txt") $RM "$SHAREDIR""article.txt"
if(-e "$SHAREDIR""choose.txt") $RM "$SHAREDIR""choose.txt"

# ______________________________________________________________________

# Name without ()'s 
set name = `$AWK -v name="$namewiki" 'BEGIN{split(name,s,"("); print s[1]}'`
# Dab flag (1 if name contain "()" )
# set dabflag = `$AWK -v name="$namewiki" 'BEGIN{if(match(name,"[(]")) {print "1"; exit} print "0"}'`

# Flag if article is to be updated or not
set update = "no"    # global update flag
set gbupdate = "no"  # Google books update flag
set namepseudo = 0

#Number of words in name
set now = `$AWK -v s="$name" 'BEGIN{$0=s; print NF}'`
#Convert spaces to +
set nameplus = `GSUB "[ ]" "+" "$name"`
#set nameplus = `$AWK -v s="$name" 'BEGIN{gsub(/ /,"+",s); print s}'`
#Convert spaces to %20
set namepercept = `GSUB "[ ]" "%20" "$name"`
#set namepercent = `$AWK -v s="$name" 'BEGIN{gsub(/ /,"%20",s); print s}'`
#Convert spaces to _
set nameunder = `GSUB "[ ]" "_" "$name"`
#set nameunder = `$AWK -v s="$name" 'BEGIN{gsub(/ /,"_",s); print s}'`
#Convert spaces to _
set namewikiunder = `$AWK -v s="$namewiki" 'BEGIN{gsub(/ /,"_",s); gsub(/'\''/,"%27",s); print s}'`


if(! "$internetarchive8" && ! "$internetarchive10" ) then

  # Save Wikipedia URL to tmpU
  echo "http://en.wikipedia.org/wiki/""$namewikiunder" > "$WI_TEMP"tmpU

  # Save Wikipedia HTML to tmpW
  $SLEEP $PA
  $WGET --retry-connrefused --waitretry=1 --read-timeout=2 --timeout=2 --no-dns-cache --user-agent="$AGENT" -q -O "$WI_TEMP"tmpW https://en.wikipedia.org/wiki/"$namewikiunder"

  # Save Wikipedia wikisource to article.txt
  $SLEEP $PA
  $WIKIGET -w "$namewikiunder" > "$WI_TEMP""article.txt"
  #if($curr) then 
  #  $WGET --retry-connrefused --waitretry=1 --read-timeout=2 --timeout=2 --no-dns-cache --user-agent="$AGENT" -q -O- https://en.wikipedia.org/wiki/Special:Export/"$namewikiunder" | \
  #  $AWK '{RS=("<text xml|</text")} NR==3' | \
  #  $AWK 'NR==2 {gsub(/&lt;/,"<");gsub(/&gt;/,">");gsub(/&quot;/,"\"");gsub(/&amp;/,"\\&");print}' RS=">"  > "$WI_TEMP""article.txt"
  #else
  #  $WGET --retry-connrefused --waitretry=1 --read-timeout=2 --timeout=2 --no-dns-cache --user-agent="$AGENT" -q -O- https://en.wikipedia.org/wiki/Special:Export/"$namewikiunder" | \
  #  $AWK '{RS=("<text xml|</text")} NR==3' | \
  #  $AWK 'NR==2 {gsub(/&lt;/,"<");gsub(/&gt;/,">");gsub(/&quot;/,"\"");gsub(/&amp;/,"\\&");gsub("{{reflist","{{Reflist");gsub(/<[Rr]eferences \/>/,"{{Reflist}}");gsub(/<[Rr]eferences\/>/,"{{Reflist}}");print}' RS=">"  > "$WI_TEMP""article.txt"
  #endif

  # Save names for other programs
  echo "$name" > "$WI_TEMP"name.txt
  echo "$namewiki" > "$WI_TEMP"namewiki.txt

  # Aproximate article length. A little smaller since awk removes blank lines.
  set articlelengthoriginal = `$AWK '{c=c+length($0)}END{print c}' "$WI_TEMP""article.txt"`

endif

$TOUCH "$WI_TEMP"out.dummy # So Awk won't exit with fatal error "awk: no match" when searching on out.*

  # Project Gutenberg

    if("$gutenberg") then

      set xtermok = 1
      echo "" > /dev/stderr
      echo "Project Gutenberg" > /dev/stderr

      if(`$AWK -v i=0 '{IGNORECASE=1} $0 ~ "{{[ ]{0,2}[Gg]utenberg[ ]{1,2}author[ ]{0,2}[|][ ]{0,2}id" {i++} END {print i}' "$WI_TEMP""article.txt"` == 0 ) then

        # Find all records where Wikipedia name (w/ parens) matches field 1 in gutenberg.dat
        $AWK -F\| -v nw="$namewiki" '$1 == nw' "$DATA_PG" > "$WI_TEMP""pg-results"

        @ pgcount = 1
        set pglength = `WC "$WI_TEMP""pg-results"`
        if($pglength >= 1) then

          while($pgcount <= $pglength)
            set idpg = `$AWK -v datafile="$WI_TEMP""pg-results" -v ln=$pgcount -f "$WI_PATH""pgid.awk"`
            set namepg = `$AWK -F"|" -v ln=$pgcount 'NR==ln {print $2}' "$WI_TEMP""pg-results"`
            if("$update" == "no") then
              set update = "yes"
              echo "\n" >> "$WI_TEMP"out.gutenberg
            endif

            if(`$AWK -v i=0 'BEGINFILE{if (ERRNO) nextfile} $0 ~ /External [Ll]inks?/ {i++} END {print i}' "$WI_TEMP"article.txt "$WI_TEMP"out.extlnk` == 0) then
              echo "==External links==" > "$WI_TEMP"out.extlnk
            endif

            if($pgcount > 1) then
              echo '* {{Gutenberg author | id='"$idpg"' }}<!---Same person has multiple IDs at PG-->' >> "$WI_TEMP"out.gutenberg
            else
              echo '* {{Gutenberg author | id='"$idpg"' }}' >> "$WI_TEMP"out.gutenberg
            endif

            @ pgcount ++

            if("$namepg" != "$namewiki") then
              echo "$namepg" >> "$WI_TEMP"namepseudo
              set namepseudo = 1
            endif
          end
        endif
      else # Generate namepseudo 
        echo "\n  Template exists.\n"
        $AWK -F\| -v nw="$namewiki" '$1 == nw' "$DATA_PG" > "$WI_TEMP""pg-results"
        @ pgcount = 1
        set pglength = `WC "$WI_TEMP""pg-results"`
        if($pglength >= 1) then
          while($pgcount <= $pglength)
            set idpg = `$AWK -v datafile="$WI_TEMP""pg-results" -v ln=$pgcount -f "$WI_PATH""pgid.awk"`
            set namepg = `$AWK -F"|" -v ln=$pgcount 'NR==ln {print $2}' "$WI_TEMP""pg-results"`
            @ pgcount ++
            if("$namepg" != "$namewiki") then
              echo "$namepg" >> "$WI_TEMP"namepseudo
              set namepseudo = 1
            endif
          end
        endif
      endif

    endif

  # LibriVox

    if("$librivox") then

      set xtermok = 1
      echo "" > /dev/stderr
      echo "LibriVox" > /dev/stderr

      if(`$AWK -v i=0 '{IGNORECASE=1} $0 ~ "{{[ ]{0,2}[Ll]ibrivox[ ]{1,2}author[ ]{0,2}[|][ ]{0,2}id" {i++} END {print i}' "$WI_TEMP""article.txt"` == 0 ) then

        # Find all records where Wikipedia name (w/ parens) matches field 3 in data.lv
        $AWK -F\| -v nw="$namewiki" '$3 == nw' "$DATA_LV" > "$WI_TEMP""field3"

        # Find all records where Wikipedia name (w/out parens) matches field 2 in data.lv && field 2 isn't "Unknown"
        $AWK -F\| -v na="$name" '$2 == na && $3 != "Unknown"' "$DATA_LV" > "$WI_TEMP""field2"

        # Find all records where Wikipedia "dob-dod" = field 4 in data.lv 
        if( -e "$WI_TEMP""field4") $RM "$WI_TEMP""field4"
        set dobw = `$AWK -F: '/Category:[0-9]{4} births/{split($2,s,"]"); split(s[1],d," "); print d[1]}' "$WI_TEMP""article.txt"`
        set dodw = `$AWK -F: '/Category:[0-9]{4} deaths/{split($2,s,"]"); split(s[1],d," "); print d[1]}' "$WI_TEMP""article.txt"`
        $AWK -F\| -v b="$dobw" -v d="$dodw" '$4 == b"-"d {print $0}' "$DATA_LV" | $SORT | $UNIQ > "$WI_TEMP""field4p"
        set lastname = `$AWK -v na="$name" 'BEGIN{c = split(na,a," "); print a[c]}' `
        $GREP "$lastname" "$WI_TEMP""field4p" > "$WI_TEMP""field4"

        # Concat, sort and uniq records found above
        $AWK 'BEGINFILE{if (ERRNO) nextfile} NF' "$WI_TEMP""field2" "$WI_TEMP""field3" "$WI_TEMP""field4" | $SORT | $UNIQ > "$WI_TEMP""lv-results"

        # For each record, see if LibriVox has books available. If so, print the {{Librivox author}} template to output.html
        @ lvcount = 1
        @ lvfinal = 1
        set lvlength = `WC "$WI_TEMP""lv-results"`
        if($lvlength >= 1) then
          while($lvcount <= $lvlength)
            set idlv = `$AWK -F\| -v ln=$lvcount '{if (NR==ln) print $1}' "$WI_TEMP""lv-results" `
            set total = `$WGET -q -O- --no-check-certificate "https://librivox.org/author/""$idlv" | $AWK -F\> '$0 ~ "Total matches" {split($4,s,"<"); s[1]=s[1]; gsub(" ","",s[1]); print s[1]}'`
            if("$total" > 0) then
              source "$WI_PATH""lvheader.csh"
              @ lvfinal ++
            endif
            @ lvcount ++
          end
          if($lvfinal < 2) echo "\n  No books found.\n"
        endif
      else
        echo "\n  Template exists.\n"
      endif
    endif

  # LibriVox2
    # To fix a bug in lv.csh that introduced false positives.

    if("$librivox2") then
      set xtermok = 1

      if(`$AWK -v i=0 '{IGNORECASE=1} $0 ~ "{{[ ]{0,2}[Ll]ibrivox[ ]{1,2}author[ ]{0,2}[|][ ]{0,2}id" {i++} END {print i}' "$WI_TEMP""article.txt"` == 1 ) then
       
        set fflag = "No"

        # Librivox ID from the Wikipedia article
        set lvid = `$AWK '{if(match($0,"{{[ ]{0,2}[Ll]ibrivox[ ]{1,2}author[ ]{0,2}[|][ ]{0,2}id=[0-9]{1,5}") != 0) {split($0,b,"="); split(b[2],c,"}"); print c[1]}}' "$WI_TEMP""article.txt"`

        $WGET -q -O- https://librivox.org/author/$lvid > "$WI_TEMP""lv.html"

        #Librivox birth-death dates 
        set dob = `$AWK /dod-dob/ "$WI_TEMP""lv.html" | $AWK -F\) 'NR==2 {split($1,s,"(-|–)"); gsub(/^[ \t]+|[ \t]+$/, "", s[1]); gsub("*|?","",s[1]); print s[1]}' RS=\(` # gsub strips white fron$
        if("$dob" == "") set dob = "AAAA"
        set dod = `$AWK /dod-dob/ "$WI_TEMP""lv.html" | $AWK -F\) 'NR==2 {split($1,s,"(-|–)"); gsub(/^[ \t]+|[ \t]+$/, "", s[2]); gsub("*|?","",s[2]); print s[2]}' RS=\(`
        if("$dod" == "") set dod = "ZZZZ"

        # Wikipedia dates via Category:
        set birth = `$AWK '{if (match($0,"Category:[0-9]{4} births") != 0) {split(substr($0,RSTART, RLENGTH),s,":"); split(s[2],d," "); print d[1]}}' "$WI_TEMP"tmpW`
        set death = `$AWK '{if (match($0,"Category:[0-9]{4} deaths") != 0) {split(substr($0,RSTART, RLENGTH),s,":"); split(s[2],d," "); print d[1]}}' "$WI_TEMP"tmpW`

        if(`$AWK -v s="$birth" 'BEGIN{print length(s)}'` > 0) then
          if($dob == $birth ) then
            set fflag = "Yes"
          endif 
        endif
        if(`$AWK -v s="$death" 'BEGIN{print length(s)}'` > 0) then
          if($dod == $death) then
            set fflag = "Yes"
          endif 
        endif

        if("$fflag" == "No") then
          echo "$namewiki" >> "$WI_PATH"lvmatch.log
        else
          echo "$namewiki" >> "$WI_PATH"lvnomatch.log
        endif

      endif

    endif

  # Google Books

    if("$gbooks" == 1 || "$gbooks2" == 1) then

      set xtermok = 1
      echo "" > /dev/stderr
      echo "Google Books" > /dev/stderr

      set gb = `$AWK -v wget="$WGET" -v tempdir="$WI_TEMP" -f "$WI_PATH"gbooks.awk`

      if("gbooks2" == 1) then
         if("$gb" == 1) then
           echo "$name" >> "$WI_PATH""googlebookhits"
         endif
      endif

      if("$gbooks" == 1) then
        if("$gb" == 1) then
          $MV "$WI_TEMP"article.txt "$WI_TEMP"article.txt.orig
          $MV "$WI_TEMP"article2.txt "$WI_TEMP"article.txt
          set update = "yes"
          set gbupdate = "yes"
         # Ring bell if update 
          # source "$WI_PATH""bell.csh"
        else
          echo "\n  none"
        endif
      endif

    endif

  # WorldCat 

    if("$worldcat") then

      set xtermok = 1
      set wcupdate = "no"

      # Check for existence of {{Worldcat id}} and {{Authority control}} templates
      set wccnt = `$AWK -v count="0" '($0 ~ "[{][{][ ]{0,2}[Ww]orldcat") {count++} END{print count}' "$WI_TEMP"article.txt`
      set authcntrlcnt = `$AWK -v count="0" '($0 ~ "[{][{][ ]{0,2}[Aa]uthority [Cc]ontrol") {count++}  END{print count}' "$WI_TEMP"article.txt`

      # Get WikiData LCNAF (P244) field
      set entity = ""
      set lcnaf = ""
echo "test 0"
$SLEEP $PA
      set entity  = `$WGET --retry-connrefused --waitretry=1 --read-timeout=2 --timeout=2 --no-dns-cache --user-agent="$AGENT" -q -O- 'https://www.wikidata.org/w/api.php?action=wbgetentities&sites=enwiki&titles='"$nameplus"'&languages=en&format=json' | $GREP -oE '{"entities":{"Q[0-9]{1,10}"' | $AWK -F\" '{print $4}'`
echo "test 1"
$SLEEP $PA
      set lcnaf = `$WGET --retry-connrefused --waitretry=1 --read-timeout=2 --timeout=2 --no-dns-cache --user-agent="$AGENT" -q -O- 'https://www.wikidata.org/w/api.php?action=wbgetclaims&entity='"$entity"'&property=P244&format=json' | $GREP -oE '"mainsnak":{"snaktype":"value","property":"P244","datatype":"string","datavalue":{"value":"[^\"]*"' | $AWK -F\" '{print $20}'`
echo "test 2"
      
      if("$wccnt" > 0) then

        # LCCN from {{worldcat id}} ie. no93018066
        set lccntemplate = 0
        set lccntemplate = `$AWK '($0 ~ "*[ ]{0,2}[{][{][ ]{0,1}[Ww]orldcat") {match($0,"*[ ]{0,2}[{][{][ ]{0,1}[Ww]orldcat id[ ]{0,2}|[ ]{0,2}[^}]*[}]",a); match(a[0],"[Ll][Cc][Cc][Nn]-[^|]*[^|^}]",b); gsub("[Ll][Cc][Cc][Nn][-]","",b[0]); gsub("[-]","",b[0]); print b[0]}' "$WI_TEMP"article.txt`

        # LCCN from {{worldcat id}} ie. no/93/018066
        # See https://en.wikipedia.org/wiki/Wikipedia:Authority_control#LCCN
        set lccn = 0
        set lccn = `$AWK '($0 ~ "*[ ]{0,2}[{][{][ ]{0,1}[Ww]orldcat") {match($0,"*[ ]{0,2}[{][{][ ]{0,1}[Ww]orldcat id[ ]{0,2}|[ ]{0,2}[^}]*[}]",a); match(a[0],"[Ll][Cc][Cc][Nn]-[^|]*[^|^}]",b); gsub("[Ll][Cc][Cc][Nn]-","",b[0]); split(b[0],c,"-"); while(i < length(c[1])) {i++; if(substr(c[1],i,1) ~ "[0-9]") break}; fdate = substr(c[1],i); split(c[1],d,"[0-9]"); if(d[1] != "" && fdate != "" && c[2] != "") print d[1] "/" fdate "/" c[2]; else print "0" }' "$WI_TEMP"article.txt`

        # LCCN clean ie. no93018066
        set lccnclean = `GSUB "/" "" "$lccn"`
        #set lccnclean = `$AWK -v s="$lccn" 'BEGIN{gsub("/","",s); print s}'`

      endif

      if("$wccnt" > 0 && `$AWK -v s="$lcnaf" 'BEGIN{print length(s)}'` == 0) then
        if(`$AWK -v s="$lccn" 'BEGIN{if(length(s) > 5) print 1; else print 0}'` > 0) then
          if("$authcntrlcnt" > 0) then

            set authctrlstr = `$AWK '{if(match($0,"[Aa]uthority [Cc]ontrol[^}]*[^}]",a)) print a[0]}' "$WI_TEMP"article.txt`
            set lccnauth = `$AWK -v s="$authctrlstr" 'BEGIN{match(s,"[Ll][Cc][Cc][Nn]=[^|]*[^|^}]",a); split(a[0],b,"="); gsub("/","",b[2]); print b[2]}'`

            if(`$AWK -v s="$lccnauth" -v t="$lccnclean" 'BEGIN{if(match(s,t)) print 1; else print 0}'` > 0 || "$lccnauth" == "") then

              if(`$AWK -v lccn="$lccn" -v s="$authctrlstr" 'BEGIN{gsub("/","\\/",lccn); lccn = sprintf("\\y%s\\y",lccn); if(match(tolower(s),tolower(lccn))) print 1; else print 0}' ` == 0) then
                set authctrlstrnew = `$AWK -v lccn="$lccn" -v s="$authctrlstr" 'BEGIN{print s "|LCCN=" lccn  }'`
              else
                set authctrlstrnew = "$authctrlstr"
              endif

echo "authctrlstr=""$authctrlstr"
echo "authctrlstrnew=""$authctrlstrnew"
              if("$authctrlstr" != "") then
                $SED 's>'"$authctrlstr"'>'"$authctrlstrnew"'>g' "$WI_TEMP"article.txt > "$WI_TEMP"article.txt.wc
              else
                $SED 's>{[Aa]uthority control>{Authority control'"$authctrlstrnew"'>g' "$WI_TEMP"article.txt > "$WI_TEMP"article.txt.wc
              endif
              $GREP -viE "*[ ]{0,2}[{][{][ ]{0,1}[Ww]orldcat [Ii][Dd]" "$WI_TEMP"article.txt.wc > "$WI_TEMP"article.txt
              set wcupdate = "yes"
            endif
          else
            set authctrlstrnew = `$AWK -v lccn="$lccn" 'BEGIN{print "Authority control|LCCN=" lccn }'`
            echo '{{'"$authctrlstrnew"'}}' >> "$WI_TEMP"out.wc
            $GREP -viE "*[ ]{0,2}[{][{][ ]{0,1}[Ww]orldcat [Ii][Dd]" "$WI_TEMP"article.txt > "$WI_TEMP"article.txt.grepv
            $CP "$WI_TEMP"article.txt.grepv "$WI_TEMP"article.txt
            set wcupdate = "yes"
          endif
        endif
      endif

      if("$wccnt" > 0 && `$AWK -v s="$lcnaf" 'BEGIN{print length(s)}'` > 0) then

        if(`$AWK -v s="$lccn" 'BEGIN{if(length(s) > 5) print 1; else print 0}'` > 0 && `$AWK -v s="$lccntemplate" -v t="$lccnclean" 'BEGIN{if(match(s,t)) print 1; else print 0}'` > 0) then
          if("$authcntrlcnt" > 0) then
            $GREP -viE "*[ ]{0,2}[{][{][ ]{0,1}[Ww]orldcat [Ii][Dd]" "$WI_TEMP"article.txt > "$WI_TEMP"article.txt.grepv
            $CP "$WI_TEMP"article.txt.grepv "$WI_TEMP"article.txt
            set wcupdate = "yes"
          else
            set authctrlstrnew = `$AWK -v lccn="$lccn" 'BEGIN{print "Authority control"}'`
            echo '{{'"$authctrlstrnew"'}}' >> "$WI_TEMP"out.wc
            $GREP -viE "*[ ]{0,2}[{][{][ ]{0,1}[Ww]orldcat [Ii][Dd]" "$WI_TEMP"article.txt > "$WI_TEMP"article.txt.grepv
            $CP "$WI_TEMP"article.txt.grepv "$WI_TEMP"article.txt
            set wcupdate = "yes"
          endif
        endif
      endif

      if("$wccnt" == 0 && `$AWK -v s="$lcnaf" 'BEGIN{print length(s)}'` > 0 ) then
        if("$authcntrlcnt" == 0) then
          echo "{{Authority control}}" >> "$WI_TEMP"out.wc
          set wcupdate = "yes"
        endif
      endif

      if("$wcupdate" == "yes") then
        echo "" > /dev/stderr
        echo "WorldCat +" > /dev/stderr
        set update = "yes"
       # Ring bell if update 
        # source "$WI_PATH""bell.csh"
      endif

    endif

  # WorldCat2
    if("$worldcat2") then
     set xtermok = 1
     set wcupdate = "no"
      set wc = `$AWK '($0 ~ "{{worldcat") {gsub("{{worldcat", "{{Worldcat"); count++}  END{print count}' "$WI_TEMP"article.txt`
      if("$wc" > 0) then
        $SED 's/{{worldcat/{{Worldcat/g' "$WI_TEMP"article.txt > "$WI_TEMP"article.txt.wc   
        $MV "$WI_TEMP"article.txt.wc "$WI_TEMP"article.txt
        set wcupdate = "yes"
      endif
      set wc = ""
      set wc = `$AWK '($0 ~ "*{{Worldcat") {gsub("*{{Worldcat", "* {{Worldcat"); count++}  END{print count}' "$WI_TEMP"article.txt`
      if("$wc" > 0) then
        $SED 's/*{{Worldcat/* {{Worldcat/g' "$WI_TEMP"article.txt > "$WI_TEMP"article.txt.wc
        $MV "$WI_TEMP"article.txt.wc "$WI_TEMP"article.txt
        set wcupdate = "yes"
      endif
      if("$wcupdate" == "yes") then
        set update = "yes"
        echo "" > /dev/stderr
        echo "WorldCat +" > /dev/stderr
      endif
    endif

    
  #Internet Archive # For adding fresh (original)
    if("$internetarchive1") then
      echo ""  > /dev/stderr
      echo "Internet Archive1" > /dev/stderr
      source "$WI_PATH""internetarchive1.csh"
    endif


  #Internet Archive # For adding fresh (updated)
    if("$internetarchive5") then
      echo ""  > /dev/stderr
      echo "Internet Archive5" > /dev/stderr
      source "$WI_PATH""internetarchive5.csh"
    endif

  #Internet Archive # For adding fresh (Catetory:XXXX births) full manual mode
    if("$internetarchive6") then
      echo ""  > /dev/stderr
      echo "Internet Archive6" > /dev/stderr
      source "$WI_PATH""internetarchive6.csh"
    endif

  #Internet Archive # For adding fresh (Catetory:XXXX births) unattended download of temp dir data
    if("$internetarchive7") then
      echo ""  > /dev/stderr
      echo "Internet Archive7 (""$namewiki"")" > /dev/stderr
      echo "Internet Archive7 (""$namewiki"")" 
      source "$WI_PATH""internetarchive7.csh"
      set update = "no"
    endif

  #Internet Archive # For adding fresh (Catetory:XXXX births) re-load from internetarchive7 temp dir data via AWB
    if("$internetarchive8") then
      echo ""  > /dev/stderr
      echo "Internet Archive8" > /dev/stderr
      set update = "yes"
      set gbupdate = "no"
      set xtermok = 1
      set ianumber = 1
      set TDIRNAME = `$AWK -v nw="$namewiki" -F\| 'BEGIN{gsub(/\(|\?|\*/,"\\\\&", nw)} {re = "^" nw "$"; if($1 ~ re) print $2 }' "$ia7db"`
      set WI_TEMP = "$TEMP_DIRECTORY""/""$TDIRNAME""/"
    endif

  #Internet Archive 10 For adding re-load from internetarchive9 temp dir data via AWB
    if("$internetarchive10") then
      echo ""  > /dev/stderr
      echo "Internet Archive10" > /dev/stderr
      set update = "yes"
      set gbupdate = "no"
      set xtermok = 1
      set ianumber = 1
      set WI_TEMP = `$AWK -v nw="$namewiki" -F\| 'BEGIN{gsub(/\(|\?|\*/,"\\\\&", nw)} {re = "^" nw "$"; if($1 ~ re) print $2 }' "$META_DIRECTORY""ia9out-index"`
      set TDIRNAME = `$AWK -v wt="$WI_TEMP" 'BEGIN{c = split(wt,a,"/"); print a[c - 1]}'`
    endif

  #Internet Archive # Verify virgin auto mode
    if("$internetarchive9") then
      echo ""  > /dev/stderr
      echo "" 
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
      echo "" 
      echo "Internet Archive9 (""$namewiki"")" > /dev/stderr
      echo "Internet Archive9 (""$namewiki"")" 
      echo ""
      echo "$WI_TEMP" 
      source "$WI_PATH""internetarchive9.csh"
      set update = "no"
    endif

  #Internet Archive # 
    if("$internetarchive9e") then
      echo ""  > /dev/stderr
      echo "" 
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
      echo "" 
      echo "Internet Archive9e (""$namewiki"")" > /dev/stderr
      echo "Internet Archive9e (""$namewiki"")" 
      echo ""
      echo "$WI_TEMP" 
      source "$WI_PATH""internetarchive9e.csh"
      set update = "no"
    endif

  #Internet Archive # 
    if("$internetarchive9e2") then
      echo ""  > /dev/stderr
      echo "" 
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
      echo "" 
      echo "Internet Archive9e2 (""$namewiki"")" > /dev/stderr
      echo "Internet Archive9e2 (""$namewiki"")" 
      echo ""
      echo "$WI_TEMP" 
      source "$WI_PATH""internetarchive9e2.csh"
      set update = "no"
    endif

  #Internet Archive # 
    if("$internetarchive9r") then
      echo ""  > /dev/stderr
      echo "" 
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
      echo "" 
      echo "Internet Archive9e2 (""$namewiki"")" > /dev/stderr
      echo "Internet Archive9e2 (""$namewiki"")" 
      echo ""
      echo "$WI_TEMP" 
      source "$WI_PATH""internetarchive9r.csh"
      set update = "no"
    endif

 # Internet Archive 3 & 4

    if("$internetarchive3" == 1 || "$internetarchive4" == 1) then # Modified method to redo existing templates
      echo ""  > /dev/stderr
      echo "Internet Archive34" > /dev/stderr
      source "$WI_PATH""internetarchive34.csh"
    endif

  # Internet Archive 2 - change {{IAA}} -> {{IAA |sname=name}}

    if("$internetarchive2") then

      set xtermok = 0

      #Extract Internet Archive Templates to iatemplate.1 iatemplate.2 etc

      $AWK '{if(match($0,"{{Internet Archive author[^}]*}}",k)) print k[0] }' "$WI_TEMP"article.txt > "$WI_TEMP"iatemplates.txt
      @ iatempcount = 0
      set iatemplength = `WC "$WI_TEMP""iatemplates.txt"`
      while($iatempcount < $iatemplength)
        @ iatempcount ++
        $AWK -v ln=$iatempcount '{if (NR==ln) print $0 }' "$WI_TEMP""iatemplates.txt" > "$WI_TEMP"iatemplate."$iatempcount"
      end
      if ( -e "$WI_TEMP"iatemplates.txt ) then
        $RM "$WI_TEMP"iatemplates.txt
      endif

      # Extra template options, abort if contains "sname,name,search", otherwise build new template with sname
      foreach iatempl (`LSF "$WI_TEMP" | "$GREP" iatemplate | "$SORT" -n -t "." -k2`)
        set template = "`$AWK 'NF' "$WI_TEMP""$iatempl"`"
        echo "\n\n\n"
        echo "$template" 
        set num = `$AWK -v s="$iatempl" 'BEGIN{split(s,a,"."); print a[2]}'`
        set options = `$AWK '{match($0,"[|][^}]*[^}]",a); print a[0]}' "$WI_TEMP"iatemplate."$num"`
        if(`$AWK -v s="$options" 'BEGIN{if(match(s,"([|]sname|[|]name|[|]search)")) {print 1; exit}}'` > 0) then
        else
          set newoptions = "|sname=""$name"" ""$options"
          set newoptions = `"$AWK" -v s="$newoptions" 'BEGIN{sub(/[ \t]+$/, "",s); print s}'`
          set newtemplate = "{{Internet Archive author ""$newoptions""}}"
          $SED 's/'"$template"'/'"$newtemplate"'/g' "$WI_TEMP"article.txt > "$WI_TEMP"article.txt.ia
          $MV "$WI_TEMP"article.txt.ia "$WI_TEMP"article.txt
          set update = "yes"
          $SLEEP 40
        endif
      end
    endif

  # Books and Writers 

    if("$bandw" == 1) then
      echo ""  > /dev/stderr
      echo "Books and Writers" > /dev/stderr
      set xtermok = 0

      # External links
      set bandwcount = `$AWK -v i=0 '{IGNORECASE=1} $0 ~ "*[ ]{0,2}[[]{0,1}[ ]{0,2}http[s]{0,1}[:]//[w]{0,1}[w]{0,1}[w]{0,1}[.]{0,1}kirjasto[.]sci[.]fi" {i++} END {print i}' "$WI_TEMP""article.txt"`
      if($bandwcount > 0 ) then
        echo "  Found 1 external link" > /dev/stderr
        $AWK -v name="$name" -v sourcefile="$WI_TEMP"article.txt -f "$WI_PATH"replacebandw.awk > "$WI_TEMP"o
        $MV "$WI_TEMP"o "$WI_TEMP"article.txt
        set update = "yes"
      endif

      # Citations
      $AWK -v name="$name" -v sourcefile="$WI_TEMP"article.txt -f "$WI_PATH"replacebandwref.awk > "$WI_TEMP"o
      #$AWK -lreadfile -v one="$WI_TEMP"o 'BEGIN{a = readfile(one); print length(a)}'
      #$AWK -lreadfile -v one="$WI_TEMP"article.txt 'BEGIN{a = readfile(one); print length(a)}'
      if(`$AWK -lreadfile -v one="$WI_TEMP"o -v two="$WI_TEMP"article.txt 'BEGIN{a = readfile(one); b = readfile(two); if(length(a) - 1 != length(b) ) print 1; else print 0}'` == 1) then
        echo "  Found citation(s)" > /dev/stderr
        $CP "$WI_TEMP"o "$WI_TEMP"article.txt
        set update = "yes"
      else
        $RM "$WI_TEMP"o
      endif

      if("$update" == "no") then
        if(`$AWK -v i=0 '{IGNORECASE=1} $0 ~ "kirjasto[.]sci[.]fi" {i++} END {print i}' "$WI_TEMP""article.txt"` > 0) then
          set ptext = "Manual intervention required. Update article and press the <ENTER> key when done..."
          printf "\n$ptext" > /dev/stderr
          set junk = ($<)
        endif
      endif

    endif

  # Template:Currency - one-time run to check and fix articles caused by a malformed programming error in Dec(?) 2015

    if("$curr" == 1) then
      echo ""  > /dev/stderr
      echo "Template:Currency" > /dev/stderr
      set xtermok = 0

      set errcount = ""
      set errcount = `$AWK '/\[\[<strong class="error"/{c=split($0,a,"(<code>|</code>)"); print c }' "$WI_TEMP""tmpW"`
      if("$errcount" != "") then
        $AWK -v nw="$namewiki" '/\[\[<strong class="error"/{c=split($0,a,"(<code>|</code>)"); printf("%s: %s\n",nw,a[2]) }' "$WI_TEMP""tmpW" >> "$WI_PATH""currency.list"
#        $AWK '{gsub(/{{Reflist}}/,"{{reflist}}",$0); print $0}' "$WI_TEMP""article.txt" > "$WI_TEMP""o"
#        $MV "$WI_TEMP""o"  "$WI_TEMP""article.txt"
        echo " " >> "$WI_TEMP""article.txt"
        set update = "yes"
      endif

      $SLEEP 1

    endif



# Run the "choose.txt" XWindow for Internet Archive 

if("$update" == "yes" && ($internetarchive1 || $internetarchive2 || $internetarchive3 || $internetarchive4 || $internetarchive5 || $internetarchive6 || $internetarchive8 || $internetarchive10) ) then

  if("$internetarchive4") then

    if("$ianumber" > 1) then
      echo "$namewiki" >> "$ia4out"
    endif

  else if("$internetarchive3") then

    if("$ianumber" > 1) then

     # Load browser with IA searches in choose.txt
      "$WI_PATH"f 2&

      if( -e "$SHAREDIR""choose.txt") $RM "$SHAREDIR""choose.txt"  
      $CP "$WI_TEMP"choose.txt "$SHAREDIR""choose.txt"
      $CP "$WI_PATH"nanoscriptchoosetxt.xterm "$WI_TEMP"nanoscriptchoosetxt.xterm
      set clength = `$AWK 'END{print NR}' "$SHAREDIR""choose.txt" `
      $SED 's/9999/'$clength'/g' "$WI_TEMP"nanoscriptchoosetxt.xterm > "$WI_TEMP"o
      $MV "$WI_TEMP"o "$WI_TEMP"nanoscriptchoosetxt.xterm
      $CHMOD 775 "$WI_TEMP"nanoscriptchoosetxt.xterm     
      if("$xtermok" == 1) then
        source "$WI_PATH""bell.csh" # ring bell
        "$XTERM" -display :0.0 -geometry 160x60 "$WI_TEMP"nanoscriptchoosetxt.xterm
      endif
      set choice = `$AWK '/Choice:[ ]{0,4}[0-9]{1,2}/{split($0,a," "); gsub(/^[ \t]+/,"",a[2]); gsub(/[ \t]+$/,"",a[2]); print a[2]}' "$SHAREDIR""choose.txt"`   
      echo "$namewiki"" (""$choice"")" >> "$WI_PATH"debug
      if(`$AWK -v s="$choice" 'BEGIN{if(s ~ /^[0-9]{1,2}$/) print 1}'` == 1) then
        if("$choice" != "0") then

          if("$choice" == "99") then      # custom search string
            set newia = `$AWK '/Choice:[ ]99[ ][[:print:]]*/{c = substr($0,12); printf("Internet Archive author |search=( %s )",c)}' "$SHAREDIR""choose.txt"`
            echo "newia=""$newia" >> "$WI_PATH"debug
          else if("$choice" == "98") then # sname string
            set newia = `$AWK '/Choice:[ ]98[ ][[:print:]]*/{c = substr($0,12); printf("Internet Archive author |sname=%s",c)}' "$SHAREDIR""choose.txt"`
            echo "newia=""$newia" >> "$WI_PATH"debug
          else
            set newia = `$AWK '/'"$choice"'. {/{match($0,"Internet Archive author[^}]*[^}]",a); print a[0] }' "$SHAREDIR""choose.txt"`
            echo "newia=""$newia" >> "$WI_PATH"debug
          endif

          if(`$AWK -v s="$newia" 'BEGIN{if(s ~ /Internet Archive/) {print 1; exit} print 0}'` > 0) then
            set oldia = ` $AWK '{if(NR==2) {match($0,"Internet Archive author[^}]{0,200}[^}]{0,2}",a); print a[0]} }' "$SHAREDIR""choose.txt"`
            echo "oldia=""$oldia" >> "$WI_PATH"debug
            if( -e "$SHAREDIR""article.txt") $RM "$SHAREDIR""article.txt"
            $SED 's/'"$oldia"'/'"$newia"'/g' "$WI_TEMP"article.txt > "$WI_TEMP"article3.txt
            $MV "$WI_TEMP"article3.txt "$WI_TEMP"article.txt
          else
            echo "-------Error------" >> "$WI_PATH"debug
            echo "choice=""$choice" >> "$WI_PATH"debug
            echo "newia=""$newia" >> "$WI_PATH"debug
            set ptext= "Unknown error in code. Press the <ENTER> key to continue..."
            printf "\n$pausetext"
            set junk = ($<)
          endif
        endif
      endif
    endif

  else if("$internetarchive5" || "$internetarchive6" || "$internetarchive8" || "$internetarchive10" ) then

    if("$ianumber" > 0) then

      if( -e "$SHAREDIR""choose.txt") $RM "$SHAREDIR""choose.txt"  

     # Copy choose.txt to temp w/ a reason if ia10
      set reason = "none"
      if($internetarchive10) then
        set reason = `$AWK -v id="$namewiki" -F\| 'BEGINFILE{if(ERRNO)nextfile}{if($1==id){print $2}}' "$META_DIRECTORY""ia9out-manual" `
        if("$reason" == "") set reason = "unknown"
      endif
      if("$reason" != "none") then
        $AWK -lreadfile -v r="$reason" -v d="$WI_TEMP""choose.txt" 'BEGIN{l=readfile(d); printf("Reason: %s\n\n%s",r,l)}' > "$SHAREDIR""choose.txt"
      else  
        $CP "$WI_TEMP""choose.txt" "$SHAREDIR""choose.txt"
      endif

     # Load browser with IA searches in choose.txt
      if("$internetarchive8" || "$internetarchive10" ) then
        "$WI_PATH"f -p "$project" -d "$TDIRNAME" &
      else
        "$WI_PATH"f -2 &
      endif

      $CP "$WI_PATH"nanoscriptchoosetxt.xterm "$WI_TEMP"nanoscriptchoosetxt.xterm
      set clength = `$AWK 'END{print NR}' "$SHAREDIR""choose.txt" `
      $SED 's/9999/'$clength'/g' "$WI_TEMP"nanoscriptchoosetxt.xterm > "$WI_TEMP"o
      $MV "$WI_TEMP"o "$WI_TEMP"nanoscriptchoosetxt.xterm
      $CHMOD 775 "$WI_TEMP"nanoscriptchoosetxt.xterm     
      if("$xtermok" == 1) then
        source "$WI_PATH""bell.csh" # ring bell
        "$XTERM" -display :0.0 -geometry 160x60 "$WI_TEMP"nanoscriptchoosetxt.xterm
      endif
      set choice = `$AWK '/Choice:[ ]{0,4}[0-9]{1,2}/{split($0,a," "); gsub(/^[ \t]+/,"",a[2]); gsub(/[ \t]+$/,"",a[2]); print a[2]}' "$SHAREDIR""choose.txt"`   
      if("$choice" < 1) set choice = 0
      echo "$namewiki"" (""$choice"")" >> "$WI_PATH"debug
      if(`$AWK -v s="$choice" 'BEGIN{if(s ~ /^[0-9]{1,2}$/) print 1}'` == 1) then
        if("$choice" != "0") then

          if("$choice" == "99") then      # custom search string
            set newia = `$AWK '/Choice:[ ]99[ ][[:print:]]*/{c = substr($0,12); printf("Internet Archive author |search=( %s )",c)}' "$SHAREDIR""choose.txt"`
            echo "newia=""$newia" >> "$WI_PATH"debug
          else if("$choice" == "98") then # sname string
            set newia = `$AWK '/Choice:[ ]98[ ][[:print:]]*/{c = substr($0,12); printf("Internet Archive author |sname=%s",c)}' "$SHAREDIR""choose.txt"`
            echo "newia=""$newia" >> "$WI_PATH"debug
          else if("$choice" == "97") then # save for later (too complicated)
            set newia="Too complicated"
          else
            set newia = `$AWK '/'"$choice"'. {/{match($0,"Internet Archive author[^}]*[^}]",a); print a[0] }' "$SHAREDIR""choose.txt"`
            echo "newia=""$newia" >> "$WI_PATH"debug
          endif

          if(`$AWK -v s="$newia" 'BEGIN{if(s ~ /Internet Archive/) {print 1; exit} print 0}'` > 0) then
            echo "* {{""$newia""}}" >> "$WI_TEMP"out.ia
          else if(`$AWK -v s="$newia" 'BEGIN{if(s ~ /Too complicated/) {print 1; exit} print 0}'` > 0) then
            if(`$AWK -v indexf="$projectmeta""ia9out-manual-toocomplicated" -v name="$namewiki" -v stamp="" -f "$WI_PATH""index.awk"` != 1) then
              echo "Error in index.awk: unable to update database ia9out-manual-toocomplicated (1)." > /dev/stderr
            endif        
            set update = "no"                        
          else
            echo "-------Error------" >> "$WI_PATH"debug
            echo "choice=""$choice" >> "$WI_PATH"debug
            echo "newia=""$newia" >> "$WI_PATH"debug
            set ptext= "Unknown error in code. Press the <ENTER> key to continue..."
            printf "\n$pausetext"
            set junk = ($<)
          endif
        endif
        if("$choice" == "0") then
          if("$gbupdate" == "no") then
            set update = "no"
          endif
        endif
      endif
    endif
  endif
endif

if("$update" == "yes") then

  # Build article.txt - Note: the following order matters for the files: article.txt, then PG, LV etc..
  $AWK 'BEGINFILE{if (ERRNO) nextfile} {print $0}' "$WI_TEMP"article.txt "$WI_TEMP"out.extlnk "$WI_TEMP"out.gutenberg "$WI_TEMP"out.ia "$WI_TEMP"out.librivox "$WI_TEMP"out.wc > "$SHAREDIR"article.txt

  # Sanity check - make sure new article.txt isn't a lot bigger than the original due to intermitten bug? 
  # Aproximate article length. A little smaller since awk removes blank lines.
  set articlelengthold = `$AWK '{c=c+length($0)}END{print c}' "$WI_TEMP"article.txt`
  set articlelengthnew = `$AWK '{c=c+length($0)}END{print c}' "$SHAREDIR"article.txt`
  if(`$AWK -v old=$articlelengthold -v new=$articlelengthnew 'BEGIN{ if( (old * 1.75) < new ) print 1; else print 0}'` == 1) then
    source "$WI_PATH""bell.csh" # ring bell
    set ptext= "New article.txt is 1.75x or more larger than original - looks like a bug. Press the <ENTER> key to continue..."
    printf "\n$pausetext"
    set junk = ($<)
  endif

  # Insert direct into external links section and skip xterm if running internetachive6 , 8 or 10
  if("$internetarchive6" || "$internetarchive8" || "$internetarchive10" ) then
    if(`$AWK -v id="$WI_TEMP" -f "$WI_PATH"extlnks.awk` == "yes" && ! -e "$WI_TEMP"out.wc) then
      $CP "$WI_TEMP"article.extlnks.txt "$SHAREDIR"article.txt
      set xtermok = 0
    endif
  endif

  # Remove leading and trailing blank lines
  if(! "$curr") then
    $AWK -i"$AWKLIB" -v f="$SHAREDIR""article.txt" 'BEGIN{stripfile(f,"inplace")}'
  endif
  

  if("$xtermok" == 1) then

    # source "$WI_PATH""bell.csh" # ring bell
    "$XTERM" -display :0.0 -geometry 160x60 "$WI_PATH"nanoscript.xterm

    # Remove leading and trailing blank lines
    $AWK -i"$AWKLIB" -v f="$SHAREDIR""article.txt" 'BEGIN{stripfile(f,"inplace")}'

  endif
else
  echo ""
  if(! "$internetarchive7" || ! "$internetarchive4" || ! "$curr") then
    echo "None found."
    $SLEEP 2
  endif
endif

if($VPN == 1) $RAS "$VPN_NAME" /disconnect

myend:
exit
