#!/bin/sh

#
# (c) 1999-2003 Oracle Corp. all rights reserved
#
# J2ME SDK Manager. Provides a command line tool to manage J2ME SDK services.
#

#This script is adapted from multiple scripts originally written by Sunny Sun
#author Sunny Sun
#author Chandra Patni
printUsage()
{
    echo Usage: j2mesdkmgr.sh \<action\> \[\<argument\>...\]
    echo where \<action\> and corresponding argument include:
    echo   -registerclass \<URL of the Class Library\> \<Name of the class\> \[\<namespace\>\]
    echo       Registers the specified class.
    echo
    echo   -registerwsdl \<URL of the WSDL\> \[\<namespace\>\]
    echo       Registers the services in the specified WSDL document.
    echo
    echo   -generatestub \[\<namespace\>.\]\<service name\> \[\<Output directory\>\[\<stub name\>\]\]
    echo       Generates a J2ME client stub.
    echo
    echo   -export \<xml file name\>
    echo       Exports all registered services to an XML file for migration to IASW.
    echo
    echo   -listservices
    echo       Lists the registered web services.
    echo
    echo   -removeservice \[\<namespace\>.\]\<service name\>
    echo       Removes the specified web service.
    echo
    echo   -help \| -?
    echo       Print a synopsis of standard options.
}


if [ $# -lt 1 ] || [ $1 = "help" ] || [ $1 = "-help" ] || [ $1 = "--help" ] || [ $1 = "?" ] || [ $1 = "-?" ] || [ $1 = "--?" ]; then
  printUsage
  exit  1
fi

. ./j2me_env.sh

if [ -z "${IAS_HOME}" ]
then
   IAS_HOME=${ORACLE_HOME}
fi

if [ -z "${ORACLE_HOME}" ]
then
    echo "ERROR!!! You MUST define ORACLE_HOME to run this script"
    echo "Please configure j2me_env.sh."
    exit  1
fi

if [ -z "${PROXY_URL}" ]
then
    echo "ERROR!!! You MUST define PROXY_URL, the URL of the J2ME proxy server, to run this script"
    echo "Please configure j2me_env.sh."
    exit  1
fi

if [ -z "${J2ME_SERVER_HOME}" ]
then
    echo "ERROR!!! You MUST define J2ME_SERVER_HOME to run this script"
    echo "Please configure j2me_env.sh."
    exit  1
fi

if [ $1 = "generatestub" ] || [ $1 = "-generatestub" ]; then
  if [ $# -lt 2 ]; then
    echo Usage:
    echo ./j2mesdkmgr.sh -generatestub \[\<namespace\>.\]\<service name\> \[\<Output directory\> \[\<stub name\>\]\]
    echo Examples:
    echo ./j2mesdkmgr.sh -generatestub TestWebService
    echo ./j2mesdkmgr.sh -generatestub TestWebService /temp
    echo ./j2mesdkmgr.sh  -generatestub TestWebService /temp TestServiceStub
    exit  1
  fi
  ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" oracle.wireless.me.server.ServiceManagerStandalone generatestub $2 $3 $4
  exit  0
fi

if [ $1 = "export" ] || [ $1 = "-export" ]; then
  if [ $# -lt 2 ]; then
    echo Usage:
    echo  ./j2mesdkmgr.sh -export \<xml file name\>
    exit  1
  fi
  ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" oracle.wireless.me.server.J2MEProxyMigrationStandalone export $2
  exit  0
fi

if [ $1 = "registerclass" ] || [ $1 = "-registerclass" ]; then
  if [ $# -lt 3 ]; then
    echo Usage:
    echo ./j2mesdkmgr.sh -registerclass \<URL of the Class Library\> \<Name of the class\> \[\<namespace\>\]
    echo Example:
    echo ./j2mesdkmgr.sh -registerclass file:/iaswv904/wireless/wdk/server/applications/wdk/wdk-web/webservice/lib/testservices.jar oracle.web.TestWebservice
    exit  1
  fi
  if [ $# -eq 3 ]
  then
    ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" oracle.wireless.me.server.RegistrationManager -mode class -libUrl $2 $3
  else
    ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" oracle.wireless.me.server.RegistrationManager -mode class -libUrl $2 -namespace $4 $3
  fi
  exit  0
fi

if [ $1 = "registerwsdl" ] || [ $1 = "-registerwsdl" ]; then
  if [ $# -lt 2 ]; then
    echo Usage:
    echo ./j2mesdkmgr.sh -registerwsdl \<URL of the WSDL\> \[\<namespace\>\]
    echo Example:
    echo ./j2mesdkmgr.sh -registerwsdl file:/iaswv904/wireless/wdk/j2me/sample/PushLite.wsdl
    exit  1
  fi
  if [ $# -eq 2 ]
  then
      # Regular execution
      ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" oracle.wireless.me.server.RegistrationManager -mode wsdl $2

      # Execution through HTTP proxy server
      # ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" -Dhttp.useProxy="true" -Dhttp.proxyHost="www-proxy.us.oracle.com" -Dhttp.proxyPort="80" -Dhttp.nonProxyHosts="localhost" oracle.wireless.me.server.RegistrationManager -mode wsdl $2
      exit  0
  else
      # Regular execution
      ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" oracle.wireless.me.server.RegistrationManager -mode wsdl -namespace $3 $2

      # Execution through HTTP proxy server
      # ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" -Dhttp.useProxy="true" -Dhttp.proxyHost="www-proxy.us.oracle.com" -Dhttp.proxyPort="80" -Dhttp.nonProxyHosts="localhost" oracle.wireless.me.server.RegistrationManager -mode wsdl -namespace $3 $2
      exit  0
  fi
fi


if [ $1 = "listservices" ] || [ $1 = "-listservices" ]; then
  ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" oracle.wireless.me.server.ServiceManagerStandalone listservices
  exit  0
fi

if [ $1 = "removeservice" ] || [ $1 = "-removeservice" ]; then
  if [ $# -ge 2 ]; then
   ${JAVA_HOME}/bin/java -mx50m -classpath "${CLASSPATH}" -DJ2ME_SERVER_HOME="${J2ME_SERVER_HOME}" -DPROXY_URL="${PROXY_URL}" -DPROXY_MODE="${PROXY_MODE}" -DPROXY_LOG_DIR="${PROXY_LOG_DIR}" oracle.wireless.me.server.ServiceManagerStandalone removeservice $2
   exit  0
  fi
  printUsage
  exit 1
fi

printUsage

