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