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

echo INFO: Configuration done.

stop-embedded-server