#!/usr/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/sbin/snap/snapsplit.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 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 
# @(#)57        1.2  src/bos/usr/sbin/snap/snapsplit.sh, cmdsnap, bos720 3/16/07 11:38:27
# COMPONENT_NAME: (cmdsnap) SNAP Splitting and Unspliting tool
#
# FUNCTIONS: snapsplit.sh
#
# ORIGINS: 27
#
# (C) COPYRIGHT International Business Machines Corp. 1991, 1994
# 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.
#
## [End of PROLOG]

# prepend to PATH to prevent unexpected aliasing of commands
export PATH="/usr/bin:/etc:/usr/sbin:$PATH"

usage() {
        echo "snapsplit [ -s <size> ] [ -H <machine name> ] [ -f <filename> ]\n"
        echo "snapsplit -u -T <timestamp> [ -H <machine name> ] ]\n"
	echo "Note: split size should be in Megabytes"
}

# Function: split
# Uses snap -b to split snap files. The output files can be identified by machine
# name and timestamp.
split() {
    # call the split command to handle the split
    /usr/bin/split -b $splitsize $snapfilesrc "$snapfiledest"
    if [ "$?" != 0 ]
    then
	err=$?
	echo "Error $err splitting the snap output file"
	exit $err
    fi

    echo "$snapfilescrc was split succesfully ... \n"
}

# Function: unsplit
# Restore a snap files that was split.
unsplit() {
    # the snapfiles that were split by the split command will be catted
    # to resconstitute original file.
    files=`ls $snapfiledest*`
    if [ -n "$files" ]
    then
	echo "Restoring . . ."
	cat $files > $snapfiledest
	snapsize=`ls -l $snapfiledest | awk -F ' ' '{print $5}'`
    fi

    # there weren't any file to cat
    if [ "$snapsize" = "" ]
    then
	echo "Did not find target snap files $snapfiledest* in directory."
	exit 7
    fi

    echo "$snapfilescrc was succesfully restored ... \n"
}

#initialize flags
unsplit=n
timeflg=n
nameflg=n

# initialize defaults
let splitsize=1024*1024
snapfilesrc=snap.pax.Z
machinename=`hostname | sed 's/\..*//'`

# process timestamp
timestamp=`date +"%D/%H/%M/%S" | sed 's/\///g'`

# process the parameter list.
set -- `getopt H:T:s:f:u $*`

if [ "$?" != 0 ]
then
    usage
    exit 1
fi

while [ "$1" != -- ]
do
  case "$1" in
      -H) machinename=$2
	  shift;shift;;
      -T) timestamp=$2
	  timeflg=y
	  shift;shift;;
      -s) let splitsize=$2*1024*1024
	  size=y
	  shift;shift;;
      -f) snapfilesrc=$2
	  nameflg=y
	  shift;shift;;
      -u) unsplit=y
	  shift;;
      *)  usage; exit 3
  esac
done

if [ "$unsplit" = y ] && [ -n "$size" ]
then
    echo "-u and -s are mutually exclusive"
    usage; exit 3
fi

if [ "$unsplit" = y ] && [ "$nameflg" = y ]
then
    echo "-u and -f are mutually exclusive"
    usage; exit 3
fi

if [ "$unsplit" = n ] && [ "$timeflg" = y ]
then
    echo "-T must be used with the -u option"
    usage; exit 3
fi

if [ "$unsplit" = y ] && [ "$timeflg" = n ]
then
    echo "-T must be used with the -u option"
    usage; exit 3
fi
   
# generate file name for snap files
snapfiledest="snap.$machinename.$timestamp.pax.Z"


if [ "$unsplit" = n ]
then
    split
else
    unsplit
fi

exit 0