#!/bin/bash

[[ $1 = -h* || $1 = --h* ]] && {
    echo "Usage: ${0##*/} [<remote>] [<thisremote>]"
    echo "Default remote is origin; default thisremote is the short hostname"
    exit 0
}

remote=${1:-origin}
thisremote=${2:-$(hostname -s)}

commands=(
    "git config --add remote.$remote.push \"+refs/heads/*:refs/remotes/$thisremote/*\""
    )

git rev-parse --show-toplevel || { echo "can't get repository" >&2; exit 1; }
git remote | grep -q $remote || { echo "no such remote" >&2; exit 1; }
if git config --get-all remote.$remote.push; then
    echo "Some existing push attribs found. Bailing."
    echo "would have done:"
    printf "%s\n" "${commands[@]}"
    exit 1
else
    set -e
    for c in "${commands[@]}"; do
        eval $c
    done
    echo "Done"
fi

