#!/usr/bin/env julia

if length(ARGS) != 3
        println("Usage: rm_iter_by.jl <dir> <prefix> <n>")
        exit(1)
end

basedir = ARGS[1]
prefix = ARGS[2]
n = parse(Int, ARGS[3])

for sim_dir in readdir(basedir)
        if !occursin(prefix, sim_dir)
                continue
        end

        sim_dir = joinpath(basedir, sim_dir)
        files = readdir(sim_dir, join=true)

        for file in files
                m = match(r"iter_(\d+).qs2", file)
                if isnothing(m)
                        continue
                end
                iter = parse(Int, m.captures[1])
                if iter % n != 0
                        rm(file)
                end
        end
end
