#!/bin/ksh
#
# hot_bck_1f.sh
# =============
#
# This script is provided by Database Specialists, Inc. 
# (http://www.dbspecialists.com) for individual use and not for sale. 
# Database Specialists, Inc. does not warrant the script in any way 
# and will not be responsible for any loss arising out of its use.
#
# Your feedback is welcome! Please send your comments about this script
# to scriptfeedback@dbspecialists.com
#
# This shell script copies one database file to the backup directory,
# either by a conventional copy or by starting a background compress
# that reads from a named pipe and copies the database file to the named
# pipe.
# 
# This script is called by the hot_bck_1ts.sh script, and should not be 
# called directly.
#
# Usage: hot_bck_1f.sh database_file backup_directory
#
# Version 06-16-2005
#

#
# Set up.
#

if [ "$#" != "2" ]
then
  echo "`basename $0`: bad arg count" 1>&2
  exit 1
fi

NAMEDPIPE="/tmp/bck$$.fifo"
COMPRESS_ERR="/tmp/bck$$.err"

DB_FILE="$1"
BCK_DIR="$2"
case "$COMPRESS" in
  "YES") BCK_FILE="${BCK_DIR}/`echo $1 | tr / ^`$COMPRESS_SUFFIX" ;;
  *)     BCK_FILE="${BCK_DIR}/`echo $1 | tr / ^`" ;;
esac

rm -f $NAMEDPIPE $COMPRESS_ERR "$BCK_FILE"
if [ "$?" != "0" ]
then
  echo "Error occurred on rm -f $NAMEDPIPE $COMPRESS_ERR $BCK_FILE" 1>&2
  exit 1
fi

echo "`date +%H:%M:%S`  Copying file $DB_FILE"

if [ "$COMPRESS" = "YES" ]
then

  #
  # Create a named pipe and set up a background compress.
  #
  
  $MKNOD_PROGRAM $NAMEDPIPE p
  RETCODE="$?"
  if [ "$RETCODE" != "0" ]
  then
    rm -f $NAMEDPIPE
    exit $RETCODE
  fi
  
  $COMPRESS_PROGRAM < $NAMEDPIPE > "$BCK_FILE" 2> $COMPRESS_ERR &
  
  #
  # Invoke cp to copy the database file through the named pipe
  # into the backup directory as a compressed file.
  #
    
  cp "$DB_FILE" $NAMEDPIPE
  RETCODE="$?"
  if [ "$RETCODE" != "0" ]
  then
    rm -f $NAMEDPIPE $COMPRESS_ERR
    exit $RETCODE
  fi
  
  #
  # Check if the background compress encountered an error.
  #
  
  if [ -s $COMPRESS_ERR ]
  then
    cat $COMPRESS_ERR 1>&2
    rm -f $NAMEDPIPE $COMPRESS_ERR
    exit 1
  fi
  
  sleep 5
  rm -f $NAMEDPIPE $COMPRESS_ERR

else

  #
  # Compression is not desired. So just do a regular copy.
  #

  cp "$DB_FILE" "$BCK_FILE"
  RETCODE="$?"
  if [ "$RETCODE" != "0" ]
  then
    exit $RETCODE
  fi

fi

exit 0


