#!/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;

our $oldPATH = $ENV{'PATH'};
$ENV{'PATH'} = "/usr/local/bin:/opt/csw/bin:/usr/bin:/usr/sbin:/sbin:/bin:/usr/ucb";

#path where ZRM utilities are installed
my $ZRM_BINPATH="/usr/local/bin";

my $USAGE="--action <backup|restore|schedule-backup|report|list|getconf|purge|parse-binlogs|check|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 usage()
{
        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 "" ){
		&usage( "ERROR: Please specify --action" );
	}
	$ENV{"ZRM_CMD_NAME"}="$0 --action $act";
	$ENV{"ZRM_LAUNCHER"}=$$;
	return $act;
}

sub main()
{
	my $act = getAction();	
	if( !defined $act ){
		&usage( "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";
        }else{
                &usage("ERROR: Unknown Action '$act'");
        }
	my $r = system( "$ZRM_BINPATH/$cmd @cmd_args" );
	exit( $r>>8 );
}
&main();
