#!/bin/bash -ex

curl https://pipeline.theorchard.io/jnlpJars/jenkins-cli.jar --output jenkins-cli.jar

java -version

declare -a arr=(\
'pull-request-builder.xml' \
'sonar-scan-builder.xml' \
'e2e-cucumber-tests-builder.xml' \
'integration-tests-builder.xml' \
'unit-lint-builder.xml' \
'docker-to-ecr-builder.xml' \
'docker-fargate-deploy-builder.xml' \
'pipeline-builder.xml' \
'validate-schema-builder.xml' \
'push-schema-builder.xml' \
)

for i in "${arr[@]}"
do
  sed -i 's/@SERVICE@/'$SERVICE'/g' graphql/$i
  sed -i 's/@GITHUB_CREDENTIAL_ID@/'$GITHUB_CREDENTIAL_ID'/g' graphql/$i
  sed -i 's/@AWS_ACCOUNT_ID@/'$AWS_ACCOUNT_ID'/g' graphql/$i
  sed -i 's/@ECR_CREDENTIAL_ID@/'$ECR_CREDENTIAL_ID'/g' graphql/$i
  sed -i 's/@SLACK_CREDENTIAL_ID@/'$SLACK_CREDENTIAL_ID'/g' graphql/$i
  sed -i 's/@SLACK_ROOMS@/'$SLACK_ROOMS'/g' graphql/$i
  sed -i 's/@ALLOWED_NODE@/ny-deploy/g' graphql/$i
  sed -i 's/@CUKES_CONFIG_AWS_CREDENTIAL_ID@/'$CUKES_CONFIG_AWS_CREDENTIAL_ID'/g' graphql/$i
  sed -i 's/@GITHUB_WEBHOOK_TOKEN_ID@/'$GITHUB_WEBHOOK_TOKEN_ID'/g' graphql/$i
  sed -i 's/@APOLLO_ENGINE_GRAPHQL_THEORCHARD_API_KEY@/'$APOLLO_ENGINE_GRAPHQL_THEORCHARD_API_KEY'/g' graphql/$i
done

java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-pull-request < graphql/pull-request-builder.xml
java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-e2e-cucumber-tests < graphql/e2e-cucumber-tests-builder.xml
java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-integration-tests < graphql/integration-tests-builder.xml
java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-unit-lint < graphql/unit-lint-builder.xml
java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-docker-to-ecr < graphql/docker-to-ecr-builder.xml
java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-docker-fargate-deploy < graphql/docker-fargate-deploy-builder.xml
java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-pipeline < graphql/pipeline-builder.xml
java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-validate-schema < graphql/validate-schema-builder.xml
java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-push-schema < graphql/push-schema-builder.xml
java -jar jenkins-cli.jar -s https://pipeline.theorchard.io/ -ssh -user ${CLI_USER} -i ${CLI_CREDENTIALS} create-job graphql-${SERVICE}-sonar-scan < graphql/sonar-scan-builder.xml
