cd8dff3966386ca2ae643279a55fdf54f32f2a58
[gnulib.git] / config / mkinstalldirs
1 #! /bin/sh
2 # mkinstalldirs --- make directory hierarchy
3 # Author: Noah Friedman <friedman@prep.ai.mit.edu>
4 # Created: 1993-05-16
5 # Public domain
6
7 errstatus=0
8 dirmode=""
9
10 usage="\
11 Usage: mkinstalldirs [-h] [--help] [-m MODE] DIR ..."
12
13 # process command line arguments
14 while test $# -gt 0 ; do
15   case "${1}" in
16     -h | --help | --h*)                 # -h for help
17       echo "${usage}" 1>&2; exit 0 ;;
18     -m)                                 # -m PERM arg
19       shift
20       test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
21       dirmode="${1}"
22       shift ;;
23     --) shift; break ;;                 # stop option processing
24     -*) echo "${usage}" 1>&2; exit 1 ;; # unknown option
25      *) break ;;                        # first non-opt arg
26   esac
27 done
28
29 # no `for var; do', it breaks on Solaris 2.7 (at least).
30 for file
31 do
32   if test -d "$file"; then
33     shift
34   else
35     break
36   fi
37 done
38
39 case $# in
40   0) exit 0 ;;
41 esac
42
43 case $dirmode in
44   '')
45     if mkdir -p -- . 2>/dev/null; then
46       echo "mkdir -p -- $*"
47       exec mkdir -p -- "$@"
48     fi ;;
49   *)
50     if mkdir -m "$dirmode" -p -- . 2>/dev/null; then
51       echo "mkdir -m $dirmode -p -- $*"
52       exec mkdir -m "$dirmode" -p -- "$@"
53     fi ;;
54 esac
55
56 for file
57 do
58   set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'`
59   shift
60
61   pathcomp=
62   for d
63   do
64     pathcomp="$pathcomp$d"
65     case "$pathcomp" in
66       -*) pathcomp=./$pathcomp ;;
67     esac
68
69     if test ! -d "$pathcomp"; then
70       echo "mkdir $pathcomp"
71
72       mkdir "$pathcomp" || lasterr=$?
73
74       if test ! -d "$pathcomp"; then
75         errstatus=$lasterr
76       else
77         if test ! -z "$dirmode"; then
78           echo "chmod $dirmode $pathcomp"
79
80           lasterr=""
81           chmod "$dirmode" "$pathcomp" || lasterr=$?
82
83           if test ! -z "$lasterr"; then
84             errstatus=$lasterr
85           fi
86         fi
87       fi
88     fi
89
90     pathcomp="$pathcomp/"
91   done
92 done
93
94 exit $errstatus