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