#!/bin/bash

#########################################
# Name: lcm-spy.sh
#
# Summary: start lcm-spy
#
# Description: launches lcm-spy
# so that CLASSPATH includes message
# decode classes
#
# Author: headley
#
# Copyright 2016 MBARI
#
#########################################

#########################################
# Script configuration defaults
# casual users should not need to change
# anything below this section
#########################################
description="Launches lcm-spy"

#################################
# Script variable initialization
#################################
VERBOSE="Y"
LCM_URL=""
LCMS_CLASSPATH="${CLASSPATH}"
LRAUV_JAR_NAME="lcmtypes.jar"
LRAUV_JAR_PATH=`pwd`
LRAUV_JAR="${LRAUV_JAR_PATH}/${LRAUV_JAR_NAME}"
LCM_JAR="/usr/local/share/java/lcm.jar"
LCM_SPY="lcm-spy"

#################################
# name: printUsage
# description: print use message
# args: none
#################################
printUsage(){
echo
echo "Description: $description"
echo
echo "usage: `basename $0` [options]"
echo "Options:"
echo "-d <path> : lcmtypes.jar dir  [$LRAUV_JAR_PATH]"
echo "-l <url>  : LCM URL           [$LCM_URL]"
echo "-v        : verbose output    [$VERBOSE]"
echo "-h        : print use message"
echo ""
echo
}

########################################
# name: vout
# description: print verbose message to stderr
# args:
#     msg: message
########################################
vout(){
if [ "${VERBOSE}" == "Y" ] || [ "${VERBOSE}" == "TRUE" ]
then
echo "$1" >&2
fi
}

########################################
# name: exitError
# description: print use message to stderr
# args:
#     msg:        error message
#     returnCode: exit status to return
########################################
exitError(){
echo >&2
echo "`basename $0`: error - $1" >&2
echo >&2
exit $2
}

########################################
# name: processCmdLine
# description: do command line processsing
# args:
#     args:       positional paramters
#     returnCode: none
########################################
processCmdLine(){
OPTIND=1
vout "`basename $0` all args[$*]"

while getopts d:l:hv Option
do
vout "processing $Option[$OPTARG]"
case $Option in
d )
    if [ -f ${OPTARG} ]
    then
    LRAUV_JAR="${OPTARG}"
    else
    LRAUV_JAR="${OPTARG}/${LRAUV_JAR_NAME}"
    fi
;;
l ) LCM_URL="-l ${OPTARG}"
;;
v ) VERBOSE="TRUE"
;;
h)
	printUsage
	echo
	lcm-spy -h
	echo
	exit 0
;;
*) exit 0 # getopts outputs error message
;;
esac
done
}

##########################
# Script main entry point
##########################


# Argument processing

# this ensures that quoted whitespace is preserved by getopts
# note use of $@, in quotes

# originally was processCmdLine $*
processCmdLine "$@"

#let "i=$OPTIND-1"
#while [ "${i}" -gt 0  ]
#do
#shift
#let "i-=1"
#done


if [ ! -d ${APP_DIR} ]
then
exitError "Directory not found [$APP_DIR]" 1
fi

if [ ! -f ${LRAUV_JAR} ]
then
echo "LCM jar not found [$LRAUV_JAR] - message decode won't work"
echo "See lrauv-application/Makefile" 
fi

if [ ! -f ${LCM_JAR} ]
then
exitError "LCM jar not found [$LCM_JAR]" 1
fi

CLASSPATH_BAK=$CLASSPATH
LCMS_CLASSPATH=${CLASSPATH}:${LRAUV_JAR}:${LCM_JARS}
export CLASSPATH=$LCMS_CLASSPATH

vout "VERBOSE        : $VERBOSE"
vout "LRAUV_HOME     : $LRAUV_HOME"
vout "LCM_URL        : $LCM_URL"
vout "LRAUV_JAR      : $LRAUV_JAR"
vout "LCM_JAR        : $LCM_JAR"
vout "LCMS_CLASSPATH : $LCMS_CLASSPATH"

${LCM_SPY} ${LCM_URL}

export CLASSPATH=$CLASSPATH_BAK

