#!/usr/bin/env bash
#
# Initialize a GIT repository.
# Copyright (c) Petr Baudis, 2005
#
# If `cg-init` is run in a non-empty directory files in the top and
# sub directory will automatically be added.
#
# OPTIONS
# -------
# -I::
#	Do not perform the initial commit.
#
# -N::
#	Only update the cache: do not copy the data into the object database.
#	This is for special purposes when you might not actually _have_ any
#	object database. This option is normally not interesting.

USAGE="cg-init [-I] [-N]"
_git_repo_unneeded=1

. ${COGITO_LIB:-/usr/lib/cogito/}cg-Xlib

no_initial_commit=
infoonly=
while optparse; do
	if optparse -I; then
		no_initial_commit=1
	elif optparse -N; then
		infoonly=-N
	else
		optfail
	fi
done

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

trap "rm -rf $_git; die \"interrupted\"" SIGINT SIGTERM

git-init-db
mkdir $_git/branches
touch $_git/refs/heads/master

git-read-tree # Seed the dircache
if ! [ "$no_initial_commit" ]; then
	[ "$(ls)" ] && find * \( -type f -o -type l \) -print0 | xargs -0 cg-add ${infoonly}
	cg-commit -C -m"Initial commit" -E ${infoonly}
fi

exit 0
