      # internetarchive9.csh -- use for auto processing, not for backtesting (see the ia9e.csh version)

      # default 
      set msg = "unknown"

      # Number of existing templates in article
      set numoftemplates = `$AWK -v i=0 '{if(match($0,"{{Internet Archive author[^}]*}}",k)) i++ }END{print i}' "$WI_TEMP"article.txt`
      # Ignore articles with these categories (sports figures etc) as they produce false positives
      set catignore = `$AWK -v id="$WI_TEMP" -f "$WI_PATH""catignore.awk"`

#     If no archive template
      if($numoftemplates < 1 && $catignore < 1) then
#     If 1+ archive template
#      if($numoftemplates > 0) then
#     if 1 archive template and it doesn't contain a search= parameter
#      if( $numoftemplates == 1 && "$searchoption" == "no" ) then

        set xtermok = 1


        # 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 || `$AWK -v s="$death" 'BEGIN{print length(s)}'` == 0) then
          set birthdeath = "none"
          set post1945 = 0
        else
          set birthdeath = "$birth""-""$death"
          if($death > 1945) then
            set post1945 = 1
          else
            set post1945 = 0
          endif
        endif

        # WikiData dates
        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}'`
        set wdbirth = `$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=P569&format=json' | $GREP -oE '"mainsnak":{"snaktype":"value","property":"P569","datavalue":{"value":{"time":"\+[0-9]{1,4}' | $AWK -F+ '{print $2; exit}'`
        set wddeath = `$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=P570&format=json' | $GREP -oE '"mainsnak":{"snaktype":"value","property":"P570","datavalue":{"value":{"time":"\+[0-9]{1,4}' | $AWK -F+ '{print $2; exit}'`
        if(`$AWK -v s="$wdbirth" 'BEGIN{print length(s)}'` == 0 || `$AWK -v s="$wddeath" 'BEGIN{print length(s)}'` == 0) then
          set wdbirthdeath = "none"
        else
          set wdbirthdeath = "$wdbirth""-""$wddeath"
        endif

        set needdates = "no"
        if("$birthdeath" == "none" && "$wdbirthdeath" != "none") then
          set birthdeath = "$wdbirthdeath"
        endif
        if("$wdbirthdeath" == "none" && "$birthdeath" != "none") then
          set needdates = "yes"
        endif
        if("$wdbirthdeath" != "none" && "$birthdeath" != "none") then
          if("$wdbirthdeath" != "$birthdeath") then
      #      set birthdeath = "$wdbirthdeath"                          # Wikidata dates are often unreliable
            set birthdeath = "$birthdeath"
          endif
        endif

        $AWK -v tempdir="$WI_TEMP" -v wpname="$name" -f "$WI_PATH"getnames.awk

        if("$namepseudo" != 0) then
          $CAT "$WI_TEMP"namepseudo "$WI_TEMP"getnames | $SORT | $UNIQ > "$WI_TEMP"temp
          $MV "$WI_TEMP"temp "$WI_TEMP"getnames
        endif

        @ iacount = 1
        set ialength = `WC "$WI_TEMP""getnames"`

        if($ialength > 0) then
          while($iacount <= $ialength)

