#========================================================================= 
#          Copyright © 2001-2004 QUALCOMM Incorporated.
#                     All Rights Reserved.
#========================================================================= 
#! /usr/local/bin/perl -w
use strict;

use lib "$ENV{BREWTOOLSDIR}\\BREW Perl Interface";
use BREWPerlIF;


###################################################################
# Sample perl application to demonstrate module-related operatoins:
#
#        1) Load 3 modules to device, 
#        2) delete 1 module.
################################################################## 

###################################################################
# Variable Declaration Section
###################################################################

# serial port name to which the device is connected
my $comPort = "COM1";

my $dllName = "$ENV{BTILDIR}\\OEMLayer\\QCOMOEM.dll";

# place holder for module directory name on PC
my $moduleDir;

# place holder for mofule name
my $moduleName;

# place hloder for storing result
my $result;


#####################################################################
# Here starts the actual processing
#####################################################################

# connect to the device
printf "Connecting to device\n" ;


if( BREWPerlIF::ConnecttoDevice($comPort, $dllName)) {
	print "Connection to Device successful\n";
} else {
	die "Could not connect to the device on specified port $comPort\n";
}

printf "\nLoading modules\n";

$moduleDir = 'C:\tmp\BrickAttack';
$moduleName = "BrickAttack";
printf "\nLoading module $moduleName from PC directory $moduleDir\n";
$result = BREWPerlIF::CopyModuleToDevice($moduleDir, $moduleName);
if ($result == 0) {
	printf "ERROR - in loading module $moduleName\n";
} else {
	printf "Successfully loaded $moduleName module\n";
}

	
$moduleDir = 'C:\tmp\Space';
$moduleName = "Space";
printf "\nLoading module $moduleName from PC directory $moduleDir\n";
$result = BREWPerlIF::CopyModuleToDevice($moduleDir, $moduleName);
if ($result == 0) {
	printf "ERROR - in loading module $moduleName\n";
} else {
	printf "Successfully loaded $moduleName module\n";
}


$moduleDir = 'C:\tmp\CalcApp';
$moduleName = "CalcApp";
printf "\nLoading module $moduleName from PC directory $moduleDir\n";
$result = BREWPerlIF::CopyModuleToDevice($moduleDir, $moduleName);
if ($result == 0) {
	printf "ERROR - in loading module $moduleName\n";
} else {
	printf "Successfully loaded $moduleName module\n";
}


$moduleName = "brickattack";
if (BREWPerlIF::DeviceModuleExists($moduleName)) {

   printf "\nDeleting module $moduleName from the device\n";
   $result = BREWPerlIF::DeleteModuleFromDevice($moduleName);
   if($result == 0) {
   	printf "ERROR -Could not delete module $moduleName\n";
   } else {
   	printf "Successfully deleted module: $moduleName\n";
   }
} else {
   printf "\nERROR - Module $moduleName not on Device... \n";
}

printf "\ndisconnecting from device\n" ;
if(BREWPerlIF::ReleaseDevice()) {
	print "disconnect from the device successful\n";
} else {
	die "Could not disconnect from the device\n";
}