#!/usr/local/bin/perl
#
#  file: create-feed
#  auth: Brad Burdick
#  desc: Create a compressed tar file of the SEC EDGAR not-cooked and
#        correction data files.
#
#  usage: create-feed [-c corr_dir] [-d data_dir] [-f feed_dir] [-n tar_file]
#           [-v] [-w work_dir]
#
##########################################################################
#  Copyright (c) 1994, 1995 Internet Multicasting Service
#
#  The SEC EDGAR Level 1 Dissemination processing software ("software")
#  was developed by the Internet Multicasting Service and may 
#  be used for academic, research, government, and internal business
#  purposes without charge.  You may not resell this code or include it
#  in a product that you are selling without prior permission of the
#  Internet Multicasting Service.
#
#  This software is provided ``as is'', without express or implied
#  warranty, and with no support nor obligation to assist in its
#  use, correction, modification or enhancement.  We assume no liability
#  with respect to the infringement of copyrights, trade secrets, or any
#  patents, and are not responsible for consequential damages.  Proper
#  use of the software is entirely the responsibility of the user.
##########################################################################

eval 'exec /usr/bin/perl -s $0 ${1+"$@"}'
  if 0;

# who am i?
($prog = $0) =~ s#.*/##;

# where we find our local libraries
push(@INC, '/usr/local/ims/lib');

# for processing command line options
require 'getopts.pl';

# Edgar date manipulation routines
require 'edgar-date.pl';

# miscellaneous support routines
require 'edgar-util.pl';

# date stamp used in file name
@today = &edgar_date;
$date = sprintf("%02d%02d%02d", $today[5], $today[4]+1, $today[3]);

# process command line options, if any
&Getopts('c:d:f:n:vw:');

# base data directory
$basedir = defined($opt_d) ? "$opt_d" : "/in/edgar";
# create base directory if it doesn't exist
&makepath($basedir, 0775);

# correction directory
$corrdir = defined($opt_c) ? "$basedir/$opt_c" : "$basedir/corrections";
# create correction directory if it doesn't exist
&makepath($corrdir, 0775);

# feed directory
$feeddir = defined($opt_f) ? "$opt_f" : "/ftp/edgar/Feed";
# create feed directory if it doesn't exist
&makepath($feeddir, 0775);

# tar file name
$tarfile = defined($opt_n) ? "$opt_n" : "$date.nc.tar";

# working directory
$workdir = defined($opt_w) ? "$basedir/$opt_w" : "$basedir/work";
# create work directory if it doesn't exist
&makepath($workdir, 0775);

# verbose output?
$verbose = defined($opt_v);

if (-f "$feeddir/$tarfile" ||
    -f "$feeddir/$tarfile.gz" ||
    -f "$feeddir/$tarfile.Z" ) {
	die "$prog: $feeddir/$tarfile already exists!  Exiting ...\n";
}

# set umask so users can't get files until we're done
$oldumask = umask(077);

print "$prog: creating $tarfile ...\n" if $verbose;
system("/bin/mv $corrdir/*.corr?? $workdir/ 2> /dev/null");
# chdir to insure relative path in tar file
system("(chdir $workdir ; /bin/tar -cf $feeddir/$tarfile *.nc *.corr??)");

print "$prog: gzip'ing $tarfile ...\n" if $verbose;
system("/usr/local/bin/gzip $feeddir/$tarfile");

# allow the world to get the new file
chmod(0664, "$feeddir/$tarfile.gz");

# clean up files
print "$prog: removing *.nc file(s) ...\n" if $verbose;
unlink <$workdir/*.nc>;
print "$prog: removing *.corr file(s) ...\n" if $verbose;
unlink <$workdir/*.corr??>;

# restore umask
umask($oldumask);

exit 0;

