#! /bin/sh
# ----------------------------------------------------------------------------
# - afnix-mkdir                                                              -
# - create directory hierarchy                                               -
# ----------------------------------------------------------------------------
# - This program is  free software;  you can  redistribute it and/or  modify -
# - it provided that this copyright notice is kept intact.                   -
# -                                                                          -
# - This  program  is  distributed in the hope  that it  will be useful, but -
# - without  any   warranty;  without  even   the   implied    warranty   of -
# - merchantability  or fitness for a particular purpose. In not event shall -
# - the copyright holder be  liable for  any direct, indirect, incidental or -
# - special damages arising in any way out of the use of this software.      -
# ----------------------------------------------------------------------------
# - copyright (c) 1999-2007 amaury darsch                                    -
# ----------------------------------------------------------------------------

# directory list
dirlist=

# ----------------------------------------------------------------------------
# - local function always make life easier                                   -
# ----------------------------------------------------------------------------

# print a usage message
usage () {
    echo "usage: afnix-mkdir [options] dir"
    echo "       -h                 print this help message"
    exit 0
}

# print an error message
error () {
    echo $1
    exit 1
}

mkd () {

    # get the directory list
    dlist=`echo ":$1" | sed -e 's/^:\//#/;s/^://;s/\// /g;s/^#/\//'`
    dpath=

    # loop in the list of directory
    for d in $dlist; do
        # start to concanate again
	dpath=${dpath}${d}
        # create the directory if any
	if test ! -d "$dpath"; then
	    mkdir $dpath  > /dev/null 2>&1 || status=$?
	fi
        # add path and check again
	dpath=${dpath}/
	if test ! -d "$dpath"; then
	    exit $status
	fi
    done
}

# ----------------------------------------------------------------------------
# - parse options - this is where we really start                            -
# ----------------------------------------------------------------------------

lastopt=
for nextopt
do
    # assign the previous option argument
    if test -n "$lastopt"; then
	eval "$lastopt=\$nextopt"
	lastopt=
	continue
    fi

    # extract options
    case "$nextopt" in
    -*=*) argsopt=`echo "$nextopt" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) argsopt= ;;
    esac

    # process options now
    case "$nextopt" in
    -h | --help)             usage ;;
    -*)                      error "illegal option $nextopt" ;;
     *)                      dirlist="$dirlist $nextopt" ;;
    esac
done

# loop in list
for d in $dirlist ; do
    mkd $d
done

# that's all folks
exit 0
