#!/bin/bash

if [ $# -le 1 ]; then
  echo "Usage: $0 <device> <command> <value>"
  echo "   command ::= station on off volume"
  exit -1
fi

MINBIN=/usr/local/minerva/bin
LOGSTUB=/var/log/minerva/tvcontrol

TVEXEC=/usr/bin/xawtv
TVCONTROL=/usr/bin/xawtv-remote
CMD=$2
VALUE=$3

# Reference usage for finddev...
#
# We've sent (and presumably rcv'd) a request from the finddev call
# Therefore:
# 1. DEVICE is actually the output from that command, which we might
#    need and so reflect to the calling command.
# 2. We exit now with a code of '0'. All Bearskin commands must check
#    for 0. If they receive it, they must also exit immediately.

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

if [ "$DEVICE" == "" ]; then
   DEVICE=":0"
fi

export DISPLAY=$DEVICE

# Do general purpose commands (without channel designations)
case "$CMD" in
  init)
     touch $LOGSTUB.lst 
     chgrp minerva $LOGSTUB.lst 
     chmod ug+rw   $LOGSTUB.lst 
     exit 0
     ;;

  volume)
     $TVCONTROL volume $VALUE
     exit 0
     ;;

  station)
     $TVCONTROL setstation $VALUE
     exit 0
     ;;

  on)
     $TVEXEC &
     exit 0
     ;;

  off)
     killall $TVEXEC
     exit 0
     ;;
esac


echo $CMD $VALUE >$LOGSTUB.lst


