s3-selftest: include addprinter/deleteprinter command.
[abartlet/samba.git/.git] / source3 / script / tests / printing / modprinter.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Getopt::Long;
6 use Cwd qw(abs_path);
7
8 my $opt_help = 0;
9 my $opt_smb_conf = undef;
10 my $opt_add = 0;
11 my $opt_delete = 0;
12
13 my $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
20 sub 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
40 usage(1) if (not $result);
41
42 usage(0) if ($opt_help);
43
44 if (!$opt_add && !$opt_delete) {
45         usage(1, "invalid: neither --add|-a nor --delete|-d set");
46 }
47
48 if (!$opt_smb_conf) {
49         usage(1, "invalid: no smb.conf file set");
50 }
51
52 my @argv = @ARGV;
53
54 my $printer_name = shift(@argv);
55 my $share_name = shift(@argv);
56 my $port_name = shift(@argv);
57 my $driver_name = shift(@argv);
58 my $location = shift(@argv);
59 my $win9x_driver_location = shift(@argv);
60 my $remote_machine = shift(@argv);
61
62 if (!defined($share_name) || length($share_name) == 0) {
63         $share_name = $printer_name;
64 }
65
66 if (!defined($share_name)) {
67         die "share name not defined";
68 }
69
70 my $tmp = $opt_smb_conf.$$;
71
72 my $section = undef;
73 my $within_section = 0;
74 my $found_section = 0;
75
76 open(CONFIGFILE_NEW, "+>$tmp") || die "Unable top open conf file $tmp";
77
78 open (CONFIGFILE, "+<$opt_smb_conf") || die "Unable to open config file $opt_smb_conf";
79 while (<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                                 die("share $share_name already exists\n");
99                         }
100                         if ($opt_delete) {
101                                 $within_section = 1;
102                                 next;
103                         }
104                 } else {
105                         print CONFIGFILE_NEW $line;
106                         $within_section = 0;
107                 }
108                 next;
109         } else {
110                 if ($within_section == 1) {
111                         next;
112                 }
113                 print CONFIGFILE_NEW $line;
114         }
115 }
116 if ($opt_add) {
117         print CONFIGFILE_NEW "[$share_name]\n\tprintable = yes\n\tpath = /tmp\n";
118 }
119 close (CONFIGFILE);
120 close (CONFIGFILE_NEW);
121
122 if ($opt_delete && ($found_section == 0)) {
123         die "share $share_name not found";
124 }
125 system("cp", "$tmp", "$opt_smb_conf");
126 unlink $tmp;
127
128 exit 0;