169626Sru#! /bin/sh
269626Sru# mkinstalldirs --- make directory hierarchy
3151497Sru
4151497Sruscriptversion=2005-06-29.22
5151497Sru
6151497Sru# Original author: Noah Friedman <friedman@prep.ai.mit.edu>
769626Sru# Created: 1993-05-16
8151497Sru# Public domain.
9151497Sru#
10151497Sru# This file is maintained in Automake, please report
11151497Sru# bugs to <bug-automake@gnu.org> or send patches to
12151497Sru# <automake-patches@gnu.org>.
1369626Sru
1469626Sruerrstatus=0
15151497Srudirmode=
1669626Sru
17151497Sruusage="\
18151497SruUsage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ...
19151497Sru
20151497SruCreate each directory DIR (with mode MODE, if specified), including all
21151497Sruleading file name components.
22151497Sru
23151497SruReport bugs to <bug-automake@gnu.org>."
24151497Sru
25151497Sru# process command line arguments
26151497Sruwhile test $# -gt 0 ; do
27151497Sru  case $1 in
28151497Sru    -h | --help | --h*)         # -h for help
29151497Sru      echo "$usage"
30151497Sru      exit $?
31151497Sru      ;;
32151497Sru    -m)                         # -m PERM arg
33151497Sru      shift
34151497Sru      test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
35151497Sru      dirmode=$1
36151497Sru      shift
37151497Sru      ;;
38151497Sru    --version)
39151497Sru      echo "$0 $scriptversion"
40151497Sru      exit $?
41151497Sru      ;;
42151497Sru    --)                         # stop option processing
43151497Sru      shift
44151497Sru      break
45151497Sru      ;;
46151497Sru    -*)                         # unknown option
47151497Sru      echo "$usage" 1>&2
48151497Sru      exit 1
49151497Sru      ;;
50151497Sru    *)                          # first non-opt arg
51151497Sru      break
52151497Sru      ;;
53151497Sru  esac
54151497Srudone
55151497Sru
5669626Srufor file
5769626Srudo
58151497Sru  if test -d "$file"; then
59151497Sru    shift
60151497Sru  else
61151497Sru    break
62151497Sru  fi
63151497Srudone
6469626Sru
65151497Srucase $# in
66151497Sru  0) exit 0 ;;
67151497Sruesac
6869626Sru
69151497Sru# Solaris 8's mkdir -p isn't thread-safe.  If you mkdir -p a/b and
70151497Sru# mkdir -p a/c at the same time, both will detect that a is missing,
71151497Sru# one will create a, then the other will try to create a and die with
72151497Sru# a "File exists" error.  This is a problem when calling mkinstalldirs
73151497Sru# from a parallel make.  We use --version in the probe to restrict
74151497Sru# ourselves to GNU mkdir, which is thread-safe.
75151497Srucase $dirmode in
76151497Sru  '')
77151497Sru    if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then
78151497Sru      echo "mkdir -p -- $*"
79151497Sru      exec mkdir -p -- "$@"
80151497Sru    else
81151497Sru      # On NextStep and OpenStep, the `mkdir' command does not
82151497Sru      # recognize any option.  It will interpret all options as
83151497Sru      # directories to create, and then abort because `.' already
84151497Sru      # exists.
85151497Sru      test -d ./-p && rmdir ./-p
86151497Sru      test -d ./--version && rmdir ./--version
87151497Sru    fi
88151497Sru    ;;
89151497Sru  *)
90151497Sru    if mkdir -m "$dirmode" -p --version . >/dev/null 2>&1 &&
91151497Sru       test ! -d ./--version; then
92151497Sru      echo "mkdir -m $dirmode -p -- $*"
93151497Sru      exec mkdir -m "$dirmode" -p -- "$@"
94151497Sru    else
95151497Sru      # Clean up after NextStep and OpenStep mkdir.
96151497Sru      for d in ./-m ./-p ./--version "./$dirmode";
97151497Sru      do
98151497Sru        test -d $d && rmdir $d
99151497Sru      done
100151497Sru    fi
101151497Sru    ;;
102151497Sruesac
10369626Sru
104151497Srufor file
105151497Srudo
106151497Sru  case $file in
107151497Sru    /*) pathcomp=/ ;;
108151497Sru    *)  pathcomp= ;;
109151497Sru  esac
110151497Sru  oIFS=$IFS
111151497Sru  IFS=/
112151497Sru  set fnord $file
113151497Sru  shift
114151497Sru  IFS=$oIFS
11569626Sru
116151497Sru  for d
117151497Sru  do
118151497Sru    test "x$d" = x && continue
11969626Sru
120151497Sru    pathcomp=$pathcomp$d
121151497Sru    case $pathcomp in
122151497Sru      -*) pathcomp=./$pathcomp ;;
123151497Sru    esac
124151497Sru
125151497Sru    if test ! -d "$pathcomp"; then
126151497Sru      echo "mkdir $pathcomp"
127151497Sru
128151497Sru      mkdir "$pathcomp" || lasterr=$?
129151497Sru
130151497Sru      if test ! -d "$pathcomp"; then
131151497Sru	errstatus=$lasterr
132151497Sru      else
133151497Sru	if test ! -z "$dirmode"; then
134151497Sru	  echo "chmod $dirmode $pathcomp"
135151497Sru	  lasterr=
136151497Sru	  chmod "$dirmode" "$pathcomp" || lasterr=$?
137151497Sru
138151497Sru	  if test ! -z "$lasterr"; then
139151497Sru	    errstatus=$lasterr
140151497Sru	  fi
141151497Sru	fi
142151497Sru      fi
143151497Sru    fi
144151497Sru
145151497Sru    pathcomp=$pathcomp/
146151497Sru  done
14769626Srudone
14869626Sru
14969626Sruexit $errstatus
15069626Sru
151151497Sru# Local Variables:
152151497Sru# mode: shell-script
153151497Sru# sh-indentation: 2
154151497Sru# eval: (add-hook 'write-file-hooks 'time-stamp)
155151497Sru# time-stamp-start: "scriptversion="
156151497Sru# time-stamp-format: "%:y-%02m-%02d.%02H"
157151497Sru# time-stamp-end: "$"
158151497Sru# End:
159