#!/usr/bin/perl

use strict;
use warnings;
use FindBin qw($Script);

my $who = ($ENV{REMOTE_USER} || die "Auth required\n")."\@$ENV{REMOTE_ADDR}";
my $base = $ENV{GIT_DIR} or die "GIT hook ENV malfunction!\n";
my $acl = {};
my $conf = $0 =~ m{(.*)/} ? `$1/configs` : `git config --list`;
while ($conf =~ s/^acl.(\w+)=(.*)$//m) {
    my $param = $1;
    my $keys = $2;
    foreach my $key (split /,/, $keys) {
        $acl->{$param}->{$key} = 1;
    }
}

$SIG{PIPE} = sub { exit 1; };
$ENV{GIT_SERVER_VERSION} ||= "UNKNOWN";

$conf !~ /^proxy/m or 0 == system "hooks/proxy", $Script or die localtime().": [$who] git-server: proxy operation failure: $?\n";
# Verify IP
if (my $banned = system "hooks/restrictip") {
    exit ($banned >> 8 || 1);
}
# Cannot pass without writers access:
my $allowed = $acl->{writers}->{$ENV{REMOTE_USER}};
if ($allowed) {
    warn localtime().": [$who] git-server v$ENV{GIT_SERVER_VERSION}: PUSH Running...\n";
}
else {
    warn localtime().": [$who] git-server: You cannot push changes. Missing [acl.writers] permissons.\n";
    exit 5; # Missing [acl.writers]
}
