- my $dlfile;
- my $dirname;
-
- $dlfile = sprintf($self->_getval('FILEPATTERN'),
- $self->_encode($video->{'SOURCE'}),
- $self->_encode($video->{'ID'}),
- $self->_encode($video->{'TITLE'}),
- $self->_encode($video->{'DLURL'}),
- $self->_encode($video)->{'URL'});
-
- $dirname = dirname($dlfile);
- if ($self->_diskfree($dirname) < $self->_getval('MINFREE')) {
- $self->error("Not enough free space to download");
- return 0;
+ my $child_pid;
+
+ # fork to background
+ $child_pid = fork();
+
+ if ($child_pid) { # parent
+ $self->debug('parent spawned process %d for download', $child_pid);
+ if (exists $self->{_API}->{wait_for_child}) {
+ $self->debug('calling wait_for_child on %d', $child_pid);
+ $self->{_API}->{wait_for_child}->($child_pid);
+ }
+ Irssi::pidwait_add($child_pid);
+ }
+ elsif (defined $child_pid) { # child
+ close STDIN;
+ close STDOUT;
+ close STDERR;
+ $self->debug('CHILD: start download');
+ $self->SUPER::_download($dlurl, $dlfile, $video);
+ $self->debug('CHILD: end download');
+ exit;
+ $self->debug('CHILD: AFTER EXIT? WTF!');