#!/usr/bin/perl 
# (C) 2005 Alex Hudson; GPLv2+
use strict;
use warnings;

my $installed_version = 0;
my $current_version = 5;
my $verfile = '/var/lib/hula/schema_version';
my $dir = '/var/lib/hula';
my $user = 'mail';

###

my %schema = (
	1 => {
		'classes/hulacmlistsmodule' => {
			containedby 	=> 'NIMS:ConnMgr Agent',
			flags 		=> '0',
			name		=> 'HulaCMListsModule',
			naming		=> 'CN',
			optional	=> "NIMS:Blocked Address\r\nNIMS:Allowed Address",
			superclass	=> 'HulaConnMgrModule',
		},
		'classes/hulacmusermodule' => {
			containedby 	=> 'NIMS:ConnMgr Agent',
			flags 		=> '0',
			name		=> 'HulaCMUserModule',
			naming		=> 'CN',
			superclass	=> 'HulaConnMgrModule',
		},
		'classes/hulacmrdnsmodule' => {
			containedby 	=> 'NIMS:ConnMgr Agent',
			flags 		=> '0',
			name		=> 'HulaCMRDNSModule',
			naming		=> 'CN',
			superclass	=> 'HulaConnMgrModule',
		},
		'classes/hulacmrblmodule' => {
			containedby 	=> 'NIMS:ConnMgr Agent',
			flags 		=> '0',
			name		=> 'HulaCMRBLModule',
			naming		=> 'CN',
			optional	=> "NIMS:RBL Host",
			superclass	=> 'HulaConnMgrModule',
		},
		'classes/hulaconnmgrmodule' => {
			containedby 	=> 'NIMS:ConnMgr Agent',
			flags 		=> '0',
			name		=> 'HulaConnMgrModule',
			naming		=> 'CN',
			superclass	=> 'Novonyx:Mail Agent',
		},
		'classes/nims%058connmgr%032agent' => {
			contains 	=> "HulaConnMgrModule\r\nHulaCMUserModule\r\nHulaCMListsModule\r\nHulaCMRBLModule\r\nHulaCMRDNSModule",
		},
	},
);

my %tree = (
	1 => {
		'internet%032services/server%032messaging%032server/connection%032manager/lists%032module' => {
			cn		=> 'Lists Module',
			'novonyx%058module%032name' => 'HULACMLISTS.NLM',
			'novonyx%058module%032version' => 1,
			'object%032class' => 'HulaCMListsModule',
		},
		'internet%032services/server%032messaging%032server/connection%032manager/rbl%032module' => {
			cn		=> 'RBL Module',
			'novonyx%058module%032name' => 'HULACMRBL.NLM',
			'novonyx%058module%032version' => 1,
			'object%032class' => 'HulaCMRBLModule',
		},
		'internet%032services/server%032messaging%032server/connection%032manager/rdns%032module' => {
			cn		=> 'RDNS Module',
			'novonyx%058module%032name' => 'HULACMRDNS.NLM',
			'novonyx%058module%032version' => 1,
			'object%032class' => 'HulaCMRDNSModule',
		},
		'internet%032services/server%032messaging%032server/connection%032manager/user%032module' => {
			cn		=> 'User Module',
			'novonyx%058module%032name' => 'HULACMUSER.NLM',
			'novonyx%058module%032version' => 1,
			'object%032class' => 'HulaCMUserModule',
		},
		'internet%032services/server%032messaging%032server/connection%032manager' => {
			'novonyx%058module%032name' => 'HULACONNMGR.NLM',
		},
	},
	3 => {
		'internet%032services/server%032messaging%032server/nmap%032agent' => {
			'nims%058spooldirectory' => '/var/spool/hula',
		},
	},
	4 => {
		'internet%032services/server%032messaging%032server/nmap%032agent' => {
			'novonyx%058rts%032handling' => '1',
		},
	},
	5 => {
		'context/server%032web%032administration' => {
			'webadmincertpath' => '/etc/hula/webadmin/osslcert.pem',
			'webadminkeypath' => '/etc/hula/webadmin/osslpriv.pem',
			'webadminpath' => '/usr/lib/hulawebadmin',
		},
	},
);

my ($errors, $reset_ownership) = (0, 0);

sub updateMDB {
	my ($area, $version) = @_;
	my ($basedir, $var);
	if ($area eq 'schema') {
		$basedir = $dir . '/mdb/schema/';
		$var = \%schema;
	}
	if ($area eq 'tree') {
		$basedir = $dir . '/mdb/tree/';
		$var = \%tree;
	}
	die "Unknown area $area" if (! defined $basedir);
	foreach my $directory (keys %{$var->{$version}}) {
		foreach my $file (keys %{$var->{$version}->{$directory}}) {
			if (! -d "$basedir/$directory") {
				`mkdir -p $basedir/$directory`;
				$reset_ownership++;
			}
			my $path = $basedir . '/' . $directory . '/' . $file;
			$reset_ownership++ if (! -e $path);
			open (FILE, ">$path") or die "Could not open $path!";
			print FILE $var->{$version}->{$directory}->{$file};
			print FILE "\r\n";
			close (FILE);
		}
	}
	if ($reset_ownership) {
		`chown $user:$user -R $basedir`;
		$reset_ownership = 0;
	}
}

sub update_version() {
	open (CONF, ">$verfile") or die "Cannot open $verfile: $!";
	print CONF $installed_version;
	close (CONF);
}

if (! -e $verfile) {
	if (-e "$dir/mdb/schema_version") {
		`mv $dir/mdb/schema_version $verfile`;
	}
}

if (! -e $verfile) {
	if (! -d "$dir/mdb") {
		$installed_version = $current_version;
	}
	update_version();
} else {
	open (CONF, "<$verfile") or die "Cannot open $verfile: $!";
	$installed_version = <CONF>;
	close (CONF);
}

if (! -d "$dir/mdb") {
	print "Hula has not been configured; not attempting to update schema\n";
	exit;
}

if ($installed_version == $current_version) {
	print "Currently at schema version $current_version; no upgrade required\n";
	exit;
}

if ($installed_version == 0) {
	# see if there are any old directory references
	my @files = `grep -l "/var/lib/netmail" -r $dir/mdb/*`;
	map { chomp; } @files;
	foreach my $file (@files) {
		open (FILE, "<$file") or $errors++;
		my @contents = <FILE>;
		close (FILE);
		map { s!/var/lib/netmail!/var/lib/hula!g; } @contents;
		open (FILE, ">$file") or $errors++;
		map { print FILE $_; } @contents;
		close (FILE);
	}
	$installed_version++ if (! $errors);
}

if ($installed_version == 1) {
	# new connection manager stuff
	updateMDB ('schema', 1);
	updateMDB ('tree', 1);
	
	$installed_version++ if (! $errors);
}

if ($installed_version == 2) {
	# move the spool to /var/spool/hula
	updateMDB ('tree', 3);
	$installed_version++ if (! $errors);
}

if ($installed_version == 3) {
	# turn on 'bounce to sender'
	updateMDB ('tree', 4);
	$installed_version++ if (! $errors);
}

if ($installed_version == 4) {
	# moving templates/snapins to correct directory
	updateMDB ('tree', 5);
	$installed_version++ if (! $errors);
}

print "Updated schema with $errors error(s), now at version: $installed_version\n";
update_version();

exit -1 if ($errors);
