#!/usr/bin/perl
use strict;
use warnings;

use lib $ENV{GL_LIBDIR};
use Gitolite::Easy;

=for usage
Usage: gitolite writable <reponame>|@all on|off

Disable/re-enable pushes to all repos or named repo.  Useful to run
non-git-aware backups and so on.

'on' enables, 'off' disables, writes (pushes) to the named repo or all repos.

With 'off', any subsequent text is taken to be the message to be shown to
users when their pushes get rejected.  If it is not supplied, it will take it
from STDIN; this allows longer messages.
=cut

usage() if not @ARGV or @ARGV < 2 or $ARGV[0] eq '-h';
usage() if $ARGV[1] ne 'on' and $ARGV[1] ne 'off';

my $repo = shift;
my $on = ( shift eq 'on' );

if ( $repo eq '@all' ) {
    _die "you are not authorized" if $ENV{GL_USER} and not is_admin();
} else {
    _die "you are not authorized" if $ENV{GL_USER} and not owns($repo);
}

my $msg = join( " ", @ARGV );
# try STDIN only if no msg found in args *and* it's an 'off' command
if ( not $msg and not $on ) {
    say2 "...please type the message to be shown to users:";
    $msg = join( "", <> );
}

my $sf = ".gitolite.down";
my $rb = $ENV{GL_REPO_BASE};

if ( $repo eq '@all' ) {
    target( $ENV{HOME} );
} else {
    target("$rb/$repo.git");
}

sub target {
    my $repodir = shift;
    if ($on) {
        unlink "$repodir/$sf";
    } else {
        _print( "$repodir/$sf", $msg );
    }
}
