1*1b8adde7SWilliam Kucharski#! /bin/sh 2*1b8adde7SWilliam Kucharski# mkinstalldirs --- make directory hierarchy 3*1b8adde7SWilliam Kucharski 4*1b8adde7SWilliam Kucharskiscriptversion=2004-02-15.20 5*1b8adde7SWilliam Kucharski 6*1b8adde7SWilliam Kucharski# Original author: Noah Friedman <friedman@prep.ai.mit.edu> 7*1b8adde7SWilliam Kucharski# Created: 1993-05-16 8*1b8adde7SWilliam Kucharski# Public domain. 9*1b8adde7SWilliam Kucharski# 10*1b8adde7SWilliam Kucharski# This file is maintained in Automake, please report 11*1b8adde7SWilliam Kucharski# bugs to <bug-automake@gnu.org> or send patches to 12*1b8adde7SWilliam Kucharski# <automake-patches@gnu.org>. 13*1b8adde7SWilliam Kucharski 14*1b8adde7SWilliam Kucharskierrstatus=0 15*1b8adde7SWilliam Kucharskidirmode="" 16*1b8adde7SWilliam Kucharski 17*1b8adde7SWilliam Kucharskiusage="\ 18*1b8adde7SWilliam KucharskiUsage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ... 19*1b8adde7SWilliam Kucharski 20*1b8adde7SWilliam KucharskiCreate each directory DIR (with mode MODE, if specified), including all 21*1b8adde7SWilliam Kucharskileading file name components. 22*1b8adde7SWilliam Kucharski 23*1b8adde7SWilliam KucharskiReport bugs to <bug-automake@gnu.org>." 24*1b8adde7SWilliam Kucharski 25*1b8adde7SWilliam Kucharski# process command line arguments 26*1b8adde7SWilliam Kucharskiwhile test $# -gt 0 ; do 27*1b8adde7SWilliam Kucharski case $1 in 28*1b8adde7SWilliam Kucharski -h | --help | --h*) # -h for help 29*1b8adde7SWilliam Kucharski echo "$usage" 30*1b8adde7SWilliam Kucharski exit 0 31*1b8adde7SWilliam Kucharski ;; 32*1b8adde7SWilliam Kucharski -m) # -m PERM arg 33*1b8adde7SWilliam Kucharski shift 34*1b8adde7SWilliam Kucharski test $# -eq 0 && { echo "$usage" 1>&2; exit 1; } 35*1b8adde7SWilliam Kucharski dirmode=$1 36*1b8adde7SWilliam Kucharski shift 37*1b8adde7SWilliam Kucharski ;; 38*1b8adde7SWilliam Kucharski --version) 39*1b8adde7SWilliam Kucharski echo "$0 $scriptversion" 40*1b8adde7SWilliam Kucharski exit 0 41*1b8adde7SWilliam Kucharski ;; 42*1b8adde7SWilliam Kucharski --) # stop option processing 43*1b8adde7SWilliam Kucharski shift 44*1b8adde7SWilliam Kucharski break 45*1b8adde7SWilliam Kucharski ;; 46*1b8adde7SWilliam Kucharski -*) # unknown option 47*1b8adde7SWilliam Kucharski echo "$usage" 1>&2 48*1b8adde7SWilliam Kucharski exit 1 49*1b8adde7SWilliam Kucharski ;; 50*1b8adde7SWilliam Kucharski *) # first non-opt arg 51*1b8adde7SWilliam Kucharski break 52*1b8adde7SWilliam Kucharski ;; 53*1b8adde7SWilliam Kucharski esac 54*1b8adde7SWilliam Kucharskidone 55*1b8adde7SWilliam Kucharski 56*1b8adde7SWilliam Kucharskifor file 57*1b8adde7SWilliam Kucharskido 58*1b8adde7SWilliam Kucharski if test -d "$file"; then 59*1b8adde7SWilliam Kucharski shift 60*1b8adde7SWilliam Kucharski else 61*1b8adde7SWilliam Kucharski break 62*1b8adde7SWilliam Kucharski fi 63*1b8adde7SWilliam Kucharskidone 64*1b8adde7SWilliam Kucharski 65*1b8adde7SWilliam Kucharskicase $# in 66*1b8adde7SWilliam Kucharski 0) exit 0 ;; 67*1b8adde7SWilliam Kucharskiesac 68*1b8adde7SWilliam Kucharski 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. 75*1b8adde7SWilliam Kucharskicase $dirmode in 76*1b8adde7SWilliam Kucharski '') 77*1b8adde7SWilliam Kucharski if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then 78*1b8adde7SWilliam Kucharski echo "mkdir -p -- $*" 79*1b8adde7SWilliam Kucharski exec mkdir -p -- "$@" 80*1b8adde7SWilliam Kucharski else 81*1b8adde7SWilliam Kucharski # On NextStep and OpenStep, the `mkdir' command does not 82*1b8adde7SWilliam Kucharski # recognize any option. It will interpret all options as 83*1b8adde7SWilliam Kucharski # directories to create, and then abort because `.' already 84*1b8adde7SWilliam Kucharski # exists. 85*1b8adde7SWilliam Kucharski test -d ./-p && rmdir ./-p 86*1b8adde7SWilliam Kucharski test -d ./--version && rmdir ./--version 87*1b8adde7SWilliam Kucharski fi 88*1b8adde7SWilliam Kucharski ;; 89*1b8adde7SWilliam Kucharski *) 90*1b8adde7SWilliam Kucharski if mkdir -m "$dirmode" -p --version . >/dev/null 2>&1 && 91*1b8adde7SWilliam Kucharski test ! -d ./--version; then 92*1b8adde7SWilliam Kucharski echo "mkdir -m $dirmode -p -- $*" 93*1b8adde7SWilliam Kucharski exec mkdir -m "$dirmode" -p -- "$@" 94*1b8adde7SWilliam Kucharski else 95*1b8adde7SWilliam Kucharski # Clean up after NextStep and OpenStep mkdir. 96*1b8adde7SWilliam Kucharski for d in ./-m ./-p ./--version "./$dirmode"; 97*1b8adde7SWilliam Kucharski do 98*1b8adde7SWilliam Kucharski test -d $d && rmdir $d 99*1b8adde7SWilliam Kucharski done 100*1b8adde7SWilliam Kucharski fi 101*1b8adde7SWilliam Kucharski ;; 102*1b8adde7SWilliam Kucharskiesac 103*1b8adde7SWilliam Kucharski 104*1b8adde7SWilliam Kucharskifor file 105*1b8adde7SWilliam Kucharskido 106*1b8adde7SWilliam Kucharski set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'` 107*1b8adde7SWilliam Kucharski shift 108*1b8adde7SWilliam Kucharski 109*1b8adde7SWilliam Kucharski pathcomp= 110*1b8adde7SWilliam Kucharski for d 111*1b8adde7SWilliam Kucharski do 112*1b8adde7SWilliam Kucharski pathcomp="$pathcomp$d" 113*1b8adde7SWilliam Kucharski case $pathcomp in 114*1b8adde7SWilliam Kucharski -*) pathcomp=./$pathcomp ;; 115*1b8adde7SWilliam Kucharski esac 116*1b8adde7SWilliam Kucharski 117*1b8adde7SWilliam Kucharski if test ! -d "$pathcomp"; then 118*1b8adde7SWilliam Kucharski echo "mkdir $pathcomp" 119*1b8adde7SWilliam Kucharski 120*1b8adde7SWilliam Kucharski mkdir "$pathcomp" || lasterr=$? 121*1b8adde7SWilliam Kucharski 122*1b8adde7SWilliam Kucharski if test ! -d "$pathcomp"; then 123*1b8adde7SWilliam Kucharski errstatus=$lasterr 124*1b8adde7SWilliam Kucharski else 125*1b8adde7SWilliam Kucharski if test ! -z "$dirmode"; then 126*1b8adde7SWilliam Kucharski echo "chmod $dirmode $pathcomp" 127*1b8adde7SWilliam Kucharski lasterr="" 128*1b8adde7SWilliam Kucharski chmod "$dirmode" "$pathcomp" || lasterr=$? 129*1b8adde7SWilliam Kucharski 130*1b8adde7SWilliam Kucharski if test ! -z "$lasterr"; then 131*1b8adde7SWilliam Kucharski errstatus=$lasterr 132*1b8adde7SWilliam Kucharski fi 133*1b8adde7SWilliam Kucharski fi 134*1b8adde7SWilliam Kucharski fi 135*1b8adde7SWilliam Kucharski fi 136*1b8adde7SWilliam Kucharski 137*1b8adde7SWilliam Kucharski pathcomp="$pathcomp/" 138*1b8adde7SWilliam Kucharski done 139*1b8adde7SWilliam Kucharskidone 140*1b8adde7SWilliam Kucharski 141*1b8adde7SWilliam Kucharskiexit $errstatus 142*1b8adde7SWilliam Kucharski 143*1b8adde7SWilliam Kucharski# Local Variables: 144*1b8adde7SWilliam Kucharski# mode: shell-script 145*1b8adde7SWilliam Kucharski# sh-indentation: 2 146*1b8adde7SWilliam Kucharski# eval: (add-hook 'write-file-hooks 'time-stamp) 147*1b8adde7SWilliam Kucharski# time-stamp-start: "scriptversion=" 148*1b8adde7SWilliam Kucharski# time-stamp-format: "%:y-%02m-%02d.%02H" 149*1b8adde7SWilliam Kucharski# time-stamp-end: "$" 150*1b8adde7SWilliam Kucharski# End: 151