@echo off
REM ###########################################################################
REM #
REM # Copyright (c) 2001, 2003, Oracle Corporation.  All rights reserved.
REM #
REM # PRODUCT
REM #       Oracle Enterprise Manager, Version 4.0.1
REM #
REM # FILENAME
REM #       emutil.bat
REM #
REM # DESCRIPTION
REM #       emutil is currently used to register job types with EM
REM # MODIFIED
REM #    rdabbott   09/04/07 - syntax fixes
REM #    rdabbott   08/30/07 - fix 4570966: get password from stdin
REM #    minfan     04/25/07 - ojdbc14 sunset
REM #    bram       02/27/06 - Change Copyright Version
REM #    smodh      02/20/06 - XbranchMerge smodh_jdbc_emcore from main 
REM #    rdabbott   09/17/05 - rm err msg for output file generation 
REM #    rdabbott   08/23/05 - hand merge 4230566 by ashugupt 
REM #    ashugupt   08/10/05 - Fix 4230566 
REM #    rdabbott   08/11/05 - fix 4529400: host port sid, jdbc 
REM #    skini      08/09/05 - fix 4529400: libs, command line params
REM #    rdabbott   08/01/05 - fix 4249800: move emutil to oracle home 
REM #    rdabbott   07/27/04 - emdrep bin in oracle home 
REM #    djoly      05/14/04 - Update core reference 
REM #    dsahrawa   01/07/04 - 
REM #    dsahrawa   01/05/04 - broken bat file now works 
REM #    mbhoopat   12/19/03 - Fix classpath 
REM #    vnukal     04/17/03 - vnukal_fix-sure-db-2_mainsa
REM #    vnukal     04/09/03 - Created
REM ###########################################################################
REM #
setlocal

set JAVA_HOME=#JAVA_HOME#
set ORACLE_HOME=#ORACLE_HOME#

echo Enterprise Manager 10.2.0.4.0
echo Copyright (c) 2002, 2007, Oracle Corporation.  All rights reserved.

REM set CP=%ORA_HOME%\oc4j\jdbc\lib\ojdbc5dms.jar;%ORACLE_HOME%\oc4j\jdbc\lib\ojdbc14dms.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-net-ee.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-utility.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-utility.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-collation.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-net.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-internal.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-servlet.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-lcsd.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-tools.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-mapping.jar;%ORA_HOME%\oc4j\jdbc\lib\orai18n-translation.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n.jar;%ORACLE_HOME%\oc4j\lib\dms.jar;%ORACLE_HOME%\jdbc\lib\ojdbc14.jar;%ORACLE_HOME%\jdbc\lib\nls_charset12.jar;%ORACLE_HOME%\j2ee\OC4J_EM\applications\em\em\WEB-INF\classes;%ORACLE_HOME%\j2ee\OC4J_EM\applications\em\em\WEB-INF\lib\emCORE.jar;%ORACLE_HOME%\j2ee\OC4J_EM\applications\em\em\WEB-INF\lib\emagentSDK.jar;%ORACLE_HOME%\j2ee\OC4J_EM\applications\em\em\WEB-INF\lib\log4j-core.jar;%ORACLE_HOME%\sysman\jlib\emCORE.jar;%ORACLE_HOME%\sysman\jlib\emagentSDKjar;%ORACLE_HOME%\sysman\jlib\log4j-core.jar;%ORACLE_HOME%\jlib\uix2.jar;%ORACLE_HOME%\lib\xmlparserv2.jar

set CP=%ORACLE_HOME%\oc4j\jdbc\lib\ojdbc5dms.jar;%ORACLE_HOME%\oc4j\jdbc\lib\ojdbc14dms.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-net-ee.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-utility.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-utility.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-collation.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-net.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-internal.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-servlet.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-lcsd.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-tools.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-mapping.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n-translation.jar;%ORACLE_HOME%\oc4j\jdbc\lib\orai18n.jar;%ORACLE_HOME%\oc4j\lib\dms.jar;%ORACLE_HOME%\jdbc\lib\ojdbc14.jar;%ORACLE_HOME%\jdbc\lib\nls_charset12.jar;%ORACLE_HOME%\j2ee\OC4J_EM\applications\em\em\WEB-INF\classes;%ORACLE_HOME%\sysman\jlib\emCORE.jar;%ORACLE_HOME%\sysman\jlib\emagentSDK.jar;%ORACLE_HOME%\sysman\jlib\log4j-core.jar;%ORACLE_HOME%\sysman\jlib\emCORE.jar;%ORACLE_HOME%\sysman\jlib\emagentSDKjar;%ORACLE_HOME%\sysman\jlib\log4j-core.jar;%ORACLE_HOME%\jlib\uix2.jar;%ORACLE_HOME%\lib\xmlparserv2.jar

