Fix a small typo in a comment and pretty it up a bit.
[Samba/gebeck_regimport.git] / examples / misc / modify_samba_config.pl
blobeb997f9b0c87097bfe0da35a3cecf8d3e136a591
1 #!/usr/bin/perl
3 ##
4 ## Simple example of how to implement a '[add|delete] share command' for
5 ## use with the Windows NT Server Manager. See smb.conf(5) for details
6 ## on the '[add|delete] share command'
7 ##
8 ## Author : Gerald (Jerry) Carter <jerry@samba.org>
9 ##
11 use POSIX qw(tmpnam);
14 ## local variables
16 my $delete_mode = undef;
17 my $add_mode = undef;
18 my $tmp_file_name = undef;
21 ## check for correct parameters
22 if ($#ARGV == 1) {
23 $delete_mode = 1;
25 elsif ($#ARGV == 3) {
26 $add_mode = 1;
28 else {
29 print "Usage: $0 configfile share [path] [comment]\n";
30 exit -1;
33 ## first param is always the config file
34 open (CONFIGFILE, "$ARGV[0]") || die "Unable to open $ARGV[0] for reading!\n";
36 ## FIXME!! Right now we throw away all comments in the file.
37 while (<CONFIGFILE>) {
39 chomp($_);
41 ## eat leading whitespace
42 $_ =~ s/^\s*//;
44 ## eat trailing whitespace
45 $_ =~ s/\s*$//;
48 ## throw away comments
49 next if (($_ =~ /^#/) || ($_ =~ /^;/));
51 ## set the current section name for storing the hash
52 if ($_ =~ /^\[.*\]$/) {
54 $_ = substr($_, 1, length($_)-2);
56 if ( length($_) ) {
57 $section = $_;
59 else {
60 print "Bad Section Name - no closing ]\n";
61 exit -1;
64 next;
67 ## check for a param = value
68 if ($_ =~ /=/) {
69 ($param, $value) = split (/=/, $_);
70 $param =~ s/./\l$&/g;
71 $param =~ s/\s+//g;
72 $value =~ s/^\s+//;
74 $config{$section}{$param} = $value;
76 next;
79 ## should have a hash of hashes indexed by section name
81 close (CONFIGFILE);
84 ## We have the smb.conf in our hash of hashes now.
85 ## Add or delete
87 if ($add_mode) {
88 $config{$ARGV[1]}{'path'} = $ARGV[2];
89 $config{$ARGV[1]}{'comment'} = $ARGV[3];
91 elsif ($delete_mode) {
92 delete $config{$ARGV[1]};
96 ## Print the resulting configuration
98 #do {
99 # $tmp_file_name = tmpnam();
100 # print "Using temporary file - $tmp_file_name\n";
101 #} while (!sysopen(TMP, $tmp_file_name, O_RDWR|O_CREAT|O_EXCL));
102 $tmp_file_name = tmpnam();
103 open (TMP, ">$tmp_file_name") || die "Unable to open temporary file for writing!\n";
105 PrintConfigFile(TMP);
107 ## now overwrite the original config file
108 close (TMP);
109 system ("cp -pf $ARGV[0] $ARGV[0].bak");
110 system ("cp -pf $tmp_file_name $ARGV[0]");
111 unlink $tmp_file_name;
114 exit 0;
120 #######################################################################################
121 ## PrintConfigFile()
123 sub PrintConfigFile {
124 my ($output) = @_;
126 ## print the file back out, beginning with the global section
127 print $output "#\n# Generated by $0\n#\n";
129 PrintSection ($output, 'global', $config{'global'});
131 foreach $section (keys %config) {
133 if ("$section" ne "global") {
134 print $output "## Section - [$section]\n";
135 PrintSection ($output, $section, $config{$section});
139 print $output "#\n# end of generated smb.conf\n#\n";
142 #######################################################################################
143 ## PrintSection()
145 sub PrintSection {
146 my ($outfile, $name, $section) = @_;
148 print $outfile "[$name]\n";
149 foreach $param (keys %$section) {
150 print $outfile "\t$param".' 'x(25-length($param)). " = $$section{$param}\n";
152 print $outfile "\n";