source: trunk/server/source3/script/tests/printing/modprinter.pl

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 2.5 KB
Line 
1#!/usr/bin/perl -w
2
3use strict;
4
5use Getopt::Long;
6use Cwd qw(abs_path);
7
8my $opt_help = 0;
9my $opt_smb_conf = undef;
10my $opt_add = 0;
11my $opt_delete = 0;
12
13my $result = GetOptions(
14 'help|h|?' => \$opt_help,
15 'smb_conf|s=s' => \$opt_smb_conf,
16 'add|a' => \$opt_add,
17 'delete|d' => \$opt_delete
18);
19
20sub usage($;$)
21{
22 my ($ret, $msg) = @_;
23
24 print $msg."\n\n" if defined($msg);
25
26 print "usage:
27
28 --help|-h|-? Show this help.
29
30 --smb_conf|-s <path> Path of the 'smb.conf' file.
31
32 --add|-a 'add' a printer.
33 --delete|-d 'delete' a printer.
34
35 printer_name share_name port_name driver_name location XX remote_machine
36";
37 exit($ret);
38}
39
40usage(1) if (not $result);
41
42usage(0) if ($opt_help);
43
44if (!$opt_add && !$opt_delete) {
45 usage(1, "invalid: neither --add|-a nor --delete|-d set");
46}
47
48if (!$opt_smb_conf) {
49 usage(1, "invalid: no smb.conf file set");
50}
51
52my @argv = @ARGV;
53
54my $printer_name = shift(@argv);
55my $share_name = shift(@argv);
56my $port_name = shift(@argv);
57my $driver_name = shift(@argv);
58my $location = shift(@argv);
59my $win9x_driver_location = shift(@argv);
60my $remote_machine = shift(@argv);
61
62if (!defined($share_name) || length($share_name) == 0) {
63 $share_name = $printer_name;
64}
65
66if (!defined($share_name)) {
67 die "share name not defined";
68}
69
70my $tmp = $opt_smb_conf.$$;
71
72my $section = undef;
73my $within_section = 0;
74my $found_section = 0;
75
76open(CONFIGFILE_NEW, "+>$tmp") || die "Unable top open conf file $tmp";
77
78open (CONFIGFILE, "+<$opt_smb_conf") || die "Unable to open config file $opt_smb_conf";
79while (<CONFIGFILE>) {
80 my $line = $_;
81 chomp($_);
82 $_ =~ s/^\s*//;
83 $_ =~ s/\s*$//;
84 if (($_ =~ /^#/) || ($_ =~ /^;/)) {
85 print CONFIGFILE_NEW $line;
86 next;
87 }
88 if ($_ =~ /^\[.*\]$/) {
89 $_ = substr($_, 1, length($_)-2);
90 if (length($_)) {
91 $section = $_;
92 } else {
93 die "invalid section found";
94 }
95 if ($section eq $share_name) {
96 $found_section = 1;
97 if ($opt_add) {
98 exit 0;
99# die("share $share_name already exists\n");
100 }
101 if ($opt_delete) {
102 $within_section = 1;
103 next;
104 }
105 } else {
106 print CONFIGFILE_NEW $line;
107 $within_section = 0;
108 }
109 next;
110 } else {
111 if ($within_section == 1) {
112 next;
113 }
114 print CONFIGFILE_NEW $line;
115 }
116}
117if ($opt_add) {
118 print CONFIGFILE_NEW "[$share_name]\n\tprintable = yes\n\tpath = /tmp\n";
119}
120close (CONFIGFILE);
121close (CONFIGFILE_NEW);
122
123if ($opt_delete && ($found_section == 0)) {
124 die "share $share_name not found";
125}
126system("cp", "$tmp", "$opt_smb_conf");
127unlink $tmp;
128
129exit 0;
Note: See TracBrowser for help on using the repository browser.