#!/usr/bin/env perl
#----------------------------------------------------------------------------
#
# Name: clean_request_members.pl
#
# Purpose: Remove records from request_members table for use in scripts
#
# Interactive Usage:
#       >clean_request_members.pl  <group_name> <group_data_id>
#                                 
#       The ENV variables DSQUERY amd OPUS_DB must be defined.
#
#
# History:
# Date     OPR      Who         Reason
# -------- -------- ----------  ---------------------------------------------
# 07/07/04 51342    Baum        Initial code
# 09/15/06 56505    MSwam       Use prefixed DB field names
# 03/25/10 64274    MSwam       Replace ST_DBlib with DBI
# 06/30/10 64432    MSwam       Use single quotes for SQLServer
#----------------------------------------------------------------------------
# set up external routines
unshift @INC,(split /:/, $ENV{PATH});
require 'do_dbi_pkg.pl';         # run query returning only record count

    #specify exit status values

    $FAILURE =      0;   # exit status 
    $SUCCESS =      1;   # exit status 

    $usage = <<EOM;
Usage: clean_request_members.pl <group_name> <data_id>

    where both arguments are required:
     <group_name>  is same as dataset name of an Ingest group OSF;
     <data_id> is 3 characters, the same as the data_id of an Ingest group OSF.
     
    These two arguments are the primary key to the records in the
    OPUS_DB request_members table.
EOM
    $num_args = scalar @ARGV;
    
    if ($num_args != 2) {
       print $usage;
       exit ($FAILURE);
    }
    $group_name = $ARGV[0];
    $data_id    = $ARGV[1];
    
    if (length($data_id) != 3) {
       print $usage;
       print "Error: second arg (data_id) must be three characters.\n";
       exit ($FAILURE);       
    }
    $DSQUERY         = $ENV{"DSQUERY"};
    $OPUS_DB         = $ENV{"OPUS_DB"};
           
    # Verify ENV variables
    if (!defined($DSQUERY) || !defined($OPUS_DB)) {
       print "Missing ENV variables: DSQUERY or OPUS_DB.\n";
       exit ($FAILURE);
    }    
    # open database for queries
    $db = DoDBIopen( $DSQUERY, $OPUS_DB, $FAILURE);
    
    my $query = <<EOQ;       
DELETE request_members 
WHERE rqm_group_name = '$group_name' and rqm_data_id = '$data_id'
EOQ
    $count = DoDBI($db, $query);
    print "Deleted $count records from request_members table.\n";

    DoDBIclose($db);
    exit ($SUCCESS);
