#!/usr/bin/perl
#
# Copyright (c) 2006 Zmanda Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Contact information: Zmanda Inc, 505 N Mathlida Ave, Suite 120
# Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
#
#

use strict;
use warnings;
use File::Spec::Functions;
use lib "/usr/local/lib/mysql-zrm";
use ZRM::Common;

our $oldPATH = $ENV{'PATH'};

#path where ZRM utilities are installed

my $ZRM_BINPATH;

if ( $^O =~ /^MSWin/ ) {
	$ZRM_BINPATH= catfile( $ZRM_HOME, "usr", "bin" );
} else {
	$ENV{'PATH'} = "/usr/local/bin:/opt/csw/bin:/usr/bin:/usr/sbin:/sbin:/bin:/usr/ucb";
	$ZRM_BINPATH="/usr/local/bin";
}


my $USAGE="--action <backup|restore|schedule-backup|report|list|getconf|setconf|purge|parse-binlogs|check|manage-backup|verify-backup|extract-backup|abort-backup>\nHelp on individual action items can be obtained by specifying --help with the required action.\nFor example: 'mysql-zrm --action backup --help'";

my @cmd_args;

sub usageTop()
{
        print STDERR "$_[0]\n";
        print STDERR "USAGE:\n";
        print STDERR "$USAGE\n";
        exit(2);
}

sub getAction()
{
	my $len = @ARGV;
	my $i;
	my $act;
	for( $i = 0; $i < $len; $i++ ){
		if( $ARGV[$i]=~/^--action=/ ){
			$act = $';
		}elsif( $ARGV[$i]=~/^--action/ ){		
			$i++;
			if( $i < $len ){
				$act = $ARGV[$i];
			}
		}else{
			my $x = "\"$ARGV[$i]\"";
			push @cmd_args, $x;			
		}
	}
	if( ! defined $act || $act eq "" ){
		&usageTop( "ERROR: Please specify --action" );
	}
	$ENV{"ZRM_CMD_NAME"}="$0 --action $act";
	$ENV{"ZRM_LAUNCHER"}=$$;
	return $act;
}

sub main()
{
	my $act = getAction();	
	if( !defined $act ){
		&usageTop( "ERROR: Please specify --action" );
	}
	my $cmd;
	if($act eq "backup") {
		$cmd = "mysql-zrm-backup";
        } elsif($act eq "restore") {
		$cmd = "mysql-zrm-restore";
        } elsif($act eq "list" ) {
		$cmd = "mysql-zrm-list";
        } elsif($act eq "purge" ){
		$cmd = "mysql-zrm-purge";
        } elsif($act eq "parse-binlogs"){
		$cmd = "mysql-zrm-parse-binlogs";
        } elsif($act eq "check"){
		$cmd = "mysql-zrm-check";
        } elsif($act eq "verify-backup"){
		$cmd = "mysql-zrm-verify-backup";
        } elsif($act eq "extract-backup"){
		$cmd = "mysql-zrm-extract-backup";
        } elsif($act eq "abort-backup"){
		$cmd = "mysql-zrm-abort-backup";
        } elsif($act eq "schedule-backup"){
		$cmd = "mysql-zrm-scheduler";
        } elsif($act eq "report"){
		$cmd = "mysql-zrm-reporter";
        } elsif($act eq "getconf"){
		$cmd = "mysql-zrm-getconf";
        } elsif($act eq "manage-backup"){
		$cmd = "mysql-zrm-manage-backup";
        } elsif($act eq "setconf"){
		$cmd = "mysql-zrm-setconf";
        }else{
                &usageTop("ERROR: Unknown Action '$act'");
        }
	my $r;
	$cmd = catfile( $ZRM_BINPATH, $cmd );
	
	if ($^O =~ /^MSWin/ ) {
	      	$cmd = "\"$cmd\"";
        }
        $r = system( "$PERL_BINARY $cmd @cmd_args" );
	exit( $r>>8 );
}
&main();
