#!/usr/bin/env perl
# This script reads an automountpoint file and generates an LDIF
# This is hacked up from ldifautomnt.pl
use strict;
use warnings;
use diagnostics;
use vars qw($fh);
use lib "./perl/lib"; # because it's not really installed
use lib "./lib"; # because it's not really installed
use Net::LDAP::Util;

sub nismap ($$) {
	my ($type, $map) = @_;
	
	print $fh join "\n",
		"dn: nismapname=$map,dc=catnip",
		#"dn: ou=$map,dc=catnip",
		'objectClass: top',
		'objectClass: nismap',
		#"objectClass: $type", # type varies for the real automap and the sub-maps
		"nisMapName: $map",
		#"ou: $map",
		'', '';
}

sub nisobj ($$$) {
	my ($map, $entry, $target) = @_;
	
	print $fh join "\n",
		"dn: nismapname=$entry,nismapname=$map,dc=catnip",
		#"dn: cn=$entry,ou=$map,dc=catnip",
		'objectClass: top',
		'objectClass: nisobject',
		#'objectClass: automount',
		"cn: $entry",
		"nisMapName: $entry",
		"nisMapEntry: $target",
		#"automountInformation: $target",
		'', '';
}

$fh = *STDOUT;
my $map = $ARGV[0] or die "$0: Usage: $0 MAPNAME";
$map =~ s/.*\///;

# Make the map itself
nismap('organizationalUnit', $map.',ou=automounter');

# Make each entry in the map
open(MAP, "<", $ARGV[0]) or die "$0: couldn't open '$ARGV[0]'";
while (my $line = <MAP>) {
	chomp $line;
	next unless $line;
	next if $line =~ m/^\s*#/;
	
	my ($entry, $flags_host) = $line =~ m/(\S+)\s+(.*)/;
	$flags_host =~ s/&$/$entry/; # undo the shorthand notation
	$entry = Net::LDAP::Util::escape_dn_value($entry); # (gtk+)--, (warp2+)--
	nisobj($map.',ou=automounter', $entry, $flags_host);
}
close MAP;
