xref: /aoo4110/main/solenv/bin/mkdir.pl (revision b1cdbd2c)
1: # -*- perl -*-
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#**************************************************************
5#
6#  Licensed to the Apache Software Foundation (ASF) under one
7#  or more contributor license agreements.  See the NOTICE file
8#  distributed with this work for additional information
9#  regarding copyright ownership.  The ASF licenses this file
10#  to you under the Apache License, Version 2.0 (the
11#  "License"); you may not use this file except in compliance
12#  with the License.  You may obtain a copy of the License at
13#
14#    http://www.apache.org/licenses/LICENSE-2.0
15#
16#  Unless required by applicable law or agreed to in writing,
17#  software distributed under the License is distributed on an
18#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19#  KIND, either express or implied.  See the License for the
20#  specific language governing permissions and limitations
21#  under the License.
22#
23#**************************************************************
24#
25# mkdir - a perl script to substitute mkdir -p
26# accepts "/", ":", and "\" as delimiters of subdirectories
27# options -p (for compatibility)
28#         -mode mode
29#
30
31use Cwd;
32
33$currdir = cwd;
34
35$MODE = 00777 ;
36
37while ( $#ARGV >= 0 ) {
38	if ( $ARGV[0] eq "-mode" ) {
39		$MODE = oct $ARGV[1] ;
40		shift @ARGV ;
41		shift @ARGV ;
42		}
43	elsif ( $ARGV[0] eq "-p" ) {
44		shift @ARGV ;
45        # -p does not do anything, it's supported just for compatibility
46		}
47	else {
48
49        $ARGV[0] =~ s?\\|:?/?g ;
50        @SUBDIRS = split "/", $ARGV[0] ;
51
52        # absolute path UNIX
53        if ( $SUBDIRS[0] eq "" ) {
54            chdir '/' ;
55            shift @SUBDIRS ;
56        }
57        # absolute path WINDOWS
58        if ( $#SUBDIRS > 1 ) {
59            if ( $SUBDIRS[1] eq "" ) {
60                if ( $SUBDIRS[0] =~ /\w/ ) {
61                    chdir "$SUBDIRS[0]:\\" ;
62                    shift @SUBDIRS ;
63                    shift @SUBDIRS ;
64                } ;
65            } ;
66        }
67
68        while (@SUBDIRS) {
69            if ( -e $SUBDIRS[0] ) {
70                if ( ! -d $SUBDIRS[0] ) {
71                    die "file exists\n"
72                }
73            }
74            else {
75                mkdir $SUBDIRS[0], $MODE or die "Can't create directory $SUBDIRS[0]"
76            }
77            chdir $SUBDIRS[0] or die "Can't cd to $SUBDIRS[0]" ;
78            shift @SUBDIRS ;
79        } ;
80
81        shift @ARGV ;
82    } ;
83	chdir $currdir;
84}
85