#            set cname = `$AWK -v ln=$iacount '{if (NR==ln) {gsub("\047","’"); print $0}  }' "$WI_TEMP""getnames" `
            set cname = `$AWK -v ln=$iacount '{if (NR==ln) { print $0} }' "$WI_TEMP""getnames" `
            @ iacount ++
            set extended = `$AWK -v c="0" -v s="$cname" 'BEGIN{c = gsub(/[^\x20-\x7F]/,"",s); print c}'`

            set sopt = "none"
            $LUA "$WI_PATH"iaa.lua "$WI_TEMP" "$cname" "$birthdeath"  
            source "$WI_PATH""ia-check.csh"

            set sopt = "t"
            $LUA "$WI_PATH"iaa.lua "$WI_TEMP" "$cname" "$birthdeath" "t"
            source "$WI_PATH""ia-check.csh"

            if("$extended" != 0) then
              set sopt = "w"
              $LUA "$WI_PATH"iaa.lua "$WI_TEMP" "$cname" "$birthdeath" "w"
              source "$WI_PATH""ia-check.csh"
            endif
          end
        endif

        # Generate tmpO/I files for preexisting templates in article.txt (this should be commented out unless working on preexisting template articles)
        # comment out unless doing backtesting of articles with existing templates, see internetarchive9e.csh
        # $AWK -v id="$WI_TEMP" -f "$WI_PATH""existingias.awk"

        # Remove duplicates from ia-results
        $AWK -v datadir="$WI_TEMP" -f "$WI_PATH""uniq-ia-results.awk"
        $MV "$WI_TEMP""ia-results.temp" "$WI_TEMP""ia-results"

        # Run ias.awk to determine auto suggestion for best match
        $AWK -v id="$WI_TEMP" -f "$WI_PATH"ias.awk


        # Determine ias primary record number, 0 if none found.
        if( -e "$WI_TEMP"ias.out) then
          set iasprimnumber = `$AWK -F\@ '{print $4}' "$WI_TEMP"ias.out ` 
        else
          set iasprimnumber = 0
        endif

        # Determine ias secondary record number(s)
        if( -e "$WI_TEMP"ias.secondary) then
          set iassecnumbers = `$AWK -F\@ '{printf $4 " "}' "$WI_TEMP"ias.secondary ` 
        else
          set iassecnumbers = 0
        endif

        # Loop through ia-results and create choose.txt entries
        @ iacount = 1
        set ialength = `WC "$WI_TEMP""ia-results"`
        if($ialength > 0) then
          while($iacount <= $ialength)

            set iafinalsopt = `$AWK -v ln=$iacount  -F\| '{if (NR==ln) print $1}' "$WI_TEMP""ia-results" `
            set iafinalname = `$AWK -v ln=$iacount  -F\| '{if (NR==ln) print $3}' "$WI_TEMP""ia-results" `
            set iafinalid = `$AWK -v ln=$iacount  -F\| '{if (NR==ln) print $4}' "$WI_TEMP""ia-results" `
            set ianumber = `$AWK -v ln=$iacount  -F\| '{if (NR==ln) {split($4,a,"@"); print a[4]} }' "$WI_TEMP""ia-results" `
           # 1 if a secondary choice 
            set issecondary = `$AWK -v s="$iassecnumbers" -v d="$ianumber" 'BEGIN{c=split(s,a," "); while(i++<c){ if(int(a[i])==int(d)) {print 1; exit}}print 0}'` 

            if( $iasprimnumber == 0 && $issecondary == 0 ) then
              set iafinalscore = `$AWK -v ln=$iacount -F\| '{if (NR==ln) print $2}' "$WI_TEMP""ia-results" `
              set iafinalscoredisplay = "$iafinalscore"
            else
              set iafinalscore = `$AWK -v id="$iafinalid" -F\| '{if ($1 ~ id) print $4}' "$WI_TEMP""ias.result" `
              set iafinalscoredisplay = `$AWK -v id="$iafinalid" -F\| '{if ($1 ~ id) print $2 $3 $4}' "$WI_TEMP""ias.result" `
            endif

           # If a "t" skip it if there is a none within 20% (unless it's a primary/secondary */** result found by ias.awk)
#            if("$iafinalsopt" == "t") then
#              @ newnumber = $ianumber - 1
#              set nonesearch = 'tmpO@'`$AWK -v s="$iafinalname" 'BEGIN{gsub(" ","_",s); print s}'`'@none@'$newnumber            
#              set noneresults = `$AWK '/'"$nonesearch"'/{split($0,a,"|"); print a[2]}' "$WI_TEMP""ia-results" `
#              set factor = `$AWK -v n="$noneresults" -v t="$iafinalscore" 'BEGIN{if(n <= 0){print 0; exit}; i = t / n; split(i,a,"."); if(a[1] > 0){print 0; exit}; print substr(a[2],1,1)}' `
#              if("$factor" > 7 && "$ianumber" != "$iasprimnumber" && $issecondary == 0 ) then   # 20% or less false postives are skipped. Don't skip primary/secondary from ias
#                @ iacount ++
#                @ ianumber = $newnumber
#                continue
#              endif
#            endif

            @ iacount ++

