#! /usr/bin/env php
<?php

// Submit a job for a BUDA (BOINC Universal Docker app) science app
// Use for debugging, but not production;
// it doesn't make a batch, so there's not association with user

function usage() {
    echo "Usage:
submit_buda [options] sci_app infile ...

--cmdline x         pass cmdline args to app
--variant x         app variant, default 'cpu'
--verbose
";
    exit();
}

$buda_root = 'buda_apps';
$infiles = [];
$verbose = false;
$variant = 'cpu';
$sci_app = null;
$cmdline = null;

for ($i=1; $i<$argc; $i++) {
    if ($argv[$i] == '--variant') {
        $variant = $argv[++$i];
    } else if ($argv[$i] == '--cmdline') {
        $cmdline = $argv[++$i];
    } else if ($argv[$i] == '--verbose') {
        $verbose = true;
    } else {
        if (!$sci_app) {
            $sci_app = $argv[$i];
        } else {
            $infiles[] = $argv[$i];
        }
    }
}

if (!$sci_app) {
    usage();
}

$variant_dir = "$buda_root/$sci_app/$variant";
if (!is_dir($variant_dir)) {
    die("No version dir\n");
}
$variant_desc = json_decode(
    file_get_contents("$variant_dir/variant.json")
);

$cmd = sprintf(
    'bin/create_work --appname buda --wu_template %s --result_template %s',
    "$variant_dir/template_in",
    "$variant_dir/template_out"
);

$cmd .= sprintf(' --command_line "--dockerfile %s %s %s"',
    $variant_desc->dockerfile,
    $verbose?'--verbose':'',
    $cmdline?$cmdline:''
);

// app files are already staged
//
$cmd .= " $variant_desc->dockerfile_phys";
foreach ($variant_desc->app_files_phys as $fname) {
    $cmd .= " $fname";
}

foreach ($infiles as $path) {
    if (!file_exists($path)) {
        die("missing input file '$path'\n");
    }
    $fname = basename($path);
    system("cp $path `bin/dir_hier_path $fname`", $ret);
    if ($ret) {
        die("Couldn't stage input file $path");
    }
    $cmd .= " $fname";
}

echo "command: $cmd\n";

if (system($cmd, $ret) === false) {
    die("system($cmd) failed\n");
}
if ($ret) {
    die("$cmd returned $ret\n");
}

echo "job created\n";

?>
