#!/bin/sh
. /usr/bin/mithraen-build-utils-functions
git_alt_pre

FORCE=0
if [ "$1" = "-f" ]; then
    FORCE=1
    shift
fi
NAME=$1

if [ -z "$NAME" ]; then
	# Try to autodetect
	if [ -f .git/remotes/origin ]; then
		NAME=`grep 'URL: git.alt:packages/' .git/remotes/origin | sed 's!.*/!!'`
		echo "Using name: '$NAME'"
	fi
fi

if [ -z "$NAME" ]; then
	echo "Using: $0 packagename"
	exit -1
fi

rm -f .git/remotes/origin 2> /dev/null
rmdir .git/remotes 2> /dev/null

if [ "$FORCE" = "0" ]; then
    T=`git config --get remote.origin.url`
fi

if [ -z "$T" ]; then
	git config 'remote.origin.url' "git.alt:packages/$NAME"
	git config 'remote.origin.push' "refs/heads/*:refs/heads/*"
	git config 'remote.origin.fetch' "refs/heads/*:refs/remotes/origin/heads/*"
else
	echo "Already exists!"
	exit -1
fi
