my $numargs = @ARGV;
if ($numargs lt 1) {
print "useage: smb-migrate test|test-commit|commit\n";
exit(1);
}
my $user_parms = 0;
my $new_conf_file = "/etc/samba/smb.conf";
my @new_conf;
my $merged_conf_file = "/etc/samba/smb.conf";
my $merge_comment = "# *** merged from original smb.conf: ***\n";
my $uncomment_comment = "# *** uncommented from original smb.conf: ***\n";
my $unique_comment = "# *** unique added from original smb.conf: ***\n";
my @merge_log;
my $log_file = "/var/log/samba/smb-migrate.log";
my $to_merge = "/etc/samba/smb.conf.tomerge";
if ($ARGV[0] eq "test" || $ARGV[0] eq "test-commit") {
$to_merge = "smb.conf";
$log_file = "smb-migrate.log";
$merged_conf_file = "smb.conf.merged";
}
my $processed = `grep -c 'original smb.conf: ***' $to_merge`;
if ($processed > 0) {
`cp $to_merge $new_conf_file`;
print "Already processed, aborting.\n";
exit 0;
}
my @old_conf = `grep -v "^#" $to_merge | grep -v "^;" | grep -v "^\$"` or die;
`cp /usr/share/samba/smb.conf.clean $new_conf_file` if $ARGV[0] !~ /test/;
my @new_conf_org = `cat $new_conf_file` or die;
mlog("Data to change/add in standard sections of smb.conf:\n\n");
sub mlog {
my (@dstring) = @_;
if ($ARGV[0] eq "test") {
print "@dstring";
} else {
push @merge_log, @dstring;
}
}
sub merge_conf {
my ($header, $new_value) = @_;
my @parmlist = split " = ", $new_value;
my $match = 0;
my $comment = '';
$comment = $unique_comment if $continuation = 0;
$continuation = 1;
$index = 0;
foreach (@new_conf_org) {
if (/^\[$header\]|;\[$header\]|^; \[$header\]|^\[$header\$\]|;\[$header\$\]|^; \[$header\$\]/) {
$header = "print" . '$' if $header eq "print";
if (/^;\[|^ my $entry = $_;
@new_conf_org[$index] =~ s/^;|^; |^ mlog("uncomment: $header line $index: $entry -> @new_conf_org[$index]");
splice(@new_conf_org, $index, 0, $uncomment_comment);
$index++
}
$start_loc = $index;
last;
}
$index++
}
my $elements = @new_conf_org;
for ($i = $start_loc + 1; $i < $elements; $i++) {
my $is_header = @new_conf_org[$i];
$is_header =~ s/^ |\t|\n//;
if ($is_header =~ /^\[|;\[| if ($match == 0) {
$continuation = 0 if $new_value !~ /\\$/;
mlog("unique: $header line $last_index: $new_value");
splice(@new_conf_org, $last_index + 1, 0, $comment, $new_value);
$last_index++;$last_index++;
}
return;
}
if ($new_value =~ /winbind/) {
$old_value = $new_value;
$new_value =~ s/winbind/idmap/;
mlog("syntax: $header: $old_value -> $new_value");
}
if (@new_conf_org[$i] =~ /@parmlist[0]/) {
if (@new_conf_org[$i] !~ /^;|^ if (@new_conf_org[$i] ne $_) {
mlog("update: $header line $i: @new_conf_org[$i] -> $new_value");
@new_conf_org[$i] = ";" . $new_conf_org[$i];
splice(@new_conf_org, $i + 1, 0, $merge_comment, $new_value);
}
$match = 1;
} else {
if (@new_conf_org[$i] =~ / = /) {
mlog("add: $header line $i: @new_conf_org[$i] -> $new_value");
splice(@new_conf_org, $i + 1, 0, $merge_comment, $new_value);
$i++;$i++;
$match = 1;
}
}
$last_index = $i;
return if ($match eq 1);
$match = 0;
}
}
return;
}
foreach (@old_conf) {
if (/^\[/) {
if (!/^\[global\]|^\[homes\]|^\[netlogon\]|^\[Profiles\]|^\[printers\]|^\[print\$\]|^\[pdf-generator\]/) {
$user_parms = 1;
push (@new_conf, $_);
} else {
$user_parms = 0;
chop;
$header = $_;
s/\[|\]|\$//g;
$bare_header = $_;
}
} else {
if ($user_parms == 1) {
push (@new_conf, $_);
} else {
if (!/^[ ]+ merge_conf($bare_header, $_);
}
}
}
}
mlog("\nNew data for smb.conf:\n\n");
mlog("@new_conf");
if ($ARGV[0] eq "commit" || $ARGV[0] eq "test-commit") {
local *NEWCONF;
open(NEWCONF, "> $merged_conf_file");
print NEWCONF @new_conf_org;
print NEWCONF @new_conf;
close NEWCONF;
local *LOGFILE;
open(LOGFILE, "> $log_file");
print LOGFILE @merge_log;
close LOGFILE
}