#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_at.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1993,2003 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#@(#)14	1.9  src/bos/usr/lib/nim/methods/c_at.sh, cmdnim, bos720  10/1/03  17:19:50 

#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_at.sh
#		
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993, 1996
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="at command name"
OPTIONAL_ATTRS=""

#*---------------------------- c_at              ------------------------------
#
# NAME: c_at   
#
# FUNCTION:
#		schedules an at job 
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#       	tmp_files
#
# RETURNS: (int)
#		0							= no errors
#		>0							= failure
#
# OUTPUT:
#-----------------------------------------------------------------------------*/

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# initialize local variables
typeset c=""

# set parameters from command line
while getopts :a:qv c
do
    case ${c} in

        a)      # validate the attr ass
                parse_attr_ass "${OPTARG}"

                # include the assignment for use in this environment
                eval ${variable}=\"${value}\"
                ;;

        q)      # show attr info
                cmd_what
                exit 0
                ;;

        v)      # verbose mode (for debugging)
                set -x
                for i in $(typeset +f)
                do
                    typeset -ft $i
                done
                ;;

        \?) # unknown option
                error ${ERR_BAD_OPT} ${OPTARG}
                ;;
    esac
done

# check for missing attrs
ck_attrs

# add to tmp_files so file will be removed
tmp_files="${tmp_files} /tmp/at.out$$"

# lets verify that the cron daemon is running
# - start it
cron_up=`${PS} -u root | ${GREP} -q cron; echo $?`
if [ -z "$cron_up" ] || [ "$cron_up" != "0" ]
then
	/usr/sbin/cron >/dev/null 2>&1 &
fi

#
# schedule the specified command
#
${ECHO} ${command} ${name} | ${AT} -k -t ${at} 2>/tmp/at.out$$ || \
	error ${ERR_CMD} ${AT} `/usr/bin/cat /tmp/at.out$$`

#
# print out job number (second field from AT stdout)
#
${AWK} '{ print $2 }' /tmp/at.out$$ 2>/dev/null

#
# success
#
exit 0

