#!/usr/bin/perl -w

use strict;
use warnings;
# Comment out next option before release!
#use diagnostics -verbose;

use Getopt::Long;
use Pod::Usage;
use Digest::SHA qw(sha1_hex);
use Exporter 'import';

use Lxc::object;

use LxctlHelpers::helper;
use LxctlHelpers::config;


our $version = "0.3.0";
our $api_ver = 1;

our $Lxc = Lxc::object->new;

sub help_me
{
	my $action = shift;
	return if !$action;

	if ($action eq '--version' || $action eq '-v') {
		print "Version: $version\n";
		exit 0;
	}

	if ($action eq '--help' || $action eq '-h') {
		pod2usage(1);
	}
	$Lxc->check();
}

sub lets_rock
{
	my $config = new LxctlHelpers::config;
	$config->load_main();
	my $action = shift @ARGV;

	help_me($action);

	my $class;
	my $helper = new LxctlHelpers::helper;

	eval {
		$helper->load_module("Lxctl/$action.pm");
		$action = "Lxctl::$action";
		$action->import;
		$class = new $action;
	} or do {
		#die "Unsupported command!\n\n$@\n\n";
		die "$@";
	};

	$class->do(@ARGV);

	return;
}

lets_rock;

__END__

=head1 NAME

lxctl - Control various aspects of lxc.

=head1 SYNOPSIS

lxctl [action] [vmname] [options]

See lxctl --man or lxctl --help for more info

=head1 OPTIONS

=over 8

=item B<--help>

Print a breif help message and exists

=item B<--man>

Prints the manual page and exits.

=item B<start>

Starts container specified in 1st argument

B<Required arguments:>

	vmname - name of the container

=item B<stop>

Stops container specified in 1st argument

B<Required arguments:>

	vmname - name of the container

=item B<create>

Creates container.

B<Required arguments:>

	vmname - name of the container

B<Optional arguments:>

	--ipaddr - IP address of the machine

	--mask/netmask - network mask of the machine

	--defgw - default gateway of the machine

	--dns - primary DNS server

	--ostemplate - template name, by default it is 'lucid_amd64'

	--config - path to configuration file, by default /etc/lxc/<container name> is used

	--root - path to root file system, by default /var/lxc/<container name> is used

	--addpkg - list of additional packages (comma-separated)

	--pkgopt - list of additional packet manager options (space-separated, but as one argument)

	--rootsz - size of logical volume for root FS, by default it is 10G

	--hostname - sets the hostname of the machine, by default <container name> is used

	--searchdomain - set a custom searchdomain in /etc/resolv.conf

	--macaddr - set the custom mac adress of the container

	--autostart - autostart container each reboot host machine

	--no-save - do not save yaml config for new container, by default $CONF_PATH/vmname.yaml is used

	--load - create container from yaml config

	--debug - show more information about install process

	--tz - set custom timezone (Europe/Moscow, UTC, etc)

	--empty - create a clear container for migrate here

=item B<set>

Changes container parameters.

B<Required arguments:>

	vmname - name of the container

B<Optional arguments:>

	--rootsz - increment of size of logical volume for root FS

	--ipaddr - IP address if the machine

	--mask/netmask - network mask of the machine

	--defgw - default gateway of the machine

	--dns - primary DNS server

	--hostname - sets the hostname of the machine

        --searchdomain - set a custom searchdomain in /etc/resolv.conf

        --macaddr - set the custom mac adress if the machine

	--userpasswd user:passwd - sets password for given user

	--onboot {yes,no} - makes containet [do not] start at boot

        --tz - set custom timezone (Europe/Moscow, UTC, etc)

	--cpu-shares - sets the CPU share of the container

	--cpus - sets the CPU cores of the container

	--mem - sets the memory share of the container (in bytes!)

	--io - sets the IO share of the container

=item B<freeze>

Freezes container

B<Required arguments:>

	vmname - name of the container

=item B<unfreeze>

Unfreezes container

B<Required arguments:>

	vmname - name of the container

=item B<list>

Lists all containers

B<Optional arguments:>

	--ipaddr - display with IP addr

	--hostname - display with hostname.

	--cgroup - display with cgroup

	--mount - display with mount point for rootfs

	--diskspace - display with free/full size

	--all - display all information

	--raw - display only vmnames

=item B<migrate>

Migrate container from localhost to remote host.

B<Required arguments>

	--vmname - container name

	--tohost - to which host we should migrate

B<Optional arguments>

	--remuser - remote username for ssh

	--remport - remote port for ssh

	--remname - remote container name

	--onboot - start on boot? 1 or 0

	--userpasswd - 'user:password' formatted password for user

	--clone - cloning, a little bit faster and softer then simple migration

	--rootsz - remote root fs size

	--afterstart - start local container again after migration

	--cpus - cpus allocated to container

	--cpu-shares - cpu time share of the container

	--mem - memory limit of the container

	--io - IO throughput

	--ipaddr - IP of the remote container

	--searchdomain - DNS search domain of the container

	--netmask - network mask

	--defgw - default gateway

	--dns - DNS server

=item B<vz2lxc>

Migrate VZ-container from remote host to local LXC container.

B<Required arguments>

	--vmname - container name

	--fromhost - from which host we should migrate

	--remname - remote container name

B<Optional arguments>

	--remuser - remote username for ssh

	--remport - remote port for ssh

	--onboot - start on boot? 1 or 0

	--rootsz - remote root fs size

	--afterstart - start local container again after migration

	--cpus - cpus allocated to container

	--cpu-shares - cpu time share of the container

	--mem - memory limit of the container

	--io - IO throughput

=back

=head1 DESCRIPTION

B<lxctl> controls lxc :)

Man page by Capitan Obvious.

=head1 AUTHOR

Anatoly Burtsev, E<lt>anatolyburtsev@yandex.ruE<gt>
Pavel Potapenkov, E<lt>ppotapenkov@gmail.comE<gt>
Vladimir Smirnov, E<lt>civil.over@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2011 by Anatoly Burtsev, Pavel Potapenkov, Vladimir Smirnov

This script is free software; you can redistribute it and/or modify
it under the same terms of GPL v2 or later, or, at your opinion
under terms of artistic license.


=cut
