#!/usr/bin/bash

# If we haven't already started running a recipe then check if beaker-harness
# is defined and if so fetch that recipe.
if [ ! -e "/etc/restraint/config.conf" ]; then
    # Read beaker vars from env
    if [ -e "/etc/profile.d/beaker-harness-env.sh" ]; then
        . /etc/profile.d/beaker-harness-env.sh
    fi

    # /proc/cmdline options override
    for x in $(cat /proc/cmdline); do
        case $x in
            BEAKER_LAB_CONTROLLER_URL=*)
                BEAKER_LAB_CONTROLLER_URL=${x#BEAKER_LAB_CONTROLLER_URL=}
                ;;
            BEAKER_RECIPE_ID=*)
                BEAKER_RECIPE_ID=${x#BEAKER_RECIPE_ID=}
                ;;
        esac
    done

if [ -n "$BEAKER_LAB_CONTROLLER_URL" -a -n "$BEAKER_RECIPE_ID" ]; then
    cat <<EOF >/etc/restraint/config.conf
[restraint]
recipe_url=$BEAKER_LAB_CONTROLLER_URL/recipes/$BEAKER_RECIPE_ID/
EOF
fi
fi
