#! /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-2019 amaury darsch                                    -
# ----------------------------------------------------------------------------

# the source directory list
srcdir=

# ----------------------------------------------------------------------------
# - 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 "error: $1"
    exit 1
}

mhdir () {

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

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

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

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

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

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

# loop in list
for d in $srcdir; do
    mhdir $d
done

# that's all folks
exit 0
