#!/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 = `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; };

$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);
}
if ($acl->{deploy}->{$ENV{REMOTE_USER}}) {
    warn localtime().": [$who] git-server: PULL waiting for notification ...\n";
    my $puller = "$who:$base";
    if (my $notification_error = system "hooks/push-notification", $puller or $?) {
        die localtime().": [$who] git-server: Too many other concurrent reads. Try again later. (ERROR: $notification_error-$?)\n";
    }
}
# If you can write or deploy, then you can also read
my $allowed = $acl->{readers}->{$ENV{REMOTE_USER}} || $acl->{writers}->{$ENV{REMOTE_USER}} || $acl->{deploy}->{$ENV{REMOTE_USER}};
warn localtime().": [$who] git-server: RUNNING PULL ...\n" if $allowed;

if (my $pid = open my $fh, "|-", "$base/hooks/logger", "") {
    if ($allowed) {
        print $fh localtime().": [$who]: pulled\n";
    }
    else {
        print $fh localtime().": [$who]: Missing [acl.readers] or [acl.deploy] permission. Blocked clone or pull attempt!\n";
    }
    close $fh;
    die localtime().": [$who] git-server: SERVER ERROR! log.logfile failed! $?\n" if $?;
}

die localtime().": [$who] git-server: You have been banned from clone or pull!\n" unless $allowed;
