#!/bin/bash

LOGDIR=/var/log/minerva/cosmic
ETCDIR=/usr/local/minerva/etc/cosmic
VARLOG=/var/log/minerva

date +"%F %H:%M CMD $0 $*" >>$VARLOG/bearskin/`basename $0`


MINBASE=/usr/local/minerva
REPORT=$MINBASE/bin/announce
#REPORT=/bin/echo

if [ -f $LOGDIR/mode ]; then
   MODE=`cat $LOGDIR/mode`
else
   MODE=0
fi

DEVICE=$1
CMD=$2

if [ "$DEVICE" == "text" ]; then
  # device is set no null, to avoid the word appearing
  # from the echo statement
  DEVICE=
  REPORT=/bin/echo
fi

# echo $* >>$LOGDIR/cmdlist

case $CMD in
    init)
           echo 0 >$LOGDIR/mode
           chgrp minerva $LOGDIR/mode
           chmod ug+rw   $LOGDIR/mode
           ;; 

   clear|root|reset)
           $REPORT default Cosmic reset
           echo 0 >$LOGDIR/mode
           ;; 

   nextmode)
       MODE=$(($MODE + 1))

       if [ -d $ETCDIR/$MODE ]; then
          echo $MODE > $LOGDIR/mode
       else
          echo 0 > $LOGDIR/mode
          MODE=0 
       fi
       $REPORT $DEVICE `cat $ETCDIR/$MODE/name`
       ;;

    mode)
       echo $MODE
       ;;

    modestatus)
       $REPORT $DEVICE `cat $ETCDIR/$MODE/name`
       if [ -f $ETCDIR/$MODE/status ]; then
          $REPORT $DEVICE `$ETCDIR/$MODE/status`
       fi
       ;;

   base1|base2|base3|base4|base5|base6|base7|base8)
       $MINBASE/etc/cosmic/base/$CMD
       ;;

   c1|c2|c3|c4|c5|c6|c7|c8)
        CMDFULL=$ETCDIR/$MODE/$CMD
        if [ -f $CMDFULL ]; then
            RESULT=`$CMDFULL`
            if [ "$RESULT" != "" ]; then
                $REPORT $DEVICE $RESULT
            fi
        fi
       ;;

   *)
       echo "Usage: $0 <device> <init|clear|nextmode|modestatus|c1|c2|c3|c4|c5|c6|c7|c8|baseX|mode>" 
       ;;

esac


