#!/usr/bin/env perl
#****** lyricue_remote/setup
# NAME
#   Setup section
# DESCRIPTION
#   Loads required modules, sets some global variables,
#   and other global things
# SOURCE
#

#
# Modules we use.
#
use strict;
use warnings;
use POSIX;
use IO::Socket::INET;
my $profile = "Default";

if ($ARGV[0]) {
    foreach (0 .. (@ARGV - 1)) {
        if ($ARGV[$_] eq "-v") {
            print "Lyricue Remote\n";
            exit;
        } elsif ($ARGV[$_] eq "-p") {
            $profile = $ARGV[$_ + 1];
            $ARGV[$_ + 1] = "";
        } elsif ($ARGV[$_] eq "-h") {
            print "\nUsage: lyricue_remote <-v> <-s> <command>\n\n";
            print "\t-v:              Prints Lyricue version information & exits\n";
            print "\t-p profile:      Sets the profile to send messsages to\n";
            print "\tcommand: Can be any of next_page, prev_page, next_song, prev_song,\n";
            print "\t         next_point, blank, osd, close or custom\n";
            print "\t         If custom then the next 3 arguments will be sent without parsing\n";
            print "\t         to the server\n";
            exit;
        } elsif ($ARGV[$_] eq "") {

            # ignore
        } elsif ($ARGV[$_] eq "next_page") {
            update_display("display", "next_page", "");
        } elsif ($ARGV[$_] eq "next_song") {
            update_display("display", "next_song", "");
        } elsif ($ARGV[$_] eq "prev_page") {
            update_display("display", "prev_page", "");
        } elsif ($ARGV[$_] eq "prev_song") {
            update_display("display", "prev_song", "");
        } elsif ($ARGV[$_] eq "blank") {
            update_display("blank", "", "");
        } elsif ($ARGV[$_] eq "next_point") {
            update_display("next_point", "", "");
        } elsif ($ARGV[$_] eq "close") {
            system("pkill -f lyricue_display");
            system("pkill -9 -f lyricue_display");
        } elsif ($ARGV[$_] eq "osd") {
            my $osd = "";
            if ($ARGV[$_+1]) {
                $osd = $ARGV[$_+1];
                $osd =~ s/:/#SEMI#/g;
                $osd =~ s/\n/#BREAK#/g;
            }
            update_display("osd",$ARGV[$_+2], $osd);
        } elsif ($ARGV[$_] eq "custom") {
            update_display($ARGV[$_+1],$ARGV[$_+2],$ARGV[$_+3]);
            exit;
        }
    }
}

#***

#****f* lyricue_remote/update_display
# NAME
#   update_display --
# SYNOPSIS
#   update_display ($command, $primary, $secondary)
# FUNCTION
#   Open a connection the the server and send a command. Status is returned
# INPUTS
#   $command - Command to send
#   $primary - First parameter to send
#   $secondary - Second parameter to send
# OUTPUT
#   Updated display
# SOURCE
#
sub update_display {
    my ($command, $primary, $secondary) = @_;

    if (!defined($secondary)) {
        $secondary = "";
    }
    if (!defined($primary)) {
        $primary = "";
    }
    my @lines = split("\n",`mysql -s -N -ulyric lyricDb -e 'SELECT host FROM status WHERE type LIKE "%normal%" OR type LIKE "%simple%" OR type LIKE "%headless%"'`);

    foreach my $entry (@lines) {
        my ($hostname,$port) = split(":", $entry,2);
        if ( my $server = IO::Socket::INET->new(
                Proto    => "tcp",
                PeerAddr => $hostname,
                PeerPort => $port
            )) {
            print $server $command . ":" . $primary . ":" . $secondary . "\n";
            if (defined(my $status = <$server>)) {
                print $status;
            }
            close($server);
        }
    }

}

#***
