#! /bin/sh
#
# The regression-test driver script for the nmea2000 driver.
# CAN is a bit different from normal serial interfaces.
#

GPSD_PORT=2948
CAN_PLAYER=canplayer
CAN_UNIT="vcan0"
BUILD=0
DEBUG=0
KEEP=0

# FIXME:  Requires GNU date extensions
# Should return an empty blank string if those are not present.
STARTTIME=`date +"%s" 2>/dev/null`

# Enables us to turn debugging up high without screwing up the diff checks
# First and Second filter out gpsd log messages.
# Third filters out gps.py verbose logging
# Fourth filters out WATCH responses
# Fifth filters out DEVICE responses
# Sixth filters out VERSION responses
# Seventh filters out device fields
GPSFILTER="sed -e /^gpsd:/d -e /^gpsfake/d -e /GPS-DATA/d -e /WATCH/d -e /DEVICE/d -e /VERSION/d"

# We need to have the build directory in $GPSD_HOME to find the new gpsd
if [ "`dirname $0`" = "." ]; then
    GPSD_HOME=`pwd`
else
    GPSD_HOME=`dirname $0`
fi

version()
{
    echo
    echo `basename $0`" : Version v0.21";
    echo
}

usage()
{
    version;
    echo "usage :" `basename $0` " [-S <portnumber>] [-u <can_device>] [-p <canplayer>] [-b] <testfile>";
    echo "      :" `basename $0` " -v";
    echo "      :" `basename $0` " -h";
    echo "  Options include:";
    echo "  -D <dbglevel>   = Debuglevel for gpsd. Default is 0.";
    echo "  -S <portnumber> = Port for gpsd communication. Default is 2948.";
    echo "  -b              = Create testfile.chk instead of checking it.";
    echo "  -h              = Print this helptext and exit.";
    echo "  -k              = Keep temporary files.";
    echo "  -p <canplayer>  = Program to replay logfile. Default is \"canplayer\"";
    echo "  -u <can_device> = Used CAN device. Default is \"vcan0\"";
    echo "  -v              = Print version and exit.";
    echo "  <testfile>      = A file created by \"candump -l <can_device> ><testfile>\".";
    echo
}

device()
{
    echo
    echo "CAN device \"${CAN_UNIT}\" does not exist."
    echo "Try as root: ./gpsinit vcan "
    echo
}

can_utils()
(
    echo
    echo "Command to play CAN logfile \"${CAN_PLAYER}\" do not exist."
    echo "Try to install \"can-utils\" from your distribution."
    echo "I your distribution do not provide \"can-utils\", then"
    echo "git clone https://gitorious.org/linux-can/can-utils.git"
    echo "in a directory outside of gpsd, build and install it."
    echo "On most linux systems, \"make\", and then \"sudo make install\" works."
    echo
)

while getopts D:S:bhkp:u:v opt
do
    case ${opt} in
        D)  DEBUG=${OPTARG};;
        S)  GPSD_PORT=${OPTARG};;
        b)  BUILD=1;;
        h)  usage; exit 0;;
        k)  KEEP=1;;
        p)  CAN_PLAYER=${OPTARG};;
        u)  CAN_UNIT=${OPTARG};;
        v)  version; exit 0;;
        \?) usage; exit 1;;
    esac
done

shift $((${OPTIND} - 1))

if [ -z $1 ]; then usage; exit 1; fi

TEST_FILE=$1

if [ `ifconfig -a | grep ${CAN_UNIT} | wc -c` -eq 0 ]; then device; exit 1; fi

if [ `command -v ${CAN_PLAYER} | wc -c` -eq 0 ]; then can_utils; exit 1; fi

TMP_PID_FILE=`mktemp --suffix=.pid`
TMP_OUT_FILE=`mktemp --suffix=.out`
TMP_OUT_FILE1=`mktemp --suffix=.out1`
TMP_LOG_FILE=`mktemp --suffix=.log`

# cleanup on exit
cleanup() {
    rm -f ${TMP_PID_FILE}
    rm -f ${TMP_OUT_FILE}
    rm -f ${TMP_OUT_FILE1}
    rm -f ${TMP_LOG_FILE}
}

if [ ${KEEP} -eq 0 ]
then
    trap cleanup EXIT
fi

${GPSD_HOME}/../gpsd/gpsd -nN -G -D ${DEBUG} -S ${GPSD_PORT} \
   -P ${TMP_PID_FILE} nmea2000://${CAN_UNIT} > ${TMP_LOG_FILE} 2>&1 &

sleep 1

${GPSD_HOME}/../clients/gpspipe -d -r -w -S -o ${TMP_OUT_FILE} :${GPSD_PORT}

sleep 1

# -t makes it go way faster, but breaks it...
${CAN_PLAYER} -I${TEST_FILE} ${CAN_UNIT}=can0

sleep 1

kill `cat ${TMP_PID_FILE}`

sleep 1

# filter it
${GPSFILTER} < ${TMP_OUT_FILE} > ${TMP_OUT_FILE1}

if [ ${BUILD} -ne 0 ]
then
    cp ${TMP_OUT_FILE1} ${TEST_FILE}.chk
else
    diff ${TEST_FILE}.chk ${TMP_OUT_FILE1}
fi

# exit success, until this works.
exit 0
