use strict;
$0 = 'smb-config-upgrade';
my $pkg_path = shift || '';
my $install_dest = shift || '';
my $install_volume = shift || '';
my $install_operation = $ENV{SCRIPT_NAME} ? $ENV{SCRIPT_NAME} : 'postupgrade';
my $SMB_CONF = $ENV{SMB_CONF} ? $ENV{SMB_CONF}
: "$install_volume/etc/smb.conf";
my $SMB_TEMPLATE = $ENV{SMB_TEMPLATE} ? $ENV{SMB_TEMPLATE}
: "$install_volume/etc/smb.conf.template";
sub read_new_template
{
my $template = shift;
my $fh;
my $lines = [];
my $snarf = 0;
open ($fh, "< $template")
or die "$0: unable to read $template: $!";
while (my $line = <$fh>) {
if ($line =~ m/BEGIN required/) {
$snarf = 1;
} elsif ($line =~ m/END required/) {
push @$lines, ($line);
$snarf = 0;
}
if ($snarf) {
push @$lines, ($line);
}
}
close($fh);
return $lines;
}
sub generate_new_config
{
my $config = shift;
my $template = shift;
my $replace = 0;
my $fh;
my $lines = [];
open ($fh, "< $config")
or die "$0: unable to read $config: $!";
while (my $line = <$fh>) {
if ($line =~ m/BEGIN required/) {
$replace = 1;
push @$lines, @$template;
next;
} elsif ($line =~ m/END required/) {
$replace = 0;
next;
}
unless ($replace) {
push @$lines, ($line);
}
}
close($fh);
return $lines;
}
sub replace_config
{
require File::Temp;
my $config = shift;
my $lines = shift;
my ($fh, $name) = File::Temp::tempfile(
TEMPLATE =>'.smb.upgrade.XXXXXXXX',
UNLINK => 0,
DIR => "$install_volume/etc");
map { $fh->write($_); } @$lines;
$fh->flush();
$fh->sync();
rename $name, $config
or die "$0: unable to replace $config: $!";
chmod 0644, $config;
$fh->close();
return 1;
}
my $template_lines;
my $config_lines;
unless ($template_lines = read_new_template($SMB_TEMPLATE)) {
die "$0: smb.conf template is corrupt or missing";
}
unless ($config_lines = generate_new_config($SMB_CONF, $template_lines)) {
die "$0: smb.conf is corrupt or missing";
}
if (scalar(@$config_lines) == 0) {
print "$0: empty $SMB_CONF?\n";
exit 1;
}
unless (replace_config($SMB_CONF, $config_lines)) {
die "$0: unable to replace $SMB_CONF";
}
exit 0;