update from prep
[gnulib.git] / config / 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 version="mkinstalldirs 2003-06-02"
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}" 1>&2; exit 0 ;;
24     -m)                                 # -m PERM arg
25       shift
26       test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
27       dirmode="${1}"
28       shift ;;
29     --version) echo "$version"; exit 0;;
30     --) shift; break ;;                 # stop option processing
31     -*) echo "${usage}" 1>&2; exit 1 ;; # unknown option
32      *) break ;;                        # first non-opt arg
33   esac
34 done
35
36 # no `for var; do', it breaks on Solaris 2.7 (at least).
37 for file
38 do
39   if test -d "$file"; then
40     shift
41   else
42     break
43   fi
44 done
45
46 case $# in
47   0) exit 0 ;;
48 esac
49
50 case $dirmode in
51   '')
52     if mkdir -p -- . 2>/dev/null; then
53       echo "mkdir -p -- $*"
54       exec mkdir -p -- "$@"
55     fi ;;
56   *)
57     if mkdir -m "$dirmode" -p -- . 2>/dev/null; then
58       echo "mkdir -m $dirmode -p -- $*"
59       exec mkdir -m "$dirmode" -p -- "$@"
60     fi ;;
61 esac
62
63 for file
64 do
65   set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'`
66   shift
67
68   pathcomp=
69   for d
70   do
71     pathcomp="$pathcomp$d"
72     case "$pathcomp" in
73       -*) pathcomp=./$pathcomp ;;
74     esac
75
76     if test ! -d "$pathcomp"; then
77       echo "mkdir $pathcomp"
78
79       mkdir "$pathcomp" || lasterr=$?
80
81       if test ! -d "$pathcomp"; then
82         errstatus=$lasterr
83       else
84         if test ! -z "$dirmode"; then
85           echo "chmod $dirmode $pathcomp"
86
87           lasterr=""
88           chmod "$dirmode" "$pathcomp" || lasterr=$?
89
90           if test ! -z "$lasterr"; then
91             errstatus=$lasterr
92           fi
93         fi
94       fi
95     fi
96
97     pathcomp="$pathcomp/"
98   done
99 done
100
101 exit $errstatus