annotate mkinstalldirs @ 0:d39e1d0d75b6

initial add
author paulo@hit-nxdomain.opendns.com
date Sat, 20 Feb 2010 21:18:28 -0800
parents
children
rev   line source
paulo@0 1 #! /bin/sh
paulo@0 2 # mkinstalldirs --- make directory hierarchy
paulo@0 3 # Author: Noah Friedman <friedman@prep.ai.mit.edu>
paulo@0 4 # Created: 1993-05-16
paulo@0 5 # Public domain
paulo@0 6
paulo@0 7 errstatus=0
paulo@0 8 dirmode=""
paulo@0 9
paulo@0 10 usage="\
paulo@0 11 Usage: mkinstalldirs [-h] [--help] [-m mode] dir ..."
paulo@0 12
paulo@0 13 # process command line arguments
paulo@0 14 while test $# -gt 0 ; do
paulo@0 15 case $1 in
paulo@0 16 -h | --help | --h*) # -h for help
paulo@0 17 echo "$usage" 1>&2
paulo@0 18 exit 0
paulo@0 19 ;;
paulo@0 20 -m) # -m PERM arg
paulo@0 21 shift
paulo@0 22 test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
paulo@0 23 dirmode=$1
paulo@0 24 shift
paulo@0 25 ;;
paulo@0 26 --) # stop option processing
paulo@0 27 shift
paulo@0 28 break
paulo@0 29 ;;
paulo@0 30 -*) # unknown option
paulo@0 31 echo "$usage" 1>&2
paulo@0 32 exit 1
paulo@0 33 ;;
paulo@0 34 *) # first non-opt arg
paulo@0 35 break
paulo@0 36 ;;
paulo@0 37 esac
paulo@0 38 done
paulo@0 39
paulo@0 40 for file
paulo@0 41 do
paulo@0 42 if test -d "$file"; then
paulo@0 43 shift
paulo@0 44 else
paulo@0 45 break
paulo@0 46 fi
paulo@0 47 done
paulo@0 48
paulo@0 49 case $# in
paulo@0 50 0) exit 0 ;;
paulo@0 51 esac
paulo@0 52
paulo@0 53 case $dirmode in
paulo@0 54 '')
paulo@0 55 if mkdir -p -- . 2>/dev/null; then
paulo@0 56 echo "mkdir -p -- $*"
paulo@0 57 exec mkdir -p -- "$@"
paulo@0 58 fi
paulo@0 59 ;;
paulo@0 60 *)
paulo@0 61 if mkdir -m "$dirmode" -p -- . 2>/dev/null; then
paulo@0 62 echo "mkdir -m $dirmode -p -- $*"
paulo@0 63 exec mkdir -m "$dirmode" -p -- "$@"
paulo@0 64 fi
paulo@0 65 ;;
paulo@0 66 esac
paulo@0 67
paulo@0 68 for file
paulo@0 69 do
paulo@0 70 set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'`
paulo@0 71 shift
paulo@0 72
paulo@0 73 pathcomp=
paulo@0 74 for d
paulo@0 75 do
paulo@0 76 pathcomp="$pathcomp$d"
paulo@0 77 case $pathcomp in
paulo@0 78 -*) pathcomp=./$pathcomp ;;
paulo@0 79 esac
paulo@0 80
paulo@0 81 if test ! -d "$pathcomp"; then
paulo@0 82 echo "mkdir $pathcomp"
paulo@0 83
paulo@0 84 mkdir "$pathcomp" || lasterr=$?
paulo@0 85
paulo@0 86 if test ! -d "$pathcomp"; then
paulo@0 87 errstatus=$lasterr
paulo@0 88 else
paulo@0 89 if test ! -z "$dirmode"; then
paulo@0 90 echo "chmod $dirmode $pathcomp"
paulo@0 91 lasterr=""
paulo@0 92 chmod "$dirmode" "$pathcomp" || lasterr=$?
paulo@0 93
paulo@0 94 if test ! -z "$lasterr"; then
paulo@0 95 errstatus=$lasterr
paulo@0 96 fi
paulo@0 97 fi
paulo@0 98 fi
paulo@0 99 fi
paulo@0 100
paulo@0 101 pathcomp="$pathcomp/"
paulo@0 102 done
paulo@0 103 done
paulo@0 104
paulo@0 105 exit $errstatus
paulo@0 106
paulo@0 107 # Local Variables:
paulo@0 108 # mode: shell-script
paulo@0 109 # sh-indentation: 2
paulo@0 110 # End:
paulo@0 111 # mkinstalldirs ends here