#!/bin/sh
# Documentation in perl code below.
# -*- perl -*-
# vim:set syn=perl:

# Ensure ZEUSHOME is setup to point to our installation
if [ -z "$ZEUSHOME" ]
then
    # Have a good guess, we live in $ZEUSHOME/zxtm/bin
    OLD_PWD=`pwd`
    cd `dirname $0`/../..
    ZEUSHOME=`pwd`
    export ZEUSHOME
    cd $OLD_PWD
fi

if [ ! -d "$ZEUSHOME/zxtm" ]
then
   echo "`basename $0`: FATAL: Cannot find installation, check \$ZEUSHOME"
   exit 1
fi

# Restart ourselves in perl
exec $ZEUSHOME/perl/miniperl -wx $0 ${1+"$@"}

#!perl -w #"
#line 28

=head1 NAME

support-report - Produce a support file

=head1 SYNOPSIS

B<support-report> [--all-logs] filename

=head1 DESCRIPTION

B<support-report> generates a support file that can be given to your
support contact to help diagnose problems.

By default the support file contains the latest log entries, specify
--all-logs to include the current log files in full and any rotated log files.

=head1 COPYRIGHT

Copyright (C) 2000-2014 Riverbed Technology, Inc. All rights reserved.

=cut

# Try to get networking commands into our PATH
$ENV{PATH} = "/sbin:/usr/etc/sbin:/usr/etc:/usr/sbin:$ENV{PATH}";

my $file;
my $all = 0;
while( @ARGV ) {
   $_ = shift;
   if( /^--all-logs$/i ) {
      $all = 1;
   } elsif( /^(-h|--help)$/i ) {
      print "Usage: $0 [--all-logs] filename\n";
      exit;
   } elsif( /^--/ ) {
      die "Unknown option: $_\n";
   } else {
      $file = $_;
      last;
   }
}
die "Usage: $0 [--all-logs] filename\n" unless( $file );

# Ensure the CGI script behaves correctly (i.e. doesn't think it is a CGI script)
delete $ENV{SCRIPT_NAME};

# All the work is done by backup.cgi
if( $all ) {
   system( "$ENV{ZEUSHOME}/zxtmadmin/docroot/backup.cgi", '--support', '--all-logs', $file );
} else {
   system( "$ENV{ZEUSHOME}/zxtmadmin/docroot/backup.cgi", '--support', $file );
}

if( $file !~ /\.(tar|tgz)$/ ) {
   # We can't tell from here where to check for the TSR if they didn't provide
   # a full filename, e.g. providing a base directory, no extension, or calling
   # via SOAP (where the filename is '-')
   exit( 0 );
} else {
   # If file now exists, exit 0. Otherwise, exit 1.
   exit( ! -e $file );
}
