use Getopt::Long;

my @consoleFileName;
my @contextInputFileName;
my @contextOutputFileName;
my @outputTextFileName;

sub initFileManager(){
    # Parser for command line arguments.
    GetOptions("console=s" => \@consoleFileName,
       "contextInput=s" => \@contextInputFileName,
       "outputTextFile=s" => \@outputTextFileName,
       "contextOutput=s" => \@contextOutputFileName);
    #
    #print "contextInput = @contextInputFileName" . "\n";
    #print "contextOutput = @contextOutputFileName" . "\n";
    #print "consoleFileName = @consoleFileName" . "\n";
}

initFileManager();

sub getConsoleFileName {
	return @consoleFileName;
}

sub printInOutputTextFile {
    # This function writes text into the output text file. Normally,
    # this is the contents of the input file with changes made to it by
    # the current script.

    eval{
        open FILE, ">>:utf8", "@outputTextFileName" or die $!;
		    for ($count = 0; $count < @_; $count++) {
		        print FILE $_[$count];   
		    }
        close FILE;     
    };
    if( $@ ) { 
       print STDERR "The variable outputTextFileName has not been defined." . "<br/>\n";    
    }    
}

sub printlnInConsole {
    # This function displays messages in the "Console" part of the
    # output field.
	eval{
	    open FILE, ">>:utf8", "@consoleFileName" or die $!;
	    print FILE $_[0] . "<br/>\n"; 
	    close FILE;		
	};
    if( $@ ) { 
       print STDERR "The variable consoleFileName has not been defined." . "<br/>\n";    
    }
}

sub consoleDebug {
    # This function displays messages for debugging purposes in the
    # output field.
	eval{
	    open FILE, ">>:utf8", "@consoleFileName" or die $!;
	    print FILE '<strong><font color="red">Debug: </font></strong>'. $_[0] ."<br/>\n"; 
	    close FILE;		
	};
    if( $@ ) { 
       print STDERR "Debugger: The variable consoleFileName has not been defined." . "<br/>\n";    
    }
}

sub consoleWarn {
    # This function displays messages for debugging purposes in the
    # output field.
	eval{
	    open FILE, ">>:utf8", "@consoleFileName" or die $!;
	    print FILE '<strong><font color="orange">Warning: </font></strong>'. $_[0] ."<br/>\n"; 
	    close FILE;		
	};
    if( $@ ) { 
       print STDERR "Debugger: The variable consoleFileName has not been defined." . "<br/>\n";    
    }
}
sub consoleStatus {
    # This function displays messages for debugging purposes in the
    # output field.
	eval{
	    open FILE, ">>:utf8", "@consoleFileName" or die $!;
	    print FILE '<strong><font color="green">Status: </font></strong>'. $_[0] ."<br/>\n"; 
	    close FILE;		
	};
    if( $@ ) { 
       print STDERR "Debugger: The variable consoleFileName has not been defined." . "<br/>\n";    
    }
}




sub printlnInContextInput {
    # This function is not used right now and maybe superfluous
	eval{
	    open FILE, ">>:utf8", "@contextInputFileName" or die $!;
	    print FILE $_[0] . "\n"; 
	    close FILE;		
	};
	if( $@ ) { 
       print STDERR "The variable contextInputFileName has not been defined." . "<br/>\n";    
    }
}

sub getInputContext(){
    # Scripts that need a context file as input read it with this
    # function.
	my @content;
	eval{
        open FILE, "<:utf8", "@contextInputFileName" or die $!;
        while(<FILE>) {
        	#print $_ . "\n";
        	push(@content, $_); 
        }
        #print "@content";
    };
    if( $@ ) { 
       print STDERR "The variable contextInputFileName has not been defined." . "<br/>\n";    
    }
    return @content;
}

sub printlnInContextOutput {
    # If a context file needs to be written, the contents should be
    # written with this function.
	eval{
        open FILE, ">>:utf8", "@contextOutputFileName" or die $!;
        print FILE $_[0] . "\n"; 
        close FILE;
	};
	if( $@ ) { 
	   print STDERR "The variable contextOutputFileName has not been defined." . "<br/>\n";	
	}
}

sub printlnInERROR {
    # This replaces the pipe to stderr. Messages directed there will
    # show up in the "Error" part of the output field.
    # we might need some transformation for html entities here:
	print STDERR escapeTags($_[0]) . "<br/>\n"; 
	# print STDERR $_[0] . "<br/>\n"; 
}

sub escapeTags{
    # store first argument of function call in scalar text
    my $text = $_[0];
    $text =~ s!\&!&amp;!g;
    $text =~ s!\<!&lt;!g;
    $text =~ s!\>!&gt;!g;
    $text =~ s!\"!&quot;!g;
    $text =~ s!\'!&apos;!g;
    return $text;
}

1;
