summaryrefslogtreecommitdiff
path: root/kernel/xpp/utils/migrate_xpp
blob: 98d45e754e62e82537f28070a07571d38e839ba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#! /usr/bin/perl -w
use strict;
use POSIX;
#
# Migrate the information from XPP_PRI_SETUP variable in
# /etc/sysconig/zaptel or /etc/default/zaptel into the
# modern configuration:
#   - pri_protocol defined in /etc/xpp.conf
#   - pri_termtype defined in /etc/genconf_parameters
#

my $conf_rh="/etc/sysconfig/zaptel";
my $conf_debian="/etc/default/zaptel";
my $conf;
my $marker = '##';
my @origlines;
my @lines;

if (-f "$conf_rh") {
	$conf="$conf_rh";
} elsif (-f "$conf_debian") {
	$conf="$conf_debian";
} else {
	warn "$0: No '$conf_rh' and no '$conf_debian'. Skipping";
	exit 0;
}

print STDERR "$0: Reading old config from '$conf'\n";
open(F, ". $conf; echo \$XPP_PRI_SETUP |") || die "$0: Failed: $!";
my $line = <F>;
close F;

sub write_file($@) {
	my $fname = shift;
	my @lines = @_;

	open(G, ">$fname.new") || die "$0: Failed opening $fname.new: $!\n";
	print G @lines;
	close G;
	rename($fname, "$fname.backup") ||
		die "$0: rename($fname, $fname.backup): $!";
	rename("$fname.new", $fname) ||
		die "$0: rename($fname.new, $fname): $!";
}

my @wordlist = split(/\s+/, $line);
my @spans;
my $global_pri_protocol;
foreach my $w (@wordlist) {
	my ($span, $val) = split(/=/, $w);
	my ($termtype, $pri_protocol) = split(/,/, $val);
	if(! defined $global_pri_protocol) {
		$global_pri_protocol = $pri_protocol;
	} elsif($global_pri_protocol ne $pri_protocol) {
		die "different pri_protocol (E1/T1/J1) in different spans\n";
	}
	push(@spans, "$span\t$termtype");
}

my $xppconf = '/etc/xpp.conf';
print STDERR "$0: Updating PRI protocol in $xppconf\n";
open(F, "$xppconf") || die "$0: Failed opening $xppconf: $!\n";
@origlines = <F>;
close F;
undef @lines;
foreach (@origlines) {
	next if /^#$marker:/;
	if(s/^pri_protocol.*/pri_protocol\t$global_pri_protocol/) {
		$_ =
			"#$marker: Automatically copied from '$conf'\n" .
			sprintf("#$marker: On %s", ctime(time)) .
			$_;
	}
	push(@lines, $_);
}
write_file($xppconf, @lines);

my $genconf = '/etc/genconf_parameters';
undef @origlines;
undef @lines;
print STDERR "$0: Updating PRI termtypes in '$genconf'\n";
open(F, "$genconf") || die "$0: Failed opening $genconf: $!\n";
my $in_termtype_block = 0;
while(<F>) {
	next if /^#$marker:/;
	next if $in_termtype_block && /^\s+/;
	if(/^pri_termtype/) {
		$in_termtype_block = 1;
		next;
	}
	push(@lines, $_);
}
close F;
my $pri_termtype =
	"#$marker: Automatically copied from '$conf'\n" .
	sprintf("#$marker: On %s", ctime(time)) .
	"pri_termtype\n";
foreach my $span (@spans) {
	$pri_termtype .= "\t$span\n";
}
push(@lines, $pri_termtype);
write_file($genconf, @lines);