#!/bin/bash

# gitmk-add -- add more interesting files produced by rules

set -e # exit on any error immediately!

readonly GOAL="$1"

readonly headref="$(git symbolic-ref HEAD)"
readonly BRANCH="${headref##refs/heads/}"

set -x

if git checkout "$BRANCH"_/AUTO/"$GOAL" \
    && git merge --no-commit -s recursive -X theirs "$BRANCH" -m "Result of: make $GOAL" \
    && make "$GOAL";
then
    set +x
    printf $"Now, git add what you need, and git commit, and git checkout [-b] %s_/OUT/%s , and tweak it." "$BRANCH" "$GOAL"
    echo
else
    readonly status=$?
    git checkout "$BRANCH"
    set +x
    printf $"make %s failed; now, fix the sources here." "$GOAL"
    echo
    exit $status
fi