#!/usr/local/bin/perl

###
###  Copyright 2000-2007 University of Illinois Board of Trustees
###  All rights reserved. 
###
###  psgconf-import - import script for psgconf directives.
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###

use warnings;
no warnings 'once';
use Getopt::Std;

use PSGConf;
require 'ctime.pl';

&getopts ('ho:v');

if ( $opt_h )  {
	warn "$0: [-h] [-v] [-o <filename>] directive [<filename>]\n";
	warn "\t-h\tThis help screen\n";
	warn "\t-o <filename>\tOutput file, defaults to STDOUT\n";
	warn "\t-v\tVerbose output\n";
	warn "\tdirective\tWhat directive to write out (REQUIRED)\n";
	warn "\t<filename>\tFile to read, defaults to STDIN\n";
	exit (0);
}

if ( $opt_o ) {
	open (FPOUT, ">$opt_o") || die "Could not open output file ($opt_o)\n";
	$fhout = \*FPOUT;
} else {
	$fhout = \*STDOUT;
}

if ( scalar @ARGV ) {
	$psgconf = PSGConf->new()
     	|| die "Can not instantiate PSGConf module\n";

	$directive = shift @ARGV;

	eval "use PSGConf::Import::$directive";

	### 
	### Look up what Data:: type we need to write out.  
	### WARNING: This is all in Config::Objective syntax
	### 
	my ($t) = $psgconf->get_data_type($directive);
	if ( $t =~ m/PSGConf::Data::/o ) {
		$t =~ s/PSGConf::Data::/PSGConf::Import::Data::/o;
		eval "use $t";
		@DataType = $t->set();
		$field_cnt = ( $t->can('count'))? $t->count(): undef;
	} else {
		die "Could not find directive ($directive)\n";
	}
} else {
	die "Need to specify a directive\n";
}

if ( scalar @ARGV ) {
	open (FPIN, "<$ARGV[0]") || die "Could not open input file ($ARGV[0])\n";
	$fhin = \*FPIN;
} else {
	$fhin = \*STDIN;
}

$self = &init($psgconf);

if ( $opt_v ) {
	printf $fhout "###\n";
	printf $fhout "### %s created by $0 %s\n",
		(defined $opt_o)? $opt_o: 'stdout', $directive;
	printf $fhout "### on %s", ctime(time());
	printf $fhout "###\n\n";
}

###
### If there are 2 or more elements in the type array, the use the first
### one as an object initaitor.
###
printf $fhout "$directive %s\n", ($#DataType > 0)? $DataType[0]: '';

while (<$fhin>) {
	chomp;

	printf $fhout ",\n"
		if ( ! defined $field_cnt || $field_cnt == scalar @field );

	@field = &parse($self, $_);

	###
	### Skip this line if we do not have the correct number
	### of fields (as defined in $field_cnt).
	###
	next
		if ( defined $field_cnt && $field_cnt != scalar @field );

	###
	### If there are 4 elements in the DataType array, the use the 
	### second and third one to initiate and terminate the line.
	###
	if ( scalar @field && scalar @DataType == 4 ) {
		printf $fhout "\t%s %s %s",
		$DataType[1], join (', ', @field), $DataType[2];

	###
	### If there are 3 elements in the DataType array, the use the 
	### second one as an object terminator.
	###
	} elsif ( scalar @field && scalar @DataType == 3 ) {
		printf $fhout "\t%-35s %s \"%s\"",
			'"' . $field[0] . '"', $DataType[1], $field[1];

	} elsif ( scalar @field && length $field[0] ) {
		printf $fhout "\t\"%s\"", $field[0];
	}
};

###
### If there is an element in the DataType array, the use the last
### one as an object terminator.
###
printf $fhout "\n%s;\n", ($#DataType >= 0)? $DataType[$#DataType]: '';

close $fhin;
close $fhout;

exit (0);


syntax highlighted by Code2HTML, v. 0.9.1