#!/usr/bin/env bash
#
# Initialize a GIT repository.
# Copyright (c) Petr Baudis, 2005
#
# Takes an optional parameter which will make it "clone" a specified
# remote repository. Note that this usage is DEPRECATED - use cg-clone
# (possibly with the -s parameter) for doing this. This functionality
# will go away from cg-init soon.

. ${COGITO_LIB}cg-Xlib

uri=$1

[ -e $_git ] && die "$_git already exists"

git-init-db
mkdir $_git/branches $_git/refs $_git/refs/heads $_git/refs/tags
touch $_git/refs/heads/master
ln -s refs/heads/master $_git/HEAD

if [ "$uri" ]; then
	echo "$uri" >$_git/branches/origin
	cg-pull origin || die "pull failed"

	cp $_git/refs/heads/origin $_git/refs/heads/master
	git-read-tree HEAD
	git-checkout-cache -a
	git-update-cache --refresh

	echo "Cloned (origin $uri available as branch \"origin\")"
else
	git-read-tree # Seed the dircache
	find * | xargs cg-add
	cg-commit -C -m"Initial commit" -e
fi
exit 0
