#!/bin/sh
# @(#) MQMBID sn=p800-003-150615.2 su=_O7DtFhOPEeWRp7-5NNeHyQ pn=install/unix/linux_2/amqicrel.sh
################################################################################
#
# Product: WebSphere MQ
#
# <N_OCO_COPYRIGHT>
# Licensed Materials - Property of IBM
#
# 5724-H72
# (c) Copyright IBM Corp. 2010 All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with
# IBM Corp.
# <NOC_COPYRIGHT>
#
# Module: amqicrel
#
# Usage:  amqicrel <install_path> [ packagename ]
#
# Description:
#  Modify installed WebSphere MQ files to allow for a relocated installation
#  This will normally be run from an installation script so messages are
#  in the form specified in the HP SD Admin Guide.
#
################################################################################

# ******************************************************************************
# * Global variables                                                           *
# ******************************************************************************
rc=0                                             # return code

MQ_INSTALLATION_PATH=$(dirname "$0")/..                          # MQ installation path

#
# Ensure responses to all commands are in english so we can parse them
#
export LANG=C
export LC_ALL=C

#
# If debug required dump out lots of potentially useful stuff and set -x
#
ID="@(#) MQMBID sn=p800-003-150615.2 su=_O7DtFhOPEeWRp7-5NNeHyQ pn=install/unix/linux_2/amqicrel.sh"
ENV=`env`
UNAME=`uname -a`
USER=`id`
MQ_DEFAULT_INSTALLATION_PATH=/opt/mqm
PRODNAME=MQSeries
TMPDIR=${TMPDIR:-/tmp}

#
# First check we are running as root, if not exit with an error
#
idnum=`id -u`
if [ ${idnum} -ne 0 ]
then
  echo "ERROR:      You must be root to run this script" >&2
  exit 1
fi
#
# Validate input - $1 must be specified and must be a directory where
# WebSphere MQ with a version >= 7.1 is installed.
# It should not be the default installation path
#
if [ $# -lt 1 ] ; then
   echo "ERROR:    No installation directory specified" >&2
   exit 1
   else MQ_INSTALLATION_PATH=$(dirname "$0")/..
fi

if [ ! -d ${MQ_INSTALLATION_PATH} ] ; then
  echo "ERROR:    Directory \"${MQ_INSTALLATION_PATH}\" does not exist" >&2
  exit 1
fi

if [ ${MQ_INSTALLATION_PATH} = ${MQ_DEFAULT_INSTALLATION_PATH} ] ; then
  echo "INFO:    Directory \"${MQ_INSTALLATION_PATH}\" is the default path, relocation not required" >&2
  exit 0
fi
#
# $2, if it exists - is the name of an installed rpm package or 'NOVPD'
# meaning that the vendor package database should not be used.
# if it does not exist do it for all MQSeries* packages which have
# an INSTPREFIXES value of the required installation path
#
if [ $# -eq 2 ] ; then
   RPM_PACKAGE_LIST=$2
   if [ z"${RPM_PACKAGE_LIST}" = z"NOVPD" ]; then
     vpd=0
   else
     vpd=1
   fi   
else
   vpd=1
   RPM_PACKAGE_LIST=`rpm -qa --qf '%{NAME}-%{VERSION}-%{RELEASE}\t%{INSTPREFIXES}\n' | grep -w "${MQ_INSTALLATION_PATH}" | cut -f1`
fi

if [ z"${RPM_PACKAGE_LIST}" = "z" ] ; then
  exit 0
fi

# For each package get a list of files.
# For each file run amqicfil
for RPM_PACKAGE_NAME in ${RPM_PACKAGE_LIST}
  do
    SHORT_RPM_PACKAGE_NAME=`echo ${RPM_PACKAGE_NAME} | cut -f1 -d"-" | sed s/MQSeriesJRE//g`
    if [ ! -z ${SHORT_RPM_PACKAGE_NAME} ] ; then
      # If we should not use the vendor package database use find
      if [ ${vpd} -ne 1 ]; then
        file_list=`find ${MQ_INSTALLATION_PATH} -type f`
      else      
        file_list=`rpm -ql ${RPM_PACKAGE_NAME}`
      fi
      for filename in ${file_list}
        do
          amqicfil_out=`${MQ_INSTALLATION_PATH}/bin/amqicfil ${MQ_INSTALLATION_PATH} ${filename} 2>&1`
        done
    fi
  done

exit 0                                           # Can't stop the installation now so return zero
