#!/bin/sh
#
# hpdiag - install the diagnostic kernel-mode driver and run hpdiags-bin
#

DRIVER="hpdvmkd.o"
DEVICE="/dev/hpdvmkd"
MAJORNUM=60

DIAGS_DIR_LIST=". /usr/share/diagnostics /opt/compaq/diags"
DIAGS_DIR=""
DIAGS_BIN="hpdiags-bin"


function check_for_user {
  if test "$UID" != "0"
  then
    echo "$0: only the superuser (root) may run this command" >&2
    exit 1
  fi
}

#function rebuild_driver {
#  ./rebuild_driver.sh
#  if ! test $? -eq 0
#  then
#    echo "$0: driver rebuild attempt unsuccessful, attempting to use default driver"
#    cp -f hpdvmkd_default.o hpdvmkd.o
#  fi
#}

function ch_diags_dir {
  for i in $DIAGS_DIR_LIST
  do
    if test -e $i/$DIAGS_BIN
    then
      DIAGS_DIR=$i
      cd $DIAGS_DIR
      return
    fi
  done
  DIAGS_DIR=`pwd`
}

function mknode {
  if test -z "`ls /dev/hpdvmkd 2>/dev/null`"
  then
     mknod $DEVICE c $MAJORNUM 0
     chmod 0600 $DEVICE
  fi
}

#function load_driver {
#
#  # create /dev/hpdvmkd
#  mknode
#
#  # try a default driver
#  if test -e kmd/`uname -r`/$DRIVER
#  then
#    #echo "trying kmd/`uname -r`/hpdvmkd.o"
#    insmod -f kmd/`uname -r`/$DRIVER 1> /dev/null 2> /dev/null
#    sleep 1
#  fi
#
#  if test -n "`/sbin/lsmod | grep hpdvmkd`"
#  then
#    #echo "$0: diagnostics are already installed on this system" >&2
#    return
#  fi
#
#  # try rebuilding the driver ...
#  if test ! -e current/$DRIVER
#  then
#    echo "$0: rebuilding driver: (requires gcc & kernel-source packages): " >&2
#    rebuild_driver
#  fi
#
#  # and inserting that one
#  if test -e kmd/current/$DRIVER
#  then
#    #echo "trying kmd/current/hpdvmkd.o"
#    insmod -f kmd/current/$DRIVER 1> /dev/null 2> /dev/null
#    sleep 1
#  fi
#
#  if test -n "`/sbin/lsmod | grep hpdvmkd`"
#  then
#    #echo "$0: diagnostics are already installed on this system" >&2
#    return
#  fi
#
#  # last resort ... 
#  if test -e $DRIVER
#  then
#    #echo "trying hpdvmkd.o"
#    insmod -f $DRIVER 1>/dev/null 2> /dev/null
#    sleep 1
#  fi
#
#  if test -n "`/sbin/lsmod | grep hpdvmkd`"
#  then
#    #echo "$0: diagnostics are already installed on this system" >&2
#    return
#  fi
#
#  # if we haven't kicked out by now, alert the user
#  if test -z "`/sbin/lsmod | grep hpdvmkd`"
#  then
#    echo
#    echo "$0: The driver was unable to load."
#    echo "$0: Some functionality will be missing, etc."
#    echo
#  fi
#
#}

function load_driver {
  # create /dev/hpdvmkd
  # mknode

  if test -e $DRIVER
  then
    #echo "trying hpdvmkd.o"
    insmod -f $DRIVER 1>/dev/null 2> /dev/null
    sleep 1
  fi
}


function create_links {
  # create libbmapi.so.6 link
  ln -s $DIAGS_DIR/libbmapi.so $DIAGS_DIR/libbmapi.so.6 1>/dev/null 2>/dev/null
}

LANGUAGE=`cat language 2>/dev/null`

function chkhttpserverconfig {
  unset HPWEBSETUP
  CURRENTDIR=`pwd`
  if [ -x "$CURRENTDIR/hpwebsetup" ]; then
    $CURRENTDIR/hpwebsetup -q: >/dev/null 2>&1
    if [ $? != 4 ]; then
      echo " " >&2
      echo "  ERROR: Can't start the Web agent. HP HTTP server must be configured" >&2
      echo "         first. The minimum configuration required is the \"administrator\"" >&2
      echo "         password which can be configured with following command:" >&2
      echo "         $CURRENTDIR/hpwebsetup -pA:yourpasswd -f" >&2
      echo " " >&2
      echo "  After configuring your password, you can start the hp Insight Diagnostics " >&2
      echo "  web service with the following command: ">&2
      echo "         $CURRENTDIR/hpdiags -w ">&2
      echo " ">&2
      exit 1
    fi
  fi
}

ch_diags_dir
chkhttpserverconfig
test -x ./$DIAGS_BIN || exit 1

check_for_user
load_driver
create_links

# to use the old pthreads over the new one
export LD_ASSUME_KERNEL=2.4.1

if [ -z "$LD_LIBRARY_PATH" ]
then
   export LD_LIBRARY_PATH=.:/usr/lib:/lib
else
   export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
fi


if test -z "$LANGUAGE" || test -n "`echo \"$*\" | grep \\"-l\\"`"
then
  exec ./$DIAGS_BIN $*
else
  exec ./$DIAGS_BIN -l $LANGUAGE $*
fi


