#!/bin/sh
#
# Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0, which is available at
# http://www.eclipse.org/legal/epl-2.0.
#
# This Source Code may also be made available under the following Secondary
# Licenses when the conditions for such availability set forth in the
# Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
# version 2 with the GNU Classpath Exception, which is available at
# https://www.gnu.org/software/classpath/license.html.
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
#

USAGE="USAGE: riutil [help|all|bkr|cxn|svc|dur|jmx|txn|clientids|listjmsobjects|query cxn id|query cxn all|destroy dur name]."
HELPMENU="-----------------------------------------------------------------------------------
$USAGE

riutil all                  ......   List Everything
riutil bkr                  ......   List MQ Brokers
riutil svc                  ......   List MQ Services
riutil cxn                  ......   List Active Connections
riutil txn                  ......   List Active Transactions
riutil dur                  ......   List Durable Subscriptions
riutil clientids            ......   List Client ID's in use
riutil jmx                  ......   List JMX info
riutil listjmsobjects       ......   List JMS objects
riutil query cxn id         ......   Query a connection
riutil query cxn all        ......   List all connections
riutil destroy dur name     ......   Destroy durable subscription
-----------------------------------------------------------------------------------"
PASSWDFILE=/tmp/passwd
TEMPFILE=/tmp/out
JNDIFSDIR=/tmp/ri_admin_objects
JNDI_FACTORY_INITIAL="java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory"
JNDI_PROVIDER_URL="java.naming.provider.url=file:///${JNDIFSDIR}"
echo "imq.imqcmd.password=admin" >$PASSWDFILE
if [ $# -eq 0 ]
then
    echo "$HELPMENU" 1>&2
    exit 1
fi
if [ $# -gt 3 ]
then
    echo "$USAGE" 1>&2
    exit 1
fi
querycxn=0
destroydur=0
listbkr=0
listcxn=0
listtxn=0
listdur=0
listjmx=0
listsvc=0
listclientids=0
listjmsobjects=0
while [ $# -ne 0 ]
do
  case $1 in
    all)
    	listbkr=1
    	listcxn=1
    	listtxn=1
    	listdur=1
    	listjmx=1
    	listsvc=1
    	listclientids=1
	listjmsobjects=1
	;;
    bkr)
	listbkr=1
	;;
    cxn)
	listcxn=1
	;;
    txn)
	listtxn=1
	;;
    dur)
	listdur=1
	;;
    svc)
	listsvc=1
	;;
    jmx)
	listjmx=1
	;;
    clientids)
	listclientids=1
	;;
    listjmsobjects)
	listjmsobjects=1
	;;
    destroy)
	shift
	cmd=$1
	case $cmd in
          dur)
	     destroydur=1
	     shift
	     name=$1
             ;;
          *) echo "$USAGE" 1>&2
             exit 1
             ;;
	esac
	;;
    query)
	shift
	cmd=$1
	case $cmd in
          cxn)
	     querycxn=1
	     shift
	     arg=$1
             ;;
          *) echo "$USAGE" 1>&2
             exit 1
             ;;
	esac
	;;
    help)
    	echo "$HELPMENU" 1>&2
	;;
     *) 
	echo "$USAGE" 1>&2
        exit 1
        ;;
  esac
  shift
done
if [ $querycxn -eq 1 ]
then
    if [ "$arg" = "all" ]
    then
	$HOME/bin/mqcxn
    else
        imqcmd query cxn -n $arg -passfile $PASSWDFILE -u admin 
    fi
elif [ $destroydur -eq 1 ]
then
    imqcmd destroy dur -n $name -passfile $PASSWDFILE -u admin 
fi
[ $listbkr -eq 1 ] && imqcmd list bkr -passfile $PASSWDFILE -u admin && sleep 2
[ $listcxn -eq 1 ] && imqcmd list cxn -passfile $PASSWDFILE -u admin && sleep 2
[ $listsvc -eq 1 ] && imqcmd list svc -passfile $PASSWDFILE -u admin && sleep 2
[ $listdur -eq 1 ] && imqcmd list dur -passfile $PASSWDFILE -u admin && sleep 2
[ $listjmx -eq 1 ] && imqcmd list jmx -passfile $PASSWDFILE -u admin && sleep 2
[ $listtxn -eq 1 ] && imqcmd list txn -passfile $PASSWDFILE -u admin && sleep 2
[ $listjmsobjects -eq 1 ] && imqobjmgr list -j "$JNDI_FACTORY_INITIAL" -j "$JNDI_PROVIDER_URL" && sleep 2
if [ $listclientids -eq 1 ] 
then
    rm -f $TEMPFILE
    imqcmd list cxn -passfile /tmp/passwd -u admin 2>&1 >$TEMPFILE
    ed $TEMPFILE >/dev/null 2>&1 <<EOF
1,10d
/Success/
-1,\$d
w
q
EOF
    connid=`cat $TEMPFILE|awk '{print $1}'`
    count=`echo $connid|wc -w` 
    echo "There are $count open connections"
    for i in $connid
    do
        clientid=`imqcmd query cxn -n $i -passfile /tmp/passwd -u admin 2>&1 | grep "Client ID" 2>&1 | awk '{print $3}'` 2>/dev/null
        echo "Query [ConnectionID=$i  ClientID=$clientid]"
    done
fi
