first pass at something ready to try/test
[iceplex.git] / yashout
1 #!/usr/bin/perl
2 #!/usr/bin/perl -w
3 # (the Shout library causes warnings if we use -w)
4 #
5 # yashout
6 #   Usage: yashout filename mountpoint [ port ]
7 #
8 # Copyright (c) 2003 Ivan Kohler
9 # All rights reserved.
10 # This program is free software; you can redistribute it and/or modify it under
11 # the same terms as Perl itself.
12
13 use strict;
14 use subs qw(daemonize);
15 use Shout;
16
17 #read options from commandline
18 my($filename, $mountpoint, $port) = @ARGV;
19
20 my $conn = new Shout (
21   ip          => 'localhost',
22   port        => $port || 8000,
23   mount       => $mountpoint,
24   icy_compat  => 0,
25   dumpfile    => undef,
26   name        => 'Fonestream audio',
27   url         => 'http://www.fonestream.com/',
28   genre       => 'talk',
29   description => 'The easiest way to webcast.',
30   bitrate     => 64,
31   ispublic    => 0,
32 );
33 $conn->connect or die "Failed to connect: ". $conn->error;
34
35 open(FILE,"<$filename") or die "Can't open $filename: $!";
36
37 daemonize(); #fork/disconnect so plex.pls will return to client
38
39 #spool file to server
40 my( $buffer, $bytes ) = ( '', 0 );
41 while ( ( $bytes = read( FILE, $buffer, 4096 ) ) > 0 ) {
42   $conn->sendData( $buffer ) && next;
43   # error handling???
44   print STDERR "Error while sending: ", $conn->error, "\n";
45   last;
46 } continue {
47   $conn->sleep;
48 }
49
50 $conn->disconnect;
51
52 sub daemonize {
53   chdir "/" or die "Can't chdir to /: $!";
54   open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
55   defined(my $pid = fork) or die "Can't fork: $!";
56   if ( $pid ) {
57     #print "yashout started with pid $pid\n"; #logging to $log_file\n";
58     exit unless $pid_file;
59     #my $pidfh = new IO::File ">$pid_file" or exit;
60     #print $pidfh "$pid\n";
61     exit;
62   }
63   open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
64   setsid                    or die "Can't start a new session: $!";
65   open STDERR, '>&STDOUT'   or die "Can't dup stdout: $!";
66
67   #$SIG{__DIE__} = \&_die;
68   #$SIG{__WARN__} = \&_logmsg;
69
70 }
71
72