# 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}

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

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

if (outcome == success) of /subsystem=iiop-openjdk:read-resource
  /subsystem=iiop-openjdk:write-attribute(name=security, value=elytron)
else 
  echo INFO: iiop-openjdk not present, skipping update to use Elytron
end-if

if (outcome == success) of /subsystem=messaging-activemq/server=default:read-resource
  /subsystem=messaging-activemq/server=default:write-attribute(name=elytron-domain, value=ApplicationDomain)
else 
  echo INFO: messaging-activemq not present, skipping update to use Elytron
end-if

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}")
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:wildfly}", 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
