#!/bin/sh -efu

. girar-sh-functions

usage()
{
	echo >&2 "$PROG: $*"
	echo >&2 "usage: $PROG <path to directory>"
	exit 1
}

[ "$#" -ge 1 ] ||
	usage 'Not enough arguments.'
[ "$#" -eq 1 ] ||
	usage 'Too many arguments.'

cd

ISPROJECT=
dir="$(validate_packages_dir "$1")"; shift
dir="$(add_git_suffix "$dir")"
printf %s "$dir" |egrep -qs '^/projects' &&
	ISPROJECT=1

[ -n "$ISPROJECT" ] && umask 0002
mkdir -p -- "$dir"
cd "$dir"
GIT_DIR=. git init --template="$GIRAR_TEMPLATES_DIR" >/dev/null ||
	fatal "$dir: failed to init-db"
if [ -n "$ISPROJECT" ]; then
	GIT_DIR=. git config "core.sharedRepository" "true"
	GIT_DIR=. git config "girar.merges" "false"
fi
girar-update-packages-list
echo "${dir%.git}" >description
install -m600 -- /dev/null git-daemon-export-ok
printf '%s:\t%s\n' "$PROG" "$PWD"
