#https://issues.redhat.com/browse/ENTMQBR-8164
NAMESPACE=testamqlog

oc delete ns $NAMESPACE --wait &>/dev/null
oc wait --for=delete ns/"$NAMESPACE" --timeout=180s &>/dev/null

oc create ns $NAMESPACE
oc project $NAMESPACE

rm LAST*.yaml
for i in $(ls *.yaml)
do
  mv $i LAST.$i
done



echo "1. Create  OperatorGroup"
cat > operator-group.yaml << EOF
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
  name: amq-broker-rhel8
  namespace: $NAMESPACE
spec:
  targetNamespaces:
  - $NAMESPACE
EOF

oc create -f operator-group.yaml


echo "2. Create  Subscription"
cat > subscription.yaml << EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: my-amq
  namespace: $NAMESPACE
spec:
  channel: 7.11.x
  name: amq-broker-rhel8
  installPlanApproval: Automatic
  source: redhat-operators
  sourceNamespace: openshift-marketplace
EOF

oc create -f subscription.yaml
sleep 5
POD_NAME=$(oc get pods | grep controller | awk {'print $1'})
oc get pods
#oc wait --for=jsonpath='{.status.phase}'=Running pod/$POD_NAME --timeout=120s
oc wait --for=condition=Ready pod/$POD_NAME --timeout=120s
oc get pods


echo "3. Create broker, default logging properties"
cat > broker-default.yaml << EOF
kind: ActiveMQArtemis
apiVersion: broker.amq.io/v1beta1
metadata:
  application: ex-aao-app
  name: ex-aao
  namespace: $NAMESPACE
spec:
  deploymentPlan:
    image: placeholder
    jolokiaAgentEnabled: false
    journalType: nio
    managementRBACEnabled: true
    messageMigration: false
    persistenceEnabled: false
    requireLogin: false
    size: 1
EOF

oc create -f broker-default.yaml
sleep 5
oc get pods
#oc wait --for=jsonpath='{.status.phase}'=Running pod/ex-aao-ss-0 --timeout=120s
oc wait --for=condition=Ready pod/ex-aao-ss-0 --timeout=120s
oc get pods

echo "4. getting default logging properties"
oc cp ex-aao-ss-0:/home/jboss/amq-broker/etc/log4j2.properties ./logging.properties

echo "5. create config map"
oc create configmap newlog4j-logging-config --from-file=logging.properties

echo "6. delete the broker, and create another with the logging.properties"
oc delete activemqartemis/ex-aao
oc wait --for=delete activemqartemis/ex-aao --timeout=180s


echo "7. Create broker, custom logging properties"
cat > broker-custom-logging.yaml << EOF
kind: ActiveMQArtemis
apiVersion: broker.amq.io/v1beta1
metadata:
  application: ex-aao2-app
  name: ex-aao2
  namespace: $NAMESPACE
spec:
  deploymentPlan:
    image: placeholder
    jolokiaAgentEnabled: false
    journalType: nio
    managementRBACEnabled: true
    messageMigration: false
    persistenceEnabled: false
    requireLogin: false
    size: 1
    extraMounts:
      configMaps:
      - "newlog4j-logging-config"
EOF

oc create -f broker-custom-logging.yaml
sleep 15
oc get pods
#oc wait --for=jsonpath='{.status.phase}'=Running pod/ex-aao-ss-0 --timeout=120s
oc wait --for=condition=Ready pod/ex-aao2-ss-0 --timeout=120s
oc get pods


echo "8. Show logs with exceptions"
oc logs pod/ex-aao2-ss-0
