#!/bin/sh
#
# SPDX-License-Identifier: CDDL-1.0
#
# Copyright 2025 Peter Tribble
#
# run the jproc utilities
#

JPDIR=$(dirname "$0")
LIBDIR=${JPDIR}/lib
ARCHLIBDIR=${LIBDIR}/$(/usr/bin/isainfo -k)
JARLIBDIR=${LIBDIR}/java
JAVA=/usr/bin/java

#
# if installed then the locations change
#
INSTALLED=false
if [ "$INSTALLED" = "true" ]; then
    ARCHLIBDIR=/usr/lib/64
    JARLIBDIR=/usr/share/jproc
fi

usage() {
    echo "Usage: jproc subcommand [options]"
    echo "Available subcommands:"
    echo " info"
    echo " usage"
    echo " userinfo"
    echo " zoneinfo"
    echo " ptree [pid]"
    echo " jptree"
}

case $# in
0)
    usage
    exit 2
    ;;
*)
    SUBCOMMAND=$1
    shift
    ;;
esac

JSONJAR=${JARLIBDIR}/openjson-1.0.13.jar
JINGLEJAR=${JARLIBDIR}/jingle.jar
JUMBLEJAR=${JARLIBDIR}/jumble.jar
JPJAR=${JARLIBDIR}/jproc.jar
JPAPIJAR=${JARLIBDIR}/jproc-api.jar
ALLJARS=${JSONJAR}:${JINGLEJAR}:${JUMBLEJAR}:${JPJAR}
DEMOPKG="uk.co.petertribble.jproc.demo"
SERVERJARS=${JARLIBDIR}/xmlrpc-common-3.1.3.jar:${JARLIBDIR}/xmlrpc-server-3.1.3.jar:${JARLIBDIR}/commons-logging-1.1.1.jar:${JARLIBDIR}/ws-commons-util-1.0.2.jar:${JARLIBDIR}/jmdns-3.6.2.jar:${JARLIBDIR}/slf4j-api-2.0.13.jar
CLIENTJARS=${JARLIBDIR}/xmlrpc-common-3.1.3.jar:${JARLIBDIR}/xmlrpc-client-3.1.3.jar:${JARLIBDIR}/commons-logging-1.1.1.jar:${JARLIBDIR}/ws-commons-util-1.0.2.jar:${JARLIBDIR}/jmdns-3.6.2.jar:${JARLIBDIR}/slf4j-api-2.0.13.jar

#
# need to find where java is
#
JLOC=/usr/java
if [ -d /usr/jdk/latest/bin ]; then
    JLOC=/usr/jdk/latest
fi

#
# Normally, -s means we're an xml-rpc client and need the client-side jars
# for remote access
#
case $1 in
-s)
    ALLJARS="${ALLJARS}:${CLIENTJARS}"
    ;;
esac

case $SUBCOMMAND in

'info')
    JPCLASS=${DEMOPKG}.JPinfo
    ;;

'userinfo')
    JPCLASS=${DEMOPKG}.UserInfo
    ;;

'zoneinfo')
    JPCLASS=${DEMOPKG}.ZoneInfo
    ;;

'usage')
    JPCLASS=${DEMOPKG}.JPusage
    ;;

'ptree')
    JPCLASS=${DEMOPKG}.PTree
    ;;

'jptree')
    JPCLASS=${DEMOPKG}.JPTree
    ;;

'server')
    JPCLASS=uk.co.petertribble.jproc.server.PServer1
    ALLJARS=${JPJAR}:${SERVERJARS}
    ;;

#
# EXPERIMENTAL
#
'jmxserver')
    JPCLASS=uk.co.petertribble.jproc.server.JProcMXserver
    # the following works for jconsole
    JFLAGS="-Dcom.sun.management.jmxremote"
    # the following for a remote client like jmanage
    JFLAGS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
    ALLJARS=${JPJAR}:${SERVERJARS}
    ;;

#
# EXPERIMENTAL
#
'jmxclient')
    ${JLOC}/bin/jconsole -J-Djava.class.path=${JLOC}/lib/jconsole.jar:${JLOC}/lib/tools.jar:"${JPAPIJAR}"
    exit 0
    ;;

#
# undocumented for debugging only
#
'jsondump')
    JPCLASS=${DEMOPKG}.JSONdump
    ;;

*)
    usage
    exit 2
    ;;

esac

#
# launch the class specified
#
LD_LIBRARY_PATH=${ARCHLIBDIR} $JAVA ${JFLAGS} -Dswing.aatext=true -cp ${ALLJARS} ${JPCLASS} "$@"
