#!/usr/local/bin/perl
#
#  file: install-edgar
#  auth: Brad Burdick
#  desc: Install daily EDGAR data feed into data tree
#
#  usage: install-edgar [-D] [-F] [-d datadir] *.hdr.sgml
#
##########################################################################
#  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#.*/##;

# allow local libraries
push(@INC, '/usr/local/ims/lib');

# Edgar general utility routines
require 'edgar-util.pl';

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

# process command line options, if any
&Getopts('Dd:F');

# run with debug on?
$debug = defined($opt_D);

# force install even if file exists?
$force = defined($opt_F);

# where to install the files
$datadir = defined($opt_d) ? "$opt_d" : "/ftp/edgar/data";

# main processing loop
foreach $sgml (@ARGV) {
	# make sure this is a SGML header file name
	next unless ($sgml =~ /hdr\.sgml$/);

	print "$sgml:\n" if $debug;

	open(IN, "$sgml") || (warn "$prog: unable to open $sgml: $!\n", next);
	while ($line = <IN>) {

		next unless $line =~ /^<CIK>/;

		chop($line);

		# get CIK, ignoring leading zeroes
		($cik = $line) =~ s/<CIK>0*(.*)/\1/;

		# check for 0000000000-YY-999999 file name
		if (substr($cik, 0, 1) eq '-') {
			substr($cik, 0, 0) = "0" x 10;
		}

		print "  $cik\n" if $debug;

		# make path if it doesn't exist
		&makepath("$datadir/$cik", 0775);

		# get matching *.txt file
		($txt = $sgml) =~ s/hdr\.sgml/txt/;

		# link the files
		$status = link("$sgml", "$datadir/$cik/$sgml");
		if ($status != 1) {
			if ($force) {
				print "$prog: forcing $datadir/$cik/$sgml\n" if $debug;
				$status = unlink "$datadir/$cik/$sgml";
				$status = link("$sgml", "$datadir/$cik/$sgml");
			} else {
				if ($! !~ /File exists/) {
					print "$prog: $datadir/$cik/$sgml: $!\n";
				}
			}
		}
		$status = link("./$txt", "$datadir/$cik/$txt");
		if ($status != 1) {
			if ($force) {
				print "$prog: forcing $datadir/$cik/$txt\n" if $debug;
				$status = unlink "$datadir/$cik/$txt";
				$status = link("$txt", "$datadir/$cik/$txt");
			} else {
				if ($! !~ /File exists/) {
					print "$prog: $datadir/$cik/$txt: $!\n";
				}
			}
		}
	}
}

exit 0;

