#!/usr/bin/perl

  # $Id: hpsetdisp.pl 11 2006-03-22 01:21:03Z yaakov $

  # hpsetdisp.pl  
  # Connects to a JetDirect equipped HP printer and uses 
  # HP's control language to set the ready message on the
  # LCD display.  Takes an IP address and message on the
  # command line. My favorite message is "INSERT COIN".
  # Keep in mind the limitations of the display when composing 
  # your clever verbiage.
  # 
  # THIS PROGRAM IS PROVIDED WITH NO WARRANTY OF ANY KIND EXPRESSED OR IMPLIED
  # THE AUTHOR CANNOT BE RESPONSIBLE FOR THE EFFECTS OF THIS PROGRAM
  # IF YOU ARE UNCERTAIN ABOUT THE ADVISABILITY OF USING IT, DO NOT!
  #
  # Yaakov (http://kovaya.com/)
  #
  # 10.21.62.16

use strict;
use warnings;

my $rdymsg = undef;

#unless (@ARGV) { print "usage: $0 <ip address> \"<RDYMSG>\"\n" ; exit }
#if ($ARGV[3]) { print "Did you forget the quotes around your clever message?\n" ; exit }
#my $peeraddr = $ARGV[0];
#my $rdymsg = $ARGV[1];
#chomp $peeraddr;

$rdymsg = $ARGV[0] if(@ARGV);

my $peeraddr = "10.21.62.16";

my @msgs = (
	"TOUCH ME",
	"STEP AWAY",
	"SET TO STUN",
	"SCORE = 3413",
	"FEED ME",
	"NEED MORE SPACE",
	"POUR ME A DRINK",
	"NICE SHIRT",
	"NEED COFFEE",
	"NO PRINT FOR YOU",
	"RADIATION LEAK",
	"HANDS UP",
	"PRESS MY BUTTON",
	"TAKE ME HOME",
	"LOOKS LIKE RAIN",
	"HELLO WORLD",
	"NICE HAIR",
	"NEED A MINT?",
	"BE GENTLE",
	"BE KIND",
	"INSERT DISK",
	"COME CLOSER",
	"TAKE A BREAK",
	"INSERT COIN",
	"SAVE A TREE",
	"\$2 = 1 CREDIT",
);

$rdymsg = $msgs[int(rand(@msgs))] unless(@ARGV);


use IO::Socket;
my $socket = IO::Socket::INET->new(
    PeerAddr  => $peeraddr,
    PeerPort  => "9100",
    Proto     => "tcp",
    Type      => SOCK_STREAM
    ) or die "Could not create socket: $!";

my $data = <<EOJ
\e%-12345X\@PJL JOB
\@PJL RDYMSG DISPLAY="$rdymsg"
\@PJL EOJ
\e%-12345X
EOJ
;

print $socket $data;

system "logger 'Set printer msg to $rdymsg'"
