Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / contrib / 3.0b1-lease-convert
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.1 2001/04/18 19:17:34 mellon Exp $
7 #
8 # Description:  Convert 3.0b1 to 3.0b2/final lease file format
9 #
10
11 require 5.004;
12
13 my $rcsID =<<'EOM';
14 $Id: 3.0b1-lease-convert,v 1.1 2001/04/18 19:17:34 mellon Exp $
15 EOM
16
17 use strict;
18
19 my $revstatement =<<'EOS';
20           switch (ns-update (delete (1, 12, ddns-rev-name, null))) {
21             case 0:
22               unset ddns-rev-name;
23               break;
24           }
25 EOS
26
27 my $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           }
33 EOS
34
35
36 if (@ARGV && $ARGV[0] =~ m!^-!) {
37     usage();
38 }
39
40
41
42 # read stdin and write stdout.
43 while (<>) {
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
71 sub usage {
72     my $prog = $0;
73     $prog =~ s!.*/!!;
74
75     print STDERR <<EOM;
76 usage: $prog [ file ]
77
78 Reads from the lease file listed on the command line (or stdin if not filename
79 given) and writes to stdout.  Converts a 3.0b1-style leases file to a 3.0b2
80 style (for ad-hoc ddns updates).
81 EOM
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.
90 sub 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
121 sub dumpRestAndExit {
122     while (<>) {
123         print;
124     }
125     exit (0);
126 }