
#!/usr/bin/ksh
# Listing 2: buc_server.ss: Back Up Check Utility, server:
# Execute the oncheck utility and find out when the last backup
# completed.  Build a datatime string, insert it into the remote_variables
# table, and return to buc_client.ss.
# This script is called by buc_client.ss.
# this shell script:
# 1) Initialize variables, check informix user, set TZ, etc.
# 2) Executes the oncheck -pr utility retrieving backup datetime.
# 3) Get the current datetime.
# 4) Format a string with the backup and current datetimes.
# 5) Insert formatted string into the remote_values table.
# 6) Return to buc_client.ss.
# author: Ed Schaefer
# Date: 05/05/2001

export INFORMIXDIR=/usr/informix
export PATH=/bin:/usr/bin:/usr/ucb:/etc:$INFORMIXDIR/bin:/usr/local/bin

DBNAME="testdb" # database name
INTERFACE_CMD="dbaccess"
DBCOMMAND="$INTERFACE_CMD $DBNAME"

# limit to the effective informix user
[ $(id|sed -e 's,^[^(]*(,,' -e 's,).*$,,' -e 1q) != "informix" ] && echo "Only \
user informix executes $0" && exit 1 

# set the time zone.  Informix commands executed remotely such as 
# oncheck won't work if the TZ isn't correct.  Solaris 7 specific
export TZ=`grep TZ= /etc/TIMEZONE|cut -d= -f2`

if [ "$#" -ne 1 ]; then
   echo "$0 needs an argument. Terminating!"
   exit 1
fi
rem_key=$1

# this function takes a date argument 
# in the form MM/DD/YYYY and returns
# YYYY-MM-DD
function return_datetime {
   set - $(echo $1|tr '/' ' ' )
   echo $3-$1-$2
}

# execute oncheck -pr and find the line:
#    Real Time Archive              02/25/01 22:47:12
#  the last archive date and time is the fifth and sixth fields
ret_str=$($INFORMIXDIR/bin/oncheck -pr|grep -i "real time" | tr "[A-Z]" "[a-z]" |awk ' { 
if ($1 == "real" && $2 == "time")
   {
   printf("%s  %s", $5, $6)
   exit # quit after finding the "real time" string
   }
}')

date1=$(echo $ret_str|cut -f 1 -d " ") # date
time1=$(echo $ret_str|cut -f 2 -d " ") # time
date1=$(return_datetime $date1)

# build a time string for the backup time and the current date time
totaltimestr="$date1 $time1 $cut_dt $(date +"%Y-%m-%d %T")"

# and insert it into the database
$DBCOMMAND > /dev/null 2>&1 <<EDS
   INSERT INTO remote_values VALUES ("$rem_key", "$totaltimestr");
EDS

