#!/usr/local/bin/perl 
# %W%   %E%
# eda_getfile.pl
#
######################################################################
# Copyright 2000 by Electronic Data Systems Corporation.  All
# rights reserved.
#
# An unpublished work created 2000.  This work is a trade secret 
# of EDS and unauthorized use or copying is prohibited.
######################################################################
#
#
# File Name: eda_getfile.pl
# Program or Package Name: eda_getfile.pl
# 
# Client: DFAS                         Author: EDS, Camp Hill PA
#
# Description:  The script retrieves Electronic Document Access (EDA) contract
#		files from the EDA web page for a specified site.
#
#               The script edits the file removing the header lines.
#
#               This progam uses the PERL module LWP::Simple.
#               
# Usage:        eda_getfile.pl <site>
#
# Required Parameters:
#     None
#
# Optional Parameters:
#    site
#	When specified it is the site name used to determine which file
#	gets downloaded, i.e., Columbus, Omaha, etc.
#
# Modification History:
#
# 1.0     020204 DCounterman  #### New program to retrieve EDA contract files.
# 1.1     031219 DCounterman  #### Added proxy capability.
#
#	NOTE:	Whenever a change is made to this program you need to update
#		the Release and/or Version number below.  It is printed in
#		the log file.

		$version   = "R5.0 v1.1";

use LWP::Simple;
use LWP::Simple qw/$ua/;
use Time::Local;

# Months table.
@month     = ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

$now       = time;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
             localtime($now);
($ysec,$ymin,$yhour,$ymday,$ymon,$yyear,$ywday,$yyday,$yisdst) =
             localtime($now - 86400);
$ydate     = sprintf("%s-%02d-%04d", $month[$ymon], $ymday, $yyear+1900);
$filename  = ("$ydate.msg");


@crnt_time = localtime($now);

$log_file  = "/prod/run_log/eda_getfile_" .
             sprintf ("%02d%02d%02d_%02d%02d%02d",
                       $crnt_time[5]%100,
                       $crnt_time[4]+1,
                       $crnt_time[3],
                       $crnt_time[2],
                       $crnt_time[1],
                       $crnt_time[0]
                     ) . ".log";

open (LOG, ">$log_file") or
  die "Can't open log file $log_file - $!";

print LOG "Program eda_getfile.pl started - $version\n\n";

$site   = "";
foreach $arg (@ARGV) {			# Get command line arguments.
  $site = ($arg);;
}

if ( $site eq "" ) {
  print STDOUT $filename . "\n";
  print "Please enter the site name: <ex. Columbus_VP, kansas, Dayton_VP > \n ";
  chomp ($site = <STDIN>);
  $site  = ($site);
}
if ( $site eq "kansas" ) {
  $site  = "DFAS-KansasCity";
}

if ( $site eq "DFAS-SanAntonio-AF" || $site eq "DFAS-SanAntonio" || $site eq "San Antonio" ) {
($ysec,$ymin,$yhour,$ymday,$ymon,$yyear,$ywday,$yyday,$yisdst) =
             localtime($now - 172800);
$ydate     = sprintf("%s-%02d-%04d", $month[$ymon], $ymday, $yyear+1900);
$filename  = ("$ydate.msg");
  $site  = "DFAS-SanAntonio-AF";
  print STDOUT $filename . "\n";
  print STDOUT $site . "\n";
}

$ua->proxy('http', 'http://webproxy:9119');

$address = "http://edadocs.ogden.disa.mil:8104/contracts/$site/$filename";

print LOG "Attempting to get url file $address\n\n";

$rc      = getstore ($address, "/tmp/$filename");

if (is_success($rc) ) {
  print LOG "Successfully retrieved the file - $rc\n\n";
}
else {
  print LOG "There was an error while attempting to retrieve the file - $rc\n";
  close LOG;
  exit;
}

$infile  = "/tmp/$filename";
$outfile = "/prod/eda/eda-in/$filename";

open(FILEIN, "<$infile");
open(FILEOUT, ">$outfile");

