#!/bin/sh
#
# Set TOOLBAR to yes if you want the MarkVision snap-in to place
# icons on the tool bar.
#
TOOLBAR=yes

#
# Set MENUBAR to yes if you want them MarkVision snap-in to add
# a menu item for MarkVision.
#
MENUBAR=yes

#
# Make sure that we are in the Lexmark OpenView Snap-in install
# directory.
# 

if [ ! -f ./install_mv4ov ] ; then
  echo "You must run install_mv4ov from the directory where it is installed."
  exit 1
fi

#
# Find the program whoami
#
if [ -x /usr/bin/whoami ] ; then
  WHOAMI=/usr/bin/whoami
else
  if [ -x /usr/ucb/whoami ] ; then
    WHOAMI=/usr/ucb/whoami
  else
    echo "Could not find whoami in /usr/bin or /usr/ucb/bin"
    exit 1
  fi
fi

#
# Set LANG to C if it is not set
#

if [ -z "$LANG" ] ; then
  LANG="C"
fi

#
# Only root is able to properly install this snap-in
#
if [ `$WHOAMI` != "root" ] ; then
  echo "This configuration script must be run by root."
  exit 1
fi

#
# Check for the OV_ enviromental variables
#
if [ "$OV_BIN" = "" ] ; then
  echo "Could not determine where OpenView is installed.\n"
  echo "You must have OpenView installed and you current enviroment must contain"
  echo "all the OV_ variables.\n"
  echo "Type:"
  echo "\nFor sh or ksh:\n\t\t. OpenView_install_dir/bin/ov.envvars.sh"
  echo "\nFor csh:\n\t\tsource OpenView_install_dir/bin/ov.envvars.csh"
  exit 1
fi

#
# Check for MarkVision for Unix
#
if [ ! -d "/usr/markvision/bin" ] ; then
  echo "MarkVision for Unix is not installed on this machine.  To use this"
  echo "snap-in, you must first install MarkVision for Unix."
  exit 1
fi

MARKVISION=/usr/markvision

#
# Lets make the new trapd.conf.lexmark and setSymbolStatus from
# their template files.  What we need to do is replace OV_BIN_PATH
# with the systems $OV_BIN path.
#

sed -e "s%OV_BIN_PATH%$OV_BIN%" trapd.conf.lexmark.template>trapd.conf.lexmark
sed -e "s%OV_BIN_PATH%$OV_BIN%" setSymbolStatus.template>setSymbolStatus

chmod 755 setSymbolStatus
mv setSymbolStatus $OV_BIN

#
# Add a new symbol to Openview for Lexmark Printers
#

DEVICE="$OV_SYMBOLS/$LANG/Device"
if [ ! -d "$DEVICE" ] ; then
  echo "Could not find OpenView Symbol directory for Device:"
  echo "Symbol directory = $OV_SYMBOLS"
  echo "Lang             = $LANG"
  $OV_BIN/ovstart
  exit 1
fi

cp Device_lexmark $DEVICE

DEVICE_BITMAPS="$OV_BITMAPS/$LANG/device"
if [ ! -d "$DEVICE_BITMAPS" ] ; then
  echo "Could not find OpenView Bitmap directory for device:"
  echo "Symbol directory = $OV_BITMAPS"
  echo "Lang             = $LANG"
  $OV_BIN/ovstart
  exit 1
fi

