#!/bin/sh
# mkdirhier --- make directory hierarchy

# Copyright (C) 1995 Noah S. Friedman

# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1992-01-10

# $Id: mkdirhier,v 1.7 1996/03/03 22:56:00 friedman Exp $

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# 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.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Commentary:

# David MacKenzie <djm@gnu.ai.mit.edu> reports that IFS=/ does not work on
# i386-sequent-dynix.

# Code:

# Name by which this script was invoked.
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`

# To prevent hairy quoting and escaping later.
bq='`'
eq="'"

usage="Usage: $progname {options} [directory-path {...}]

Options are:
-D, --debug                  Turn on shell debugging ($bq${bq}set -x$eq$eq).
-h, --help                   You're looking at it.
-v, --verbose                Print new directory names as they're made.
"

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
debug=
verbose=

# If --verbose is specified, fd 3 is duped to fd 1 instead.
exec 3> /dev/null

# Parse command line arguments.
# Make sure that all wildcarded options are long enough to be unambiguous.
# It's a good idea to document the full long option name in each case.
# Long options which take arguments will need a `*' appended to the
# canonical name to match the value appended after the `=' character.
while test $# != 0; do
  case "$1" in
    -D | --debug | --d* )
      debug=t
      shift
     ;;
    -h | --help | --h )
      echo "$usage" 1>&2
      exit 1
     ;;
    -v | --verbose | --v* )
      exec 3>&1
      shift
     ;;
    -- )     # Stop option processing
      shift
      break
     ;;
    -? | --* )
      case "$1" in
        --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
        * )     arg="$1" ;;
      esac
      exec 1>&2
      echo "$progname: unknown or ambiguous option $bq$arg$eq"
      echo "$progname: Use $bq--help$eq for a list of options."
      exit 1
     ;;
    -??* )
      # Split grouped single options into separate args and try again
      optarg="$1"
      shift
      set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` ${1+"$@"}
      shift
     ;;
    * )
      break
     ;;
  esac
done

case "$debug" in t ) set -x ;; esac

errstatus=0

for file in ${1+"$@"}; do
  # If filename begins with a `-', put `./' in front of it to avoid
  # confusing mkdir.
  case "$file" in
    -* ) file="./$file" ;;
  esac

  oIFS="$IFS"
  IFS='/'
  set fnord $file
  IFS="$oIFS"
  shift

  case "$file" in
    /* ) pathcomp='/'  ;;
    -* ) pathcomp='./' ;;
    *  ) pathcomp=''   ;;
  esac

  for d in ${1+"$@"}; do
    pathcomp="$pathcomp$d"

    if test ! -d "$pathcomp" ; then
      echo "$pathcomp" 1>&3
      mkdir "$pathcomp" || errstatus=$?
    fi

    pathcomp="$pathcomp/"
  done
done

exit $errstatus

# mkdirhier ends here