SONARQUBE_HOST=sonarqube:9000

echo "Waiting for sonarqube to be ready..."

until [ "$(curl --silent --show-error --connect-timeout 1 http://${SONARQUBE_HOST}/api/system/status | grep 'UP')" ];
do
  sleep 5
done
echo "SonarQube reporting UP.  Wait 5 seconds to be sure."
sleep 5

echo "Updating admin password on host ${SONARQUBE_HOST}"
curl -u admin:admin -X POST "http://${SONARQUBE_HOST}/api/users/change_password?login=admin&previousPassword=admin&password=${SONARQUBE_PASSWORD}"

echo "Disabling force authentication for demo purposes"
curl -u admin:${SONARQUBE_PASSWORD} --request POST "http://sonarqube:9000/api/settings/set?key=sonar.forceAuthentication&value=false"

echo "Installing plugins ${SONARQUBE_PLUGINS} on server ${SONARQUBE_HOST}"

for i in ${SONARQUBE_PLUGINS//,/ }
do
  echo "Installing $i"
  curl -u admin:${SONARQUBE_PASSWORD} --data "key=$i" http://${SONARQUBE_HOST}/api/plugins/install
done

echo "Restarting server ${SONARQUBE_HOST} ..."
curl  -u admin:${SONARQUBE_PASSWORD} -X POST http://${SONARQUBE_HOST}/api/system/restart
