#========================================================================= 
#          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 directory-related operatoins: 
#
#        1) It copies some directories from PC to device,
#           (in "<BREW MOD directory>/helloworld" directory),
#        2) Deletes above directoriess from device.
#
# Note: Following internal conversion will be done to the directory path 
#       specified in the client PERL sctipt:
#
# -----------------------------------------------------------------------
# User specified path		==>	Converted full device path
# -----------------------------------------------------------------------
# /xxx				==> <BREW_MOD_DIR>/xxx
# /mod/xxx			==> <BREW_MOD_DIR>/xxx
# /shared/xxx			==> <BREW_SHARED_DIR>/xxx
# /mif/xxx			==> NOT ALLOWED
#
# where xxx could be any directory path, e.g. helloworld, helloworld/tmp, etc.
#
###########################################################################

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

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

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

# Array containing file names to be copied to the device
my @dirToDevice = ("C:\\tmp\\dir1", 
                    "C:\\tmp\\dir2");

# destination directory on the device
my $deviceDes = "/helloworld";

# place holder for storing basename of device file
my $baseName;

# place holder for storing file name while iterating thru filename array
my $dir;


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

# connect to the device
printf "Connecting to device\n" ;
if( BREWPerlIF::ConnecttoDevice($port,$dllName )) {
	print "Connection to the device successful\n";
} else {
	die "ERROR - connect to the device on specified port $port\n";
}

# Prepare the device environment
# delete the directory  if exist
if (BREWPerlIF::DeviceDirExists($deviceDes)) {
   printf "\n Deleting device dir: $deviceDes \n";

   if (BREWPerlIF::DeleteDirFromDevice($deviceDes)) {
      printf "\n Device Dir deleted:  $deviceDes \n";
   } else {
      printf "\n ERROR - Device dir NOT deleted: $deviceDes \n";
   }
}

# Copy two sub-directories to device
foreach $dir (@dirToDevice) {
   $baseName = (reverse(split(/\\/,$dir)))[0];

   if (BREWPerlIF::CopyDirToDevice($dir, "$deviceDes/Tmp/$baseName")) {
       printf "Successfully copied $deviceDes/Tmp/$baseName to device\n\n";
   } 
   else {
       printf "ERROR -  in copying $deviceDes/Tmp/$baseName to device\n\n";
   }
}


# Clean up the device environment
# delete the directory  if exist
if (BREWPerlIF::DeviceDirExists($deviceDes)) {
   printf "\n Deleting device dir: $deviceDes \n";

   if (BREWPerlIF::DeleteDirFromDevice($deviceDes)) {
      printf "\n Device Dir deleted:  $deviceDes \n";
   } else {
      printf "\n ERROR - Device dir NOT deleted: $deviceDes \n";
   }
}


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