#!/bin/sh

sms_usage() 
{
	[ $# -gt 0 ] && echo "sms: $*"
	echo "
usage: sms [options...] <phone_number> <sms_content>

Options:
	-s  use the second modem
" >&2
	exit 1
}

MSERVICE="modemd"
MINDEX="modem"

# Parse options
while [ $# -gt 0 ]; do
	arg="$1"
	case $arg in
	-s)		
		MSERVICE="modemd2"
		MINDEX="modem2"		
		;;
	-*)
		sms_usage "unknown option $arg"
		;;
	*)
		break
		;;
	esac
	shift
done

[ $# -lt 2 ] && sms_usage "insufficient arguments "

RESULT=`renv get ${MINDEX}_ready 2>/dev/null`
if [ ${RESULT:-false} != true ]; then
    echo "failed to send AT, modem not ready" >&2
    exit 1
fi

rmsg -w -t 30 ${MSERVICE} cmd sms phonenum "$1" content "$2" 2>/dev/null
RETVAL=$?
if [ ${RETVAL} == 0 ]; then
    echo OK
else
    echo ERROR
fi
exit ${RETVAL}
