#! /bin/bash

if [[ "$1" != RUNONLY ]]
then
  # make clean || exit 125
  git clean -x -d -f
  ./default-configure -d --prefix=$HOME/local.new \
    --with-fdtarball=dosemu-freedos-1.0-bin.tgz \
    --sysconfdir=$HOME/local.new/etc --disable-fdpp \
    --mandir=$HOME/local.new/share/man --datarootdir=$HOME/local.new/share \
    || exit 125
  make || exit 125
  make install || exit 125
  (cd ~/local.new/; ./updatedosemu.sh)
fi
truncate --size=0 /tmp/vptty-trace.log
if [[ "$2" != MANUAL ]]
then
  (sleep 5; echo -ne "keep\r" >> /tmp/vptty-linux) &
else
  rm -f ~/test-ready
  echo >> ~/test-waiting "[$(date '+%Y-%m-%d %H:%M:%S %z %b %a')] Test is waiting."
  echo >> ~/test-log "[$(date '+%Y-%m-%d %H:%M:%S %z %b %a')] Test $(git describe) is waiting."
  while [[ ! -f ~/test-ready ]]
  do
    sleep 1
  done
  echo >> ~/test-log "[$(date '+%Y-%m-%d %H:%M:%S %z %b %a')] Test $(git describe) starting."
fi
~/local.new/bin/dosemu -K ~/wwwecm/test/20220707/ \
  -E "ldebugu.com /c='r dco or= 4800;\;comment;q'" \
  -I "serial { com 2 device /tmp/vptty-dos }" -dumb -td -kt
if cat /tmp/vptty-trace.log | grep -q ';comment'
then
  echo Test success.
  if [[ "$2" == MANUAL ]]
  then
    echo >> ~/test-log "[$(date '+%Y-%m-%d %H:%M:%S %z %b %a')] Test success."
  fi
  exit 0
else
  echo Test error.
  if [[ "$2" == MANUAL ]]
  then
    echo >> ~/test-log "[$(date '+%Y-%m-%d %H:%M:%S %z %b %a')] Test error."
  fi
  exit 1
fi