cp bitmaps/device/* $DEVICE_BITMAPS

#
# Load lexmark1.mib into OpenView
#
LEXMARK_MIB="$MARKVISION/etc/lexmark1.mib"
echo "Loading lexmark1.mib into OpenView"
$OV_BIN/xnmloadmib -load $LEXMARK_MIB -replace

STATUS=$?
if [ "$STATUS" -ne 0 ] ; then
  if [ "$STATUS" -eq 1 ] ; then
    echo "xnmloadmib: Invalid command line.";
    exit 1
  fi
  if [ "$STATUS" -eq 2 ] ; then
    echo "xnmloadmib: Syntax error detected in $LEXMARK_MIB.";
    exit 1
  fi
  if [ "$STATUS" -eq 5 ] ; then
    echo "xnmloadmib: Could not access $LEXMARK_MIB.";
    exit 1
  fi
  if [ "$STATUS" -eq 6 ] ; then
    echo "xnmloadmib: $LEXMARK_MIB is not a file.";
    exit 1
  fi
  if [ "$STATUS" -eq 7 ] ; then
    echo "xnmloadmib: Error accessing MIB Database.";
    exit 1
  fi
  if [ "$STATUS" -eq 8 ] ; then
    echo "xnmloadmib: Error creating MIB Database.";
    exit 1
  fi
  if [ "$STATUS" -eq 7 ] ; then
    echo "xnmloadmib: Error creating compiled form MIB Database.";
    exit 1
  fi
fi

#
# Add events here
#

$OV_BIN/xnmevents -load trapd.conf.lexmark

#
# Stop the OpenView daemons.
#

echo "Stopping all OpenView processes"
$OV_BIN/ovstop

# Add the Lexmark oid to OpenViews oid_to_type file
# Move the old oid_to_type to oid_to_type.orig.
# cat oid_to_type.orig and Lexmark's oid_to_type file
# to the new oid_to_type.
#

OID_TO_TYPE="$OV_CONF/oid_to_type"

if [ ! -r "$OID_TO_TYPE" ] ; then
  echo "Could not find OpenView oid_to_type file:"
  echo "Configuration directory = $OV_CONF"
  $OV_BIN/ovstart
  exit 1;
fi

grep "`cat oid_to_type`" $OID_TO_TYPE >>/dev/null

if [ $? -eq 1 ]  ; then
  mv $OID_TO_TYPE ${OID_TO_TYPE}.orig
  if [ $? -ne 0 ] ; then
    echo "Could not move:\n\t$OID_TO_TYPE to ${OID_TO_TYPE}.orig"
    $OV_BIN/ovstart
    exit 1;
  fi
  
  cat ${OID_TO_TYPE}.orig oid_to_type >$OID_TO_TYPE
  if [ $? -ne 0 ] ; then
    echo "Could not create new oid_to_type."
    mv ${OID_TO_TYPE}.orig $OID_TO_TYPE
    $OV_BIN/ovstart
    exit 1;
  fi
fi

#
# Add the Lexmark oid to OpenViews oid_to_sym file
# Move the old oid_to_sym to oid_to_sym.orig.
# cat oid_to_sym.orig and Lexmark's oid_to_sym file
# to the new oid_to_sym.
#

OID_TO_SYM="$OV_CONF/$LANG/oid_to_sym"

if [ ! -r "$OID_TO_SYM" ] ; then
  echo "Could not find OpenView oid_to_sym file:"
  echo "Configuration directory = $OV_CONF"
  echo "Language                = $LANG"
  $OV_BIN/ovstart
  exit 1;
fi

grep "`cat oid_to_sym`" $OID_TO_SYM >>/dev/null

if [ $? -eq 1 ]  ; then
  mv $OID_TO_SYM ${OID_TO_SYM}.orig
  if [ $? -ne 0 ] ; then
    echo "Could not move:\n\t$OID_TO_SYM to ${OID_TO_SYM}.orig"
    $OV_BIN/ovstart
    exit 1;
  fi
  
  cat ${OID_TO_SYM}.orig oid_to_sym >$OID_TO_SYM
  if [ $? -ne 0 ] ; then
    echo "Could not create new oid_to_sym."
    mv ${OID_TO_SYM}.orig $OID_TO_SYM
    $OV_BIN/ovstart
    exit 1;
  fi
fi

#
# Add the lexmark fields now
#

if [ ! -d "$OV_FIELDS/$LANG" ] ; then
  echo "Could not find OpenView fields directory:"
  echo "Fields directory = $OV_FIELDS"
  echo "Lang             = $LANG"
  $OV_BIN/ovstart
  exit 1
fi

cp lexmark_fields $OV_FIELDS/$LANG

#
# Check and see if then registration directory exists.
#

if [ ! -d "$OV_REGISTRATION/$LANG" ] ; then
  echo "Could not find OpenView registration directory:"
  echo "Registration directory = $OV_REGISTRATION"
  echo "Lang                   = $LANG"
  $OV_BIN/ovstart
  exit 1
fi

#
# If something broke in the install up to now, then we need to
# immeadetly exit.  From this point on, do as much as possible
# if errors occur.
#

#
# Lets build the menu interface to OpenView.  The icon bar and
# pull down menu changes are optional.  The popup menu is not 
# optional and must be installed.  Build the interface in the
# following steps.  
#
#   1) Write the header information to the markvision file.
#   2) If the toolbar is requested then install the toolbar
#      icons and add the toolbar options to the markvision file.
#   3) If the pulldown menu is requested then add the pulldown
#      menu options to the markvision file.
#   4) Write the trailer information to the markvision file.

# 
# Write the header information to the markvision file.
#
MARKVISION="$OV_REGISTRATION/$LANG/markvision"

cat markvision.header > $MARKVISION

#
# Add toolbar
#
if [ "$TOOLBAR" = "yes" ] ; then 
#
# Install the tool bar icons
#
  if [ ! -d "$OV_BITMAPS/$LANG/toolbar" ] ; then
    echo "Could not find OpenView bitmaps directory for the tool bar:"
    echo "Bitmap directory = $OV_BITMAPS"
    echo "Lang             = $LANG"
  fi

  cp bitmaps/toolbar/printer_mgmt.24.pm $OV_BITMAPS/$LANG/toolbar
  cp bitmaps/toolbar/adapter_mgmt.24.pm $OV_BITMAPS/$LANG/toolbar
  
  cat markvision.toolbar >> $MARKVISION
fi

#
# Add menubar
#
if [ "$MENUBAR" = "yes" ] ; then 
  cat markvision.menubar >> $MARKVISION
fi

#
# Add trailing information.
#

cat markvision.footer >> $MARKVISION

#
# Start up everything now
#
$OV_BIN/ovstart ovtopmd

$OV_BIN/ovw -fields

$OV_BIN/ovtopofix -a -o 1.3.6.1.4.1.641.1

$OV_BIN/ovstart