:check_args
IF a%1a == aa goto :displayHelp
IF a%2a == aa goto :displayHelp

IF NOT "%1" == ""register"" IF NOT "%1" == "register" goto :displayHelp
IF NOT "%2" == ""jobtype""  IF NOT "%2" == "jobtype"  goto :displayHelp


REM check for -o flag
set FILE_ONLY=0
set outfile=
IF NOT "%3" == ""-o"" IF NOT "%3" == "-o" goto :get_jdbc_flag

    set FILE_ONLY=1
    set outfile=%4
    shift
    shift

:get_jdbc_flag
REM check for jdbc flag
set JDBC_FLAG=0
IF NOT "%3" == ""-jdbc"" IF NOT "%3" == "-jdbc" goto :get_username

    IF %FILE_ONLY%==1 goto :both_o_jdbc

    set JDBC_FLAG=1
    shift

:get_username
IF NOT "%3" == ""-username"" IF NOT "%3" == "-username" goto :get_password

    set repuser=%4
    shift
    shift

:get_password
IF NOT "%3" == ""-password"" IF NOT "%3" == "-password" goto :get_alias

    set reppwd=%4
    shift
    shift

:get_alias
IF NOT "%3" == ""-alias"" IF NOT "%3" == "-alias" goto :get_help

    set conndesc=%4
    shift
    shift

:get_help
IF NOT "%3" == ""-help"" IF NOT "%3" == "-help" goto :get_bad_op
    set FULL_HELP=1
    goto :usage

:get_bad_op
set BAD_OP=%3
set BAD_OP=%BAD_OP:~0,1%
IF NOT "%BAD_OP%" == ""-"" IF NOT "%BAD_OP%" == "-" goto :get_xml_file
    set HELP_MSG=Unexpected option: %3
    goto :displayHelp


:get_xml_file
IF NOT "%3" == "" goto :store_xml_file
    set HELP_MSG=Input filename not specified.
    goto :displayHelp

:store_xml_file
set xmlfile=%3
set basefile=%~n3
IF NOT %FILE_ONLY% == 1 set outfile=%ORACLE_HOME%\sysman\admin\emdrep\bin\%basefile%_JT.sql

if "" == "%repuser%" if "" == "%reppwd%" if "" == "%conndesc%" goto :old_syntax

if a%4a == aa goto :check_user_alias
    set HELP_MSG=Repository connection parameters can be specified by options or after the jobtype filename, but not both.
    goto :displayHelp


:check_user_alias
if defined repuser if defined conndesc goto :generate
    set HELP_MSG=Both -username and -alias must be supplied for secure mode.
    goto :displayHelp


:old_syntax
IF NOT a%6a == aa goto :get_old_syntax
    set HELP_MSG=The repository connection details must be specified.
    goto :displayHelp

:get_old_syntax
set repuser=%4
set reppwd=%5
set conndesc=%6
set DEBUG=%7


:get_h_p_s
if "%8" == "" goto :generate

    REM convert host port sid to descriptor
    set REP_HOST=%6
    set REP_PORT=%7
    set REP_SID=%8
    set DEBUG=%9
    set conndesc="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=%REP_HOST%)(Port=%REP_PORT%))(CONNECT_DATA=(SID=%REP_SID%)))"

:generate
rem echo FILE_ONLY=%FILE_ONLY%
rem echo JDBC_FLAG=%JDBC_FLAG%
rem echo xmlfile=%xmlfile%
rem echo basefile=%basefile%
rem echo outfile=%outfile%
rem echo repuser=%repuser%
rem echo reppwd=%reppwd%
rem echo conndesc=%conndesc%
rem echo DEBUG=%DEBUG%

