#!/bin/bash
#
# Create a new repo.
# THIS HAS TO BE RUN ON THE GIT SERVER!

# WARNING:
# This file is maintained within ansible
# All local changes will be lost.


# Figure out the environment we're running in
GITROOT=/srv/git/rpms

# check if a moron is driving me
if [ ! -d $GITROOT ] ; then
    # we're not on the git server (this check is fragile)
    echo "ERROR: This script has to be run on the git server."
    echo "ERROR: Homer sez 'Duh'."
    exit -9
fi

# Local variables
VERBOSE=0
TEST=
IGNORE=
AUTHOR="Fedora Release Engineering <rel-eng@lists.fedoraproject.org>"
GIT_SSH_URL="ssh://localhost"

Usage() {
    cat <<EOF
Usage:
    $0 <package_name>

    Creates a new repo for <package_name>

Options:
    -h,--help			This help message
EOF
}

if [ $# -gt 2 ]; then
    Usage
    exit -1
fi

# parse the arguments
while [ -n "$1" ] ; do
    case "$1" in
	-h | --help )
	    Usage
	    exit 0
	    ;;

	* )
	    PACKAGE="$1"
	    ;;
    esac
    shift
done

# I hate shell scripting.  I'm sure the above is totally wrong

# check the arguments
if [ -z "$PACKAGE" ] ; then
    Usage
    exit -1
fi

# Sanity checks before we start doing damage
[ $VERBOSE -gt 1 ] && echo "Checking package $PACKAGE..."
if [ -d $GITROOT/$PACKAGE.git ] ; then
    echo "ERROR: Package module $PACKAGE already exists!" >&2
    exit -1
fi

# Just don't run as root, mmkey?
if [ "$(id -un)" = "root" ] ; then
    echo "Please run this script as yourself"
    exit -3
fi

# "global" permissions check
if [ ! -w $GITROOT ] ; then
    echo "ERROR: You can not write to $GITROOT"
    echo "ERROR: You can not create repos"
    exit -1
fi

# Now start working on creating those branches
# Create a tmpdir to do some git work in
TMPDIR=$(mktemp -d /tmp/tmpXXXXXX)

# First create the master repo
mkdir $GITROOT/$PACKAGE.git
pushd $GITROOT/$PACKAGE.git >/dev/null
git init -q --shared --bare
echo "$PACKAGE" > description # This is used to figure out who to send mail to.
git config --add hooks.mailinglist "$PACKAGE-owner@fedoraproject.org,scm-commits@lists.fedoraproject.org"
git config --add hooks.maildomain fedoraproject.org
popd >/dev/null

# Now clone that repo and create the .gitignore and sources file
git clone -q /srv/git/rpms/$PACKAGE.git $TMPDIR/$PACKAGE
pushd $TMPDIR/$PACKAGE >/dev/null
touch .gitignore sources
git add .
git commit -q -m 'Initial setup of the repo' --author "$AUTHOR"
git push -q origin master
popd >/dev/null

# Setup our update hooks
mkdir -p $GITROOT/$PACKAGE.git/hooks/update-chained.d
ln -s /usr/share/gitolite/hooks/common/update \
    $GITROOT/$PACKAGE.git/hooks/update-chained.d/update-gitolite
ln -s /usr/share/git-core/update-block-push-origin \
    $GITROOT/$PACKAGE.git/hooks/update-chained.d/update-block-push-origin

# This one kicks off all the others in update-chained.d
ln -s /usr/share/git-core/update-chained $GITROOT/$PACKAGE.git/hooks/update

# Setup our post-receive hooks
mkdir -p $GITROOT/$PACKAGE.git/hooks/post-receive-chained.d
ln -s /usr/share/git-core/mail-hooks/gnome-post-receive-email \
    $GITROOT/$PACKAGE.git/hooks/post-receive-chained.d/post-receive-email
ln -s /usr/share/git-core/post-receive-fedmsg \
    $GITROOT/$PACKAGE.git/hooks/post-receive-chained.d/post-receive-fedmsg

# This one kicks off all the others in post-receive-chained.d
ln -s /usr/share/git-core/post-receive-chained \
    $GITROOT/$PACKAGE.git/hooks/post-receive

rm -rf $TMPDIR
echo "Done."
