package XXV::MODULES::STREAM;
use strict;

use Tools;
use Event::File;
use Locale::gettext;
use File::Basename;
use File::Find;
use Proc::Killfam;
use File::Path;

$SIG{CHLD} = 'IGNORE';

# This module method must exist for XXV
# ------------------
sub module {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $args = {
        Name => 'STREAM',
        Description => gettext('This module generate streams from recordings.'),
        Version => '0.01',
        Date => '06.09.2004',
        Author => 'xpix',
        Preferences => {
            host => {
                description => gettext('Used host of referred link inside playlist.'),
                default     => 'localhost',
                type        => 'string',
                required    => gettext('This is required!'),
            },
            netvideo => {
                description => gettext('Path from remote video directory (SambaDir).'),
                default     => '\\\\vdr\\video',
                type        => 'string',
            },
        },
        Commands => {
            playrecord => {
                description => gettext("Play the record over samba or nfs."),
                short       => 'pre',
                callback    => sub{ $obj->play_record(@_) },
                DenyClass   => 'rlist',
            },
            livestream => {
                description => gettext("Stream a channel 'cid'. This required the streamdev plugin!"),
                short       => 'lst',
                callback    => sub{ $obj->live_stream(@_) },
                DenyClass   => 'remote',
            },
        },
    };
    return $args;
}

# ------------------
sub new {
# ------------------
	my($class, %attr) = @_;
	my $self = {};
	bless($self, $class);

    # paths
    $self->{paths} = delete $attr{'-paths'};

	# who am I
    $self->{MOD} = $self->module;

    # all configvalues to $self without parents (important for ConfigModule)
    map {
        $self->{$_} = $attr{'-config'}->{$self->{MOD}->{Name}}->{$_} || $self->{MOD}->{Preferences}->{$_}->{default}
    } keys %{$self->{MOD}->{Preferences}};

    # create Template object
    $self->{tt} = Template->new(
      START_TAG    => '\<\?\%',		    # Tagstyle
      END_TAG      => '\%\?\>',		    # Tagstyle
      INTERPOLATE  => 1,                # expand "$var" in plain text
      PRE_CHOMP    => 1,                # cleanup whitespace
      EVAL_PERL    => 1,                # evaluate Perl code blocks
      ABSOLUTE     => 1,
    );

    # The Initprocess
    my $erg = $self->init or return error('Problem to initialze Module');

	return $self;
}

# ------------------
sub init {
# ------------------
    my $obj = shift || return error ('No Object!' );

    1;
}


# ------------------
sub live_stream {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $watcher = shift || return error ('No Watcher!');
    my $console = shift || return error ('No Console');
    my $channel = shift || return $console->message(gettext("No ChannelID to Stream! Please use livestream 'cid'"));

    lg sprintf('Call live stream with channel "%s" from User: %s',
        $channel,
        $console->{USER}->{Name}
        );

    if($console->typ eq 'HTML') {
        $console->{nopack} = 1;
        $console->out(sprintf('http://%s:3000/PES/%d', $obj->{host}, $channel), "audio/x-mpegurl");
    }
}

# ------------------
sub play_record {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $watcher = shift || return error ('No Watcher!');
    my $console = shift || return error ('No Console');
    my $recid   = shift || return $console->message(gettext("No RecordID to Play! Please use rplay 'rid'"));

    my $rmod = main::getModule('RECORDS');
    my $videopath = $rmod->{videodir};
    my $path = $rmod->IdToPath($recid)
        or return $console->message(gettext(sprintf('I can\'t found recid: %s', $recid)));

    lg sprintf('Call play record "%s" from User: %s',
        $path,
        $console->{USER}->{Name}
        );

    my $data;
    foreach my $file (glob("$path/???.vdr")) {
        $file =~ s/^$videopath//si;
        $file =~ s/^[\/|\\]//si;
        my $URL = sprintf("%s/%s\r\n", $obj->{netvideo}, $file);
        $URL =~s/\//\\/g
            if($URL =~ /^\\\\/sig);  # Samba \\host/xxx/yyy => \\host\xxx\yyy
        $data .= $URL;
    }

    if($console->typ eq 'HTML') {
        $console->{nopack} = 1;
        $console->out($data, "audio/x-mpegurl");
    }
}
1;
