#!/usr/bin/env bash
#
# Setup a public GIT repository.
# Copyright (c) Petr Baudis, 2005
#
# Setup a public GIT repository, that is, one that has no attached working
# copy and you typically only push into it and pull from it. You need to run
# this command before you will be able to push into the repository for the
# first time, but you will be also unable to pull from the repository until
# you push into it at first.
#
# Therefore the workflow is to first init a regular private repository, then
# use this command to create the public one, then add the appropriate remote
# branch (`cg-branch-add origin ...`) in your private repository and then
# push to the public repository.
#
# Use `cg-init` if you want to instead create a new GIT project.
#
# The command will create the repository to reside in DIRECTORY (the directory
# must not exist before calling this command). By default, it will be
# world-readable, but writable only by you. If you want to make it possible
# for multiple users to push, create a group for them and use the '-g'
# parameter, which will make `cg-admin-setuprepo` set up the permissions
# properly.
#
# The repository will also be set up so that `git-update-server-info` will
# be automagically re-ran after each push, in short making it suitable for
# HTTP access.
#
# Note that you might need to do other additional steps, like touching the
# 'git-daemon-export-ok' file if you want to make the repository accessible
# by the git daemon (serving the 'git://...' URIs).
#
# OPTIONS
# -------
# -g GROUP::
#	Name of the UNIX group covering the users authorized to push into
#	the repository. If unspecified, only you (who ran this command) will
#	have write (push) access.
#
# NOTES
# -----
# It may happen that you are using version of GIT missing the default
# post-update hook, or that the relevant template hooks are not available
# on your system or with your GIT installation. In that case, in order to
# have your repository properly accessible with HTTP, you need to add this
# to .git/hooks/post-update (and then make it eXecutable):
#
#	#!/bin/sh
#	exec git-update-server-info

USAGE="cg-admin-setuprepo [-g GROUP] DIRECTORY"
_git_repo_unneeded=1

. ${COGITO_LIB}cg-Xlib || exit 1

unixgroup=
while optparse; do
	if optparse -g=; then
		unixgroup="$OPTARG"
	else
		optfail
	fi
done


export GIT_DIR="${ARGS[0]}"
_git="$GIT_DIR"

[ "$_git" ] || die "missing name of the repository directory"
[ -e "$_git" ] && die "$_git already exists"


if [ "$unixgroup" ]; then
	umask 002
else
	umask 022
fi

mkdir "$_git" || exit 1
if [ "$unixgroup" ]; then
	chgrp "$unixgroup" "$_git" && chmod 2775 "$_git" || exit 1
fi

git-init-db

# Enable git-update-server-info after each push.
if ! chmod a+x "$_git/hooks/post-update"; then
	echo "Due to a missing post-update hook, HTTP pull will not work properly on this repository." >&2
	echo "See the NOTES section of cg-admin-setuprepo(1)'s documentation for more details." >&2
fi
