17c478bd9Sstevel@tonic-gate#! /bin/sh
27c478bd9Sstevel@tonic-gate# mkinstalldirs --- make directory hierarchy
37c478bd9Sstevel@tonic-gate
4*1b8adde7SWilliam Kucharskiscriptversion=2004-02-15.20
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate# Original author: Noah Friedman <friedman@prep.ai.mit.edu>
77c478bd9Sstevel@tonic-gate# Created: 1993-05-16
87c478bd9Sstevel@tonic-gate# Public domain.
97c478bd9Sstevel@tonic-gate#
107c478bd9Sstevel@tonic-gate# This file is maintained in Automake, please report
117c478bd9Sstevel@tonic-gate# bugs to <bug-automake@gnu.org> or send patches to
127c478bd9Sstevel@tonic-gate# <automake-patches@gnu.org>.
137c478bd9Sstevel@tonic-gate
147c478bd9Sstevel@tonic-gateerrstatus=0
157c478bd9Sstevel@tonic-gatedirmode=""
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gateusage="\
187c478bd9Sstevel@tonic-gateUsage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ...
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gateCreate each directory DIR (with mode MODE, if specified), including all
217c478bd9Sstevel@tonic-gateleading file name components.
227c478bd9Sstevel@tonic-gate
237c478bd9Sstevel@tonic-gateReport bugs to <bug-automake@gnu.org>."
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate# process command line arguments
267c478bd9Sstevel@tonic-gatewhile test $# -gt 0 ; do
277c478bd9Sstevel@tonic-gate  case $1 in
287c478bd9Sstevel@tonic-gate    -h | --help | --h*)         # -h for help
297c478bd9Sstevel@tonic-gate      echo "$usage"
307c478bd9Sstevel@tonic-gate      exit 0
317c478bd9Sstevel@tonic-gate      ;;
327c478bd9Sstevel@tonic-gate    -m)                         # -m PERM arg
337c478bd9Sstevel@tonic-gate      shift
347c478bd9Sstevel@tonic-gate      test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
357c478bd9Sstevel@tonic-gate      dirmode=$1
367c478bd9Sstevel@tonic-gate      shift
377c478bd9Sstevel@tonic-gate      ;;
387c478bd9Sstevel@tonic-gate    --version)
397c478bd9Sstevel@tonic-gate      echo "$0 $scriptversion"
407c478bd9Sstevel@tonic-gate      exit 0
417c478bd9Sstevel@tonic-gate      ;;
427c478bd9Sstevel@tonic-gate    --)                         # stop option processing
437c478bd9Sstevel@tonic-gate      shift
447c478bd9Sstevel@tonic-gate      break
457c478bd9Sstevel@tonic-gate      ;;
467c478bd9Sstevel@tonic-gate    -*)                         # unknown option
477c478bd9Sstevel@tonic-gate      echo "$usage" 1>&2
487c478bd9Sstevel@tonic-gate      exit 1
497c478bd9Sstevel@tonic-gate      ;;
507c478bd9Sstevel@tonic-gate    *)                          # first non-opt arg
517c478bd9Sstevel@tonic-gate      break
527c478bd9Sstevel@tonic-gate      ;;
537c478bd9Sstevel@tonic-gate  esac
547c478bd9Sstevel@tonic-gatedone
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gatefor file
577c478bd9Sstevel@tonic-gatedo
587c478bd9Sstevel@tonic-gate  if test -d "$file"; then
597c478bd9Sstevel@tonic-gate    shift
607c478bd9Sstevel@tonic-gate  else
617c478bd9Sstevel@tonic-gate    break
627c478bd9Sstevel@tonic-gate  fi
637c478bd9Sstevel@tonic-gatedone
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gatecase $# in
667c478bd9Sstevel@tonic-gate  0) exit 0 ;;
677c478bd9Sstevel@tonic-gateesac
687c478bd9Sstevel@tonic-gate
69*1b8adde7SWilliam Kucharski# Solaris 8's mkdir -p isn't thread-safe.  If you mkdir -p a/b and
70*1b8adde7SWilliam Kucharski# mkdir -p a/c at the same time, both will detect that a is missing,
71*1b8adde7SWilliam Kucharski# one will create a, then the other will try to create a and die with
72*1b8adde7SWilliam Kucharski# a "File exists" error.  This is a problem when calling mkinstalldirs
73*1b8adde7SWilliam Kucharski# from a parallel make.  We use --version in the probe to restrict
74*1b8adde7SWilliam Kucharski# ourselves to GNU mkdir, which is thread-safe.
757c478bd9Sstevel@tonic-gatecase $dirmode in
767c478bd9Sstevel@tonic-gate  '')
77*1b8adde7SWilliam Kucharski    if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then
787c478bd9Sstevel@tonic-gate      echo "mkdir -p -- $*"
797c478bd9Sstevel@tonic-gate      exec mkdir -p -- "$@"
807c478bd9Sstevel@tonic-gate    else
817c478bd9Sstevel@tonic-gate      # On NextStep and OpenStep, the `mkdir' command does not
827c478bd9Sstevel@tonic-gate      # recognize any option.  It will interpret all options as
837c478bd9Sstevel@tonic-gate      # directories to create, and then abort because `.' already
847c478bd9Sstevel@tonic-gate      # exists.
857c478bd9Sstevel@tonic-gate      test -d ./-p && rmdir ./-p
86*1b8adde7SWilliam Kucharski      test -d ./--version && rmdir ./--version
877c478bd9Sstevel@tonic-gate    fi
887c478bd9Sstevel@tonic-gate    ;;
897c478bd9Sstevel@tonic-gate  *)
90*1b8adde7SWilliam Kucharski    if mkdir -m "$dirmode" -p --version . >/dev/null 2>&1 &&
91*1b8adde7SWilliam Kucharski       test ! -d ./--version; then
927c478bd9Sstevel@tonic-gate      echo "mkdir -m $dirmode -p -- $*"
937c478bd9Sstevel@tonic-gate      exec mkdir -m "$dirmode" -p -- "$@"
947c478bd9Sstevel@tonic-gate    else
957c478bd9Sstevel@tonic-gate      # Clean up after NextStep and OpenStep mkdir.
96*1b8adde7SWilliam Kucharski      for d in ./-m ./-p ./--version "./$dirmode";
977c478bd9Sstevel@tonic-gate      do
987c478bd9Sstevel@tonic-gate        test -d $d && rmdir $d
997c478bd9Sstevel@tonic-gate      done
1007c478bd9Sstevel@tonic-gate    fi
1017c478bd9Sstevel@tonic-gate    ;;
1027c478bd9Sstevel@tonic-gateesac
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gatefor file
1057c478bd9Sstevel@tonic-gatedo
1067c478bd9Sstevel@tonic-gate  set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'`
1077c478bd9Sstevel@tonic-gate  shift
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate  pathcomp=
1107c478bd9Sstevel@tonic-gate  for d
1117c478bd9Sstevel@tonic-gate  do
1127c478bd9Sstevel@tonic-gate    pathcomp="$pathcomp$d"
1137c478bd9Sstevel@tonic-gate    case $pathcomp in
1147c478bd9Sstevel@tonic-gate      -*) pathcomp=./$pathcomp ;;
1157c478bd9Sstevel@tonic-gate    esac
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate    if test ! -d "$pathcomp"; then
1187c478bd9Sstevel@tonic-gate      echo "mkdir $pathcomp"
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate      mkdir "$pathcomp" || lasterr=$?
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate      if test ! -d "$pathcomp"; then
1237c478bd9Sstevel@tonic-gate	errstatus=$lasterr
1247c478bd9Sstevel@tonic-gate      else
1257c478bd9Sstevel@tonic-gate	if test ! -z "$dirmode"; then
1267c478bd9Sstevel@tonic-gate	  echo "chmod $dirmode $pathcomp"
1277c478bd9Sstevel@tonic-gate	  lasterr=""
1287c478bd9Sstevel@tonic-gate	  chmod "$dirmode" "$pathcomp" || lasterr=$?
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate	  if test ! -z "$lasterr"; then
1317c478bd9Sstevel@tonic-gate	    errstatus=$lasterr
1327c478bd9Sstevel@tonic-gate	  fi
1337c478bd9Sstevel@tonic-gate	fi
1347c478bd9Sstevel@tonic-gate      fi
1357c478bd9Sstevel@tonic-gate    fi
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate    pathcomp="$pathcomp/"
1387c478bd9Sstevel@tonic-gate  done
1397c478bd9Sstevel@tonic-gatedone
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gateexit $errstatus
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate# Local Variables:
1447c478bd9Sstevel@tonic-gate# mode: shell-script
1457c478bd9Sstevel@tonic-gate# sh-indentation: 2
1467c478bd9Sstevel@tonic-gate# eval: (add-hook 'write-file-hooks 'time-stamp)
1477c478bd9Sstevel@tonic-gate# time-stamp-start: "scriptversion="
1487c478bd9Sstevel@tonic-gate# time-stamp-format: "%:y-%02m-%02d.%02H"
1497c478bd9Sstevel@tonic-gate# time-stamp-end: "$"
1507c478bd9Sstevel@tonic-gate# End:
151