# SccsId[] = "%W% (USL function) %G%"
              CTS_name="CHECK_TAPE_STATUS"
              if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
                 [ ."`set|egrep '^$CTS_name\(\)\{$'`" != . ] && CTS_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 '/^'$CTS_name'[=\(]?/'`" != . ] && CTS_loaded=1
                 else # Linux
                    [ ."`typeset -F|awk '/^'$CTS_name'[=\(]?/'`" != . ] && CTS_loaded=1
                 fi
              fi
              if [ 0${CTS_loaded} -eq 0 ]; then
              #----------------------------------------------------------------------#
              CHECK_TAPE_STATUS() # 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`}
                : ${sp:="                    "}
                CTS_ID="$script_name($CTS_name)"

                if [ ."$1" = . ]; then
                   EMAIL_MSG "SEVERE ERROR (Function): $CTS_ID" \
                     "${sp}Insufficient args."                  \
                     "${sp}Usage: $CTS_name tape_dev."          \
                     "${sp}       Where tape-dev = /rmt/dev/0h (for example)."
                   return 2
                fi

                $AWK -v device=$1 -v sp="$sp" \
                  'BEGIN \
                   {
                     mt_cmd = "mt -f "device" status"
                     sleep_secs = 60
                     for (i=0; i < 10; i++)
                     {
                      "date \"+%Y-%m-%d %T\" 2>&1" | getline yyyymmddhhmmss
                       close("date \"+%Y-%m-%d %T\" 2>&1")
                       print yyyymmddhhmmss, mt_cmd

                       #-------------------------------------------#
                       # Reissue mt command and print the output.  #
                       #-------------------------------------------#
                       mt_status = offline = 0
                       while (mt_cmd" 2>&1" | getline mt_output)
                       {
                         print sp""mt_output
                         if (     mt_output ~ /[Nn]o such file or directory/)
                           mt_status = 1
                         else if (mt_output ~ /[Nn]o such device or address/)
                           mt_status = 1
                         else if (mt_output ~ /[Ww]rite protected/)
                           mt_status = 1
                         else if (mt_output ~ /[Nn]o tape loaded or drive offline/)
                           offline   = 1
                       }
                       close(mt_cmd" 2>&1")

                       if (mt_status == 1)
                         exit mt_status
                       else if (offline == 1)
                       { #---------------------------------------------------#
                         # Only snooze and retry if offline (perhaps we are  #
                         # waiting for the device to rewind or be loaded).   #
                         #---------------------------------------------------#
                         system("sleep "sleep_secs)
                         sleep_secs += 30 # Sleep progressively longer
                         continue
                       }

                       exit mt_status
                     } # for (i=0; i < 10; i++)
                   } # E.O.BEGIN'

                #--------------------------------------------------------#
                return $? # Success (0), means the tape is OK, or        #
                #           failure (1) means that it failed the check.  #
                #--------------------------------------------------------#
              } # "CTS_" prefix identifies this function's local variables.
              fi

              #======================================================================#
              #                       D O C U M E N T A T I O N                      #
              #======================================================================#
              #                                                                      #
              #      Author: Bob Orlando                                             #
              #                                                                      #
              #        Date: December 14, 1996                                       #
              #                                                                      #
              #  Program ID: check_tape_status.sh                                    #
              #                                                                      #
              #       Usage: CHECK_TAPE_STATUS tape-dev  (e.g. /rmt/dev/0h).         #
              #                                                                      #
              #     Purpose: Check tape device status with error checking.           #
              #                                                                      #
              #     Globals: No global variables assigned from this function.        #
              #              "CTS_" prefix identifies local function variables.      #
              #                                                                      #
              # Exit_status: 0  = success                                            #
              #              1  = failure                                            #
              #              >1 = Function error (e.g. insufficient args)            #
              #                                                                      #
              #       Calls: EMAIL_MSG, CP_DEV_NULL_EXIT_ERR, and CHMOD_EXIT_ERR     #
              #              library functions.                                      #
              #                                                                      #
              #       Notes: .....................................................   #
              #              .....................................................   #
              #                                                                      #
              #    Modified: 2004-04-02 Bob Orlando                                  #
              #                 v1.8  * Expand $AWK testing and assignment.          #
              #                                                                      #
              #              2004-03-03 Bob Orlando                                  #
              #                 v1.7  * Change set|egrep|awk to just set|egrep.      #
              #                                                                      #
              #----------------------------------------------------------------------#
            
Artificial Intelligence is no match for natural stupidity.
©Copyright Bob Orlando, 1996-2011
All rights reserved.
http://www.OrlandoKuntao.com
E-mail: Bob@OrlandoKuntao.com
Last update: Jan. 26, 2011
by Bob Orlando