#!/bin/sh

# Replacement for `mkdir -p' which is not supported on all systems.
# Based on mkinstalldirs --- make directory hierarchy, created: 1993-05-16
#
# Authors: Noah Friedman <friedman@prep.ai.mit.edu>
#          Frank Heckenbach <frank@pascal.gnu.de>
#
# Public domain

mode=""
if [ x"$1" = x"-m" ]; then
  mode="$2"
  shift
  shift
fi

if [ $# = 0 ]; then
  echo "Usage: `basename "$0"` [-m MODE] directories" >&2
  echo "  -m MODE  Set the mode of created directories to mode" >&2
  exit 1
fi

errstatus=0

for file
do
   set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'`
   shift

   pathcomp=
   for d
   do
     pathcomp="$pathcomp$d"
     case "$pathcomp" in
       -* ) pathcomp=./$pathcomp ;;
     esac

     if test ! -d "$pathcomp"; then
#        echo "mkdir $pathcomp" 1>&2

        mkdir "$pathcomp" || lasterr=$?

        if [ x"$mode" != x ]; then
          chmod "$mode" "$pathcomp" || lasterr=$?
        fi

        if test ! -d "$pathcomp"; then
          errstatus=$lasterr
        fi
     fi

     pathcomp="$pathcomp/"
   done
done

exit $errstatus
