#!/usr/bin/env perl

use strict;
use warnings;
use v5.10;
use File::Basename;
use Net::IP;

use ACU::Log;
$ACU::Log::log_file = "/var/log/hooks/" . basename($0) . ".log";

my $ip = $1 if ($ENV{'SSH_CLIENT'} =~ m/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/);

log DEBUG, "Connection to $ENV{GL_REPO} from $ip";

# First, check if the repository is in the YYYY/ directory
exit 0 if ($ENV{GL_REPO} !~ /^2[0-9]{3}\/.+\/.+/);


say "Votre IP est : $ip.";

$ip = Net::IP->new($ip) or die ("IP invalide");

my $schoolnetwork = Net::IP->new('10.41.0.0/16');

if ($ip->overlaps($schoolnetwork) != $IP_A_IN_B_OVERLAP)
{
    log ERROR, "Vous n'êtes pas autorisé à envoyer vos modifications depuis cette IP.";
    exit 1;
}

my $sshnetwork = Net::IP->new('10.41.253.0/24');

if ($ip->overlaps($sshnetwork) == $IP_A_IN_B_OVERLAP)
{
    log ERROR, "Vous n'êtes pas autorisé à envoyer vos modifications depuis cette IP.";
    exit 1;
}


exit 0;
