# Read input specs and generate KML files
mastfile="gentree.places"
outfile="kmldoit.places"
tmpfile="kmldoit.tmp"
echo "Enter name search:"
read varname
grep "$varname" htmldir/namfile
numlines=0
line=`grep "$varname" htmldir/namfile`
numlines=`echo "$line" | wc -l | cut -d " " -f 1`
echo "$numlines keys found for \"$varname\""
while [ $numlines -ne 1 ] ; do
   if [ $numlines -eq 0 ] ; then
      echo "No names found for \"$varname\", aborting"
      exit 1
   fi
   if [ $numlines -gt 1 ] ; then
      echo "Enter key for desired user:"
      read varname
      chr1=`echo "$varname" | cut -c 1`
      if [ "$chr1" = "@" ] ; then
         line=`grep "^$varname" htmldir/namfile`
         numlines=`echo "$line" | wc -l | cut -d" " -f 1`
      else
         echo "key must begin with @ character"
      fi
   fi
done
key=`echo "$line" | cut -d " " -f 1`
echo "Processing key: $key"
echo "Enter ancestors (a), descendants (d) or relatives (r):"
read cgitype 
if [ "$cgitype" = "a" ] ; then
   cginame="genanc"
elif [ "$cgitype" = "d" ] ; then
   cginame="gendesc"
elif [ "$cgitype" = "r" ] ; then
   cginame="genrel"
else
   echo "Invalid entry: \"$cgitype\", aborting"
   exit 1
fi
type=""
while [ "$type" = "" ] ; do
   echo "Enter records to search (birth,christening,death,buried,event,married,mevent,all):"
   read entype
   typ1=`echo "$entype" | cut -c1`
   typ12=`echo "$entype" | cut -c1-2`
   if [ "$typ12" = "bi" ] ; then
      type="1BIRT"
   elif [ "$typ1" = "c" ] ; then
      type="2CHR"
   elif [ "$typ1" = "d" ] ; then
      type="6DEAT"
   elif [ "$typ12" = "bu" ] ; then
      type="7BURI"
   elif [ "$typ1" = "e" ] ; then
      type="3EVEN"
   elif [ "$typ12" = "ma" ] ; then
      type="4MARR"
   elif [ "$typ12" = "me" ] ; then
      type="5MAEV"
   elif [ "$typ1" = "a" ] ; then
      type="ALL"
   fi
done
echo "type=$type"
rm -f $outfile
touch $outfile
./kmlruncgi.sh $cginame $key >$tmpfile 2>&1
while read gedline ; do
   akey=`echo "$gedline" | cut -d " " -f 1`
#   echo "Checking for $akey"
   if [ "$type" = "ALL" ] ; then
#      grep "^$akey" $mastfile
      grep "^$akey" $mastfile >>$outfile
   else
#      grep "^$akey $type" $mastfile
      grep "^$akey $type" $mastfile >>$outfile
   fi
done < $tmpfile
sort -u $outfile >$tmpfile
rm $outfile
mv $tmpfile $outfile
cnt=`cat $outfile | wc -l`
echo "Found $cnt places"
echo "Places file is $outfile"
./kmldoit2.sh