#!/usr/bin/perl
use Pod::Usage;
use Getopt::Long;
use strict;

use constant UPGRADE_MANAGER	=>'/usr/sbin/hspc-upgrade-manager';

my %force;

sub process_config
{
	my @priority = sort {$force{$a} <=> $force{$b}} keys %force;
	my $hspc_um_options = ' --finish --force common/init/'.join(' --force common/init/', @priority);
	return system(&UPGRADE_MANAGER . $hspc_um_options);
}

sub main
{
	die "You need to be root to run this script.\n" unless($< == 0);

	my %options;
	GetOptions(
		'usage|u'			=> \$options{usage},
		'help|h|?'			=> \$options{help},
		'admin-create'		=> \$options{create},
		'admin-passwd=s'	=> \$options{passwd},
		'admin-email=s'		=> \$options{email},
		'apache-conf'		=> \$options{apacheconf},
		'hspc-conf'			=> \$options{hspcconf},
		'reset-hostname'	=> \$options{resethostname},
		'firewall'			=> \$options{firewall},
		'sql-password'		=> \$options{sqlpassword},
	) or  pod2usage(-exitstatus => 2, -verbose => 0);

	pod2usage(-exitstatus => 0, -verbose => 2) if $options{help};
	pod2usage(-exitstatus => 0, -verbose => 1) if $options{usage};

	$ENV{ADMPASSWORD}	= $options{passwd}	if($options{passwd});
	$ENV{ADMEMAIL}		= $options{email}	if($options{email});

	if($options{create} || $options{email} || $options{passwd}){
		$force{'admin-setup.pl'}	= 1;
		$force{'sendemail.sh'}		= 4;
	}
	
	$force{'http_conf.pl'} = 2 if $options{apacheconf};
	
	if($options{hspcconf} || $options{resethostname}){
		$force{'change_hostname.sh'} = 3;
	}
	$force{'firewall.sh'} = 1 if($options{firewall});

	if($options{sqlpassword}){
		$force{'root_sql_passwd.sh'}	= 1;
		$force{'sendemail.sh'}			= 4;
	}
	pod2usage(-exitstatus => 0, -verbose => 1) unless %force;
	exit process_config();
}

main();

__END__

=head1 NAME

hspc-config Post configuration Parallels Business Automation - Standard script.

=head1 SYNOPSIS

B<hspc-config> OPTION [OPTION1]...

=head1 OPTIONS

=over 4

=item B<--admin-create>

			create administrator

=item B<--admin-passwd password>

			set administrator password

=item B<--admin-email email>

			set administrator e-mail address

=item B<--apache-conf>

			create apache configuration files

=item B<--hspc-conf>

			create Parallels Business Automation - Standard configuration file

=item B<--firewall>

			(re)enable firewall using iptables

=item B<--sql-password>

			generate new MySQL root password

=item B<--reset-hostname>

			the same as --hspc-conf --apache-conf

=item B<--help>

			output this message


=back

=head1 DESCRIPTION

hspc-config script manage core system and HSPC parameters via init scripts & hspc-upgrade-manager.

=head1 COPYRIGHT

Copyright (c) 2007 SWsoft.
