summaryrefslogtreecommitdiff
path: root/kernel/xpp/utils/zaptel_drivers
blob: e53d56f15ae8a4f3577ed74d7f4482d5083f9399 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#! /usr/bin/perl -w
use strict;
use File::Basename;
BEGIN { my $dir = dirname($0); unshift(@INC, "$dir", "$dir/zconf"); }

use Errno;
use Getopt::Std;
use Zaptel::Hardware;

my %opts;
my $etc_modules = '/etc/modules';
my $zaptel_redhat = '/etc/sysconfig/zaptel';
my $zaptel_debian = '/etc/default/zaptel';
my $zaptel_conffile;

getopts('vdM', \%opts) || die "$0: Bad options\n";

if(-f $zaptel_redhat) {
	$zaptel_conffile = $zaptel_redhat;
} elsif(-f $zaptel_debian) {
	$zaptel_conffile = $zaptel_debian;
	$opts{'d'} = 1 if $opts{'M'};
} else {
	die "$0: Could not find '$zaptel_redhat' nor '$zaptel_debian'\n";
}

my @drivers = Zaptel::Hardware->drivers;

sub update_zaptel_distro(@) {
	my @driver_list = @_;
	my $varname = 'MODULES';
	my $newfile = "${zaptel_conffile}.new";
	my $backupfile = "${zaptel_conffile}.bak";

	print "Updating $zaptel_conffile\n" if $opts{'v'};
	open(NEWFILE, ">$newfile") || die "$0: Failed to open '$newfile': $!\n";
	open(BACKUPFILE, ">$backupfile") || die "$0: Failed to open '$backupfile': $!\n";
	if(open(IN, $zaptel_conffile)) {
		while(my $line = <IN>) {
			print BACKUPFILE $line;
			chomp $line;
			next if $line =~ /^${varname}=/;	# Skip old defs.
			print NEWFILE "$line\n";
		}
		close IN;
	} elsif(defined($!{ENOENT})) {
		print "Creating $zaptel_conffile\n" if $opts{'v'};
	} else {
		die "$0: Failed opening '$zaptel_conffile': $!\n";
	}
	print NEWFILE "${varname}='@driver_list'\n";
	close NEWFILE;
	close BACKUPFILE;
	rename($newfile, $zaptel_conffile) ||
		die "$0: rename($newfile, $zaptel_conffile) failed: $!\n";
}

# This is for Debian.
sub update_etc_modules(@) {
	my @driver_list = @_;
	my $newfile = "${etc_modules}.new";
	my $backupfile = "${etc_modules}.bak";
	# Just to make module loading order deterministic.
	my @module_order = qw(
			wct4xxp
			wcte12xp
			wcte11xp
			wct1xxp
			wanpipe
			tor2
			torisa
			qozap
			vzaphfc
			zaphfc
			ztgsm
			wctdm24xxp
			wctdm
			opvxa1200
			wcfxo
			pciradio
			wcusb
			xpp_usb
			ystdm8xx
			zma8xx
			);

	open(NEWFILE, ">$newfile") || die "$0: Failed to open '$newfile': $!\n";
	open(BACKUPFILE, ">$backupfile") || die "$0: Failed to open '$backupfile': $!\n";
	if(open(IN, $etc_modules)) {
		print "Updating $etc_modules\n" if $opts{'v'};
		while(my $line = <IN>) {
			print BACKUPFILE $line;
			chomp $line;
			next if grep(/^\s*${line}\s*$/, @module_order, @driver_list);
			print NEWFILE "$line\n";
		}
		close IN;
	} elsif(defined($!{ENOENT})) {
		print "Creating $etc_modules\n" if $opts{'v'};
	} else {
		die "$0: Failed opening '$etc_modules': $!\n";
	}
	foreach my $d (@module_order) {
		print NEWFILE "$d\n" if grep($d eq $_, @driver_list);
	}
	close NEWFILE;
	close BACKUPFILE;
	rename($newfile, $etc_modules) ||
		die "$0: rename($newfile, $etc_modules) failed: $!\n";
}

if($opts{'d'}) {
	update_etc_modules(@drivers);
}
if($opts{'M'}) {
	update_zaptel_distro(@drivers);
}

if(!$opts{'d'} && !$opts{'M'}) {
	print join("\n", @drivers),"\n";
}

__END__

=head1 NAME

zaptel_drivers - Show drivers required for installed zaptel devices.

=head1 SYNOPSIS

zaptel_drivers [-vdM]

=head1 DESCRIPTION

This script shows by default the list of drivers required for currently
installed zaptel devices.

Options:

=over 4

=item -v

Verbose

=item -d

Generate /etc/modules for Debian systems.

=item -M

Generate distribution dependent module configuration file:
  /etc/sysconfig/zaptel    # For RedHat like systems
  /etc/default/zaptel      # For Debian like systems

On debian systems, specifying this option turns on the '-d' option as well.

=back