#!/bin/bash
# vim: set ft=sh

set -e

path=${1}
if [ -z "$path" ]; then
  echo "usage: $0 <path to submodule>"
  exit 1
fi

cd "$(dirname $0)/.."

gitmodules=${PWD}/.gitmodules

cd "${path}"

ORIGIN=$(git config --get remote.origin.url)
BRANCH=$(git rev-parse --abbrev-ref HEAD)

set -x

git config --file "$gitmodules" submodule.${path}.path ${path}
git config --file "$gitmodules" submodule.${path}.url ${ORIGIN}

if [ "$BRANCH" != "HEAD" ]; then
  git config --file "$gitmodules" submodule.${path}.branch $BRANCH
fi