#!/bin/sh
# $Revision: 1.1 $
# Sun Packaging Preinstall script for Media Manager
#
# Prepend /usr/5bin to PATH on Suns to get System V behaviour from echo.
PATH=/usr/5bin:$PATH

# For most supported platforms
AWK=/bin/nawk
PS_EF="ps -ef"

# Make sure this is being run from root.
ISROOT=`/usr/bin/id | egrep "^uid=0\("`
if [ "${ISROOT}" = "" ] ; then
  echo "
  This must be run while logged in as root."
  exit 1
fi

XPID=`$PS_EF | grep vmd | grep -v grep`
PID=`echo $XPID | cut -d" " -f2`
if [ "$PID" != "" ] ; then
  echo " Can not install when media manager daemon is running." 
  exit 3
fi
XPID=`$PS_EF | grep ltid | grep -v grep`
PID=`echo $XPID | cut -d" " -f2`
if [ "$PID" != "" ] ; then
  echo " Can not install when media manager daemon is running." 
  exit 3
fi
#
# check if reinstalling Volume Manager
#
if [ -f ${INSTALL_VM_DIR}/openv/volmgr/version ] ; then
  VM_DIR=$INSTALL_VM_DIR/openv/volmgr
  VM_BIN_DIR=$VM_DIR/bin
  VM_SUBDIRS="help"
  MACHINE=`head -1 $VM_DIR/version | cut -f2 -d" "`

  for vm_subdir in $VM_SUBDIRS
  do
   if [ ! -d ${VM_DIR}/${vm_subdir}.${OLD_VM_LEVEL} ] ; then
     echo "Copying ${VM_DIR}/${vm_subdir} to ${VM_DIR}/${vm_subdir}.${OLD_VM_LEVEL}"
     cp -r ${VM_DIR}/${vm_subdir} ${VM_DIR}/${vm_subdir}.${OLD_VM_LEVEL}
   fi
  done

  for vm_binary in $VM_BINS
  do
    if [ ! -f ${VM_BIN_DIR}/${vm_binary}.${OLD_VM_LEVEL} ] ; then
      if [ -f ${VM_BIN_DIR}/${vm_binary} ] ; then
        echo "Copying ${VM_BIN_DIR}/${vm_binary} to ${VM_BIN_DIR}/${vm_binary}.${OLD_VM_LEVEL}"
        cp ${VM_BIN_DIR}/${vm_binary} ${VM_BIN_DIR}/${vm_binary}.${OLD_VM_LEVEL}
      fi
   fi
 done

 if [ -d /etc/ltid.d ] && [ "${MACHINE}" != "CD4000" ] ; then
    # Copy ltid database to new location.

    if [ ! -f $VM_DIR/database/ltidevs -a -f /etc/ltid.d/ltidevs ] ; then
      echo "Copying /etc/ltid.d/ltidevs to $VM_DIR/database/ltidevs"
      cp /etc/ltid.d/ltidevs $VM_DIR/database/ltidevs
      chmod 600 $VM_DIR/database/ltidevs
    fi

    if [ ! -f $VM_DIR/database/robotic_def -a -f /etc/ltid.d/robotic_def ] ; then
      echo "Copying /etc/ltid.d/robotic_def to $VM_DIR/database/robotic_def"
      cp /etc/ltid.d/robotic_def $VM_DIR/database/robotic_def
      chmod 600 $VM_DIR/database/robotic_def
    fi

    if [ -f /etc/ltid.d/ltilocations ] ; then
      echo "Copying /etc/ltid.d/ltilocations to $VM_DIR/database/ltilocations.${OLD_VM_LEVEL}"
      cp /etc/ltid.d/ltilocations $VM_DIR/database/ltilocations.${OLD_VM_LEVEL}

    fi
    echo "Moving /etc/ltid.d to /etc/ltid.d.obsolete."
    echo "It may be removed after upgrade is complete"
    mv /etc/ltid.d /etc/ltid.d.obsolete

  else # MACHINE is CD4000

  # /etc/ltid.d already gone.

    if [ -f $VM_DIR/database/ltilocations -a "${MACHINE}" != "CD4000" ] ; then
      echo "Copying $VM_DIR/database/ltilocations to $VM_DIR/database/ltilocations.${OLD_VM_LEVEL}"
      cp $VM_DIR/database/ltilocations $VM_DIR/database/ltilocations.${OLD_VM_LEVEL}
    fi
  fi # MACHINE is CD4000
  if [ -f $VM_DIR/bin/tldtest.scsi ] ; then
    echo "Removing obsolete file $VM_DIR/bin/tldtest.scsi"
    rm -f $VM_DIR/bin/tldtest.scsi
  fi

  if [ -f $VM_DIR/bin/tldcd.scsi ] ; then
    echo "Removing obsolete file $VM_DIR/bin/tldcd.scsi"
    rm -f $VM_DIR/bin/tldcd.scsi
  fi

  if [ -f $VM_DIR/bin/hp.mkdev ] ; then
    echo "Removing obsolete file $VM_DIR/bin/hp.mkdev"
    rm -f $VM_DIR/bin/hp.mkdev
  fi

  if [ -f $VM_DIR/bin/xvmadm.171A ] ; then
    echo "Removing obsolete file $VM_DIR/bin/xvmadm.171A"
    rm -f $VM_DIR/bin/xvmadm.171A
  fi
  echo
fi # if version file exists

if [ ! -d ${INSTALL_VM_DIR}/openv ] ; then
  echo " create ${INSTALL_VM_DIR}/openv"
  mkdir -p ${INSTALL_VM_DIR}/openv
fi
if [ ! -h /usr/openv ] && [ ! -d /usr/openv ] ; then
  ln -s ${INSTALL_VM_DIR}/openv /usr/openv
fi
