mkdir -p build
artefact="latest"

# loop through all the branches
for branch in $(git for-each-ref --format='%(refname:strip=3)' refs/remotes); do
    echo $branch;
    if [[ $branch == "master" ]]; then
        artefact="stable";
    elif [[ $branch == "develop" ]]; then
        artefact="latest";
    else
        artefact="unstable";
    fi
    echo $artefact;
    git checkout -f $branch
    git reset --hard $branch
    git pull

    # Generate the config dynamically
    echo 'url: "https://r3.pages.uni.lu"' > _config_url.yml

    # build the website
    bundle install
    bundle exec jekyll build --config "_config.yml,_config_url.yml" --baseurl="howto-cards/$artefact" -d "build/$artefact"

    if [[ $branch == "master" ]]; then
        # set the 404
        cp howto-cards/$artefact/404.html build/404.html
    fi
done

# checkout the current branch
echo $CI_COMMIT_REF_NAME
git checkout -f $CI_COMMIT_REF_NAME
git reset --hard origin/$CI_COMMIT_REF_NAME

# set the auto redirection
cp .ci/.autoRedirect build/index.html
