#!/usr/bin/perl -w
################################################################################
#  Author:      Joseph C. Pietras - Joseph.Pietras@gmail.com
#  License:     GNU GENERAL PUBLIC LICENSE Version 2
#  GitHub:      svn co https://github.com/ossCare/svnPlus
#               git https://github.com/ossCare/svnPlus.git
#  SourceForge: git clone git://git.code.sf.net/p/svnplus/code svnplus-code
################################################################################
# ENTER: INITIALIZE THE PROGRAM
use warnings;
use strict;
use Cwd 'abs_path'; # to be able to get full path to this file
# LEAVE: INITIALIZE THE PROGRAM
################################################################################

################################################################################
# ENTER: find the name of this program and its directory
my $NAME=&abs_path($0);         # print "NAME=$NAME\n";
#  $NAME =~ s@/@\\@g;           # change all "/" to "\" chars WINDOWS?
my $DIRE = $NAME;               # init to full path for finding directory we live
   $NAME =~ s@.*\\@@;           # print "NAME=$NAME\n";
   $NAME =~ s@.*/@@;            # print "NAME=$NAME\n";
   $DIRE =~ s@\\[^\\][^\\]*$@@; # print "DIRE=$DIRE\n";
   $DIRE =~ s@/[^/][^/]*$@@;    # print "DIRE=$DIRE\n";
my $dbglvl;
# LEAVE: find the name of this program and its directory
################################################################################

################################################################################
# ENTER: CONFIG FILE, PRE-CONFIG FILE and REQUIRED SUBROUTINE FILE
require "$DIRE/tagprotect.pl";          # subroutines, read them in
# LEAVE: CONFIG FILE, PRE-CONFIG FILE and REQUIRED SUBROUTINE FILE
################################################################################

################################################################################
# ENTER: PARSE COMMAND LINE AND CONFIGURATION FILE
$ARGV[0] = '--help' if (int(@ARGV) == 0);
&ParseCLI($NAME, $DIRE, \@ARGV); # ParseCLI will die  with errors if it fails
$dbglvl = &ParseCFG(); # ParseCFG dies if it can't read config
# LEAVE: PARSE COMMAND LINE AND CONFIGURATION FILE
################################################################################

## # SINCE NOT USED JUST DON'T BOTHER
## ################################################################################
## # ENTER: READ "LOCK TOKENS" FROM STDIN -> NOTE: script doesn't use the locks
## # to run from command line redirect stdin with "< /dev/null"
## #y $lockTokens  = ""; # can get "strict" warnings in debug without this
## my $lockTokens  = do { local $/ = ""; <STDIN> };
## $lockTokens //= "";
## print STDERR "$NAME: " . ' $lockTokens= ' . '"' . $lockTokens . '"' . "\n" if ( $dbglvl > 4);
## # LEAVE: READ "LOCK TOKENS" FROM STDIN -> NOTE: script doesn't use the locks
## ################################################################################

if ( &SimplyAllow() )
{
    # if the commit is ok, because it does impact protected directories,
    # but debug is wanted then this script must exit NON-zero, which
    # causes the commit to fail but the client gets the standard error.
    # A zero exit causes the STDERR to be squashed.  If any "True"
    # errors occured, "SimplyAllow" will have printed them to standard
    # error and would have returned 0.
    if ( $dbglvl > 0 )
    {
        print STDERR "$NAME: SimplyAllow succeeded but script exiting 1 (FAIL) because debug is enabled.\n";
        exit 1;
    }
    exit 0;
}

if ( &AllowCommit() )
{
    # if the commit is ok, i.e.: it is allowed, but debug is wanted
    # then this script must exit NON-zero, which causes the commit to
    # fail but the client gets the standard error.  A zero exit causes
    # the STDERR to be squashed.    If any "True" errors occurred,
    # "AllowCommit" will have printed them to standard error and would
    # have returned 0.
    if ( $dbglvl > 0 )
    {
        print STDERR "$NAME: AllowCommit succeeded but script exiting 1 (FAIL) because debug is enabled.\n";
        exit 1;
    }
    exit 0;
}

# commit is not allowed, sub "AllowCommit" has already output the reason
print STDERR "$NAME: exit 1 (FAIL) this is a true prevent commit condition.\n" if ( $dbglvl > 0 );
exit 1;
