#!/bin/bash

if [ $# -lt 1 ]; then
  echo "Usage: $0 <device> <command> <frequency>"
  echo "   command ::= get set on off"
  exit -1
fi

MINBASE=/usr/local/minerva
MINBIN=$MINBASE/bin
LOGSTUB=/var/log/minerva/radio
DEVICE=$1
CMD=$2
FREQ=$3

DEVICE=`$MINBIN/finddev fmradio $*`
if [ $? == 0 ]; then
  echo $DEVICE
  exit 0;
fi


if [ "$DEVICE" == "default" ]; then
	DEVICE=
else
  DEVICE="-d $DEVICE"
fi

RADIO=/usr/bin/fm

case $CMD in
  init)
    # We can't retrieve the actual frequency, so we set it to something
    # we know.
    touch $LOGSTUB.err
    echo 104.4 > $LOGSTUB.frq
    echo 0 >$LOGSTUB.ply
    $RADIO $DEVICE off
    ;;
 
  enumdev)
     $MINBIN/enumdev fmradio
     exit $?
     ;;

  get)
     cat $LOGSTUB.frq
    ;;

	getstationlist)
		cat $MINBASE/conf/fmstations.conf
		;;

  set)
     $RADIO $DEVICE $FREQ >/dev/null 2>$LOGSTUB.err
     if [ $? == 0 ]; then
        echo $FREQ > $LOGSTUB.frq
        echo 1 >$LOGSTUB.ply
     fi
    ;;

  isplaying)
    cat $LOGSTUB.ply
    ;;

  on)
     $RADIO $DEVICE `cat $LOGSTUB.frq`  >/dev/null 2>$LOGSTUB.err
     echo 1 >$LOGSTUB.ply
     ;;

  off)
     $RADIO $DEVICE off >/dev/null 2>$LOGSTUB.err
     echo 0 >$LOGSTUB.ply
     ;;

  *)
    ;;
esac

