#! /bin/sh

usage() {
cat << END
A poor man's mkdir -p.  Useful on old systems that don't have the -p option.
It won't work right if a target directory includes whitespace; tough luck.
Options:
    -h     print this info, then exit.
    -oxxx  chown xxx any created directory (put no space between -o and xxx).
    -gxxx  chgrp xxx any created directory (put no space between -g and xxx).
    -pxxx  chperm xxx any created directory (put no space between -p and xxx).
END
}

owner=
group=
perm=
while [ $# -gt 0 ] ; do
    case "$1" in
    -h ) usage ; exit 0 ;;
    -o* ) owner=`expr "$1" : '-o\(.*\)'` ; shift
    	case "$owner" in "" ) usage ; exit 1 ;; esac
	;;
    -g* ) group=`expr "$1" : '-g\(.*\)'` ; shift
    	case "$group" in "" ) usage ; exit 1 ;; esac
	;;
    -p* ) perm=`expr "$1" : '-p\(.*\)'` ; shift
    	case "$perm" in "" ) usage ; exit 1 ;; esac
	;;
    -- ) break ;;
    -* ) echo "unknown option $1" ; usage ; exit 1 ;;
    * ) break ;;
    esac
done

for longpath in ${1+"$@"} ; do
    case "$longpath" in
    /* ) dir="" ;;
    * )  dir=. ;;
    esac
    for d in `echo "$longpath" | sed -e 'sX/X Xg'` ; do
	dir="$dir/$d"
	test -d $dir || {
	    mkdir $dir || exit
	    case "$owner" in "" ) ;; * ) chown $owner $dir ;; esac
	    case "$group" in "" ) ;; * ) chgrp $group $dir ;; esac
	    case "$perm" in "" ) ;; * ) chmod $perm $dir ;; esac
	}
    done
done
