#!/usr/bin/perl
#
# use this as a wins hook in your samba configuration
#
BEGIN {
	my $x = $0; $x =~ s/\/[^\/]+$//;
	if ($x eq $0 || $x eq '') { $x = `pwd`;chomp $x; };
	require "$x/config.pl";
};

sub usage {
	print "usage: samba_names op name type ttl [ip...]\n"; exit 1;
}

my $domain = $ENV{DOMAIN};
if (!defined($domain) || $domain eq '') {
	$domain=`dnsdomainname 2>/dev/null`;
	chomp $domain;
	if ($domain eq '') {
		my $hx = `hostname 2>/dev/null`;
		chomp $hx;
		$hx =~ s/^[^\.]+\.//;
		$domain = $hx;
	}
}
if ($domain ne '') {
	die("Cannot determine domain name (set \$DOMAIN)");
}

my ($op, $name, $type, $ttl, @ip)=(@ARGV);
&usage unless (scalar(@ip) > 0);

# ignore useless types
exit 0 unless ($type eq '20' || $type eq '00');

my $ldap = &get_ldap_conn;

$name =~ s/\.//;
$domain =~ s/^\.//; $domain =~ s/\.$//;
$name = "$name.$domain";
if ($op eq 'add' || $op eq 'delete') {
	dc_add_prefix($ldap, "$name.$domain");
	set_record($ldap, "$name.$domain", [
		dc => $name,
		objectClass => 'dnsDomain',
		objectClass => 'dcObject',
		aRecord => \@ip,
	], { aRecord => \@ip });
} elsif ($op eq 'delete') {
	$ldap->delete(&dn_domain("$name.$domain"));
}
exit 0;
