1package filehelper;
2
3#**************************************************************
4#
5#  Licensed to the Apache Software Foundation (ASF) under one
6#  or more contributor license agreements.  See the NOTICE file
7#  distributed with this work for additional information
8#  regarding copyright ownership.  The ASF licenses this file
9#  to you under the Apache License, Version 2.0 (the
10#  "License"); you may not use this file except in compliance
11#  with the License.  You may obtain a copy of the License at
12#
13#    http://www.apache.org/licenses/LICENSE-2.0
14#
15#  Unless required by applicable law or agreed to in writing,
16#  software distributed under the License is distributed on an
17#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18#  KIND, either express or implied.  See the License for the
19#  specific language governing permissions and limitations
20#  under the License.
21#
22#**************************************************************
23
24
25
26use strict;
27use warnings;
28use strict;
29use English;                  # $OSNAME, ...
30use stringhelper;
31
32BEGIN {
33    use Exporter   ();
34    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
35
36    $VERSION     = 1.00;
37    # if using RCS/CVS, this may be preferred
38    $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
39    @ISA         = qw(Exporter);
40    @EXPORT      = qw(&dospath &unixpath &appendPath &appendClass &setPrefix &getToolsPrefix &rmkdir &getJavaPathSeparator &getJavaFileDirSeparator &getFromPathes &convertCygwinPath);
41    %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
42    # your exported package globals go here,
43    # as well as any optionally exported functions
44    @EXPORT_OK   = ( ); # qw($Var1 %Hashit &func3);
45}
46
47
48# ------------------------------------------------------------------------------
49# helper, to change all file separators
50sub dospath($)
51{
52    my $sPath = shift;
53    if ($OSNAME eq "MSWin32")
54    {
55        # make out of '/' a '\'
56        $sPath =~ s/\//\\/g;
57    }
58    else
59    {
60    }
61    return $sPath;
62}
63
64sub unixpath($)
65{
66    my $sPath = shift;
67    if ($OSNAME ne "MSWin32")
68    {
69        # make out of '\' a '/'
70        $sPath =~ s/\\/\//g;
71    }
72    else
73    {
74    }
75    return $sPath;
76}
77
78# ------------------------------------------------------------------------------
79# sub getGlobalInstSet()
80# {
81#     my $sJumbo;
82#     if ($OSNAME eq "MSWin32")
83#     {
84#         # $sJumbo = "\\\\so-gfxcmp-lin\\jumbo_ship\\install";
85#         $sJumbo = "\\\\jumbo.germany.sun.com\\ship\\install";
86#     }
87#     elsif ($OSNAME eq "cygwin")
88#     {
89#         $sJumbo = "//jumbo.germany.sun.com/ship/install";
90#     }
91#     else
92#     {
93#         $sJumbo = "/net/jumbo.germany.sun.com/ship/install";
94#     }
95#     return $sJumbo;
96# }
97
98# ------------------------------------------------------------------------------
99# sub getSolarisLockFile()
100# {
101#     my $sSolarisLockFile = "/tmp/.ai.pkg.zone.lock-afdb66cf-1dd1-11b2-a049-000d560ddc3e";
102#     return $sSolarisLockFile;
103# }
104#
105# sub checkForSolarisLock()
106# {
107#     if ($OSNAME eq "solaris")
108#     {
109#         # wait until the internal installer lock is gone
110#         while ( -e getSolarisLockFile() )
111#         {
112#             while ( -e getSolarisLockFile() )
113#             {
114#                 log_print( "Warning: Wait active until installer lock is gone. \n");
115#                 sleep 1;
116#             }
117#             sleep 5;
118#         }
119#         log_print( "[ok], lock is gone.\n");
120#     }
121# }
122#
123# sub deleteSolarisLock()
124# {
125#     if ($OSNAME eq "solaris")
126#     {
127#         sleep 1;
128#         unlink getSolarisLockFile();
129#
130#         sleep 1;
131#         if ( -e getSolarisLockFile() )
132#         {
133#             # try delete the file as super user?
134#             `sudo rm -f getSolarisLockFile()`;
135#             sleep 1;
136#         }
137#     }
138# }
139
140# ------------------------------------------------------------------------------
141sub appendPath($$)
142{
143    my $sPath = shift;
144    my $sAddPath = shift;
145    if ($sPath && $sAddPath)
146    {
147        if (! endswith($sPath, "/") &&
148            ! endswith($sPath, "\\"))
149        {
150            # getJavaFileDirSeparator();
151            $sPath .= "/";
152        }
153        $sPath .= $sAddPath;
154    }
155    return $sPath;
156}
157
158sub appendClass($$)
159{
160    my $sPath = shift;
161    my $sAddPath = shift;
162
163    my $sSeparator = getJavaPathSeparator();
164    if ($sPath && $sAddPath)
165    {
166        if (! endswith($sPath, $sSeparator))
167        {
168            # getJavaFileDirSeparator();
169            $sPath .= $sSeparator;
170        }
171        $sPath .= $sAddPath;
172    }
173    return $sPath;
174}
175
176# ------------------------------------------------------------------------------
177
178our $sPrefix;
179sub setPrefix($)
180{
181    $sPrefix = shift;
182}
183
184sub getPrefix()
185{
186    return $sPrefix;
187}
188
189# ------------------------------------------------------------------------------
190our $programprefix;
191
192# sub getProgramPrefix($)
193# {
194#     my $sDBDistinct = shift;
195#
196#     my $sProgramPrefix;
197#     if (! $programprefix)
198#     {
199#         if ($OSNAME eq "MSWin32")
200#         {
201#             # $programprefix = "C:/gfxcmp/programs";
202#             $programprefix = "C:";
203#             if (getPrefix() eq "performance")
204#             {
205#                 $programprefix = "D:";
206#             }
207#             $programprefix = appendPath($programprefix, "gp");
208#         }
209#         elsif ($OSNAME eq "linux")
210#         {
211#             $programprefix = "/space/" . getPrefix() . "/programs";
212#         }
213#         elsif ($OSNAME eq "solaris")
214#         {
215#             $programprefix = "/space/" . getPrefix() . "/programs";
216#         }
217#         else
218#         {
219#             print "Error: This environment isn't supported yet.\n";
220#             exit(1);
221#         }
222#     }
223#     $sProgramPrefix = appendPath($programprefix, substr($sDBDistinct, 0, 19));
224#     return $sProgramPrefix;
225# }
226# ------------------------------------------------------------------------------
227our $toolsprefix;
228
229sub getToolsPrefix()
230{
231    my $sToolsPrefix;
232    if (! $toolsprefix)
233    {
234        if ($OSNAME eq "MSWin32")
235        {
236            $toolsprefix = "C:";
237            if (getPrefix() eq "performance")
238            {
239                $toolsprefix = "D:";
240            }
241        }
242        elsif ($OSNAME eq "linux")
243        {
244            $toolsprefix = "/space";
245        }
246        elsif ($OSNAME eq "solaris")
247        {
248            $toolsprefix = "/space";
249        }
250        else
251        {
252            print "Error: This environment isn't supported yet.\n";
253            exit(1);
254        }
255        $toolsprefix = appendPath($toolsprefix, getPrefix());
256        $toolsprefix = appendPath($toolsprefix, "tools");
257    }
258    $sToolsPrefix = $toolsprefix;
259    return $sToolsPrefix;
260}
261
262# also Windows safe
263sub rmkdir($)
264{
265    my($tpath) = shift;
266    my $dir;
267    my $accum = "";
268
269    my @dirs = split(/\//, $tpath);
270    if ( $#dirs eq 0 )
271    {
272        @dirs = split("\\\\", $tpath);
273    }
274
275    foreach $dir (@dirs)
276    {
277	$accum = "$accum$dir/";
278	if($dir ne "")
279	{
280	    if(! -d "$accum")
281	    {
282		mkdir ($accum);
283                chmod (0777,$accum);
284	    }
285	}
286    }
287}
288
289# ------------------------------------------------------------------------------
290sub getJavaPathSeparator()
291{
292    my $ps = ":";
293    if ($OSNAME eq "MSWin32" || $OSNAME eq "cygwin")
294    {
295        $ps = ";";
296    }
297    return $ps;
298}
299# ------------------------------------------------------------------------------
300sub getJavaFileDirSeparator()
301{
302    my $sfs = "/";
303    if ($OSNAME eq "MSWin32")
304    {
305        $sfs = "\\";
306    }
307    return $sfs;
308}
309# ------------------------------------------------------------------------------
310sub getFromPathes($$)
311{
312    my $sPathesIni = shift;
313    my $searchvalue = shift;
314    my $sResult;
315    if ( -e $sPathesIni)
316    {
317        local *PATHES;
318        if (open(PATHES, "$sPathesIni"))
319        {
320            my $line;
321            while ($line = <PATHES>)
322            {
323                chomp($line);
324                if ($line =~ /^$searchvalue=(.*)$/)
325                {
326                    $sResult = $1;
327                }
328            }
329            close(PATHES);
330        }
331    }
332    return $sResult;
333}
334
335sub convertCygwinPath($)
336{
337    my $sPath = shift;
338
339    if ($OSNAME eq "cygwin")
340    {
341        # print "Cygwin Path Patch.\n" if ($verbose);
342        if ($sPath =~ /\/cygdrive\/(.)/)
343        {
344            my $Letter = $1;
345            $sPath =~ s/\/cygdrive\/${Letter}/${Letter}\:/;
346            # print "Cygwin Path Patch: '$sPath'\n" if ($verbose);
347        }
348    }
349    return $sPath;
350}
351
352
353
3541;
355