# This CLI script allows to enable Microprofile for the standalone configurations.
# By default, standalone.xml is updated.
# Run it from JBOSS_HOME as:
# bin/jboss-cli.sh --file=docs/examples/enable-microprofile.cli [-Dconfig=<standalone-full.xml|standalone-ha.xml|standalone-full-ha.xml>]

embed-server --server-config=${config:standalone.xml}

echo INFO: Updating configuration to use elytron

/subsystem=undertow/application-security-domain=other:add(security-domain=ApplicationDomain)
/subsystem=ejb3/application-security-domain=other:add(security-domain=ApplicationDomain)
/subsystem=batch-jberet:write-attribute(name=security-domain, value=ApplicationDomain)

# This applies to full configurations only.
if (outcome == success) of /subsystem=messaging-activemq/server=default:read-resource
  /subsystem=messaging-activemq/server=default:undefine-attribute(name=security-domain)
  /subsystem=messaging-activemq/server=default:write-attribute(name=elytron-domain, value=ApplicationDomain)
end-if

/subsystem=remoting/http-connector=http-remoting-connector:write-attribute(name=sasl-authentication-factory, value=application-sasl-authentication)
/subsystem=remoting/http-connector=http-remoting-connector:undefine-attribute(name=security-realm)

/core-service=management/access=identity:add(security-domain=ManagementDomain)
/core-service=management/management-interface=http-interface:write-attribute(name=http-upgrade,value={enabled=true, sasl-authentication-factory=management-sasl-authentication})
/core-service=management/management-interface=http-interface:write-attribute(name=http-authentication-factory,value=management-http-authentication)
/core-service=management/management-interface=http-interface:undefine-attribute(name=security-realm)

echo INFO: Removing security subsystem and ManagementRealm.

/core-service=management/security-realm=ManagementRealm:remove
/subsystem=security:remove
/extension=org.jboss.as.security:remove

echo INFO: Adding microprofile subsystems.

if (outcome != success) of /subsystem=microprofile-config-smallrye:read-resource
  /extension=org.wildfly.extension.microprofile.config-smallrye:add
  /subsystem=microprofile-config-smallrye:add
else
  echo INFO: microprofile-config-smallrye already in configuration, subsystem not added.
end-if

if (outcome != success) of /subsystem=microprofile-fault-tolerance-smallrye:read-resource
  /extension=org.wildfly.extension.microprofile.fault-tolerance-smallrye:add
  /subsystem=microprofile-fault-tolerance-smallrye:add
else
  echo INFO: microprofile-fault-tolerance-smallrye already in configuration, subsystem not added.
end-if

if (outcome != success) of /subsystem=microprofile-health-smallrye:read-resource
  /extension=org.wildfly.extension.microprofile.health-smallrye:add
  /subsystem=microprofile-health-smallrye:add(security-enabled=false, empty-liveness-checks-status="${env.MP_HEALTH_EMPTY_LIVENESS_CHECKS_STATUS:UP}", empty-readiness-checks-status="${env.MP_HEALTH_EMPTY_READINESS_CHECKS_STATUS:UP}", empty-startup-checks-status="${env.MP_HEALTH_EMPTY_STARTUP_CHECKS_STATUS:UP}")
else
  echo INFO: microprofile-health-smallrye already in configuration, subsystem not added.
end-if

if (outcome != success) of /subsystem=microprofile-jwt-smallrye:read-resource
  /extension=org.wildfly.extension.microprofile.jwt-smallrye:add
  /subsystem=microprofile-jwt-smallrye:add
else
  echo INFO: microprofile-jwt-smallrye already in configuration, subsystem not added.
end-if

if (outcome != success) of /subsystem=microprofile-metrics-smallrye:read-resource
  /extension=org.wildfly.extension.microprofile.metrics-smallrye:add
  /subsystem=microprofile-metrics-smallrye:add(exposed-subsystems=["*"], prefix="${wildfly.metrics.prefix:jboss}", security-enabled=false)
else
  echo INFO: microprofile-metrics-smallrye already in configuration, subsystem not added.
end-if

if (outcome != success) of /subsystem=microprofile-openapi-smallrye:read-resource
  /extension=org.wildfly.extension.microprofile.openapi-smallrye:add
  /subsystem=microprofile-openapi-smallrye:add
else
  echo INFO: microprofile-openapi-smallrye already in configuration, subsystem not added.
end-if

if (outcome != success) of /subsystem=microprofile-opentracing-smallrye:read-resource
  /extension=org.wildfly.extension.microprofile.opentracing-smallrye:add
  /subsystem=microprofile-opentracing-smallrye:add
else
  echo INFO: microprofile-opentracing-smallrye already in configuration, subsystem not added.
end-if

echo INFO: Configuration done.

stop-embedded-server