#!/bin/ksh
#FILE: pbfuncs

function lookup
 {
  find_pattern "search for" $1
  matches=$?
  echo "matches is $matches"
  if [ $matches -gt 0 ]
  then
   get_matches
   clear
   echo "Found $match_array_count matches\n"
   print_matches
  else
   echo "NO MATCH FOUND"
  fi
 echo "DONE, <cr> to cont... \c"
 read junk
 }

function add_record
 {
  clear
  cat <<END
   You will be prompted for the following info IN THE FOLLOWING ORDER:
	Lastname:
	Firstname: 
	Middle Initial:
	Street Address:
	Apt:
	City:
	State:
	Zip:
	Phone:
	Comment:
   Leave non-applicable fields empty by hitting return at their prompt.
END
echo "\n<cr> to cont... "
read junk
  while true
   do
    clear
    echo "Lastname: \c"
    read lastname
    echo "Firstname:  \c"
    read firstname
    echo "Middle Initial: \c"
    read middle_initial
    echo "Street Address: \c"
    read street_address
    echo "Apt: \c"
    read apt
    echo "City: \c"
    read city
    echo "State: \c"
    read state
    echo "Zip: \c"
    read zip
    echo "Phone: \c"
    read phone
    echo "Comment: \c"
    read comment
    outline=$lastname
    outline=$outline:$firstname
    outline=$outline:$middle_initial
    outline=$outline:$street_address
    outline=$outline:$apt
    outline=$outline:$city
    outline=$outline:$state
    outline=$outline:$zip
    outline=$outline:$phone
    outline=$outline:$comment
    cp $dbfile ${dbfile}.tmp
    echo $outline >> ${dbfile}.tmp
    sort ${dbfile}.tmp > $dbfile
    rm ${dbfile}.tmp
    echo "\n$outline added to dbfile"
    echo "\nDo you wish to add another record (y/n)? \c"
    read answer
    if [ "$answer" != "y" -a "$answer" != "Y" ]
    then
     return
    fi
   done
 }


function change_record
 {
  find_pattern "change"
  matches=$?
  if [ $matches -eq 0 ]
  then
   return 1
  else
   exec 4< $dbfile
   > changeout.$$
   while read -u4 inline
    do
     count=`echo $inline |grep -ic $pattern`
     if [ $count -gt 0 ]
     then
      print_record "$inline"
      echo "Change this record (y/n)? \c"
      read change_answer
      case $change_answer in
       y|Y) lastname=`echo $inline |cut -d: -f1`
            firstname=`echo $inline |cut -d: -f2`
            middle_initial=`echo $inline |cut -d: -f3`
            street_address=`echo $inline |cut -d: -f4`
            apt=`echo $inline |cut -d: -f5`
            city=`echo $inline |cut -d: -f6`
            state=`echo $inline |cut -d: -f7`
            zip=`echo $inline |cut -d: -f8`
            phone=`echo $inline |cut -d: -f9`
            comment=`echo $inline |cut -d: -f10`
            for var in lastname firstname middle_initial street_address apt \
	               city state zip phone comment
             do
	      change_field $var
             done
             outline=$lastname
             outline=$outline:$firstname
             outline=$outline:$middle_initial
             outline=$outline:$street_address
             outline=$outline:$apt
             outline=$outline:$city
             outline=$outline:$state
             outline=$outline:$zip
             outline=$outline:$phone
             outline=$outline:$comment
             echo $outline >> changeout.$$
	     echo "<cr> to cont..."
	     read junk
	    ;;
	 *) echo $inline >> changeout.$$
	    ;;#don't change a matching record
      esac
     (( count = $count + 1 ))
   else
    echo $inline >> changeout.$$
   fi
   done
   sort changeout.$$ > $dbfile
   rm changeout.$$
   return 0
  fi
 }

function change_field
 {
  field=$1
  eval echo "$field is \${$field}, change it \(y/n\)? \\\c"
  read change_answer
  case $change_answer in
     y|Y) echo "Enter new $field: \c"
	  read newfield
	  eval $field="\$newfield"
          #eval echo "\\${field} is \${$field}"
	  ;;
       *) eval echo "\${$field} NOT changed"
	  ;;
    esac
 }

