#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/bin/cdat/types/snap/snap.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# 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 

phase_check()
{
    # Only LPAR and VIOS are supported for the snap collect
    [ "$CDAT_TYPE" = 'LPAR' -o "$CDAT_TYPE" = 'VIOS' ] || {
        mlog -1 "The snap collect type is not supported on $CDAT_TYPE"
        exit 1	# IGNORE
    }
}

phase_execute()
{
    # Run the /usr/sbin/snap command with specified options.
    # We override the output directory to $SNAPDIR by adding the -d
    # option after the options specified by the user (last wins.)
    remote_cmd "yes n | /usr/sbin/snap $opts -d $SNAPDIR"
    [ $? -ne 0 ] && {
	mlog -1 "The snap command failed on $CDAT_HOST"
	exit 128
    }
    # Create the $SNAPDIR/snap.pax.Z archive.
    remote_cmd "/usr/sbin/snap -c -d $SNAPDIR"
    [ $? -ne 0 ] && {
	mlog -1 "Cannot create $SNAPDIR/snap.pax.Z on $CDAT_HOST"
	exit 128
    }
}

phase_grab()
{
    # Retrieve the $SNAPDIR/snap.pax.Z archive from the remote node.
    get_file $SNAPDIR/snap.pax.Z .
    [ $? -ne 0 ] && {
	mlog -1 "Cannot retrieve $SNAPDIR/snap.pax.Z from $CDAT_HOST"
	exit 128
    }
}

phase_clean()
{
    # Remove the $SNAPDIR directory from the remote node.
    remote_cmd "yes y | /usr/sbin/snap -r -d $SNAPDIR"
    remote_cmd "rm -fr $SNAPDIR"
}

PATH=$CDAT_SRVC_DIR:${PATH}

# We use the /tmp/cdat_snap directory instead of the defaut /tmp/ibmsupt
# as it causes problems with RBAC if the /tmp/ibmsupt directory already
# exists but is not owned by the cdat user.
SNAPDIR=/tmp/cdat_snap

opts="$*"
[ -z "$opts" ] && opts="-a"

case $CDAT_PHASE in
check)
    phase_check
    ;;
execute)
    phase_execute
    ;;
grab)
    phase_grab
    ;;
clean)
    phase_clean
    ;;
esac

exit 0
