{
    use strict;
    use warnings;
    use esmith::Logger;
    use esmith::ConfigDB;
    # we need perl-Time-TAI64 for timestamp conversion
    use Time::TAI64 qw /unixtai64/;
    # we need perl-Date-Manip to convert to a unix timestamp
    use Date::Manip qw /UnixDate/;
    use File::Copy;

    my $DB = esmith::ConfigDB->open_ro or die ("cannot open configuration database");

    #we start a hash for name because we don't want several same names & ip
    my %allocated_name = ();
    my %allocated_ip_dhcplease = ();

    # copy of dhcpd.leases file to /tmp because working directly on the original
    # is not a good idea
    copy("/var/lib/dhcpd/dhcpd.leases","/tmp/tmpdhcpd.leases") 
        or die ("Error dhcp-dns Service : Unable to copy the /var/lib/dhcpd/dhcpd.leases"); 

   #we create a fonction because we want to write in log
   sub log2messages
   {
       my $message = shift;
       tie *FH, 'esmith::Logger';
       print FH "$message";
       close FH;
   }


   #Text::DHCPparse forked because it doesn't allow to retrieve the end of lease
   #Only the start is found by the leaseparse of DHCPparse.pm s/starts/ends
   sub leaseparse {
   my $logfile = shift;
   my ( %list, $ip );
   open FILE, $logfile or die;

   while (<FILE>) {
      next if /^#|^$/;
      if (/^lease (\d+\.\d+\.\d+\.\d+)/) {
         $ip = $1; 
         $list{$ip} = sprintf("%-17s", $ip);
      }
      /^\s*hardware ethernet (.*);/ && ($list{$ip} .= sprintf("%-19s", $1));
      /^\s*ends \d (.*);/ && ($list{$ip} .= sprintf("%-21s", $1));
      /^\s*(abandoned).*/ && ($list{$ip} .= sprintf("%-19s", $1));
      /^\s*client-hostname "(.*)";/ && ($list{$ip} .= sprintf("%-17s", $1));
   }

   close FILE;

   # make all entries 74 characters long to format properly
   foreach (keys %list) {
      #$list{$_} = sprintf("%-74s", $list{$_}) if (length$list{$_} < 76);
      $list{$_} = sprintf("%-74.74s", $list{$_});
   }

   return \%list;
}

    # now we parse the leases
    my $return = leaseparse('/tmp/tmpdhcpd.leases');
    my ($ip,$time,$mac,$name);

    # variable with local domain value (default is  mycompamy.local) and retrieve the server name
    my $localdomain = $DB->get_value('DomainName') or die ("Unable retrieve the DomainName property");
    my $servername  = $DB->get_value('SystemName') or die ("Unable retrieve the SystemName property");

    $OUT .= "# A records for dhcp hosts in $localdomain\n";

    foreach (keys %$return) {
        ($ip, $time, $mac, $name) = unpack("A17 A21 A19 A30", $return->{$_});

        # when the dhcp lease is over $name is empty .. we want only non empty one
        if ( $name ne "" ) 
        {
            # we skip also ips & names already allocated
            unless (exists $allocated_ip_dhcplease{$ip} || exists $allocated_name{$name})
            {
                # Convert lease end time to the format expected as
                # see:  http://cr.yp.to/djbdns/tinydns-data.html
                my $ts = UnixDate($time, "%s");
                my $endtai = unixtai64($ts);
                $endtai =~ s/@//;

                # Determine TTL
                my $ttl = '';
                $ttl = 0 unless ($ts <= time);
                $OUT .= "=$name.$localdomain:$ip:$ttl:$endtai\n" unless ($name eq $servername);
                log2messages("The hostname of this server ($servername) is used with different IP in /var/lib/dhcpd/dhcpd.leases")  
                    if ($name eq $servername); 
            }

            $allocated_ip_dhcplease{$ip} = 1;
            $allocated_name{$name} ++;

            log2messages("The hostname \"$name\" is duplicated with different IP in /var/lib/dhcpd/dhcpd.leases")  
                if ($allocated_name{$name} >1);
        }
    }
    # remove the temp file
    unlink "/tmp/tmpdhcpd.leases"
      or log2messages("Error dhcp-dns Service : Unable to remove the /tmp/tmpdhcpd.leases");
}

