#!/usr/bin/env perl

# Copyright (C) 2022 The University of Notre Dame
# This software is distributed under the GNU General Public License.
# See the file COPYING for details.

# perl_driver takes the name of a perl file and locates its dependencies via static analysis.
# This is used directly by makeflow_linker and not meant to be invoked directly

print "*Perl $]\n";

my $use_named = false;

my $target = @ARGV[0];
if(@ARGV[0] == "--use-named") {
	$target = @ARGV[1];
	$use_named = true;
}

my @modules = ();

open my $file_handle, $target or die "Could not open $target: $!";
LINES: {
	my $pod = 0;
	while (my $line = <$file_handle>) {
		if($pod){
			if($line =~/=cut/){
				$pod = 0;
			}
		}
		else {
			if ($line =~ /__END__/){
				last LINES;
			}
			elsif ($line =~ /^=/){
				$pod = 1;
			}
			elsif ($line =~ /use (\D\S*);/){
				push(@modules, $1);
			}
		}
	}
}
close $file_handle;

while (my $current_module = shift(@modules)){
	ITER: {
		foreach $prefix (@INC) {
			my $realfilename = "$prefix/$current_module.pm";
			if (-f $realfilename) {
				print "$realfilename $current_module\n";
				last ITER;
			}
		}
	}
}
