#!/bin/bash
#barStart=16088
results="nvbareml.csv"
lastEntry=$(grep "Membersince" $results | tail -n1)
echo "last entry detected as $lastEntry"
lastBar="${lastEntry:0:5}"
echo "last bar # processsed successfully was $lastBar"
barStart=$((lastBar + 1))
echo "bar processing starting from $barStart"
bar=$barStart
barEnd=$1
output="nvbareml.htm"
#browsh --debug --http-server-mode
browsh="localhost:4333/"
url="https://nvbar.org/for-the-public/find-a-lawyer/?usearch="
url=$browsh$url
delay=86
echo "processing Bar # $barStart to Bar # $barEnd"
echo "Bar #|Name|Member since|Company|Address|Phone|Fax|Email|Law school"
while [ $bar -lt $barEnd ]; do
   echo -e "Bar #: $bar"
   grep "Membersince" $results | tail -n1
   echo -e "Crawling $url$bar"
   curl -s "$url$bar" > "$output"
   echo -e "output file $output written"
   echo -e "parsing and appending results to $results"
   # Bar
   echo -n "$bar|" >> $results
   # Name
   grep -m 1 -B 3 "Bar #" $output | sed -e 's/^[[:blank:]]*//; s/[[:blank:]]*$//' | head -n 1 | tr -d '\n' >> $results
   echo -n "|" >> $results
   # Member since
   grep -m 1 "Member since" $output | tr -d '[:space:]' >> $results
   echo -n "|" >> $results
   # Company
   grep -m 1 "Company" $output | sed -e 's/^[[:blank:]]*//; s/[[:blank:]]*$//' | head -n 1 | tr -d '\n' >> $results
   echo -n "|" >> $results
   # Address
   grep -m 1 -B 1 "Phone" $output | sed -e 's/^[[:blank:]]*//; s/[[:blank:]]*$//' | head -n 1 | tr -d '\n' >> $results
   echo -n "|" >> $results
   # Phone
   grep -m 1 "Phone" $output | tr -d '[:space:]' >> $results
   echo -n "|" >> $results
   # Fax
   grep -m 1 "Fax" $output | tr -d '[:space:]' >> $results
   echo -n "|" >> $results
   # Email
   grep -m 1 "Email" $output | tr -d '[:space:]' >> $results
   echo -n "|" >> $results
   # Law school
   grep -m 1 "Law school" $output | sed -e 's/^[[:blank:]]*//; s/[[:blank:]]*$//' | head -n 1 | tr -d '\n' >> $results
   echo -e "" >> $results
   echo -e "Sleeping $delay seconds..."
   sleep $delay
   ((bar++))
done