#!/usr/bin/env bash

place="$1"
if [ "$#" = "0" -o "$place" = "-h" -o "$place" = "--help" ]; then
    echo "run-in [CRATE,...|all] CMD ARGS..."
    exit 0;
fi
shift

if [ "$place" = "all" ]; then
    place="bit,check,estimate,slowsieve,sieve,."
fi

echo "${place}," |
    while read -d , name; do
        if [ -z "$name" ]; then continue; fi

        if [ "$name" = "." ]; then
            echo "primal"
            "$@";
        else
            dir="primal-${name}"
            ( cd "$dir" && echo "$name" && "$@"; )
            res=$?
            if [ "$res" != "0" ]; then
                exit $res
            fi
        fi
    done
