#!/bin/bash
set -euo pipefail

VERSION=7.6.0.fuse-760021-redhat-00001

if [[ ! -f fuse-karaf-${VERSION}.zip ]]; then
  curl -sSLO http://indy.psi.redhat.com/api/content/maven/hosted/pnc-builds/org/jboss/fuse/fuse-karaf/${VERSION}/fuse-karaf-${VERSION}.zip
fi

echo "$(curl -s http://indy.psi.redhat.com/api/content/maven/hosted/pnc-builds/org/jboss/fuse/fuse-karaf/${VERSION}/fuse-karaf-${VERSION}.zip.sha1) fuse-karaf-${VERSION}.zip" | sha1sum -c -

rm -rf fuse-karaf-${VERSION}
unzip -q fuse-karaf-${VERSION}.zip
cd fuse-karaf-${VERSION}
patch etc/users.properties << EOF
30,31c30,31
< #admin = admin,_g_:admingroup
< #_g_\:admingroup = group,admin,manager,viewer,systembundles,ssh
---
> admin = admin,_g_:admingroup
> _g_\:admingroup = group,admin,manager,viewer,systembundles,ssh
EOF
# commented out to leave the default memory
#patch bin/setenv << EOF
#40c40
#< # export JAVA_MAX_MEM # Maximum memory for the JVM
#---
#> export JAVA_MAX_MEM=4G # Maximum memory for the JVM
#EOF
patch etc/org.ops4j.pax.url.mvn.cfg << EOF
--- etc/org.ops4j.pax.url.mvn.cfg.orig	2020-02-14 18:04:24.918640459 +0100
+++ etc/org.ops4j.pax.url.mvn.cfg	2020-02-14 18:08:52.991824343 +0100
@@ -76,7 +76,8 @@
     https://repo1.maven.org/maven2@id=maven.central.repo, \\
     https://maven.repository.redhat.com/ga@id=redhat.ga.repo, \\
     https://maven.repository.redhat.com/earlyaccess/all@id=redhat.ea.repo, \\
-    https://repository.jboss.org/nexus/content/groups/ea@id=fuseearlyaccess
+    https://repository.jboss.org/nexus/content/groups/ea@id=fuseearlyaccess, \\
+    http://nexus.fuse-qe.eng.rdu2.redhat.com/repository/fuse7-all/@id=qe.all
 
 #
 # If set to true, http://repo1.maven.org/maven2@id=central will be used as additional remote repository.
EOF
java -version

watch_karaf() {
  while [[ ! -f karaf.pid ]]; do
    sleep 0.5
  done
  jstat -gcutil -t $(cat karaf.pid) 3s
}
watch_karaf &

bin/fuse << EOF > fuse.log
la
feature:install camel-elasticsearch-rest
la
bundle:uninstall 249
bundle:uninstall 250
bundle:install mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.elasticsearch-client/6.1.1_2
bundle:install mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.elasticsearch/6.1.1_2
bundle:install mvn:org.apache.logging.log4j/log4j-core/2.13.0
feature:install camel-elasticsearch

EOF
