#!/usr/bin/perl -w

#=============================================================================|
#                *** PROPRIETARY RIGHTS NOTICE ***                            |
#   This material contains proprietary information of MULI Holdings P/L,      |
#   Wahroonga, NSW, Australia and/or its licensor, embodying                  |
#   "CONFIDENTIAL INFORMATION" ideas and expressions, no part of which may    |
#   be reproduced or transmitted without permission of MULI Management P/L.   |
#      ***  COPYRIGHT  1982 - 2013  ***  MULI Holdings P/L  ***               |
#                          All rights reserved.                               |
#=============================================================================|

#############################################################
#
#=>V15-001	BrowCh	28.02.19	R00000652	change for mbbag_run
# N=>018	ChaoMa	08.07.14        Support new Muliclient
# N=>017	ChaoMa	06.07.12        Support new Muliclient on Mac
# N=>016	ChaoMa	03.02.12        Delete preload libtcmalloc_minimal
# N=>015	ChaoMa	02.02.12        Fix peload libtcmalloc_minimal on ubuntu 64bits
# N=>014	ChaoMa	02.02.12        Fix peload libtcmalloc_minimal
# N=>013	ChaoMa	01.02.12        Preload libtcmalloc_minimal
# N=>012	ChaoMa	18.01.11        Redirect stderr to $log
# N=>011	ChaoMa	28.09.10        Add "--magicnumber" option
# N=>010	ChaoMa	28.09.10        Add magic number as an optional arguments
# N=>009	ChaoMa	08.09.10        Rewrite regex of options handling
# N=>008	ChaoMa	08.09.10        Enable debug options for nbbag_run
# N=>007	ChaoMa	07.09.10        Handling exception in log writing
#                                       Close STDERR before exec for security
# N=>006	ChaoMa	06.09.10        Add "--app_option" option
# N=>005	ChaoMa	06.09.10        Handling nbbag_run options
# N=>004	ChaoMa	06.09.10	Only permit nbbag_run running
# N=>003	ChaoMa	03.09.10	Disable the usage of GNOME, KDE and LXDE
# N=>002	ChaoMa	03.09.10	Remove prompt information for security
# N=>001	ChaoMa	03.09.10	Merge muli_shell into dump_shell
# N=>000	ChaoMa	01.09.10	initial version
#
#############################################################

my $bash = "/bin/bash";
my $log = "/usr/local/muli/log/dump_shell.log";

sub valid_cmd
{
    my @cmds = @_;

    my $app_opt = '(-a|--app_option)\s+\w{4}';
    my $debug_opt = '(-v|--verbose)[0-6]?';
    my $magic_opt = '(-g|--magicnumber)\s+[[:xdigit:]]+';
    my $opt1 = "${app_opt}(\\s+${debug_opt})?";
    my $opt2 = "(${debug_opt}\\s+)?${app_opt}";

    return 0 if ($cmds[0] ne "-c" || $cmds[1] !~ /x2go/ && $cmds[1] ne "scp -t -r ~");
    exit 0 if ($cmds[1] =~ /x2gostartagent/ 
	       && $cmds[1] !~ /\s+R\s+mbbag_run\s+(${opt1}|${opt2})(\s+${magic_opt})?\s*/);
    return 1;
}

sub log_arg
{
    my @arg = @_;

    eval {
	if ($arg[0] =~ /log/) {
	    open (FLOG, ">>$log") or die;
	    print FLOG "argv: $_\n" or die foreach @arg;
	    print FLOG "\n" or die;
	    close (FLOG);
	}
    };
}
log_arg($0, @ARGV);

close(STDERR);
eval { open(STDERR, ">>$log") or die; };
exec $bash, @ARGV if ($#ARGV == 1 && valid_cmd(@ARGV));

@ARGV = ();
1 while (<>);
