#!/usr/bin/perl -w

=head1 NAME

 ReMoot - the pseudo-universal remote control wrapper

=head1 VERSION

 The documentation below refers to the "remoot-remote" program in ReMoot 0.9

=head1 DESCRIPTION

 ReMoot is a command wrapper for a number of media programs running on
 GNU/Linux systems and possibly other *nix systems as well. The official
 version of the package is maintained by Andreas Wallberg
 (andreas.wallberg@gmail.com) and Joel Berglund (joel.berglund@gmail.com).

 "remoot-remote" is a front-end to the daemoot daemon and is written in PerlTk

 "remoot-remote" is licenced under the Perl Artistic License 2.0. Please see

 http://www.perlfoundation.org/artistic_license_2_0 and
 http://www.fsf.org/licensing/licenses/index_html#PerlLicense for details.

 Copyright  2007 Andreas Wallberg <andreas.wallberg@gmail.com>

=cut

# We need Perl::Tk for this program

use Tk;
use strict;


# ============================================================================
# MAIN PROGRAM
# ============================================================================


# Check if daemoot is running and start it if not
# -----------------------------------------------

my @running = `ps -u $ENV{USER} -o command`;

my $instance;
foreach my $app ( @running ) {

    $instance++ if $app =~ m/(perl)+.+daemoot/;

}

system("daemoot &") unless $instance;

# Making the main window
# ----------------------

my $mw = MainWindow->new;
$mw->title("ReMoot-remote");

# Making buttons that call remoot using different switches.


# PREVIOUS
# --------

$mw->Button(   
                -text       => "prev" ,
                -command    => sub { execute_arg("prev") },

            )->pack(
                    -side       => 'left',
                    -expand     => 1 ,
                    -fill       => 'y' ,
            );


# PLAY/PAUSE
# ----------

$mw->Button(
                -text               => "play/pause" ,
                -background         => 'OliveDrab3' ,
                -activebackground   => 'OliveDrab2' ,
                -command            => sub { execute_arg("playpause") },

            )->pack(
                        -side       => 'left' ,
                        -expand     => 0 ,
                        -fill       => 'y' ,
            );


# NEXT
# ----

$mw->Button(
                -text       => "next" ,
                -command    => sub { execute_arg("next") },

            )->pack(
                        -side   => 'left' ,
                        -expand => 0 ,
                        -fill   => 'y' ,
            );


# STOP
# ----

$mw->Button(
                -text               => "stop" ,
                -background         =>"tomato3" ,
                -activebackground   => "tomato2" ,
                -command            => sub { execute_arg("stop") },

            )->pack(
                        -side       => 'right' ,
                        -expand     => 1 ,
                        -fill       => 'y' ,
            );


# VOLUP
# -----

$mw->Button(
                -text               => "vol +" ,
                -background         => "LightSkyBlue3" ,
                -activebackground   => "LightSkyBlue2" ,
                -command            => sub { execute_arg("volup") },

            )->pack(
                        -side       => 'top' ,
                        -expand     => 0 ,
                        -fill       => 'x' ,
            );

# VOLDOWN
# -------

$mw->Button(
                -text               => "vol -" ,
                -background         => "LightSkyBlue3" ,
                -activebackground   => "LightSkyBlue2" ,
                -command            => sub { execute_arg("voldown") },

            )->pack(
                        -side       => 'bottom' ,
                        -expand     => 1 ,
                        -fill       => 'x' ,
            );

# MUTE
# ----

$mw->Button(
                -text               => "mute" ,
                -background         => "SkyBlue4" ,
                -activebackground   => "SkyBlue3" ,
                -command            => sub { execute_arg("mute") },

            )->pack(
                        -side       => 'bottom' ,
                        -expand     => 0 ,
                        -fill       => 'x'
            );

MainLoop;

# ============================================================================
# SUBROUTINES
# ============================================================================

sub execute_arg {

    my $argument = $_[0];

    open (FIFO, ">" , "$ENV{HOME}/.remoot/fifo")
    or die "$ENV{HOME}/.remoot/fifo does not seem to be accessible:\n$!";

    print FIFO "$argument\n"
    or die "$ENV{HOME}/.remoot/fifo does not seem to be accessible:\n$!";

    close FIFO;

}
