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