#! /bin/sh
#
# @(#)lmasm	1.5 02/13/02 SMI
# Copyright (c) 1999-2000 Sun Microsystems, Inc. All Rights Reserved.
# All Rights Reserved.
#

PATH=/usr/sbin:/usr/bin
export PATH
basedir=`echo $0 | sed "s-/lib/nsr/lmasm--"`
PATH=${PATH}:${basedir}/lib/nsr:${basedir}/bin/nsr:${basedir}/bin
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/opt/lanman/lib
export 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 "Incorrect arguments for lmasm."
  exit 1
}

# Process arguments
#
# Sets the following shell variables:
# Exec_path= 	Path for ASM executables
# Files=	List of files to operate on
# Mode=		"s", "r", or "c"
#
Myname=`basename $0`
Exec_path=`dirname $0`
Files=
Mode=
Verbose=
pnp=`pwd`/
export pnp
opt=

while getopts i:f:z:t:m:p:benvdsrc option
do
  case "$option" in
    i|f|z|t|m)
        opt="$opt -$option $OPTARG"
        ;;
    b|e|n|d)
        opt="$opt -$option"
        ;;
    p)
	# We always set pnp=`pwd`/
	# so just ignore this arg
	;;
    v)
	Verbose=true
	opt="$opt -v"
	;;
    c|r|s)
	if [ "$Mode" ]; then
	  asm_usage
	fi
	Mode=$option
	;;
    \?)
	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 = s -a "$Files" = "" ]; then
  asm_error "Must specify at least one file with -s flag"
  asm_usage
fi

if [ $Mode = r ]; then
  lmasmd &
  sleep 1
  eval 'lmnsr.restore.wrap |' \
    nsrfile -r -N "$Myname" -C '"lmnsr.restore.unwrap . %"' "$opt" "$@"
  sleep 1
  lmnsr.lmasmd.stop

else # $Mode = s or $Mode = c
  echo "$ASMINHERIT" | grep -v $Myname > /tmp/$$
  ASMINHERIT="`cat /tmp/$$`"'
'
  rm -f /tmp/$$
  export ASMINHERIT
  eval uasm -$Mode "$opt" "$@" | lmnsr.save "$pnp"
fi
