#!/bin/bash

source ../environment.conf

WORKER_NAME=master

... supreesed nom related configs

sudo -E -u wildfly $JBOSS_HOME/bin/add-user.sh -u $CONTROLLER_USERNAME -p $CONTROLLER_PASSWORD --silent
sudo -E -u wildfly $JBOSS_HOME/bin/add-user.sh -u $OPERADOR_USERNAME -p $OPERADOR_PASSWORD -a -r ApplicationRealm --silent
sudo -E -u wildfly $JBOSS_HOME/bin/add-user.sh -u ejbserver -p ejb -a -r ApplicationRealm --silent

... supreesed nom related configs

sudo -E -u $JBOSS_USER \
    $JBOSS_HOME/bin/jboss-cli.sh \
    -Djboss.domain.master.address=127.0.0.1 <<EOF

embed-host-controller --std-out=echo --domain-config=domain.xml --host-config=host.xml
#Remote EJB Secret
/host=$WORKER_NAME/core-service=management/security-realm=remote-ejb-security-realm:add()
/host=$WORKER_NAME/core-service=management/security-realm=remote-ejb-security-realm/server-identity=secret:add(value=ZWpi)
stop-embedded-host-controller

EOF

$JBOSS_HOME/bin/jboss-cli.sh --connect \
	--controller=$CONTROLLER_HOST:$CONTROLLER_PORT \
	--user=$CONTROLLER_USERNAME --password=$CONTROLLER_PASSWORD <<EOF
# Mark the commands below to be run as a batch
batch


/socket-binding-group=erp-standard-sockets:add(default-interface=public)
/socket-binding-group=erp-standard-sockets/socket-binding=ajp/:add(port=\${jboss.ajp.port:8009})
/socket-binding-group=erp-standard-sockets/socket-binding=http/:add(port=\${jboss.http.port:8080})
/socket-binding-group=erp-standard-sockets/socket-binding=https/:add(port=\${jboss.https.port:8443})
/socket-binding-group=erp-standard-sockets/socket-binding=txn-recovery-environment/:add(port=4712)
/socket-binding-group=erp-standard-sockets/socket-binding=txn-status-manager/:add(port=4713)
/socket-binding-group=erp-standard-sockets/remote-destination-outbound-socket-binding=mail-smtp:add(host=localhost, port=25)

#Remote EJB outbound server
/socket-binding-group=erp-standard-sockets/remote-destination-outbound-socket-binding=remote-erp:add(host=\${ERP.default.host:localhost}, port=\${ERP.default.port:8080})
/socket-binding-group=erp-standard-sockets/remote-destination-outbound-socket-binding=remote-workflow:add(host=\${DCWF.default.host:localhost}, port=\${DCWF.default.port:8080})
/socket-binding-group=erp-standard-sockets/remote-destination-outbound-socket-binding=remote-emissor-email:add(host=\${DCEmissorEmail.default.host:localhost}, port=\${DCEmissorEmail.default.port:8080})


#Execute the batch
run-batch
EOF

$JBOSS_HOME/bin/jboss-cli.sh --connect \
	--controller=$CONTROLLER_HOST:$CONTROLLER_PORT \
	--user=$CONTROLLER_USERNAME --password=$CONTROLLER_PASSWORD <<EOF
# Mark the commands below to be run as a batch
batch

#Remote EJB outbound references
/profile=$PROFILE_NAME/subsystem=remoting/remote-outbound-connection=remote-workflow-connection:add(outbound-socket-binding-ref=remote-workflow, protocol=http-remoting, security-realm=remote-ejb-security-realm, username=ejbserver)
/profile=$PROFILE_NAME/subsystem=remoting/remote-outbound-connection=remote-workflow-connection/property=SASL_POLICY_NOANONYMOUS:add(value=false)
/profile=$PROFILE_NAME/subsystem=remoting/remote-outbound-connection=remote-workflow-connection/property=SSL_ENABLED:add(value=false)

/profile=$PROFILE_NAME/subsystem=remoting/remote-outbound-connection=remote-emissor-email-connection:add(outbound-socket-binding-ref=remote-emissor-email, protocol=http-remoting, security-realm=remote-ejb-security-realm, username=ejbserver)
/profile=$PROFILE_NAME/subsystem=remoting/remote-outbound-connection=remote-emissor-email-connection/property=SASL_POLICY_NOANONYMOUS:add(value=false)
/profile=$PROFILE_NAME/subsystem=remoting/remote-outbound-connection=remote-emissor-email-connection/property=SSL_ENABLED:add(value=false)

/profile=$PROFILE_NAME/subsystem=remoting/remote-outbound-connection=remote-erp-connection:add(outbound-socket-binding-ref=remote-erp, protocol=http-remoting, security-realm=remote-ejb-security-realm, username=ejbserver)
/profile=$PROFILE_NAME/subsystem=remoting/remote-outbound-connection=remote-erp-connection/property=SASL_POLICY_NOANONYMOUS:add(value=false)
/profile=$PROFILE_NAME/subsystem=remoting/remote-outbound-connection=remote-erp-connection/property=SSL_ENABLED:add(value=false)

#Execute the batch
run-batch
EOF

$JBOSS_HOME/bin/jboss-cli.sh --connect \
	--controller=$CONTROLLER_HOST:$CONTROLLER_PORT \
	--user=$CONTROLLER_USERNAME --password=$CONTROLLER_PASSWORD <<EOF
# Mark the commands below to be run as a batch
batch

# ERP RMI lookup properties
/server-group=$SERVER_GROUP/system-property=ERP.default.containerName:add(value=jboss,boot-time=true)
/server-group=$SERVER_GROUP/system-property=ERP.default.host:add(value=$LOADBALANCE_HOST,boot-time=true)
/server-group=$SERVER_GROUP/system-property=ERP.default.port:add(value=$LOADBALANCE_PORT,boot-time=true)

# Execute the batch
run-batch
EOF

$JBOSS_HOME/bin/jboss-cli.sh --connect \
	--controller=$CONTROLLER_HOST:$CONTROLLER_PORT \
	--user=$CONTROLLER_USERNAME --password=$CONTROLLER_PASSWORD <<EOF
# Mark the commands below to be run as a batch
batch

# ERP RMI lookup properties Workflow
/server-group=$SERVER_GROUP/system-property=DCWF.default.containerName:add(value=jboss,boot-time=true)
/server-group=$SERVER_GROUP/system-property=DCWF.default.host:add(value=$LOADBALANCE_HOST,boot-time=true)
/server-group=$SERVER_GROUP/system-property=DCWF.default.port:add(value=$LOADBALANCE_PORT,boot-time=true)

# ERP RMI lookup properties EmissorEmail
/server-group=$SERVER_GROUP/system-property=DCEmissorEmail.default.containerName:add(value=jboss,boot-time=true)
/server-group=$SERVER_GROUP/system-property=DCEmissorEmail.default.host:add(value=$LOADBALANCE_HOST,boot-time=true)
/server-group=$SERVER_GROUP/system-property=DCEmissorEmail.default.port:add(value=$LOADBALANCE_PORT,boot-time=true)


# Execute the batch
run-batch
EOF