#            if("$iafinalscore" > 0 || "$iafinalscore" == 0) then
            if("$iafinalscore" > 0) then
              if("$update" == "no") then
                set update = "yes"
              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

              # Show a "*" next to auto suggestion from ias.awk - one star is primary suggestion, two stars is secondary suggestion(s)
              if( $ianumber == $iasprimnumber ) then
                set comment = ' '"$iafinalscoredisplay"' *'
              else if( $issecondary ) then
                set comment = ' '"$iafinalscoredisplay"' **'
              else
                set comment = ' '"$iafinalscoredisplay"
              endif

              # Only used for backtesting see internetarchive9e.csh           
              # if("$ianumber" == "19") then # Retain article.txt existing template options. Error prone if > 1 existing template, picks the first one blindly.
              #   echo "$ianumber"'. {{'`$AWK '{ if(match($0,"{{Internet Archive author[^}]*}}",k)) {gsub(/{|}/,"",k[0]);print k[0];exit} }' "$WI_TEMP"article.txt`'}}'"$comment" >> "$WI_TEMP"choose.txt
              if("$iafinalname" == "$name") then
                if("$needdates" == "yes") then
                  if("$iafinalsopt" != "none") then
                    echo "$ianumber"'. {{Internet Archive author |sname='"$iafinalname"' |birth='"$birth"' |death='"$death"' |sopt='"$iafinalsopt"'}}'"$comment" >> "$WI_TEMP"choose.txt       
                  else
                    echo "$ianumber"'. {{Internet Archive author |sname='"$iafinalname"' |birth='"$birth"' |death='"$death"'}}'"$comment" >> "$WI_TEMP"choose.txt       
                  endif
                else
                  if("$iafinalsopt" != "none") then   
                    echo "$ianumber"'. {{Internet Archive author |sname='"$iafinalname"' |sopt='"$iafinalsopt"'}}'"$comment" >> "$WI_TEMP"choose.txt       
                  else
                    echo "$ianumber"'. {{Internet Archive author |sname='"$iafinalname"'}}'"$comment" >> "$WI_TEMP"choose.txt       
                  endif
                endif
              else
                if("$needdates" == "yes") then
                  if("$iafinalsopt" != "none") then
                    echo "$ianumber"'. {{Internet Archive author |sname='"$iafinalname"' |birth='"$birth"' |death='"$death"' |sopt='"$iafinalsopt"'}}'"$comment" >> "$WI_TEMP"choose.txt       
                  else
                    echo "$ianumber"'. {{Internet Archive author |sname='"$iafinalname"' |birth='"$birth"' |death='"$death"'}}'"$comment" >> "$WI_TEMP"choose.txt       
                  endif
                else
                  if("$iafinalsopt" != "none") then
                    echo "$ianumber"'. {{Internet Archive author |sname='"$iafinalname"' |sopt='"$iafinalsopt"'}}'"$comment" >> "$WI_TEMP"choose.txt       
                  else
                    echo "$ianumber"'. {{Internet Archive author |sname='"$iafinalname"'}}'"$comment" >> "$WI_TEMP"choose.txt       
                  endif
                endif
              endif
            endif
          end
          echo -n "Choice: " >> "$WI_TEMP"choose.txt

        else
          set ianumber = 0
          echo "\n  No books found.\n"
          $SLEEP 3
        endif      

      else if($numoftemplates == 1) then
        set ianumber = 0
        set msg = "templatefound"
        echo "\nAbort. Reason: Template found.\n"
        $SLEEP 3
      else if($numoftemplates > 1) then
        set ianumber = 0
        set msg = "multipletemplatesfound"
        echo "\nAbort. Reason: Multiple templates found.\n"
        $SLEEP 3
      else if($catignore == 1) then
        set ianumber = 0
        set msg = "catignore"
        echo "\nAbort. Reason: Categorory ignored.\n"
        $SLEEP 3  
      else 
        set ianumber = 0
        set msg = "unknown"
        echo "\nAbort. Reason: unknown " $catignore " " $numoftemplates ". \n"
        $SLEEP 3  
      endif # if template exists

      if( ! -e "$WI_PATH""project.cfg") then
        echo "Unable to find " "$WI_PATH""project.cfg"
        exit
      else
        set project = `$AWK '{if($0 ~ /default[.]id/) {split($0,a,/[ ]{0,10}[=][ ]{0,10}/); print a[2]}}' "$WI_PATH""project.cfg"`
      endif

      # Update/pack ia9out-index (location of temp directories tied to namewiki)
      if(`$AWK -v indexf="$projectmeta""ia9out-index" -v name="$namewiki" -v stamp="$WI_TEMP" -f "$WI_PATH""index.awk"` != 1) then
        echo "Error in index.awk: unable to update database ""$projectmeta""ia9out-index (1)." > /dev/stderr
      endif

      if(-e "$WI_TEMP"ias.out) then

        set goodtogo = 1
        set isoption = `$AWK -F\@ '{print $3}' "$WI_TEMP"ias.out`

        # Secondary names mark as manual     
        if(-e "$WI_TEMP""ias.secondary") then
          if( `WC "$WI_TEMP""ias.secondary"` > 0) then
            if(`$AWK -v indexf="$projectmeta""ia9out-manual" -v name="$namewiki" -v stamp="secondary" -f "$WI_PATH""index.awk"` != 1 ) then
              echo "Error in index.awk: unable to update database ia9out-manual (2)."  > /dev/stderr
            endif
          endif
          set goodtogo = 0
        endif

        # Name contains a comma - needs manual processing
        if( `$AWK -v s="$namewiki" 'BEGIN{if(match(s,/[,]/)) print 1; else print 0}'` ) then
          if(`$AWK -v indexf="$projectmeta""ia9out-manual" -v name="$namewiki" -v stamp="comma" -f "$WI_PATH""index.awk"` != 1 ) then
            echo "Error in index.awk: unable to update database ia9out-manual (3)." > /dev/stderr
          endif
          set goodtogo = 0
        endif

        # Test if able to automatically insert template into article.txt - mark as manual if fail
        echo "* {{Internet Archive author |sname=dummy template}}" > "$WI_TEMP"out.ia
        $CP "$WI_TEMP"article.txt "$WI_TEMP"article.txt.insertion
        if( `$AWK -v id="$WI_TEMP" -f "$WI_PATH"extlnks.awk` =~ "no*" ) then
          if(`$AWK -v indexf="$projectmeta""ia9out-manual" -v name="$namewiki" -v stamp="insertion" -f "$WI_PATH""index.awk"` != 1 ) then
            echo "Error in index.awk: unable to update database ia9out-manual (4)." > /dev/stderr
          endif
          set goodtogo = 0
        endif
        if(-e "$WI_TEMP"article.extlnks.txt) $RM "$WI_TEMP"article.extlnks.txt
        if(-e "$WI_TEMP"out.ia) $RM "$WI_TEMP"out.ia
        $MV "$WI_TEMP"article.txt.insertion "$WI_TEMP"article.txt

        if( $goodtogo ) then
          if(`$AWK -v indexf="$projectmeta""ia9out-goodtogo" -v name="$namewiki" -v stamp="" -f "$WI_PATH""index.awk"` != 1 ) then
            echo "Error in index.awk: unable to update database ia9out-goodtogo (10)." > /dev/stderr
          endif
        endif

      else if( ! -e "$WI_TEMP"ias.out && -e "$WI_TEMP"ias.secondary) then

        # Secondary names mark as manual     
        if( `WC "$WI_TEMP""ias.secondary"` > 0) then
          if(`$AWK -v indexf="$projectmeta""ia9out-manual" -v name="$namewiki" -v stamp="secondary" -f "$WI_PATH""index.awk"` != 1 ) then
            echo "Error in index.awk: unable to update database ia9out-manual (2)."  > /dev/stderr
          endif
        endif

      else
 
        if("$msg" != "unknown") then
          if(`$AWK -v indexf="$projectmeta""ia9out-foundtemplate" -v name="$namewiki" -v stamp="$msg" -f "$WI_PATH""index.awk"` != 1) then
            echo "Error in index.awk: unable to update database ia9out-foundtemplate (8)." > /dev/stderr
          endif
        endif

      endif
