#Functions for the Automagic BCCD test suite!
package bccd_test_suite;
use strict;
use Carp;
use Cwd;
use POSIX;
use Readonly;

Readonly my $SHAREDCODE => ".sharedcode";

# Return lines matching a regex
sub line_match {
    my ( $file, $re ) = @_;
    my $lines;

    open( my $FILE, '<', $file )
      or croak "Can't open $file for reading: $!\n";

    while ( my $line = <$FILE> ) {
        chomp $line;
        if ( $line =~ m{$re} ) {
            push( @{$lines}, $line );
        }
    }

    close($FILE);

    return $lines;
}

#XXX
# Uses command-line find. Perl's built-in find subroutine
# is excessively complex for this simple task
sub find_by_name {
    my ( $directory, $name ) = @_;

    open( PATH, "find $directory -name $name|" )
      or croak "could not find script $!";

    my $path = <PATH>;

    close PATH;
    return $path;
}

#Trim function to remove whitespace
sub trim {
    my ($string) = @_;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

#Collect meaningful information from the syncdir command
sub get_syncdir {
    my ($tmpoutput) = @_;
    open( my $FILE, '<', $tmpoutput )
      or die "could not open temporary file: $!";
    my $tmp = "";
    while ( $tmp !~ m/\// ) {
        $tmp = readline $FILE;
    }
    my $startindex = index( $tmp, "/", );
    my $endindex = index( $tmp, " ", $startindex );
    my $syncdir = substr( $tmp, $startindex, $endindex - 1 );
    close $FILE;
    return $syncdir;
}

#Get test list from a file
sub read_list {
    my ($path) = @_;
    my @testlist = ();

    open( FILE, '<', $path ) or die "could not open list file: $!";
    @testlist = grep( !/^$|^#/, <FILE> );    #Remove empty
                                             #and comment lines
    close(FILE);

    map ( {s/#.*$//} @testlist );
    map ( {s/\*//} @testlist );
    map ( trim, @testlist );

    return @testlist;
}

sub list_files_recursive {
    my ($directory) = @_;

    open( LIST, "ls -FR1 $directory|" );    #ls ignores files and directories
                                            #starting with '.' be sure to mimic
                                            #this functionality if you change
                                            #this command to not use the shell.

    #cut out directories and blank space
    my @files = grep ( !/\/$|:$|^$/, <LIST> );
    close(LIST);
    chomp @files;

    @files = map { s/\*$//;  $_ } @files;
    
    return @files;
}

return 1;
