2003-06-14 Karl Berry <karl@gnu.org>
[platform/upstream/automake.git] / lib / mkinstalldirs
1 #! /bin/sh
2 # mkinstalldirs --- make directory hierarchy
3 # Original author: Noah Friedman <friedman@prep.ai.mit.edu>
4 # Created: 1993-05-16
5 # Public domain.
6
7 scriptversion=2003-06-14.23
8
9 errstatus=0
10 dirmode=""
11
12 usage="\
13 Usage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ...
14
15 Create each directory DIR (with mode MODE, if specified), including all
16 leading file name components.
17 "
18
19 # process command line arguments
20 while test $# -gt 0 ; do
21   case $1 in
22     -h | --help | --h*)         # -h for help
23       echo "$usage"
24       exit 0
25       ;;
26     -m)                         # -m PERM arg
27       shift
28       test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
29       dirmode=$1
30       shift
31       ;;
32     --version)
33       echo "$0 $scriptversion"
34       exit 0
35       ;;
36     --)                         # stop option processing
37       shift
38       break
39       ;;
40     -*)                         # unknown option
41       echo "$usage" 1>&2
42       exit 1
43       ;;
44     *)                          # first non-opt arg
45       break
46       ;;
47   esac
48 done
49
50 for file
51 do
52   if test -d "$file"; then
53     shift
54   else
55     break
56   fi
57 done
58
59 case $# in
60   0) exit 0 ;;
61 esac
62
63 case $dirmode in
64   '')
65     if mkdir -p -- . 2>/dev/null; then
66       echo "mkdir -p -- $*"
67       exec mkdir -p -- "$@"
68     fi
69     ;;
70   *)
71     if mkdir -m "$dirmode" -p -- . 2>/dev/null; then
72       echo "mkdir -m $dirmode -p -- $*"
73       exec mkdir -m "$dirmode" -p -- "$@"
74     fi
75     ;;
76 esac
77
78 for file
79 do
80   set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'`
81   shift
82
83   pathcomp=
84   for d
85   do
86     pathcomp="$pathcomp$d"
87     case $pathcomp in
88       -*) pathcomp=./$pathcomp ;;
89     esac
90
91     if test ! -d "$pathcomp"; then
92       echo "mkdir $pathcomp"
93
94       mkdir "$pathcomp" || lasterr=$?
95
96       if test ! -d "$pathcomp"; then
97         errstatus=$lasterr
98       else
99         if test ! -z "$dirmode"; then
100           echo "chmod $dirmode $pathcomp"
101           lasterr=""
102           chmod "$dirmode" "$pathcomp" || lasterr=$?
103
104           if test ! -z "$lasterr"; then
105             errstatus=$lasterr
106           fi
107         fi
108       fi
109     fi
110
111     pathcomp="$pathcomp/"
112   done
113 done
114
115 exit $errstatus
116
117 # Local Variables:
118 # mode: shell-script
119 # sh-indentation: 2
120 # eval: (add-hook 'write-file-hooks 'time-stamp)
121 # time-stamp-start: "scriptversion="
122 # time-stamp-format: "%:y-%02m-%02d.%02H"
123 # time-stamp-end: "$"
124 # End: