#!/bin/sh
#
# $Id: imsasm,v 1.9.2.2 2004/04/02 00:50:33 shum Exp $
# Copyright (c) 2000 Sun Microsystems, Inc. All Rights Reserved.
# All Rights Reserved.
#
# imsasm resides in <msg.RootPath>/lib/msg and there 
# is a symbolic link to it from /usr/lib/nsr
#
# This shell script implements an "external ASM". For each name
# passed on the command line we invoke imsbackup to generate a
# data stream that is fed to nsrfile which turns it into
# a save stream. On recover we do the reverse, invoke nsrfile
# to read the save stream and turn it back into a data stream
# that is fed to imsrestore.
#

#
# Messaging variables
#
SERVERROOT=<msg.RootPath>
MSBIN=${SERVERROOT}/sbin
CONFIGROOT=${SERVERROOT}/config
LD_LIBRARY_PATH=${SERVERROOT}/lib
export CONFIGROOT LD_LIBRARY_PATH

#
# Print on stderr
#
asm_echo() {
  echo "$*" >&2
}

#
# Print an error message
#
asm_error() {
  asm_echo "$Myname: $*"
}

#
# Print usage message
#
asm_usage() {
  asm_echo "Usage: imsasm -s [-benov] [-ix] [ -t time ] [ -f proto ] [ -p ppath ] file..."
  asm_echo "       imsasm -r [-dnv] [ -i {nNyYrR} ] [ -m <src>=<dst> ] -z suffix [ -p path ] file..."
  asm_echo "       imsasm -c [-nv] [ -p path ] file..."
  exit 1
}

#
# dirname function
#
# prints a non-NULL directory name for the given file
#	SunOS made dirname optional!!!
#
dirname() {
	expr X"${1}" : X'\(.*\)/.*' \| X"${1}" : X'\(/\)[^/]*$' \| '.'
}

#
# Begining of main shell program
#
asm_echo "##########################################################"
asm_echo $0 $*
#
# Process arguments
#
# Sets the following shell variables:
# Exec_path= 	Path for nsrfile
# Files=	List of files to operate on
# Mode=		"SAVE", "RECOVER", or "COUNT"
#
Myname=imsasm
Exec_path=/usr/bin/nsr
Files=
Mode=
Verbose=false
opt="-N imsasm"
path=.

while getopts i:f:z:t:m:p:benovdsrc option
do
  case "$option" in
    i|f|z|t|m)
        opt="$opt -$option $OPTARG"
        ;;
    b|e|n|o|d)
        opt="$opt -$option"
        ;;
    p)
       path="$OPTARG"
       ;;
    v)
       Verbose=true
       opt="$opt -$option"
       ;;
    c)
       if [ "$Mode" = RECOVER -o "$Mode" = SAVE ]; then
         asm_usage
       fi
       Mode=COUNT
       opt="$opt -c"
       ;;
    r) 
       if [ "$Mode" = COUNT -o "$Mode" = SAVE ]; then
         asm_usage
       fi
       Mode=RECOVER
       opt="$opt -r"
       ;;
    s) 
       if [ "$Mode" = RECOVER -o "$Mode" = COUNT ]; then
         asm_usage
       fi
       Mode=SAVE
       opt="$opt -s"
       ;;
    \?)
        asm_usage
        ;;
  esac
done

shift `expr $OPTIND - 1`
Files="$@"

if [ "$Mode" = "" ]; then
  asm_error "No mode specified, use -s or -r"
  asm_usage
fi

if [ $Mode = SAVE -a "$Files" = "" ]; then
  asm_error "Must specify at least one file with -s flag"
  asm_usage
fi

Nsrfile_args=$opt

if [ $Mode = RECOVER ]; then
  if [ "$Verbose" = true ]; then
    asm_echo $Exec_path/nsrfile -F -C "$MSBIN/imsrestore -f- -cy" $Nsrfile_args -p "$path" $Files
  fi
  $Exec_path/nsrfile -F -C "$MSBIN/imsrestore -f- -cy" $Nsrfile_args -p "$path" "$@"
else # $Mode = SAVE or $Mode = COUNT
  if [ "$Verbose" = true ]; then
    asm_echo $Exec_path/nsrfile -C "$MSBIN/imsbackup -n $path -f- -- %" $Nsrfile_args -p "$path" "$Files"
  fi
  $Exec_path/nsrfile -C "$MSBIN/imsbackup -n $path -f- -- %" $Nsrfile_args -p "$path" "$@"
fi

