# MODLOAD.PM
#
# Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
#


# Runtime module-loading routine for loading e32tools modules into 'main' module


package Modload;

require Exporter;
@ISA=qw(Exporter);

@EXPORT=qw(
	Load_SetVerbose
	Load_SetModulePath
	Load_ModuleL
	Load_PlatModListL
);


use Pathutl;

my %Mode;
my $ModulePath;

BEGIN {
	$Mode{'Verbose'}=0;
}

sub Load_SetVerbose () {
	$Mode{'Verbose'}=1;
}

sub Load_SetModulePath ($) {
	$ModulePath=$_[0];
}

sub Load_ModuleL (@) {
# Loads a module into the 'main' package, including all the functions the module defines for export

	my @ModBaseList=@_;
	my $ModBase;
	foreach $ModBase (@ModBaseList) {
		$ModBase=uc $ModBase;
		die "ERROR: Can't load \"$ModulePath$ModBase.PM\"\n" unless -e "$ModulePath$ModBase.PM";
		if ($Mode{Verbose}) {
			print "Loading Module: \"",$ModBase,".PM\"\n";
		}
		package main;
		require $ModBase.".PM" or die "ERROR: Can't load function from \"$ModulePath$ModBase.PM\"\n";
		my $Package=ucfirst lc $ModBase;
		$Package->import;
	}
}

sub Load_PlatModListL () {
# returns the platform modules which can be called directly in MAKMAKE or BLDMAKE's command-line.
# this works by checking whether the special variable $COMMAND_LINE_PLATFORM is declared
# in the way specified by the regular expression below for the module

	my $MODULEDIR=&Path_ChopPath($ModulePath);
	opendir MODULEDIR, $MODULEDIR or die "ERROR: Can't open dir \"$MODULEDIR\"\n";
	my @PMFiles= grep /.PM$/io, readdir MODULEDIR; # case-insensitive - someone might rename
	my @CallablePlats=();
	my $FILE;
	FILE: foreach $FILE (@PMFiles) {
		$FILE="$MODULEDIR\\$FILE";
		open FILE,"<$FILE" or die "\nERROR: Can't open file \"$FILE\"\n";
		while (<FILE>) {
			if(/^my \$COMMAND_LINE_PLATFORM;\n$/o) {
				push @CallablePlats, &Path_Split('Base',$FILE);
				close FILE or die "\nERROR: Can't close File \"$FILE\"\n";
				next FILE;
			}
		}
		close FILE or die "\nERROR: Can't close File \"$FILE\"\n";
	}
	@CallablePlats;
}

1;
