blob: 8ec7320244074a9689c256eb7c260df5aec4ebe1 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001#!/usr/bin/perl
2#
3# Copyright Statement:
4# --------------------
5# This software is protected by Copyright and the information contained
6# herein is confidential. The software may not be copied and the information
7# contained herein may not be used or disclosed except with the written
8# permission of MediaTek Inc. (C) 2005
9#
10# BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
11# THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
12# RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
13# AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
14# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
15# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
16# NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
17# SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
18# SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
19# THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
20# NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
21# SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
22#
23# BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
24# LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
25# AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
26# OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
27# MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
28#
29# THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
30# WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
31# LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
32# RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
33# THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
34#
35#
36#*****************************************************************************
37#*****************************************************************************
38##*
39##* Filename:
40##* ---------
41##* copy_files.pl
42##*
43##* Project:
44##* --------
45##* Maui_Software
46##*
47##* Description:
48##* ------------
49##* This script copy the files and directories to another position and
50##*
51##* Author:
52##* -------
53##* kk Lin-Wang (mtk04222)
54##*
55##*============================================================================
56
57use strict;
58use warnings;
59use File::Copy;
60use File::Basename;
61
62($#ARGV < 1) && &Usage;
63
64#opt
65my $opt = $ARGV[0];
66shift(@ARGV);
67
68# the destination
69my $dest = pop(@ARGV);
70
71# source files
72my @src = @ARGV;
73
74if ($opt =~ /^\-f\b/i)
75{
76 @src = glob("@src");
77 copy_file($dest,@src);
78}
79elsif ($opt =~ /^\-r\b/i)
80{
81 copy_dir(@src,$dest);
82}
83else
84{
85 &Usage;
86}
87
88#******************************************************************************
89## FUNCTION
90## copy_files
91## DESCRIPTION
92## copy files to another position
93## PARAMETER
94## @src [IN] an array of source files
95## $des [IN] the destination of copying files
96##
97## EXAMPLE
98## &copy_files($des, @src);
99###******************************************************************************
100sub copy_file
101{
102 my ($des, @src) = @_ ;
103 my $bname = "";
104 my $result = 0;
105 foreach my $src_file (@src)
106 {
107 if (-f $des)
108 {
109 unlink($des) or die "[copy_files.pl][Error:] remove '$des' failed: $!";
110 }
111 elsif (-d $des)
112 {
113 $bname = basename($src_file);
114 if (-e "$des/$bname")
115 {
116 unlink("$des/$bname") or die "[copy_files.pl][Error:] remove '$des/$bname' failed: $!";
117 }
118 }
119 if ($^O eq "MSWin32") {
120 copy($src_file ,$des) or die "[copy_files.pl][Error:] copy '$src_file' to '$des' failed: $!";
121 } else {
122 $result = system("cp -p \'$src_file\' \'$des\'");
123 die "[copy_files.pl][Error:] copy '$src_file' to '$des' failed: $!" if ($result);
124 }
125 }
126}
127
128
129#******************************************************************************
130## FUNCTION
131## copy_dir
132## DESCRIPTION
133## Copy a directory including sub directories
134## PARAMETER
135## $from_dir [IN] source folder
136## $to_dir [IN] destination folder
137## EXAMPLE
138###******************************************************************************
139sub copy_dir
140{
141 my $result =0;
142 my ($from_dir, $to_dir) = @_;
143 opendir my($dir), $from_dir or die "[copy_files.pl][Error:] Could not open dir '$from_dir': $!";
144 for my $file (readdir $dir)
145 {
146 next if ($file =~/\.$/ || $file =~/\b\.\.\b/ );
147 my $src = "$from_dir/$file";
148 my $dest = "$to_dir/$file";
149
150 if(!-d $to_dir)
151 {
152 mkdir($to_dir) or die "[copy_files.pl][Error:] Create folder '$to_dir' failed: $!";
153 }
154
155 if(-d $src)
156 {
157 &copy_dir($src, $dest);
158 }
159 else
160 {
161 if(-e $dest){
162 unlink("$dest") or die "[copy_files.pl][Error:] remove '$dest' failed: $!";
163 }
164 if ($^O eq "MSWin32"){
165 copy($src, $dest) or die "[copy_files.pl][Error:] copy '$src' to '$dest' failed: $!";
166 chmod(0640,$dest) if ($^O eq "MSWin32");
167 } else {
168 $result = system("cp -p \'$src\' \'$dest\'");
169 die "[copy_files.pl][Error:] copy '$src' to '$dest' failed: $!" if ($result);
170 }
171 }
172 }
173 closedir($dir);
174}
175
176
177#******************************************************************************
178## FUNCTION
179## Usage
180## DESCRIPTION
181## Display the manipulation of this script
182##******************************************************************************
183
184sub Usage
185{
186 warn <<"_END_OF_USAGE";
187Usage:
188 copy_files.pl [-f|-r] [source[file1, file2,...]] [destination]
189Example:
190 perl copy_files.pl -f source_file1 source_file2 destination
191 perl copy_files.pl -f source_file destination_file
192 perl copy_files.pl -r source_DIR destination_DIR
193Description:
194 -f Suppresses prompting to confirm you want to overwrite an existing destination file.
195 -r copy directories recursively
196
197_END_OF_USAGE
198 exit 1;
199}
200