#!/usr/bin/perl -w
# +-======-+ 
#  Copyright (c) 2003-2007 United States Government as represented by 
#  the Admistrator of the National Aeronautics and Space Administration.  
#  All Rights Reserved.
#  
#  THIS OPEN  SOURCE  AGREEMENT  ("AGREEMENT") DEFINES  THE  RIGHTS  OF USE,
#  REPRODUCTION,  DISTRIBUTION,  MODIFICATION AND REDISTRIBUTION OF CERTAIN 
#  COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE UNITED STATES GOVERNMENT AS 
#  REPRESENTED BY THE GOVERNMENT AGENCY LISTED BELOW ("GOVERNMENT AGENCY").  
#  THE UNITED STATES GOVERNMENT, AS REPRESENTED BY GOVERNMENT AGENCY, IS AN 
#  INTENDED  THIRD-PARTY  BENEFICIARY  OF  ALL  SUBSEQUENT DISTRIBUTIONS OR 
#  REDISTRIBUTIONS  OF THE  SUBJECT  SOFTWARE.  ANYONE WHO USES, REPRODUCES, 
#  DISTRIBUTES, MODIFIES  OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED 
#  HEREIN, OR ANY PART THEREOF,  IS,  BY THAT ACTION, ACCEPTING IN FULL THE 
#  RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT.
#  
#  Government Agency: National Aeronautics and Space Administration
#  Government Agency Original Software Designation: GSC-15354-1
#  Government Agency Original Software Title:  GEOS-5 GCM Modeling Software
#  User Registration Requested.  Please Visit http://opensource.gsfc.nasa.gov
#  Government Agency Point of Contact for Original Software:  
#  			Dale Hithon, SRA Assistant, (301) 286-2691
#  
# +-======-+ 
#=======================================================================
#
# Name: getsponsor.pl
# Purpose: get task groupID either from a file which maps users to tasks
#          or from the getsponsor utility or from the groups command
#
# output
# => $groupID (printed to standard output)
#
# Notes:
# 1. If more than one groupID is available, then the user is prompted
#    to select one.
# 2. If no groupID is found, a blank is printed.
#
# !REVISION HISTORY:
# 03May2007  Stassi  Initial version.
# 27Apr2009  Stassi  Divided script into subprograms and added "noprompt" option
# 24Jun2009  Stassi  Added option to get groupID from task map file
# 24Jul2009  Stassi  Added option to get groupID from "groups" command
# 30Jul2010  Stassi  Added option to write output to file instead of STDOUT
# 09Aug2010  Stassi  Moved to getsponsor.pm package; script now just a wrapper
#=======================================================================
use strict;
use FindBin qw($Bin);
use lib ("$Bin");
use getsponsor "get_spcode";

# global variables
#-----------------
my ($groupID, %flags, $outfile);

# main program
#-------------
{
    init();
    $groupID = get_spcode(\%flags);

    if ($outfile) {
        open OUT, "> $outfile" or die ">> Error << opening file: $outfile: $!";
        print OUT "$groupID\n";
        close OUT;
    }
    else {
        print "$groupID\n";
    }
}

#=======================================================================
# name - init
# purpose - initialize variables, get runtime parameters
#=======================================================================
sub init {
    use Getopt::Long;
    my ($usefirst, $dflt, $grpID, $mapfile, $menu, $quiet, $help);

    # initializations
    #----------------
    $groupID = "";

    # runtime parameters
    #-------------------
    GetOptions("1"        => \$usefirst,
               "f=s"      => \$mapfile,
               "dflt"     => \$dflt,
               "grpID=s"  => \$grpID,
               "menu"     => \$menu,
               "o=s"      => \$outfile,
               "q"        => \$quiet,
               "h"        => \$help);
    usage() if $help;

    # set flags hash for call to get_spcode()
    #----------------------------------------
    $flags{"grpID"}    = $grpID if $grpID;
    $flags{"mapfile"}  = $mapfile if $mapfile;
    $flags{"dflt"}     = 1 if $dflt;
    $flags{"menu"}     = 1 if $menu;
    $flags{"quiet"}    = 1 if $quiet;
    $flags{"usefirst"} = 1 if $usefirst;

    # write menu to STDERR
    #---------------------
    $flags{"stderr"} = 1;   
}

#=======================================================================
# name - usage
# purpose - print usage information to standard output
#=======================================================================
sub usage {
    use File::Basename;
    my $name;

    $name = basename $0;
    print STDERR <<"EOF";

usage: $name [options]

   options:
     -1            (This is #1) Use first available groupID as the default.
                   Otherwise the script defaults to last available groupID.
     -f mapfile    file containing mapping between users and tasks
     -dflt         Return default group ID (options not displayed unless -menu)
     -grpID id     Group ID to check (options not displayed unless -menu)
                      if id is valid, then it will be returned as groupID
                      if not, then default will be returned as groupID
     -h            Print this usage information
     -menu         Print menu with -dflt or -grpID flags
     -q            Quiet mode; do not print some of the warning messages
EOF
exit;
}
