#! /usr/bin/perl
#
#		Convert the ypserv.conf file from the 1.3 format
#		to the new 2.0 format.
#
# Version:	@(#)convert-ypserv-conf  1.0  miquels@cistron.nl
#

$| = 1;

unless (open(FD, "<$ARGV[0]")) {
	print STDERR "convert-ypserv-conf: $ARGV[0]: $!\n";
	exit 1;
}
my @stat = stat FD;

my $old;
my $new;

while (<FD>) {
	$old .= $_;
	chomp;
	s/^(#\s*Host\s*:)\s*Map\s*:\s*Security\s*:\s*Passwd_mangle\s*/$1 Domain  : Map              : Security/;

	unless (/^(#?\s*\S+\s*):(\s*\S+\s*):(\s*(?:port|none|deny)\s*)(?::(\s*\S+\s*))?$/) {
		$new .= "$_\n";
		next;
	}
	my ($host, $map, $sec, $mangle) = ($1, $2, $3, $4);
	if ($mangle =~ m/^\s*yes(:\d+)?/) {
		my $port = $1;
		$sec =~ s#(port|none)#$1/mangle$port#;
	}
	$new .= "$host: *       :$map:$sec\n";
}
close FD;

exit 0 if ($old eq $new);

print "Converting version 1.3 ypserv.conf to 2.0...";

unless (open(FD, ">$ARGV[0].$$")) {
	print STDERR "convert-ypserv-conf: $ARGV[0].$$: $!\n";
	exit 1;
}

chown $stat[4], $stat[5], "$ARGV[0].$$";
chmod $stat[2], "$ARGV[0].$$";

print FD $new;
close FD;
unless (rename("$ARGV[0].$$", $ARGV[0])) {
	print STDERR "convert-ypserv-conf: $ARGV[0].$$: $!\n";
	unlink "$ARGV[0].$$";
	exit 1;
}

print "done.\n";

exit 0;

