#!/opt/local/bin/perl

#..................End include subroutines ................

&check_arguments($#ARGV);              #verify command line

&process_arguments(@ARGV);             #input file = $opt_i
                                       #output file = $opt_o

&open_file("INFILE", "", "$opt_i");  #update mode

my(%MAIN_ADD_DECK)= ();


#:::::::::::::::::::  SUBROUTINES :::::::::::::::::::::::::::

#  ~~~~~~~~~~~ READING OF INPUT FILE BEGINS HERE ~~~~~~~~~~
while ($line = &read_from_file("INFILE")) {
	next if ( (!($line =~ /f.\d{9}\../i )) || $line =~ /^\s*--/ ); # weedout comment & non data lines
	chomp $line;                              #remove end of line marker
	if ( $line =~ /--/ ){
		split /--/, $line, 2;
		$line = join '', $_[0]; 
	}
	
	if ( $line =~ /\&\&/ ){
		split /\&\&/, $line, 2;
		$line = join '', $_[0]; 
	}

	$line =~ s/\s+//g;                        #finally squeeze out spaces
	$line =~ s/new|set//i; 

	#... tag family head to members ONLY if you encounter =
	($family_head, $line)= split /=/, $line, 2 if ( $line =~ /=/); #tag family head to members 
	push @{ $MAIN_ADD_DECK{$family_head} }, "$line";
}
close INFILE;

&open_file("OUTFILE", "", ">$opt_o");  #update mode

foreach $family (sort (keys %MAIN_ADD_DECK )){
	print OUTFILE "$family = {",  @{ $MAIN_ADD_DECK{$family} }, "}\n";
}
close OUTFILE;

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


#----------------------------------------------------------
#PROGRAM:   famenia4_pkg1.pl
#PURPOSE:   subbroutine adddeck on network - famenia4 part
#DATE:      05-06-97
#AUTHOR:    william ampeh
#----------------------------------------------------------

# *********************************************************
#----------------------------------------------------------
#     MODULES                     Purpose
#     ~~~~~~~                     ~~~~~~~
# 1. check_arguments     verifies No. of args on command line
# 2. error_message       displays appropriate error messages
# 3. process_arguments   distinguish b/n input/output files
# 4. open_file           open file for READ-ONLY
#----------------------------------------------------------
#
# ............... SUBROUTINES START FROM HERE .............


#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ...............check command line arguments ..............

sub check_arguments{
	local ($num_args) = @_;
	if ( $num_args < 3 ){                 #i.e., 0 1 2 3
		&error_messages;
	}
}

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ..........displays appropriate error messages ...........

sub error_messages{
	print("\n\n\n\n                                E   R   R   O   R\n");
	print("\n\n       USAGE: $0 -i {excel filename} -o {output filename }");
	print("\n\n          EG: $0 -i table1a.xls -o table1a.txt\n\n");
	print("\n\n              -  E X E C U T I O N    S U P P R E S S E D  ! ! ! - \n\n");
	print ("\a\n\n\n");
	exit (1);                   #++ failure
}

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# .......... distinguish b/n input/output files ...........

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sub process_arguments              #determine -d -f -q
{

	#require 'getopts.pl' ;      # do is better than require
	do 'getopts.pl' || die "\n\n\n\a\tCOULD NOT LOCATE getops.pl\n\n\n" ;

	&Getopts('i:o:');                     #similar to getopts 
                                         #in sh, but better

	if ( !defined($opt_i) || !defined($opt_o) ){
		&error_messages;
	} 
}

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ................ open file as sepcified .................

sub open_file
{
   local ($filevar, $filemode, $filename) = @_;
   open ($filevar, $filemode . $filename) ||
      die("\a Can't open $filename");
}


#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#................. read fron input file ...................

sub read_from_file
{
   local ($filevar) = @_;
   <$filevar>;
}

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ................ print to output file....................

sub print_to_file
{
   local ($filevar, $line) = @_;
   print $filevar ($line);
}

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


# @@@@@@@@@@@@@@@@@@@@@@@@@@ End of Subroutine marker @@@@@@@@@@@@@@@@@@@@@
1;

