#!/usr/bin/env perl
#----------------------------------------------------------------------------
#
# Name: dms_asn_delete.pl
# 
# This perl script is used to display all the baselined SMS names and end times
# for SMS names that begin with a given string.
#
# History:
# Date     OPR      Who         Reason
# -------- -------- ----------  ---------------------------------------------
# 08/25/05 54219    Baum        Initial code 
#----------------------------------------------------------------------------
# set up external routines
unshift @INC,(split /:/, $ENV{PATH});
require 'printmsg.pl';       # prints a formatted status message
require 'runsql.pl';         # run query returning only record count
require 'sql_select_pkg.pl'; # run queries that return records

#specify exit status values

    $PROCESS_FAILURE = 1; 
    $PROCESS_SUCCESS = 0; 

    $usage = <<"EOM";
Usage:
   dms_asn_sms.pl sms_prefix
   
      where: 
      
      sms_prefix is first few character of the sms_id values.

   This scripts reports all the baselined sms names and end times 
   from the sms_catalog where the sms names start with the sms_prefix.
   
   For example to find all the baselined sms_ids for 2002, use:
   >dms_asn_sms.pl 02
   
EOM

#check for arguments
    $num_arg = scalar @ARGV;
    if ($num_arg != 1) {
       print $usage;
       PrintMsg("E","Invalid arguments - try again.");
       exit ($PROCESS_FAILURE);
    } else {
       $sms_prefix = uc($ARGV[0]);
    }

# get the required external variables

    $DSQUERY=           $ENV{"DSQUERY"};
    $SPSS_DB=           lc($ENV{"SPSS_DB"});

# validate required ENV
    if (!defined($DSQUERY) || !defined($SPSS_DB) ) {
        PrintMsg("E", "DSQUERY and SPSS_DB must be defined.");
        exit ($PROCESS_FAILURE);
    }
# begin processing

    PrintMsg ("I","--- start --- Display Baselined SMS Names -------");
    PrintMsg ("I","--- sms_prefix: $sms_prefix");
    PrintMsg ("I","--- SPSS_DB: $SPSS_DB");

#---------------------------------------------------------------------
# open database for queries
#---------------------------------------------------------------------
    $db = new ST_DBlib $DSQUERY, $SPSS_DB;
    die if $db == -1;

#---------------------------------------------------------------------
# query baselined SMS names 
#---------------------------------------------------------------------
    $query = <<"EOQ";
SELECT sms_id, end_time 
FROM  sms_catalog
WHERE sms_id like "$sms_prefix%" and
      sms_send_stt = "B"
EOQ
    
    $err_msg  = "Cannot query sms_catalog.";
    $err_msg2 = "Cannot query next sms_catalog record.";
    $count = 0;
    ($sms_id, $end_time) = FirstRecordSql( $db, $query, $err_msg);
    
    while (defined($sms_id)) {
	print  "SMS ", $sms_id, " ending ", substr($end_time,0,17), "\n";
        $count += 1;
        ($sms_id, $end_time) = NextRecordSql( $db, $err_msg2);
    }
    PrintMsg("I","Found $count baselined SMS names.");
    
#---------------------------------------------------------------------
# end of all queries
#---------------------------------------------------------------------
    $db->dbclose;
    PrintMsg ("I","---  end  --- Display Baselined SMS Names -------");
    exit ($PROCESS_SUCCESS);
