# SccsId[] = "%W% (USL function) %G%"
              ME_name="MKDIR_EXIT_ERR"
              if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
                 [ ."`set|egrep '^$ME_name\(\)\{$'`" != . ] && ME_loaded=1
              else # Korn or Bash shell and function already loaded?
                 if [ `expr "\`uname -s\`" : "[Ll][Ii][Nn][Uu][Xx]"` -eq 0 ]; then
                    [ ."`typeset +f|awk '/^'$ME_name'[=\(]?/'`" != . ] && ME_loaded=1
                 else # Linux
                    [ ."`typeset -F|awk '/^'$ME_name'[=\(]?/'`" != . ] && ME_loaded=1
                 fi
              fi
              if [ 0${ME_loaded} -eq 0 ]; then
              #----------------------------------------------------------------------#
              MKDIR_EXIT_ERR() # Function documentation located at bottom.           #
              #----------------------------------------------------------------------#
              { [ ."${AWK}" = . ] && { { [ -x /usr/bin/nawk ] && AWK=/usr/bin/nawk; } \
                                  ||   { [ -x /bin/gawk     ] && AWK=/bin/gawk    ; } \
                                  ||   { [ -x /usr/bin/awk  ] && AWK=/usr/bin/awk ; }; }

                if [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi

                . $SHLIB/email_msg.sh # Calls $SHLIB/exit.sh

                #------------------------------------------------------------#
                # If the following variables are not set, use these defaults.#
                #------------------------------------------------------------#
                : ${script_name:=`basename $0`}
                : ${name_root:=`echo $script_name|$AWK '{sub(/^\.+/,"");sub(/\..*/,"");print}'`}
                : ${logger_p="user.err"}
                : ${logger_t="$LOGNAME[$$]"}
                : ${sp:="                    "}
                : ${tmp:=/var/tmp}

                if [ ."$log" = . ]; then
                   log=$name_root.log
                   [ ."$teelog" = . ] && teelog="cat"
                fi

                ME_ID="$script_name($ME_name)"

                #--------------------------------------------------------------------#
                #               Process function options and arguments               #
                #               --------------------------------------               #
                # We use while-loop instead of getopts because -p (logger priority)  #
                # option and -t (logger tag option) may or may not have an optarg    #
                # (the user may just want the defaults).                             #
                #--------------------------------------------------------------------#
                ME_options="lptm"
                ME_opt_l=0
                ME_opt_p=0
                ME_opt_t=0
                ME_pri="$logger_p" # $logger_p and $logger_t are assigned defaults
                ME_tag="$logger_t" # within library defaults.sh.
                ME_opts=""
                ME_args=""
                while [ ."$1" != . ]
                do
                   #-------------------------------------------------------------#
                   # If we have an option list (e.g. "-xyz") change that list to #
                   # individual "-x -y -z" options.  Else, just pass the option  #
                   # along.                                                      #
                   #-------------------------------------------------------------#
                   ME_n=`expr "$1". : '-.*'`
                   if [ $ME_n -gt 0 ]; then
                      if [ $ME_n -eq 2 ]; then
                         ME_optargs="$1"
                      else
                         ME_optargs=`echo "$1"|sed 's/[a-zA-Z]/-& /g;s/--*/-/'`
                      fi

                      for ME_optarg in $ME_optargs
                      do
                         case $ME_optarg in
                           -l ) ME_opt_l=1
                                . $SHLIB/write_err_to_syslogs.sh
                                ;;
                           -p ) if [ $ME_opt_l -eq 1 -a $ME_opt_p -eq 0 ]; then
                                   ME_opt_p=1
                                   if [ -`expr "$2" : '\(\-\)['$ME_options']'` != "--" ]; then
                                      ME_pri="$2"
                                      shift
                                   fi
                                else
                                   ME_opts="$ME_opts $ME_optarg"
                                fi
                                ;;
                           -t ) if [ $ME_opt_l -eq 1 -a $ME_opt_t -eq 0 ]; then
                                   ME_opt_t=1
                                   ME_tag="$2"
                                   if [ -`expr "$2" : '\(\-\)['$ME_options']'` != "--" ]; then
                                      ME_tag="$2"
                                      shift
                                   fi
                                else
                                   ME_opts="$ME_opts $ME_optarg"
                                fi
                                ;;
                           -m ) # This is mkdir's "mode" option, so just pass it along.
                                ME_opts="$ME_opts $ME_optarg $2"
                                shift
                                ;;
                         esac
                      done # for ME_optarg in $ME_optargs
                   else
                      [ ."$ME_args" != . ]       \
                        && ME_args="$ME_args|$1" \
                        || ME_args="$1"
                   fi
                   shift
                done # while [ ."$1" != . ]
                ME_opts=`expr "$ME_opts" : ' *\(.*\)'` # Strip leading blanks
                ME_args=`expr "$ME_args" : '|*\(.*\)'` # Strip leading stiles

                #--------------------------------------------------------------#
                # If we have sufficient arguments, then attempt mkdir command. #
                #--------------------------------------------------------------#
                ME_status=0
                ME_cmd="mkdir $ME_opts"

                if [ ."$ME_args" != . ]; then
                   echo "$ME_args"|awk -F\| '{for (i=1;i<=NF;i++) print $i}' \
                    | while read ME_dir
                      do
                         echo "`date '+%Y-%m-%d %T'` $ME_cmd \"$ME_dir\"" | $teelog
                         ME_stderr=`$ME_cmd "$ME_dir" 2>&1`
                         ME_status=$?
                         if [ $ME_status -ne 0 ]; then
                            ME_m1="'$ME_cmd \"$ME_dir\"'"
                            ME_m2="ERROR=$ME_stderr."
                            ME_m3="$script_name terminated."
                            EMAIL_MSG "FATAL ERROR: $ME_ID" \
                              "${sp}$ME_m1"                 \
                              "${sp}$ME_m2"                 \
                              "${sp}$ME_m3"

                            if [ $ME_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                               WRITE_ERR_TO_SYSLOGS -p "$ME_pri" -t "$ME_tag" \
                                 "ABORT: $ME_ID $ME_m1 $ME_m2 $ME_m3"
                            fi
                            EXIT 1
                         fi # if [ $ME_status -ne 0 ]; then
                      done # while read ME_dir
                   return 0
                fi # if [ ."$ME_args" != . ]; then

                #----------------------------------------------------------------#
                # Reaching this point means we have an insufficient args error.  #
                #----------------------------------------------------------------#
                ME_m1="Insufficient args."
                ME_m2="$script_name terminated."
                EMAIL_MSG "FATAL ERROR (Function): $ME_ID"                   \
                  "${sp}$ME_m1\n"                                            \
                  "${sp}Usage: $ME_name [-l [-p priority] [-t tag]]"         \
                  "${sp}                      mkdir_options dir [...]\n"     \
                  "${sp}                 -l = Notify via logger command"     \
                  "${sp}                      (default is false)"            \
                  "${sp}                 -p = Logger priority string"        \
                  "${sp}                      (default is 'user.err')"       \
                  "${sp}                 -t = Logger tag string"             \
                  "${sp}                      (default is '$LOGNAME[$$]')\n" \
                  "${sp}Option notes: Option (-l) MUST precede -p and -t."   \
                  "${sp}$ME_m2"
                if [ $ME_opt_l -eq 1 ]; then # Logger option? (1 = yes)
                   WRITE_ERR_TO_SYSLOGS -p "$ME_pri" -t "$ME_tag" \
                     "ABORT: $ME_ID $ME_m1 $ME_m2"
                fi

                EXIT 1
              } # "ME_" prefix identifies this function's local variables.
              fi

              #======================================================================#
              #                       D O C U M E N T A T I O N                      #
              #======================================================================#
              #                                                                      #
              #      Author: Bob Orlando (Bob@OrlandoKuntao.com)                     #
              #                                                                      #
              #        Date: January 29, 1997                                        #
              #                                                                      #
              #  Program ID: mkdir_exit_err.sh                                       #
              #                                                                      #
              #       Usage: MKDIR_EXIT_ERR [-l [-p priority] [-t tag]] \            #
              #                                   mkdir_options dir [...]            #
              #                                                                      #
              #                              -l = Notify via logger command          #
              #                                   (default is false)                 #
              #                              -p = Logger priority string             #
              #                                   (default is "user.err")            #
              #                              -t = Logger tag string                  #
              #                                   (default is "$LOGNAME[$$]")        #
              #                                                                      #
              #              Option notes: Option (-l) MUST precede -p and -t.       #
              #                            'UNIX command options' MUST follow        #
              #                            function options.  If command option      #
              #                            is -l, -p, or -t, and you've specified    #
              #                            function option -l, then you must also    #
              #                            supply both function options, -p and -t.  #
              #                                                                      #
              #     Purpose: Make directories with error checking and reporting      #
              #              via email and logger, and exiting on error.             #
              #                                                                      #
              # Description: This function makes an entire directory tree, exiting   #
              #              on error with notification via the EMAIL_MSG library    #
              #              function.  Optionally (-l), the user may have the error #
              #              logged in /var/adm/messages via the logger command.     #
              #              Logger -p and -t options and option arguments also      #
              #              may be provided (both have defaults if omitted).        #
              #                                                                      #
              #     Globals: No global variables assigned from this function.        #
              #              "ME_" prefix identifies local function variables.       #
              #                                                                      #
              #       Calls: EMAIL_MSG and EXIT library functions.                   #
              #              Optionally, WRIME_ERR_TO_SYSLOGS is also called.        #
              #                                                                      #
              # Exit_status: Exits with failure (1) on error, else returns 0.        #
              #                                                                      #
              #     Notes:   .....................................................   #
              #              .....................................................   #
              #                                                                      #
              #    Modified: 2004-04-02 Bob Orlando                                  #
              #                 v1.15 * Expand $AWK testing and assignment.          #
              #                                                                      #
              #              2004-03-03 Bob Orlando                                  #
              #                 v1.14 * Change set|egrep|awk to just set|egrep.      #
              #                                                                      #
              #----------------------------------------------------------------------#
            
Artificial Intelligence is no match for natural stupidity.
©Copyright Bob Orlando, 1997-2011
All rights reserved.
http://www.OrlandoKuntao.com
E-mail: Bob@OrlandoKuntao.com
Last update: Jan. 26, 2011
by Bob Orlando