#!/usr/bin/perl

###########################
### Autor: Sebastian Enger / B.Sc. 
### Copyright: Sebastian Enger
### Licence: BSD
### Version: 1.1.b  - 20080716@19.Uhr
### Contact: sebastian.enger@gmail.com | icq: 135846444
### Latest Version: PENDRIVE:\Programming\Perl\IncrementalBackup
###########################

use strict;
use Parallel::ForkManager;		# perl -MCPAN -e 'force install "Parallel::ForkManager"'

my @files = (
	"/server/moviestreamz/daily_2kino.pl",
	#"/server/moviestreamz/daily_ddl.pl",
	"/server/moviestreamz/daily_g-stream.pl",
	"/server/moviestreamz/daily_kkiste.pl",
	#"/server/moviestreamz/daily_movstreams.pl",
	#".pl",
);

foreach my $file (@files){
	if ( $file =~ /\.pl$/ig ){ 
		print "Starting: $file\n";
		system("perl -c $file");
		#system("perl $file");
	}
}


my $MAX_PARALLEL_THREADS = scalar(@files);
my $pm 		= new Parallel::ForkManager($MAX_PARALLEL_THREADS);
			
for ( my $count = 1; $count <= $MAX_PARALLEL_THREADS; $count++) {

	my $element = pop(@files); 
	$pm->set_max_procs($MAX_PARALLEL_THREADS);
	
	# Forks and returns the pid for the child:
	my $pid = $pm->start and next; 
		print "Now Executing: $element\n";
		system("perl $element");
	$pm->finish; # Terminates the child process
	
} # for ( my $count = 1; $count <= MAX_PARALLEL_THREADS; $count++) {

$pm->wait_all_children;

print "The program ran for ", time() - $^T, " seconds\n";
sleep 500;
exit;