#!/bin/bash
# PODNAME: cube
# ABSTRACT: quick script to send things to hypnocube rather than piping yourself

# easy way to write to cube rather than echoing to the pipe

function show_usage {
  echo '

It is easiest to use the cube command to control the cube.

Colors can be either by HTML name (red,blue), by rgb values (255,255,0) or by 
hex triplet (#FF0000 or 0xFF0000). Short versions of these are also possible
#FF0 or 0X0ff.

Additionally "random" picks a random color and "clear"  or "off" is an alias for black.

Cube Commands

update   
    update the sube with the commands that have been entered

all, cls, clear {color}
    set all the LEDS to the named color
    requires update
    a random color will set the entire cube to a random value
    cube all 255,255,255 : update
    cube all 255 0 0 : update

pix, pixel, xyz  {x} {y} {z} {color}
    set a single pixel referenced with coordinates to a color
    requires update
    cube pixel 3 2 1 red : update

pos, position, index, i {index} {color}
    set a single pixel referenced with an index 0..63 to a color
    requires update
    cube index 23 red : update

x, xp, xplane {plane} {color} 
    set all LEDS in an X plane to a color
    requires update
    a random color will set all LEDS in the plane to different values
    cube xp 1 darkslategray : update

y, yp, yplane {plane} {color}
    set all LEDS in an Y plane to a color
    requires update
    a random color will set all LEDS in the plane to different values
    cube yplane 1 pink : update

z, zp, zplane {plane} {color}
    set all LEDS in an Z plane to a color
    requires update
    a random color will set all LEDS in the plane to different values
    cube z 1 goldenrod : update

sphere {color}  
    create a sphere of LEDS in the given color
    requires update
    a random color will set the entire sphere to a random value
    cube sphere random : update

sleep {time}
    pause the activity for a number of seconds

pulse {times} {color}
    clears the cube and the pulses the color for a maximum of 3 times
    requires update
    cube pulse 2 green

flash {times}   
    flash the current cube setting on and off for a number of times
    this happends in the background so can be interrupted by another command
    does not requires update
    cube flash 10

matrix {color}
    run a matrix effect in the background, continues until another command
    is called
    does not require update
    cube matrix random ; sleep 60 ; cube sphere darkblue : update

colors  
    show each LED as a different color 
    requires update
    cube colors : update

'
  exit
}

if [ "$1" == "-h" -o "$1" == "--help" ] ; then
  show_usage
fi

CUBE=/tmp/hypnocube

if [ ! -e $CUBE ] ; then
  ps ax|grep cubepipe |grep -vq grep
  if [ "$?" == "1" ] ; then
    echo STDERR "starting cube as it is not running"
    cubepipe --daemon
    sleep 3
  else
    echo STDERR "cubepipe is running, but there is no pipe ($CUBE) - restarting"
    cubepipe --restart --daemon
    sleep 3
  fi
fi

status=1
if [ ! -p $CUBE ] ; then
  echo STDERR "$CUBE is not a pipe"
else
  if [ "$1" != "" ] ; then
    # make sure we do not follow on from last instruction
    echo ": $@" >> $CUBE
    status=0
  fi
fi
# perl pod may get added so lets exit before thats an issue
exit $status 

__END__

=pod

=encoding UTF-8

=head1 NAME

cube - quick script to send things to hypnocube rather than piping yourself

=head1 VERSION

version 1.900.400

=head1 AUTHOR

Kevin Mulholland <moodfarm@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Kevin Mulholland.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
