#!/usr/bin/perl

BEGIN { unshift @INC, '/usr/local/cpanel' if ! grep { $_ eq '/usr/local/cpanel' } @INC; }

# This hack is required to clear out preloaded binary crap from dnsadmin-ssl
# And use the awesome special find_namedconf method
# Note on 9/22/2011:
#   Make sure we wipe /var/cpanel/native_ssl_ok
#   As binary dnsadmin-ssl use cPanel fake Perl 5.6.2 and it doesn't support
#   a lot of modules, such as 'lib', 'Data::Dumper' .... etc.
if (-e "/var/cpanel/native_ssl_ok") {
  delete $INC{'Cpanel/DNSLib.pm'};
  require Cpanel::DNSLib;
  unlink "/var/cpanel/native_ssl_ok";
}

our $mypid = $$;
our $updater = "/bin/updatemydns";
our $debugger = "/var/log/dnsadmin.hook";

*main::FORM = $main::rform unless %main::FORM;

if (-x $updater) {
    if ($action eq 'SAVEZONE') {
        open (UPD, "|$updater --update-stdin $FORM{zone}");
        print UPD $FORM{'zonedata'};
        close UPD;
    }
    elsif ($action =~ /^REMOVEZONES?$/) {
        foreach my $zone (split m/,/, $FORM{zone}) {
            system $updater, '--delete', $zone;
        }
    }
    elsif ($action eq 'SYNCZONES') {
        foreach my $key (keys %FORM) {
            if ($key =~ /^cpdnszone\-([\w\.\-]+)$/) {
                my $zone = $1;
                open (UPD, "|$updater --update-stdin $zone");
                print UPD $FORM{$key};
                close UPD;
            }
        }
    }
}

my $log = undef;
END {
  print $log scalar(localtime),": [$$] END ACTION [$action]\n" if $log && $$ == $mypid;
}

require IO::File;
if ($log = new IO::File ">>$debugger") {
  $log->autoflush(1);
  my $prelock = $0;
  my $domain = $FORM{zone} ? " - [$FORM{zone}]" : "";
  $0 = "dnsadmin - $action$domain - SUPER GENTLE ANTIGRINDER PRELOCK: Sleeping since ".scalar(localtime);
  print $log scalar(localtime),": [$$] PRE ACTION [$action]$domain\n";

  if (my $prehandler = $Whostmgr::DnsAdmin::BH_prelock_handler->{$action}) { eval { $prehandler->(\%FORM, $log) }; }

  flock($log, LOCK_EX);

  $0 = $prelock;
  print $log scalar(localtime),": [$$] TOP ACTION [$action]\n";
  require Data::Dumper;
  print $log "FORM = ".Data::Dumper::Dumper(\%FORM);
}

if (1) {
#if (-f '/etc/enableddns') {    # feature control option for debugging
    DDNS_BLOCK : {
        if ($action && grep { $action eq $_ } qw/QUICKZONEADD REMOVEZONE REMOVEZONES SAVEZONE SYNCZONES/) {

            require Whostmgr::DnsAdmin::BH_Utils;

            my $synczones_extract_zone_regexp = qr/^cpdnszone-(.+)$/;
            my @ZONES;
            if ($FORM{'zone'}) {
                push @ZONES, $FORM{'zone'};
            }
            elsif ($action eq 'SYNCZONES' && grep { /$synczones_extract_zone_regexp/ } keys %FORM) {
                push @ZONES, map { /$synczones_extract_zone_regexp/; $1 } grep { /$synczones_extract_zone_regexp/ } keys %FORM;
            }
            else {
                print $log "SOFTWARE BUG: No arg 'zone' for action [$action]\n";
                last DDNS_BLOCK;
            }
            foreach my $z (@ZONES) {
                last DDNS_BLOCK if Whostmgr::DnsAdmin::BH_Utils::is_ddns_migrating($z, $log);
            }

            require Cpanel::cPanelFunctions;
            my $shorthostname = Cpanel::cPanelFunctions::shorthostname();
            if ($shorthostname !~ /ns0/ and %FORM and scalar keys %FORM) {
                require Whostmgr::DnsAdmin::BH_phonehome;
                Whostmgr::DnsAdmin::BH_phonehome::dnsadmin_phonehome($action, \%FORM, $log);
            }
        }
    }
}
if (my $handler = $Whostmgr::DnsAdmin::BH_handler->{$action}) { eval { $handler->(\%FORM, $log) }; }

1;
