#!/bin/sh

TESTOPTS=""
export TESTOPTS
STARTALK="./startalk"
#STARTALK="/bin/true"
TEST="./runtest"

if [ ! -x "$STARTALK" ]; then
  echo "Startalk isn't built!  Run 'make' first."
  exit 1
fi

if [ -f .stdev ]; then
  stdev="`cat .stdev`"
fi
if [ -r /dev/pcsphone ]; then
  stdev="/dev/pcsphone"
fi

echo "OK, I'm going to ask you a few questions."
echo "To cancel, hit CTRL-C at any time.  During the tests,"
echo "you may have to hit CTRL-C several times."
echo "For help, type ?"

echo ""

echo "First, let's just make sure the program compiled"
echo "and that it's basically working right.  This doesn't"
echo "send anything at all to your phone."
echo ""
while /bin/true; do
  printf "Hit [ENTER] to start the tests (or CTRL-C to cancel) >"
  read blah
    case "$blah" in
      '') break;;
      \?) echo
          echo "To start the basic tests, hit [ENTER]."
          echo ""
          echo "These tests will just run StarTalk and make sure that it"
          echo "is working internally.  No communication will be done"
          echo "with your phone."
          echo ""
          echo "You can hit CTRL-C to cancel."
          echo ""
          continue;;

      *) echo "Hit [ENTER] to continue, or hit CTRL-C to cancel.";;
    esac
done

if $TEST test.d/[0-2][0-9]; then
  echo "OK, your copy of startalk compiled OK, and basic command-line"
  echo "and file parsing works fine.  That's a good start!"
else
  echo "Something went wrong in those tests."
  echo "The files which have the error messages in them are listed above."
  echo "I'm giving up; if you think you've fixed the problem, run me"
  echo "again."
  exit 2
fi

while /bin/true
do
  echo "Plug the phone into your computer now."
  printf "%s" "Path to device where phone is connected? [$stdev] >"
  read newdev
  echo

  if [ "$newdev" = "?" ]; then
    echo "I need the full path to the device where your StarTAC phone"
    echo "is connected.  This will probably be a serial port."
    echo "For information on how your operating system names its"
    echo "serial port devices, consult the documentation for your system."
    echo "Hit CTRL-C to cancel."
    echo
    continue
  fi
  if [ -z "$newdev" ]; then
    newdev="$stdev"
  fi
  if [ -z "$newdev" ]; then
    echo "I need a device name to continue.  Hit CTRL-C to cancel."
    continue
  fi

  echo "OK, I'm going to test that device..."
  
  if [ "$newdev" != "/dev/pcsphone" ]; then
    TESTOPTS="$TESTOPTS -p $newdev"
  fi
  if $TEST 30; then
    echo "It worked!"
  else
    echo 
    cat test.d/30.errs
    echo
    echo "An error occured testing your phone.  The error message should"
    echo "appear above."
    echo "Please make sure your phone is plugged in, turned on, and connected"
    echo "to the device you've specified."
    echo "Hit CTRL-C to cancel."
    echo
    continue
  fi
 
  break
done

echo "$newdev" >.stdev_new || exit 1
mv .stdev_new .stdev || exit 1

echo
echo "Now I'm going to run some phonebook and settings reading tests."
if $TEST test.d/3[1-9]; then
  echo "Those worked too!  Looking good so far."
else
  echo "Something went wrong in those tests."
  echo "The files which have the error messages in them are listed above."
  echo "I'm giving up; if you think you've fixed the problem, run me"
  echo "again."
  exit 2
fi

if [ ! -s test.d/entry99.out ]
then
  entry99empty="yes"
elif grep '^position: 99' test.d/entry99.out >/dev/null &&
     grep '^name: Test' test.d/entry99.out >/dev/null &&
     grep '^company: Test' test.d/entry99.out >/dev/null &&
     grep '^phone-home: 0123456789' test.d/entry99.out >/dev/null 
then
  entry99empty="yes"
elif [ "`grep -v '^position: 99' test.d/entry99.out |grep -v '^$' |wc -l |sed -e 's/^  *//' -e 's/  *$//'`" = "0" ]
then
  entry99empty="yes"
else
  echo
  echo "The write tests overwrite entry number 99, and you"
  echo "have an entry there currently.  You'll have to remove"
  echo "the entry to continue with the tests."
  echo "Here is the entry:"
  echo
  cat test.d/entry99.out
  echo
  exit 1
fi

echo
echo "Now I'm going to run some phonebook writing tests."
echo "These tests should only overwrite entry number 99,"
echo "and you don't seem to have an entry there."

while /bin/true
do
  printf "%s" "To continue with the write tests (overwriting entry 99), enter y >"
  read blah
  case "$blah" in
    [yY]*) break;;
    \?) echo
        echo "To test writing to the StarTAC phonebook, I need to"
        echo "actually write to an entry.  I use Entry 99, the last"
        echo "entry in your phonebook, to do this."
        echo "The tests shouldn't do anything besides overwrite this"
        echo "entry, but if something goes wrong just about anything"
        echo "could happen.  So I'm asking you to enter y to give"
        echo "your permission."
        echo "You can hit CTRL-C to cancel."
        echo
        continue;;

    *) echo "Enter y to continue, or hit CTRL-C to cancel.";;
  esac
done

if $TEST test.d/[5-7][0-9]
then
  echo "All of the write tests worked, too!"
else
  echo "Something went wrong in those tests."
  echo "The files which have the error messages in them are listed above."
  echo "I'm giving up; if you think you've fixed the problem, run me"
  echo "again."
  exit 2
fi

echo
echo "Congratulations, you seem to have a fully working copy"
echo "of StarTalk!"

exit 0

