#!/usr/bin/perl

# all gitolite CLI tools run as sub-commands of this command
# ----------------------------------------------------------------------

=for args
Usage:  gitolite [sub-command] [options]

The following built-in subcommands are available; they should all respond to
'-h' if you want further details on each:

    setup                       1st run: initial setup; all runs: hook fixups
    compile                     compile gitolite.conf

    query-rc                    get values of rc variables

    list-groups                 list all group names in conf
    list-users                  list all users/user groups in conf
    list-repos                  list all repos/repo groups in conf
    list-phy-repos              list all repos actually on disk
    list-memberships            list all groups a name is a member of
    list-members                list all members of a group

Warnings:
  - list-users is disk bound and could take a while on sites with 1000s of repos
  - list-memberships does not check if the name is known; unknown names come
    back with 2 answers: the name itself and '@all'

In addition, running 'gitolite help' should give you a list of custom commands
available.  They may or may not respond to '-h', depending on how they were
written.
=cut

# ----------------------------------------------------------------------

use FindBin;

BEGIN { $ENV{GL_BINDIR} = $FindBin::RealBin; }
BEGIN { $ENV{GL_LIBDIR} = "$ENV{GL_BINDIR}/lib"; }
use lib $ENV{GL_LIBDIR};
use Gitolite::Rc;
use Gitolite::Common;

use strict;
use warnings;

# ----------------------------------------------------------------------

my ( $command, @args ) = @ARGV;
gl_log( 'cli', 'gitolite', @ARGV ) if -d $rc{GL_ADMIN_BASE} and $$ == ( $ENV{GL_TID} || 0 );
args();

# the first two commands need options via @ARGV, as they have their own
# GetOptions calls and older perls don't have 'GetOptionsFromArray'

if ( $command eq 'setup' ) {
    shift @ARGV;
    require Gitolite::Setup;
    Gitolite::Setup->import;
    setup();

} elsif ( $command eq 'query-rc' ) {
    shift @ARGV;
    query_rc();    # doesn't return

# the rest don't need @ARGV per se

} elsif ( $command eq 'compile' ) {
    require Gitolite::Conf;
    Gitolite::Conf->import;
    compile(@args);

} elsif ( $command eq 'trigger' ) {
    trigger(@args);

} elsif ( my $c = _which( "commands/$command", 'x' ) ) {
    trace( 2, "attempting gitolite command $c" );
    _system( $c, @args );

} elsif ( $command eq 'list-phy-repos' ) {
    _chdir( $rc{GL_REPO_BASE} );
    print "$_\n" for ( @{ list_phy_repos(@args) } );

} elsif ( $command =~ /^list-/ ) {
    trace( 2, "attempting lister command $command" );
    require Gitolite::Conf::Load;
    Gitolite::Conf::Load->import;
    my $fn = lister_dispatch($command);
    print "$_\n" for ( @{ $fn->(@args) } );

} else {
    _die "unknown gitolite sub-command";
}

gl_log('END') if $$ == $ENV{GL_TID};

exit 0;

sub args {
    usage() if not $command or $command eq '-h';
}

# ----------------------------------------------------------------------
