#!/usr/bin/perl -w
# $Id: folders,v 1.3 2001/08/01 02:15:03 christopher Exp $
###
# selectFolder - script to select a subset of records based on Folder name.
#
# This script also produces output records in the correct format for 
# using with my TeX file.
#
# Distributed under the GNU Lesser General Public License v2.1.  See
# the accompanying lgpl.txt file for the license text; if the file was
# missing you may always obtain a copy from http://www.fsf.org/.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# Copyright (c)2001 Christopher Rath <Christopher@Rath.ca>.
###

use CSV;		# Comma Separated Value module.

###
# Constants for reading Newton Names records.
#
my($FOLDER) = "Folder";


###
# Get filename to process.
#	* for now we'll cop-out and hard-code a name.
#
($#ARGV != 1) || die "$0 : USAGE: $0 <NewtonDataFile>\n"; 

my($input)      = $ARGV[0]; 

### 
# Open the input file for processing. 
# 
if (!open(INPUT, "$input")) { 
    die "$0: ERROR, cannot open input file, $input\n" 
      ."\tno data generated.\n"; 
} else { 
    my($firstLine) = scalar(<INPUT>); 
    my(%CSVfields) = CSVinit($firstLine); 

    # 
    # Validate field list to ensure data file contains all the fields we need. 
    # 

    if (! CSVvalidate(\%CSVfields, $FOLDER)) {

	die "Some fields are missing from the input file."; 
    } else {
	my($first) = 1;			# Only true for first record output.
	my(@record);                    # Where we'll store parsed records.
	my($outrec,			# Output record (a string).
	   $wk_ad1,			# Address working string.
	   $wk_ad2);			# Address working string.

	#### 
	# Process input file, summarizing results. 
	# 
	while (<INPUT>) { 
	    @record = CSVsplit($_);

	    print "$record[$CSVfields{$FOLDER}]\n";
	}
    }
}
