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