3.0b1-lease-convert revision 1.2
1#!/usr/bin/perl
2#
3# Start Date:   Mon, 26 Mar 2001 14:24:09 +0200
4# Time-stamp:   <Monday, 26 March 2001 16:09:44 by brister>
5# File:         leaseconvertor.pl
6# RCSId:        $Id: 3.0b1-lease-convert,v 1.2 2005/08/11 17:13:21 drochner Exp $
7#
8# Description:  Convert 3.0b1 to 3.0b2/final lease file format
9#
10
11require 5.004;
12
13my $rcsID =<<'EOM';
14$Id: 3.0b1-lease-convert,v 1.2 2005/08/11 17:13:21 drochner Exp $
15EOM
16
17use strict;
18
19my $revstatement =<<'EOS';
20	  switch (ns-update (delete (1, 12, ddns-rev-name, null))) {
21	    case 0:
22	      unset ddns-rev-name;
23	      break;
24	  }
25EOS
26
27my $fwdstatement =<<'EOS';
28	  switch (ns-update (delete (1, 1, ddns-fwd-name, leased-address))) {
29	    case 0:
30	      unset ddns-fwd-name;
31	      break;
32	  }
33EOS
34
35
36if (@ARGV && $ARGV[0] =~ m!^-!) {
37    usage();
38}
39
40
41
42# read stdin and write stdout.
43while (<>) {
44    if (! /^lease\s/) {
45	print;
46    } else {
47	my $lease = $_;
48	while (<>) {
49	    $lease .= $_;
50	    # in a b1 file we should only see a left curly brace on a lease
51	    # lines. Seening it anywhere else means the user is probably
52	    # running a b2 or later file through this.
53	    # Ditto for a 'set' statement.
54	    if (m!\{! || m!^\s*set\s!) {
55		warn "this doesn't look like a 3.0b1 file. Ignoring rest.\n";
56		print $lease;
57		dumpRestAndExit();
58	    }
59
60	    last if m!^\}\s*$!;
61	}
62
63	# $lease contains all the lines for the lease entry.
64	$lease = makeNewLease($lease);
65	print $lease;
66    }
67}
68
69
70
71sub usage {
72    my $prog = $0;
73    $prog =~ s!.*/!!;
74
75    print STDERR <<EOM;
76usage: $prog [ file ]
77
78Reads from the lease file listed on the command line (or stdin if not filename
79given) and writes to stdout.  Converts a 3.0b1-style leases file to a 3.0b2
80style (for ad-hoc ddns updates).
81EOM
82
83    exit (0);
84}
85
86
87
88# takes a string that's the lines of a lease entry and converts it, if
89# necessary to a b2 style lease entry. Returns the new lease in printable form.
90sub makeNewLease {
91    my ($lease) = @_;
92
93    my $convertedfwd;
94    my $convertedrev;
95    my $newlease = "";
96    foreach (split "\n", $lease) {
97	if (m!^(\s+)(ddns-fwd-name|ddns-rev-name)\s+(\"[^\"]+\"\s*;)!) {
98	    $newlease .= $1 . "set " . $2 . " = " . $3 . "\n";
99
100	    # If there's one of them, then it will always be the -fwd-. There
101	    # may not always be a -rev-.
102	    $convertedfwd++;
103	    $convertedrev++ if ($2 eq "ddns-rev-name");
104	} elsif (m!^\s*\}!) {
105	    if ($convertedfwd) {
106		$newlease .= "\ton expiry or release {\n";
107		$newlease .= $revstatement if $convertedrev;
108		$newlease .= $fwdstatement;
109		$newlease .= "\t  on expiry or release;\n\t}\n";
110	    }
111	    $newlease .= "}\n";
112	} else {
113	    $newlease .= $_ . "\n";
114	}
115    }
116
117    return $newlease;
118}
119
120
121sub dumpRestAndExit {
122    while (<>) {
123	print;
124    }
125    exit (0);
126}
127