#!/usr/bin/env perl

use Time::HiRes qw(usleep);

# dummy version of qsub that just forks a process and sleeps

my @hold;
my ($out, $err) = ("", "");
while (@ARGV && $ARGV[0] =~ /^-/) {
    my $arg = shift;
    if ($arg eq '-hold_jid') {
	my $hold_jobs = shift;
	@hold = split /,/, $hold_jobs;
    } elsif ($arg eq '-o') {
	$out = ">" . shift;
    } elsif ($arg eq '-e') {
	$err = "2>" . shift;
    } elsif ($arg eq '--fake-arg') {
	shift;
    } elsif ($arg eq '--echo') {
	my $text = shift;
	my $file = shift;
	system "echo $text >$file";
    }
}

my $child = fork();
if ($child) {
    print "Job number is ", $child, "\n";
} else {
    for my $hold (@hold) {
	while (1) {
	    my $pids = `ps -opid`;
	    my @pids = split /\s+/, $pids;
	    last unless grep($_ eq $hold,@pids);
	}
    }
    usleep(1e5);  # sleep for 100ms
    system "@ARGV $out $err";
}
