#!/bin/sh
# @(#)$Mu: tools/mutdirhier,v 1.1 $

##
## initialize
##
me=`basename $0`
version='0.5(0.1)'

##
## check for help first so we don't have to think about it
## later
##
useline="$me [-xXhH] [-v] [-V] paths"
if [ X"$1" = X-x -o X"$1" = X-X -o X"$1" = X-h -o X"$1" = X-H ]; then
	cat <<_DONE_DONE_DONE_
$useline
	mkdir whole directory trees.  If you have mkdirhier from
	the X distribution or a recent GNU mkdir (i.e. one that
	groks the -p option), then use them instead since this
	script is just a quick hack.

	-x,-X,-h,-H	Display this explanation and exit.
	-V		Display version and exit.
	-v		Be verbose (on stderr).
	paths		The directories to make.  Note that these
			must be full path names (i.e. they must
			start with '/').
_DONE_DONE_DONE_
	exit 0
fi

##
## now check for a version request
##
if [ X"$1" = X-V ]; then
	echo "$me version $version"
	exit 0
fi

##
## deal with the command line options
##
fini="no"
verbose="no"
while [ $# -gt 0 -a $fini != "yes" ]; do
	case X"$1" in
	X-v)	verbose="yes"
		shift;;
	X--)	fini="yes"
		shift;;
	X-*)	echo "$me : unknown switch '$1'"
		exit 1;;
	X*)	fini="yes"
		;;
	esac
done

##
## make sure we've got the rest of the args we need
##
if [ $# -lt 1 ]; then
	echo "$me : what do you want me to mkdir?"
	exit 1
fi

##
## start looping through the directories
##
for d in "$@"; do
	sofar=""
	parts=`echo $d | awk -v FS='//*' '{for(i=1;i<=NF;++i){print $i}}'`
	for p in $parts; do
		sofar="$sofar/$p"
		[ $verbose = "yes" ] && echo "$me : attempting $sofar" 1>&2
		if [ ! -d $sofar ]; then
			mkdir $sofar
			status=$?
			if [ $status -ne 0 ]; then
				echo "$me : failure"
				exit $status
			fi
		fi
	done
done
