#!/usr/bin/perl

#quit unless we have the correct number of command-line args
my $num_args = $#ARGV + 1;
if ($num_args != 2) {
    print "\nUsage: config_parser <config file path> <result file path>\n\n";
    exit;
}

# declare variables
my $invalid_char_count = 0;
my $config_file = $ARGV[0];
my $result_file = $ARGV[1];

# check if the results file exists and delete it
if (-f $result_file) {
    unlink $result_file
        or die "Cannot delete $result_file: $!";
}

open CONF, "<", $config_file or die "Couldn't open file $config_file, $!";
open RES, "+>", $result_file or die "Couldn't open file $result_file, $!";

print RES "Invalid characters '<' or '>' found in the following lines of config file.\nCorrect them and download the config file again.\n";
while (my $line = <CONF>) {
	if (($line !~ /^#/) && ($line =~ /\<|\>/)) { 
			++$invalid_char_count;
			#print RES "$line"
			#	or die "Cannot write to $result_file";
	}
}	
close(CONF);
close(RES);

if($invalid_char_count =~ 0) {
    unlink $result_file
        or die "Cannot delete $result_file: $!";
}
