#!/usr/bin/perl -w
# osdServer.pl - process incoming text events based on keyword matching and
#                display using Ghosd
use strict;
use FileHandle;
use Socket;
use Thread;
use Config;
use Thread qw(async);

if( @ARGV != 1 ){ die "specify a port to listen on " }
my %osdHash = ();
my $proto    = "";
my $port     = "";
my $port1    = "";
my $sockAddr = "";
my $sockAddr1= "";
my $fullCmd  = "";
my $fullCmd1 = "";

# readKeyWordFile reads type, keywords, (text/image) parameters
# text parameters:
# X coordinate, Y coordinate, red, green, blue, font size, fade In, fade Out
# image parameters:
# image filename, X coordinate, Y coordinate
# osdServer.confg format is:  type _#_ keywords _#_ osd parameters
sub readOsdFile  
{
  open(OSDFILE,"/etc/osdServer.config") or die "no osdServer.config file: $!";

    while(<OSDFILE>){

      # ignore comment lines
      if( !/^#/ ){
        chomp($_);
        my @arrLine = split "_#_";
        $arrLine[0] =~ s/ //g;
        $osdHash{ lc($arrLine[0]) }{ $arrLine[1] } = $arrLine[2];
      }#if not a comment line

    }#for each line in file

  close(OSDFILE);

}#readOsdFile

sub matchWords
{
  my $msgString = $_[0];
  my $type      = $_[1];
#  print "msgstring  $msgString \n";
  
 # print "type  $type\n " ;
  # for the type specified
  for my $kwList ( keys %{ $osdHash{$type} } )
  {
    # for each word
    for my $checkWord ( split " ", $kwList )
    {
      $checkWord =~ s/ //g;
      if( $msgString =~ /$checkWord/i )
      { 
        if( $type eq "text" )
        {   
          $fullCmd .= qq{./text '$msgString' $osdHash{text}{$kwList} &};

           #print "$fullCmd\n";
        }else
        {  
	my @realstring = split(/:::/, $msgString);	  
          print "$realstring[0] \n";
	  $fullCmd .= qq{/usr/bin/imageGhosd  -i $osdHash{image}{$kwList} '$realstring[0]'  &};
#           print "full $fullCmd\n";
	  #print "msgstring-  $msgString \n";
        }
      }#if matching word
    }#for each check word
  }#for each key word list

}#checkText

sub matchWords1
{
  my $msgString = $_[0];
  my $type      = $_[1];
#  print "msgstring  $msgString \n";
  print "Here ia m"  ;
 # print "type  $type\n " ;
  # for the type specified
  for my $kwList ( keys %{ $osdHash{$type} } )
  {
    # for each word
    for my $checkWord ( split " ", $kwList )
    {
      $checkWord =~ s/ //g;
      if( $msgString =~ /$checkWord/i )
      { 
        if( $type eq "text" )
        {   
          $fullCmd1 .= qq{./text '$msgString' $osdHash{text}{$kwList} &};

           #print "$fullCmd\n";
        }else
        {  
	my @realstring = split(/:::/, $msgString);	  
          print "$realstring[0] \n";
	  $fullCmd1 .= qq{/usr/bin/imageGhosd  -i $osdHash{image}{$kwList} '$realstring[0]'  &};
           print "full $fullCmd1\n";
	  #print "msgstring-  $msgString \n";
        }
      }#if matching word
    }#for each check word
  }#for each key word list
}

sub sub1 { 
	while( 1 )
	{
	  my $acceptSock1 = accept(HANDLER, SERVER1) or die "accept: $!\n";
	  my($acceptPort1, $acceptIpAddr) = sockaddr_in($acceptSock1);
	
	  autoflush HANDLER 1;
	  while(my $msgString1 = <HANDLER>)
	  {
	    chomp($msgString1);
	    matchWords1($msgString1,"text");
	    matchWords1($msgString1,"image");
	
	    $fullCmd1 = `$fullCmd1` unless $fullCmd1 eq ""; 
	    $fullCmd1 = "";
	  }#while handler

	  close HANDLER or die "close HANDLER: $!\n";
	}#while forever
}

# begin main program

readOsdFile();

$proto = getprotobyname('tcp');
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!\n";
socket(SERVER1, PF_INET, SOCK_STREAM, $proto) or die "socket: $!\n";

$port     = $ARGV[0];
$port1     = $port + 1;
$port1     = 5001; 

$sockAddr = sockaddr_in($port, INADDR_ANY);
$sockAddr1 = sockaddr_in($port1,  INADDR_ANY);

bind(SERVER, $sockAddr) or die "bind: $!\n";
bind(SERVER1, $sockAddr1) or die "bind: $!\n";

listen(SERVER, SOMAXCONN) or die "listen: $!\n";
listen(SERVER1, SOMAXCONN) or die "listen: $!\n";



$Config{usethreads} or die "Recompile Perl with threads to run this program.";

my  $thr = threads->new(\&sub1);




#listen foreve 

while( 1 )
{
  my $acceptSock = accept(HANDLER, SERVER) or die "accept: $!\n";
  my($acceptPort, $acceptIpAddr) = sockaddr_in($acceptSock);

  autoflush HANDLER 1;
  while(my $msgString = <HANDLER>)
  {
    chomp($msgString);

    matchWords($msgString,"text");
    matchWords($msgString,"image");

    $fullCmd = `$fullCmd` unless $fullCmd eq ""; 
    $fullCmd = "";
  }#while handler

  close HANDLER or die "close HANDLER: $!\n";

}#while forever