#!/pkg/bin/perl -w
#------------------------------------------------------------------
# Copyright (c) 2008-2011 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------

# This helper script suggests a location to use for the 'show tech mpls ldp'
# when one has not been provided by the user.

# get optional location argument passed from user
$locn = shift;

# RUN a show cmd, and return the output as a scalar
sub exec_cmd {
    die "cannot fork: $!" unless defined($pid = open(SAFE_KID9, "-|"));
    if ($pid == 0) {
	#child
 
        #redirect stderr to stdout
	open STDERR, ">&STDOUT";

	exec(@_);
    } else {
	#parent 
	$out = "";
	while(<SAFE_KID9>) {
	    $out .= $_;
	}
	close SAFE_KID9;
	#$out;
    }
    $out;
}

$show_place = exec_cmd("showtech_helper","-n","show placement program mpls_ldp");

if ($show_place =~ /Invalid input/) {
    # not supported on this platform.
    exit;
}

if ($locn) {
    if ($show_place =~ /$locn/) {
        # the specified location matches the placement info. GOOD.
	exit;
    }
    print STDERR "\nWARNING: Invalid location $locn specified!\n\n";
}

print STDERR "Tech data must be collected from the location(s) where mpls_ldp is running.\n";

print STDERR "  Location of LDP can be obtained using 'show placement program mpls_ldp'\n";

print STDERR "Usage: show tech-support mpls ldp location <...>\n";
