#!/usr/bin/env julia

# process arguments
help = false
solver = "GLPK"
model_file = nothing
nworkers = "1"
freeargs = String[]
optind = 1
while optind <= length(ARGS)
    if ARGS[optind] in ("-s", "--solver")
        global optind += 1
        global solver = ARGS[optind]
    elseif ARGS[optind] in ("-w", "--nworkers")
        global optind +=1
        global nworkers = ARGS[optind]    
    elseif ARGS[optind] == "-h"
        global help = true
        break
    else
        push!(freeargs, ARGS[optind])
    end
    global optind += 1
end

if help
    println(
        """Usage: fbcmt-memote-run [-s|--solver <GLPK>] [-h|--help] [-w|--nworkers <1>] <model>""",
    )
    exit(0)
end

if length(freeargs) == 1
    global model_file = last(freeargs)
else
    @error "Expected only one argument, the model location, got $(length(freeargs)) instead."
    exit(1) 
end

if !isfile(model_file)
    @error "model location argument is not a name of a file"
    exit(1)
end

solver = Symbol(solver)
nw = parse(Int64, nworkers)

using FBCModelTests, FBCModelTests.Memote
@eval using $solver
import Distributed

if nw > 1
    Distributed.addprocs(nw, exeflags=`--project=$(Base.active_project())`) # necessary due to issue #48217 in julialang
    Distributed.@everywhere using FBCModelTests, FBCModelTests.Memote
    Distributed.@everywhere @eval using $solver
end

optimizer = @eval $solver.Optimizer

@info "Initialization done, starting MEMOTE..." model_file optimizer nworkers

# run the Memote
Memote.run_tests_toplevel(model_file, optimizer; workers=Distributed.workers())
exit(0)

