#!/bin/sh
set -euC

##/// git-gitolite 1.0
#////
#//// Manage the counterpart of a local Git repository on a remote gitolite
#//// host.
#////
###// usage:
#////   git-gitolite help
#////   git-gitolite info
#////   git-gitolite init <license> [<description> [<references>]]
#////   git-gitolite remote
#////   git-gitolite head
#////   git-gitolite publish
#////   git-gitolite unpublish
#////   git-gitolite rm
#////   git-gitolite <command> [<args>...]
#////   git-gitolite --help|-h
#////   git-gitolite --version
#////
###// commands:
####/   help
#////     Runs the gitolite 'help' command.
####/   info
#////     Runs the gitolite 'info' command.
####/   init
#////     Creates and commits, separately, readme and license files.
#////     A note about the <license> (which should be an SPDX identifier), as
#////     well as <description> and <references> if given, is inserted into the
#////     readme. The readme is opened in the editor configured for git
#////     for review before each commit. This command then (re)sets the remote
#////     URL, creates the remote repository and pushes.
####/   remote
#////     (Re)sets the remote URL.
####/   head
#////     Resets the remote HEAD to point to the same ref as the local HEAD,
#////     making it the default view on a web server.
####/   publish
#////     Grants read permissions to 'gitweb', making it viewable through a
#////     web server.
####/   unpublish
#////     Revokes read permissions from 'gitweb', preventing it from being
#////     viewable through a web server.
####/   rm
#////     (Unlocks and) removes the remote repository.
####/   <command>
#////     Perform a gitolite command, filling in the host and repository name
#////     automatically. See `git-gitolite help` for a list of commands.
#////
###// environment variables:
####/   GIT_GITOLITE_USER
#////     The gitolite user under which repositories are found.
#////     [default: $USER]
####/   GIT_GITOLITE_HOST
#////     The host to connect to.
#////     [default: git.$(dnsdomainname)]
####/   GIT_GITOLITE_REMOTE
#////     The git remote used to identify the gitolite server.
#////     [default: origin]

## Messages
prog="$(basename "$0")"
help()    { sed -n 's|^#[#/]*/ \?||p' "$0";                 exit 0; }
version() { help | awk '/^$/{++p;next}p==0';                exit 0; }
usage()   { help | awk '/^$/{++p;next}p==2';                exit 0; }
parse()   { printf '%s: error: %s\n'   "$prog" "$1"; usage; exit 1; } >&2
error()   { printf '%s: error: %s\n'   "$prog" "$1";        exit 1; } >&2
warning() { printf '%s: warning: %s\n' "$prog" "$1";                } >&2
opt()     { [ $# -gt 1 ] || parse "option '$1' value not provided"; }
arg()     { [ $# -gt 1 ] || parse "argument '$1' not provided";     }
argend()  { [ $# -eq 0 ] || parse "unrecognized argument '$1'";     }

## Parse special options
case "${1:-}"
in
  '-h'|'--help') help; ;;
  '--version') version; ;;
esac

## Parse required arguments
arg 'command' "$@"; command="$1"; shift;

case "$command" in
  'init')
    ## Parse required arguments
    arg 'license' "$@"; license="$1"; shift;

    ## Parse optional arguments
    description='<DESCRIPTION>'
    references=''
    [ $# -eq 0 ] || { description="$1"; shift; }
    [ $# -eq 0 ] || { references="$1";  shift; }
    ;;
esac

## Parse unrecognized arguments
case "$command" in
  'help'|'info'|'init'|'remote'|'head'|'publish'|'unpublish'|'rm')
    argend
    ;;
esac

## Parse environment variables
user="${GIT_GITOLITE_USER:-"$USER"}"
host="${GIT_GITOLITE_HOST:-"git.$(dnsdomainname)"}"
remote="${GIT_GITOLITE_REMOTE:-"origin"}"