while ( <FILEIN> ) {
  chomp ($rec = $_);
  @array = split /,/, $rec;
  next if (scalar(@array) != 9);
  print FILEOUT "$rec\n";
}

close FILEIN;
close FILEOUT;

# Run Friday's File 
if ( $wday eq "1" ) {
($ysec,$ymin,$yhour,$ymday,$ymon,$yyear,$ywday,$yyday,$yisdst) =
             localtime($now - 259200); 

$ydate     = sprintf("%s-%02d-%04d", $month[$ymon], $ymday, $yyear+1900);
$filename2  = ("$ydate.msg");
  print STDOUT $filename2 . "\n";

$ua->proxy('http', 'http://webproxy:9119');

$address = "http://edadocs.ogden.disa.mil:8104/contracts/$site/archive/$filename2";

print LOG "Attempting to get url file $address\n\n";

$rc      = getstore ($address, "/tmp/$filename2");

if (is_success($rc) ) {
  print LOG "Successfully retrieved the file - $rc\n\n";
}
else {
  print LOG "There was an error while attempting to retrieve the file - $rc\n";
  close LOG;
  exit;
}

$infile  = "/tmp/$filename2";
$outfile = "/prod/eda/eda-in/$filename2";

open(FILEIN, "<$infile");
open(FILEOUT, ">$outfile");

while ( <FILEIN> ) {
  chomp ($rec = $_);
  @array = split /,/, $rec;
  next if (scalar(@array) != 9);
  print FILEOUT "$rec\n";
}

close FILEIN;
close FILEOUT;

}
# Run Saturday's File 
if ( $wday eq "1" ) {
($ysec,$ymin,$yhour,$ymday,$ymon,$yyear,$ywday,$yyday,$yisdst) =
             localtime($now - 172800);

# Processing for fridays file - test purposes.....
#             localtime($now - 259200); 

$ydate     = sprintf("%s-%02d-%04d", $month[$ymon], $ymday, $yyear+1900);
$filename3  = ("$ydate.msg");
  print STDOUT $filename3 . "\n";

$ua->proxy('http', 'http://webproxy:9119');

$address = "http://edadocs.ogden.disa.mil:8104/contracts/$site/archive/$filename3";

print LOG "Attempting to get url file $address\n\n";

$rc      = getstore ($address, "/tmp/$filename3");

if (is_success($rc) ) {
  print LOG "Successfully retrieved the file - $rc\n\n";
}
else {
  print LOG "There was an error while attempting to retrieve the file - $rc\n";
  close LOG;
  exit;
}

$infile  = "/tmp/$filename3";
$outfile = "/prod/eda/eda-in/$filename3";

open(FILEIN, "<$infile");
open(FILEOUT, ">$outfile");

while ( <FILEIN> ) {
  chomp ($rec = $_);
  @array = split /,/, $rec;
  next if (scalar(@array) != 9);
  print FILEOUT "$rec\n";
}

close FILEIN;
close FILEOUT;

}

print LOG "File processed and placed in $outfile\n";
close LOG;

=pod

=head1 NAME

eda_getfile.pl - An EDM script for retrieving EDA contract files.

=head1 SYNOPSIS

eda_getfile.pl B<site>

=head1 DESCRIPTION

eda_getfile.pl retrieves Electronic Document Access (EDA) contract files from the EDA web page for a specified site.

The script edits the file removing the header lines.

This progam uses the PERL module LWP::Simple.

=back

=head1 USAGE

eda_getfile.pl B<site>

=head1 REQUIRED PARAMETERS

=over 3

=item None

=head1 OPTIONAL PARAMETERS

=over3

=item site

When specified it is the site name used to determine which file gets downloaded, i.e., Columbus, Omaha, etc.

=back

=head1 AUTHOR and ACKNOWLEDGEMENTS

The original code was written by Daniel Counterman.
It has since been revised by nobody.

=head1 Change log

     $Log: eda_getfile.pl,v $

=cut
