138889Sjdp#! /bin/sh
238889Sjdp# mkinstalldirs --- make directory hierarchy
3218822Sdim
4218822Sdimscriptversion=2005-06-29.22
5218822Sdim
6218822Sdim# Original author: Noah Friedman <friedman@prep.ai.mit.edu>
738889Sjdp# Created: 1993-05-16
8218822Sdim# Public domain.
9218822Sdim#
10218822Sdim# This file is maintained in Automake, please report
11218822Sdim# bugs to <bug-automake@gnu.org> or send patches to
12218822Sdim# <automake-patches@gnu.org>.
1338889Sjdp
1438889Sjdperrstatus=0
15218822Sdimdirmode=
1638889Sjdp
17130561Sobrienusage="\
18218822SdimUsage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ...
1938889Sjdp
20218822SdimCreate each directory DIR (with mode MODE, if specified), including all
21218822Sdimleading file name components.
22218822Sdim
23218822SdimReport bugs to <bug-automake@gnu.org>."
24218822Sdim
25130561Sobrien# process command line arguments
26130561Sobrienwhile test $# -gt 0 ; do
27130561Sobrien  case $1 in
28130561Sobrien    -h | --help | --h*)         # -h for help
29218822Sdim      echo "$usage"
30218822Sdim      exit $?
31130561Sobrien      ;;
32130561Sobrien    -m)                         # -m PERM arg
33130561Sobrien      shift
34130561Sobrien      test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
35130561Sobrien      dirmode=$1
36130561Sobrien      shift
37130561Sobrien      ;;
38218822Sdim    --version)
39218822Sdim      echo "$0 $scriptversion"
40218822Sdim      exit $?
41218822Sdim      ;;
42130561Sobrien    --)                         # stop option processing
43130561Sobrien      shift
44130561Sobrien      break
45130561Sobrien      ;;
46130561Sobrien    -*)                         # unknown option
47130561Sobrien      echo "$usage" 1>&2
48130561Sobrien      exit 1
49130561Sobrien      ;;
50130561Sobrien    *)                          # first non-opt arg
51130561Sobrien      break
52130561Sobrien      ;;
53130561Sobrien  esac
54130561Sobriendone
5538889Sjdp
56130561Sobrienfor file
57130561Sobriendo
58130561Sobrien  if test -d "$file"; then
59130561Sobrien    shift
60130561Sobrien  else
61130561Sobrien    break
62130561Sobrien  fi
63130561Sobriendone
6438889Sjdp
65130561Sobriencase $# in
66130561Sobrien  0) exit 0 ;;
67130561Sobrienesac
6838889Sjdp
69218822Sdim# Solaris 8's mkdir -p isn't thread-safe.  If you mkdir -p a/b and
70218822Sdim# mkdir -p a/c at the same time, both will detect that a is missing,
71218822Sdim# one will create a, then the other will try to create a and die with
72218822Sdim# a "File exists" error.  This is a problem when calling mkinstalldirs
73218822Sdim# from a parallel make.  We use --version in the probe to restrict
74218822Sdim# ourselves to GNU mkdir, which is thread-safe.
75130561Sobriencase $dirmode in
76130561Sobrien  '')
77218822Sdim    if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then
78130561Sobrien      echo "mkdir -p -- $*"
79130561Sobrien      exec mkdir -p -- "$@"
80218822Sdim    else
81218822Sdim      # On NextStep and OpenStep, the `mkdir' command does not
82218822Sdim      # recognize any option.  It will interpret all options as
83218822Sdim      # directories to create, and then abort because `.' already
84218822Sdim      # exists.
85218822Sdim      test -d ./-p && rmdir ./-p
86218822Sdim      test -d ./--version && rmdir ./--version
87130561Sobrien    fi
88130561Sobrien    ;;
89130561Sobrien  *)
90218822Sdim    if mkdir -m "$dirmode" -p --version . >/dev/null 2>&1 &&
91218822Sdim       test ! -d ./--version; then
92130561Sobrien      echo "mkdir -m $dirmode -p -- $*"
93130561Sobrien      exec mkdir -m "$dirmode" -p -- "$@"
94218822Sdim    else
95218822Sdim      # Clean up after NextStep and OpenStep mkdir.
96218822Sdim      for d in ./-m ./-p ./--version "./$dirmode";
97218822Sdim      do
98218822Sdim        test -d $d && rmdir $d
99218822Sdim      done
100130561Sobrien    fi
101130561Sobrien    ;;
102130561Sobrienesac
103130561Sobrien
104130561Sobrienfor file
105130561Sobriendo
106218822Sdim  case $file in
107218822Sdim    /*) pathcomp=/ ;;
108218822Sdim    *)  pathcomp= ;;
109218822Sdim  esac
110218822Sdim  oIFS=$IFS
111218822Sdim  IFS=/
112218822Sdim  set fnord $file
113130561Sobrien  shift
114218822Sdim  IFS=$oIFS
115130561Sobrien
116130561Sobrien  for d
117130561Sobrien  do
118218822Sdim    test "x$d" = x && continue
119218822Sdim
120218822Sdim    pathcomp=$pathcomp$d
121130561Sobrien    case $pathcomp in
122130561Sobrien      -*) pathcomp=./$pathcomp ;;
123130561Sobrien    esac
124130561Sobrien
125130561Sobrien    if test ! -d "$pathcomp"; then
126130561Sobrien      echo "mkdir $pathcomp"
127130561Sobrien
128130561Sobrien      mkdir "$pathcomp" || lasterr=$?
129130561Sobrien
130130561Sobrien      if test ! -d "$pathcomp"; then
131218822Sdim	errstatus=$lasterr
132130561Sobrien      else
133218822Sdim	if test ! -z "$dirmode"; then
134130561Sobrien	  echo "chmod $dirmode $pathcomp"
135218822Sdim	  lasterr=
136218822Sdim	  chmod "$dirmode" "$pathcomp" || lasterr=$?
137130561Sobrien
138218822Sdim	  if test ! -z "$lasterr"; then
139218822Sdim	    errstatus=$lasterr
140218822Sdim	  fi
141218822Sdim	fi
142130561Sobrien      fi
143130561Sobrien    fi
144130561Sobrien
145218822Sdim    pathcomp=$pathcomp/
146130561Sobrien  done
14738889Sjdpdone
14838889Sjdp
14938889Sjdpexit $errstatus
15038889Sjdp
151130561Sobrien# Local Variables:
152130561Sobrien# mode: shell-script
153130561Sobrien# sh-indentation: 2
154218822Sdim# eval: (add-hook 'write-file-hooks 'time-stamp)
155218822Sdim# time-stamp-start: "scriptversion="
156218822Sdim# time-stamp-format: "%:y-%02m-%02d.%02H"
157218822Sdim# time-stamp-end: "$"
158130561Sobrien# End:
159