xref: /libev/mkinstalldirs (revision 93823e6c)
1e3a38431SPaul Bohm#! /bin/sh
2e3a38431SPaul Bohm# mkinstalldirs --- make directory hierarchy
3e3a38431SPaul Bohm
4*93823e6cSPaul Bohmscriptversion=2009-04-28.21; # UTC
5*93823e6cSPaul Bohm
6*93823e6cSPaul Bohm# Original author: Noah Friedman <[email protected]>
7*93823e6cSPaul Bohm# Created: 1993-05-16
8*93823e6cSPaul Bohm# Public domain.
9*93823e6cSPaul Bohm#
10*93823e6cSPaul Bohm# This file is maintained in Automake, please report
11*93823e6cSPaul Bohm# bugs to <[email protected]> or send patches to
12*93823e6cSPaul Bohm# <[email protected]>.
13*93823e6cSPaul Bohm
14*93823e6cSPaul Bohmnl='
15*93823e6cSPaul Bohm'
16*93823e6cSPaul BohmIFS=" ""	$nl"
17e3a38431SPaul Bohmerrstatus=0
18*93823e6cSPaul Bohmdirmode=
19e3a38431SPaul Bohm
20e3a38431SPaul Bohmusage="\
21*93823e6cSPaul BohmUsage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ...
22*93823e6cSPaul Bohm
23*93823e6cSPaul BohmCreate each directory DIR (with mode MODE, if specified), including all
24*93823e6cSPaul Bohmleading file name components.
25*93823e6cSPaul Bohm
26*93823e6cSPaul BohmReport bugs to <[email protected]>."
27e3a38431SPaul Bohm
28e3a38431SPaul Bohm# process command line arguments
29e3a38431SPaul Bohmwhile test $# -gt 0 ; do
30e3a38431SPaul Bohm  case $1 in
31e3a38431SPaul Bohm    -h | --help | --h*)         # -h for help
32*93823e6cSPaul Bohm      echo "$usage"
33*93823e6cSPaul Bohm      exit $?
34e3a38431SPaul Bohm      ;;
35e3a38431SPaul Bohm    -m)                         # -m PERM arg
36e3a38431SPaul Bohm      shift
37e3a38431SPaul Bohm      test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
38e3a38431SPaul Bohm      dirmode=$1
39e3a38431SPaul Bohm      shift
40e3a38431SPaul Bohm      ;;
41*93823e6cSPaul Bohm    --version)
42*93823e6cSPaul Bohm      echo "$0 $scriptversion"
43*93823e6cSPaul Bohm      exit $?
44*93823e6cSPaul Bohm      ;;
45e3a38431SPaul Bohm    --)                         # stop option processing
46e3a38431SPaul Bohm      shift
47e3a38431SPaul Bohm      break
48e3a38431SPaul Bohm      ;;
49e3a38431SPaul Bohm    -*)                         # unknown option
50e3a38431SPaul Bohm      echo "$usage" 1>&2
51e3a38431SPaul Bohm      exit 1
52e3a38431SPaul Bohm      ;;
53e3a38431SPaul Bohm    *)                          # first non-opt arg
54e3a38431SPaul Bohm      break
55e3a38431SPaul Bohm      ;;
56e3a38431SPaul Bohm  esac
57e3a38431SPaul Bohmdone
58e3a38431SPaul Bohm
59e3a38431SPaul Bohmfor file
60e3a38431SPaul Bohmdo
61e3a38431SPaul Bohm  if test -d "$file"; then
62e3a38431SPaul Bohm    shift
63e3a38431SPaul Bohm  else
64e3a38431SPaul Bohm    break
65e3a38431SPaul Bohm  fi
66e3a38431SPaul Bohmdone
67e3a38431SPaul Bohm
68e3a38431SPaul Bohmcase $# in
69e3a38431SPaul Bohm  0) exit 0 ;;
70e3a38431SPaul Bohmesac
71e3a38431SPaul Bohm
72*93823e6cSPaul Bohm# Solaris 8's mkdir -p isn't thread-safe.  If you mkdir -p a/b and
73*93823e6cSPaul Bohm# mkdir -p a/c at the same time, both will detect that a is missing,
74*93823e6cSPaul Bohm# one will create a, then the other will try to create a and die with
75*93823e6cSPaul Bohm# a "File exists" error.  This is a problem when calling mkinstalldirs
76*93823e6cSPaul Bohm# from a parallel make.  We use --version in the probe to restrict
77*93823e6cSPaul Bohm# ourselves to GNU mkdir, which is thread-safe.
78e3a38431SPaul Bohmcase $dirmode in
79e3a38431SPaul Bohm  '')
80*93823e6cSPaul Bohm    if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then
81e3a38431SPaul Bohm      echo "mkdir -p -- $*"
82e3a38431SPaul Bohm      exec mkdir -p -- "$@"
83*93823e6cSPaul Bohm    else
84*93823e6cSPaul Bohm      # On NextStep and OpenStep, the 'mkdir' command does not
85*93823e6cSPaul Bohm      # recognize any option.  It will interpret all options as
86*93823e6cSPaul Bohm      # directories to create, and then abort because '.' already
87*93823e6cSPaul Bohm      # exists.
88*93823e6cSPaul Bohm      test -d ./-p && rmdir ./-p
89*93823e6cSPaul Bohm      test -d ./--version && rmdir ./--version
90e3a38431SPaul Bohm    fi
91e3a38431SPaul Bohm    ;;
92e3a38431SPaul Bohm  *)
93*93823e6cSPaul Bohm    if mkdir -m "$dirmode" -p --version . >/dev/null 2>&1 &&
94*93823e6cSPaul Bohm       test ! -d ./--version; then
95e3a38431SPaul Bohm      echo "mkdir -m $dirmode -p -- $*"
96e3a38431SPaul Bohm      exec mkdir -m "$dirmode" -p -- "$@"
97*93823e6cSPaul Bohm    else
98*93823e6cSPaul Bohm      # Clean up after NextStep and OpenStep mkdir.
99*93823e6cSPaul Bohm      for d in ./-m ./-p ./--version "./$dirmode";
100*93823e6cSPaul Bohm      do
101*93823e6cSPaul Bohm        test -d $d && rmdir $d
102*93823e6cSPaul Bohm      done
103e3a38431SPaul Bohm    fi
104e3a38431SPaul Bohm    ;;
105e3a38431SPaul Bohmesac
106e3a38431SPaul Bohm
107e3a38431SPaul Bohmfor file
108e3a38431SPaul Bohmdo
109*93823e6cSPaul Bohm  case $file in
110*93823e6cSPaul Bohm    /*) pathcomp=/ ;;
111*93823e6cSPaul Bohm    *)  pathcomp= ;;
112*93823e6cSPaul Bohm  esac
113*93823e6cSPaul Bohm  oIFS=$IFS
114*93823e6cSPaul Bohm  IFS=/
115*93823e6cSPaul Bohm  set fnord $file
116e3a38431SPaul Bohm  shift
117*93823e6cSPaul Bohm  IFS=$oIFS
118e3a38431SPaul Bohm
119e3a38431SPaul Bohm  for d
120e3a38431SPaul Bohm  do
121*93823e6cSPaul Bohm    test "x$d" = x && continue
122*93823e6cSPaul Bohm
123*93823e6cSPaul Bohm    pathcomp=$pathcomp$d
124e3a38431SPaul Bohm    case $pathcomp in
125e3a38431SPaul Bohm      -*) pathcomp=./$pathcomp ;;
126e3a38431SPaul Bohm    esac
127e3a38431SPaul Bohm
128e3a38431SPaul Bohm    if test ! -d "$pathcomp"; then
129e3a38431SPaul Bohm      echo "mkdir $pathcomp"
130e3a38431SPaul Bohm
131e3a38431SPaul Bohm      mkdir "$pathcomp" || lasterr=$?
132e3a38431SPaul Bohm
133e3a38431SPaul Bohm      if test ! -d "$pathcomp"; then
134e3a38431SPaul Bohm	errstatus=$lasterr
135e3a38431SPaul Bohm      else
136e3a38431SPaul Bohm	if test ! -z "$dirmode"; then
137e3a38431SPaul Bohm	  echo "chmod $dirmode $pathcomp"
138*93823e6cSPaul Bohm	  lasterr=
139e3a38431SPaul Bohm	  chmod "$dirmode" "$pathcomp" || lasterr=$?
140e3a38431SPaul Bohm
141e3a38431SPaul Bohm	  if test ! -z "$lasterr"; then
142e3a38431SPaul Bohm	    errstatus=$lasterr
143e3a38431SPaul Bohm	  fi
144e3a38431SPaul Bohm	fi
145e3a38431SPaul Bohm      fi
146e3a38431SPaul Bohm    fi
147e3a38431SPaul Bohm
148*93823e6cSPaul Bohm    pathcomp=$pathcomp/
149e3a38431SPaul Bohm  done
150e3a38431SPaul Bohmdone
151e3a38431SPaul Bohm
152e3a38431SPaul Bohmexit $errstatus
153e3a38431SPaul Bohm
154e3a38431SPaul Bohm# Local Variables:
155e3a38431SPaul Bohm# mode: shell-script
156e3a38431SPaul Bohm# sh-indentation: 2
157*93823e6cSPaul Bohm# eval: (add-hook 'write-file-hooks 'time-stamp)
158*93823e6cSPaul Bohm# time-stamp-start: "scriptversion="
159*93823e6cSPaul Bohm# time-stamp-format: "%:y-%02m-%02d.%02H"
160*93823e6cSPaul Bohm# time-stamp-time-zone: "UTC"
161*93823e6cSPaul Bohm# time-stamp-end: "; # UTC"
162e3a38431SPaul Bohm# End:
163