#!/bin/bash
if [ $# -eq 0 ]; then
	echo "[!]  no repository specified"
	exit
fi

REMOTE="origin" # TODO can we get this as argument but optionally?

if [ $# -eq 1 ]; then
	IFS='/' read -ra FRAGS <<< "$1"
	REPOSITORY=${FRAGS[-1]}
	REPONAME=${FRAGS[-1]}
else
	REPOSITORY="$2" # with .git
	REPONAME="$2"   # without .git
fi

if [[ ! "$REPOSITORY" == *.git ]]; then
	REPOSITORY="$REPOSITORY.git"
fi

if [[ "$REPONAME" == *.git ]]; then
	REPONAME=$(echo "$REPONAME" | sed 's/\.git$//')
fi

git clone --mirror "$1" "/srv/git/$REPOSITORY" --origin "$REMOTE"
git config -f "/srv/git/$REPOSITORY/config" "remote.$REMOTE.direction" "down"

cd "$REPOSITORY"
/srv/git/.hooks/post-update