#!/bin/bash

#########################################
# Name: licfg
#
# Summary: Configure licor LI-850
#
# Description: Set/show licor configuration options
#
# Author: k headley
#
# Copyright MBARI 2018
#
#########################################

#########################################
# Script configuration defaults
# casual users should not need to change
# anything below this section
#########################################
description="Set/show licor configuration options"

#################################
# Script variable initialization
#################################
VERBOSE="N"
unset LI_SHOW
unset LI_PORT

#################################
# Function Definitions
#################################
#################################
# name: sigTrap
# description: signal trap callback
# will interrupt 
# args: none
#################################
sigTrap(){
    exit 0;
}

#################################
# name: printUsage
# description: print use message
# args: none
#################################
printUsage(){
    echo
    echo "Description: $description"
    echo
    echo "usage: `basename $0` [options]"
	echo "Options:"
    echo "-v             : verbose output          [$VERBOSE]"
    echo "-s p           : show configuration"
	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: imsg
# description: echos with indent
# args:
#     msg: message
#  indent: indent level
########################################
imsg(){
  OIFS=$IFS
  IFS=""
  msg_pad="                                        "
  let "msg_indent=${1}"
  shift
  all="${@}"
  printf "%s%s\n" ${msg_pad:0:${msg_indent}} ${all}
  IFS=$OIFS
}

########################################
# name: msg
# description: echos with default indent
# args:
#     msg: message
########################################
msg(){
 imsg $MSG_INDENT "${*}"
}

########################################
# 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 hs:v Option
	do
		vout "processing $Option[$OPTARG]"
		case $Option in
			v ) VERBOSE="TRUE"
			;;
            s ) LI_SHOW="Y"
				LI_PORT=${OPTARG}
            ;;
			h)printUsage
				exit 0
			;;
			*) exit 0 # getopts outputs error message
			;;
		esac
	done
}

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


# Argument processing
# Accepts arguments from command line 
# or pipe/file redirect
# Comand line settings override config
# file settings

if [ -t 0 ]
then
	if [ "$#" -eq 0 ];then
		printUsage
		exit -1
	else
        # 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
	fi
fi

vout "LI_SHOW : ${LI_SHOW}"
vout "LI_PORT : ${LI_PORT}"
vout "VERBOSE : ${VERBOSE}"


#echo "open screen session..."
#/usr/bin/screen -dmS LICOR ${LI_PORT} 9600
#echo "disabling output..."
#/usr/bin/screen -S LICOR -X stuff "<li850><cfg><outrate>0</outrate></cfg></li850>"
#echo "requesting config"
#/usr/bin/screen -S LICOR -X stuff "<li850><cfg>?</cfg></li850>"
#echo "reading..."
#/usr/bin/screen -S LICOR -X readreg foo ${LI_PORT}
#echo "reg $foo"
#echo "enabling output..."
#/usr/bin/screen -S LICOR -X stuff "<li850><cfg><outrate>0</outrate></cfg></li850>"
#echo "ending session..."
#/usr/bin/screen -S LICOR -X quit
#/usr/bin/screen -S LICOR -X kill

stty -f ${LI_PORT} raw speed 9600 cs8 -ignpar -cstopb -echo

echo "turning output off..."
echo "<li850><cfg><outrate>0</outrate></cfg></li850>" > ${LI_PORT}
#foo=" "
#while [ ${#foo} -gt 0 ]
#do
#read foo < ${LI_PORT}
#echo "read $foo"
#done

#echo "requesting config"
#echo "<li850><cfg>?</cfg></li850>"  > ${LI_PORT}
#read foo < ${LI_PORT}
#echo "config: $foo"
#echo "turning output on..."
#echo "<li850><cfg><outrate>1</outrate></cfg></li850>" > ${LI_PORT}



#"<li850><cfg><outrate>0</outrate></cfg></li850>"
#"<li850><cfg><outrate>1</outrate></cfg></li850>"
#"<li850><cfg>?</cfg></li850>"
#"<li850><rs232>?</rs232></li850>"
#"<li850><rs232><raw>?</raw></rs232></li850>"
#
#"<li850><data>?</data></li850>"

