#!/bin/sh

#
# 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 deconfiguration 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

#
# Remove setSymbolStatus
#
rm -f $OV_BIN/setSymbolStatus

#
# Unload lexmark1.mib into OpenView
#
LEXMARK_MIB="$MARKVISION/etc/lexmark1.mib"
echo "Unloading lexmark1.mib into OpenView"
$OV_BIN/xnmloadmib -unload $LEXMARK_MIB 

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

#
# Remove events here
#

echo "Remove Lexmark Traps"
$OV_BIN/xnmevents -delete irCleared irOutputfull irLoadPaper irPaperJam irTonerLow irServiceReq irDiskErr irCoverOpen irPageComplexity irOffline irClearedTypeII adapter

#
# Stop the OpenView daemons.
#

echo "Stopping all OpenView processes"
$OV_BIN/ovstop

#
# Restore the original oid_to_type file
#
#
#OID_TO_TYPE="$OV_CONF/oid_to_type"
#
#if [ -r "${OID_TO_TYPE}.orig" ] ; then
#  echo "Restoring oid_to_type"
#  rm -f $OID_TO_TYPE
#  mv ${OID_TO_TYPE}.orig $OID_TO_TYPE
#fi

#
# Restore the original oid_to_sym file
#
#
#OID_TO_SYM="$OV_CONF/$LANG/oid_to_sym"
#
#if [ -r "${OID_TO_SYM}.orig" ] ; then
#  echo "Restoring oid_to_sym"
#  rm -f $OID_TO_SYM
#  mv ${OID_TO_SYM}.orig $OID_TO_SYM
#fi

#
# Remove the lexmark fields now
#

if [ -r "$OV_FIELDS/$LANG/lexmark_fields" ] ; then
  echo "Removing lexmark_fields"
  rm -f $OV_FIELDS/$LANG/lexmark_fields
fi

MARKVISION="$OV_REGISTRATION/$LANG/markvision"

if [ -r $MARKVISION ] ; then
  echo "Removing markvision from the registration directory"
  rm -f $MARKVISION
fi

if [ -r "$OV_SYMBOLS/$LANG/Device/Device_lexmark" ] ; then
  echo "Removing Symbol Registration for Device_lexmark"
  rm -f $OV_SYMBOLS/$LANG/Device/Device_lexmark
fi

if [ -r "$OV_BITMAPS/$LANG/toolbar/printer_mgmt.24.pm" ] ; then
  echo "Removing printer_mgmt.24.pm"
  rm -f $OV_BITMAPS/$LANG/toolbar/printer_mgmt.24.pm
fi

if [ -r "$OV_BITMAPS/$LANG/toolbar/adapter_mgmt.24.pm" ] ; then
  echo "Removing adapter_mgmt.24.pm"
  rm -f $OV_BITMAPS/$LANG/toolbar/adapter_mgmt.24.pm
fi

if [ -d "$OV_BITMAPS/$LANG/device" ] ; then
  echo "Removing lexprinter Bitmaps"
  rm -f $OV_BITMAPS/$LANG/device/lexprinter*
fi


#
# Start up everything now
#

echo Starting ovtopmd
$OV_BIN/ovstart ovtopmd

echo Fixing fields
$OV_BIN/ovw -fields

echo Running ovtopofix
$OV_BIN/ovtopofix -a -o 1.3.6.1.4.1.641.1

echo Starting rest of OpenView
$OV_BIN/ovstart

