#!/bin/sh
#
# NAME
#        mkdirtree - create a directory tree.
#
# SYNOPSIS
#        mkdirtree directory [mode]
#
# DESCRIPTION
#        mkdirtree creates an entire directory tree with one command
#        Directory levels that already exists will be silently ignored,
#        and all other directory levels will be created.
#        "directory" must be absolute, not relative.
#        If "mode" is given all created directorys will be chmod'ed
#        to "mode", or else the mode will be set to 755.
#
# RETURN VALUE
#        0 for success
#        1 if any of the directorylevels can't be created and doesn't
#          already exist.
#

tmp=""
for sub in `echo $1 | sed 's|/| |g'` ; do
	tmp="$tmp/$sub"
	if [ -d $tmp ]; then
		continue
	else
		if [ -e $tmp ]; then
			echo "There is already a file named $tmp"
			echo "Can't create directory with the same name!"
			exit 1
		fi
	fi
	if mkdir "$tmp" >/dev/null 2>&1; then
		if [ -n "$2" ] ; then
			chmod "$2" "$tmp" >/dev/null 2>&1
		else
			chmod 755 "$tmp" >/dev/null 2>&1
		fi
	else
		echo "The directory $tmp does not exist and could not be created."
		exit 1
	fi
done

exit 0
