#!/bin/sh
#
#  file:  do-edgar
#  auth:  Brad Burdick
#  desc:  Attempt to automate the SEC EDGAR extraction as much as possible.
#
#  usage: do-edgar YYMMDD
#
##########################################################################
#  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.
##########################################################################

# make sure files remain group writeable
umask 02

bindir='/usr/local/ims/bin'
edgardir='/in/edgar'
ftpdir='/ftp/edgar'

corrfile="../corr.${1}"

process_edgar() {

	if [ ! -f ${1} ] ; then
		echo "$0: ${1}: file not found"
		exit 1
	fi

	# go to edgar area
	cd ${edgardir}

	${bindir}/fix-edgar ${1}
	${bindir}/extract-edgar ${1}

	cd ${edgardir}/work
	${bindir}/split-edgar *.nc

	# MUST be done before corrections
	${bindir}/process-edgar -a *.sgml

	cd ${edgardir}/corrections
	${bindir}/correct-edgar -a *.corr* > ${corrfile} 2>&1

	if [ `wc -l ${corrfile} | awk '{print $1}'` != "1" ] ; then
		echo "Check the corrections file: ${corrfile} ..."
		exit 1
	fi
}


main() {

	if [ "$1" != "-install" ] ; then
		process_edgar ${1+"$@"}
	else
		shift;
	fi

	datestamp="$1"

	cd ${edgardir}/work
	${bindir}/create-feed -n "$datestamp.nc.tar" &
	if [ $? -ne 0 ] ; then
		echo "install-edgar aborted"
		exit 1
	fi


	/bin/mv *.sgml *.txt /ftp/edgar/data/private/

	# install new submissions
	cd ${ftpdir}/data/private
	${bindir}/install-edgar *.sgml
	if [ $? -ne 0 ] ; then
		echo "install-edgar failed"
		exit 1
	fi

	/bin/rm -f *.sgml *.txt
	
	# create WAIS index of sgml header files
	${bindir}/index-edgar > /tmp/edgar.out 2>&1 &

	# create and process master index files
	cd ${ftpdir}/full-index
	${bindir}/create-index -m ; process-index -cfm master.idx

	# create and process daily index files
	cd ${ftpdir}/daily-index
	${bindir}/create-index -D $datestamp ; \
	  ${bindir}/process-index -D $datestamp -cf master.$datestamp.idx
}

if [ $# -lt 1 ] ; then
	echo "usage: $0 [-install] infile"
	exit 1
fi

main ${1+"$@"}

exit 0

