      #Extract Internet Archive Templates
      #echo "$namewiki" >> "$WI_TEMP"choose.txt
      #$AWK '{if(match($0,"{{Internet Archive author[^}]*}}",k)) print k[0] }' "$WI_TEMP"article.txt >> "$WI_TEMP"choose.txt
      #echo "" >> "$WI_TEMP"choose.txt

#      if(`$AWK -v i=0 '{if(match($0,"{{Internet Archive author[^}]*}}",k)) i++ }END{print i}' "$WI_TEMP"article.txt` < 1) then
      if(`$AWK -v i=0 '{if(match($0,"{{Internet Archive author[^}]*}}",k)) i++ }END{print i}' "$WI_TEMP"article.txt` > 0) 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
#echo "nameplus=" "$nameplus"
#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"
#echo "entity=" "$entity"
#$SLEEP $PA
        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}'`
#echo "test 2"
#$SLEEP $PA
        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}'`
        #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","datatype":"time","datavalue":{"value":{"time":"\+[0-9]{1,15}-' | $AWK -F\" '{split($22,a,"+"); split(a[2],b,"-"); i=0; while(i < length(b[1])){i++; if(substr(b[1],i,1) != "0") {printf("%s",substr(b[1],i,length(b[1]) - i + 1));exit}}}'`
#echo "test 3"
        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
#echo "wdbirthdeath = " "$wdbirthdeath"
#echo "birthdeath = " "$birthdeath"
          set needdates = "yes"
        endif
        if("$wdbirthdeath" != "none" && "$birthdeath" != "none") then
          if("$wdbirthdeath" != "$birthdeath") then
            set birthdeath = "$wdbirthdeath"
          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 = "tx"
              $LUA "$WI_PATH"iaa.lua "$WI_TEMP" "$cname" "$birthdeath" "tx"
              source "$WI_PATH""ia-check.csh"
            endif

          end
        endif

        # 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
        if( -e "$WI_TEMP"ias.out) then
          set iasnumber = `$AWK -F\@ '{print $4}' "$WI_TEMP"ias.out ` 
        else
          set iasnumber = 0
        endif

        @ 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 iafinalscore = `$AWK -v ln=$iacount -F\| '{if (NR==ln) print $2}' "$WI_TEMP""ia-results" `
            set iafinalname = `$AWK -v ln=$iacount  -F\| '{if (NR==ln) print $3}' "$WI_TEMP""ia-results" `
            set ianumber = `$AWK -v ln=$iacount  -F\| '{if (NR==ln) {split($4,a,"@"); print a[4]} }' "$WI_TEMP""ia-results" `

            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" `
              #echo "---------------" >> "$WI_PATH""debug"
              #echo `$AWK -v ln=$iacount  -F\| '{if (NR==ln) print $0}' "$WI_TEMP""ia-results"` >> "$WI_PATH""debug"
              #echo "noneresults=""$noneresults" >> "$WI_PATH""debug"
              #echo "iafinalscore=""$iafinalscore" >> "$WI_PATH""debug"
              set factor = `$AWK -v n="$noneresults" -v t="$iafinalscore" 'BEGIN{i = t / n; split(i,a,"."); if(a[1] > 0){print 0; exit}; print substr(a[2],1,1)}' `
              #echo "factor=""$factor" >> "$WI_PATH""debug"
              if("$factor" > 7 && "$ianumber" != "$iasnumber") then   # 20% or less false postives are skipped
                @ 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 links" {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
              if($ianumber == $iasnumber) then
                set comment = ' '"$iafinalscore"' *'
              else
                set comment = ' '"$iafinalscore"
              endif

              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
#          set junk = ($<)
        endif      

      else
        set ianumber = 0
        echo "\n  Template already exists.\n"
#        set junk = ($<)
        $SLEEP 3
      endif # if template exists

