#!/bin/sh
#
# Short script to create a new Git repository and take care of setting it up
# for public visibility and receiving pushes.  Follows the Git manual
# documentation for setting up a public repository.
#
# Copyright 2008, 2013, 2017-2018 Russ Allbery <eagle@eyrie.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# SPDX-License-Identifier: MIT

set -e
if [ -z "$1" ] ; then
    echo "Usage: git-create-public <directory> [<description>]" >&2
    exit 1
fi

# Make sure the directory is fully qualified and ends in *.git.
dir="$1"
shift
case "$dir" in
    /*) ;;
    *)  dir="/srv/git/$dir" ;;
esac
case "$dir" in
    *.git) ;;
    *)     echo "Pathname must end in .git" >&2 ; exit 1 ;;
esac

# Create the directory (and throw an error if it already exists).
if [ -e "$dir" ] ; then
    echo "Path $dir already exists" >&2
    exit 1
fi
mkdir -p "$dir"

# Now, set up the archive.
cd "$dir"
git --bare init
touch git-daemon-export-ok
git --bare update-server-info
mv hooks/post-update.sample hooks/post-update

# We use a standard URL for Git access, so we can set cloneurl.
relative=$(echo "$dir" | sed 's,^/srv/git/,,')
echo "https://git.eyrie.org/git/$relative" > cloneurl

# If we were given a description, save it.
if [ -n "$1" ] ; then
    echo "$@" > description
fi
exit 0

# Documentation.  Use a hack to hide this from the shell.  Because of the
# above exit line, this should never be executed.
DOCS=<<__END_OF_DOCS__

=for stopwords
Allbery

=head1 NAME

git-create-public - Create a new public Git repository under /srv/git

=head1 SYNOPSIS

B<git-create-public> I<directory> [I<description>]

=head1 DESCRIPTION

Creates a new public Git repository for git.eyrie.org under F</srv/git>.  The
I<directory> argument is either a path under F</srv/git> or is taken to be
relative to that directory.  It must end in C<.git>.  The second argument, if
given, is used as the description of the repository.

This script sets up the repository to be accessible by B<git-daemon> and
creates a F<cloneurl> file pointing to the anonymous Git URL for the
repository.  It also runs C<git update-server-info> and enables the sample
F<post-update> hook.

=head1 SEE ALSO

git-daemon(1), git-update-server-info(1)

=head1 AUTHOR

Russ Allbery <eagle@eyrie.org>

=cut

__END_OF_DOCS__
