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