function delete_record
 {
  find_pattern "delete"
  matches=$?
  if [ $matches -eq 0 ]
  then
   return 1
  else
   exec 4< $dbfile
   > delout.$$
   while read -u4 inline
    do
     count=`echo $inline |grep -ic $pattern`
     if [ $count -gt 0 ]
     then
      print_record "$inline"
      echo "Delete this record (y/n)? \c"
      read delete_answer
      case $delete_answer in
       y|Y) echo "Are you sure you want to delete THIS record (y/n)? \c"
	    read really_do_it
	    case $really_do_it in
	     y|Y) echo "RECORD DELETED!!!!!!"
		  ;;
	       *) echo "NOT deleting record!!!"
		  echo $inline >> delout.$$
		  ;;
	    esac
	    ;;
	 *) echo "NOT deleting record!!!"
	    echo $inline >> delout.$$
	    ;;
      esac
      (( matches = $matches + 1 ))
     else
      echo $inline >> delout.$$
     fi
    done
   mv delout.$$ $dbfile
  fi
  echo "DONE... <cr> to continue... \c"
  read junk
  return 0
 } 

function print_it
 {
  exec 4< $dbfile
  echo "Destination printer? \c"
  read dest_printer
  > print.$$
  while read -u4 inline
   do
    print_record "$inline" >> /tmp/print.$$
   done
  if [ -z "$dest_printer" ]
  then
   lp /tmp/print.$$
  else
   lp -d$dest_printer /tmp/print.$$
  fi
  if [ $? -eq 0 ]
  then
   echo "File sent to $dest_printer, <cr> to cont... \c"
  else
   echo "Print error!!, <cr> to cont... \c"
  fi
  read junk
  rm /tmp/print.$$
 }

function print_record
 {
  lastname=`echo $1 |cut -d: -f1`
  firstname=`echo $1 |cut -d: -f2`
  middle_initial=`echo $1 |cut -d: -f3`
  street_address=`echo $1 |cut -d: -f4`
  apt=`echo $1 |cut -d: -f5`
  city=`echo $1 |cut -d: -f6`
  state=`echo $1 |cut -d: -f7`
  zip=`echo $1 |cut -d: -f8`
  phone=`echo $1 |cut -d: -f9`
  comment=`echo $1 |cut -d: -f10`
  echo "\n$lastname, $firstname $middle_initial"
  echo "$street_address\c"
  if [ ! -z "$apt" ]
  then
   echo ", Apt $apt"
  else
   echo
  fi
  echo "$city, $state     $zip"
  echo "$phone"
  echo "$comment\n"
 }

function find_pattern
 {
  clear
  prompt=$1
  if [ -z "$prompt" ]
  then
   $prompt="search for"
  fi
  pattern=$2
  if [ -z "$pattern" ]
  then
   echo "\n\nPattern for record to $1? \c"
   read pattern
   if [ -z "$pattern" ]
   then
    return 0
   fi
  fi
  count=`grep -ic $pattern $dbfile`
  echo "Found $count matches"
  if [ $count -eq 0 ]
  then
   return 0
  else
   return $count
  fi
 }

function get_matches
 {
   match_array_count=1
   matchcount=$count
   exec 4< $dbfile
   while read -u4 inline
    do
     count=`echo $inline |grep -ic $pattern`
     if [ $count -gt 0 ]
     then
      match_array[$match_array_count]="$inline"
      (( match_array_count = $match_array_count + 1 ))
     fi
    done
    (( match_array_count = $match_array_count - 1 ))
   return 0
 }

function print_matches
 {
   count=1
   while [ $count -le $match_array_count ]
    do
     echo "$count:"
     print_record "${match_array[$count]}"
      if [ $count -lt $match_array_count ]
      then
       echo "<cr> for next match... "
       read junk
      fi
     (( count = $count + 1 ))
    done
 }