REM generate sql file
if  NOT "%DEBUG%" == "" goto :run_java
    set DEBUG="nodebug"

:run_java

if %JDBC_FLAG% == 1 goto :gen_jdbc

    %JAVA_HOME%\bin\java -classpath %CP% oracle.sysman.emSDK.job.dtd.JobTypeHandler %xmlfile% %outfile% %DEBUG%
    set exitval=%errorlevel%

    IF %FILE_ONLY% == 1 goto :check_output_errors

    IF NOT %exitval% == 0 goto :gen_errors
        echo QUIT >> %outfile%

        REM commit job type to database
        echo Committing type to database....
        if     defined reppwd set login_str=-S -L %repuser%/%reppwd%
        if NOT defined reppwd set login_str=   -L %repuser%

        %ORACLE_HOME%\bin\sqlplus %login_str%@%conndesc% @%outfile%
        set exitval=%errorlevel%
        goto :check_commit_error

:gen_jdbc
    REM JDBC VERSION
    echo Committing type to database....
    echo - using jdbc connection
    %JAVA_HOME%\bin\java -classpath %CP% oracle.sysman.emSDK.job.dtd.JobTypeHandler %xmlfile% -jdbc %repuser% %reppwd% %conndesc% %DEBUG%
    set exitval=%errorlevel%
    goto :check_commit_error

:check_commit_error
IF NOT %exitval% == 0 goto :commit_errors

    echo Commit to database complete
    goto :normalExit

:commit_errors
    echo Errors commiting to database, exiting
    goto :normalExit

:gen_errors
    rem No error message is necessary, since JobTypeHandler will generate 
    rem it's own error message(s)
    rem Also, since there's no similar message on linux, this leads to difs
    rem echo Errors generating output file, %outfile%, exiting
    goto :normalExit


:check_output_errors
IF NOT %exitval% == 0 goto :output_errors

    echo Output file %outfile% saved successfully
    goto :normalExit

:output_errors
    echo Error saving output file, %outfile%, exiting
    goto :normalExit

:normalExit
if defined NEED_EXIT_CODE exit %exitval%
goto :EOF

REM display usage
:both_o_jdbc
set HELP_MSG=-o is not compatible with -jdbc
goto :check_error

:displayHelp
IF NOT "%1" == ""-help"" IF NOT "%1" == "-help"  goto :check_error
    set FULL_HELP=1
    goto :usage


:check_error
printf "\n"
if     defined HELP_MSG echo Error: %HELP_MSG%
if NOT defined HELP_MSG echo Error: Incorrect command option


:usage
printf "\nUsage:\n"
printf "    emutil register jobtype [options] [connect details] <Job Definition File>\n"
printf "    CONNECT DETAILS:\n"
printf "	-username <username> Repository login username\n"
printf "	-password <password> Repository login password\n"
printf "	-alias    <alias>    Repository alias or connect descriptor\n"
printf "	                     \n"
printf "\n"


if "%FULL_HELP%" == "1" goto :full_help
    printf "    Try  emutil -help for more information.\n\n"
    if defined NEED_EXIT_CODE exit 2
    goto :EOF


:full_help
printf "\n    OPTIONS:\n"
printf "	-o <output>: Output the generated PL/SQL into the specified\n"
printf "	file as an executable PL/SQL script. In this case, the job\n"
printf "	type is not registered with the repository\n"
printf "\n"
printf "	-jdbc: Use JDBC driver to connect to the repository. This option\n"
printf "	is mutually exclusive to the -o option.\n\n\n"


printf "    Deprecated Usage:\n"
printf "	emutil register jobtype [options] <Job Definition File>\n"
printf "	[<rep user> <rep passwd> <rep host> <rep port> <rep sid>]\n"
printf "	OR\n"
printf "	emutil register jobtype [options] <Job Definition File>\n"
printf "	[<rep user> <rep passwd> <rep connect descriptor>]\n\n"

if defined NEED_EXIT_CODE exit 2
