#!/bin/bash

set -eux

if [ "${DIB_MIDONET_release}" = "OSS" ]; then
  MIDONET_REPOS_FILE=midonet-oss
elif [ "${DIB_MIDONET_release}" = "MEM" ]; then
  MIDONET_REPOS_FILE=midonet-mem
else
  echo "DIB_MIDONET_release is not set to 'OSS' or 'MEM'" >&2
  exit 1;
fi

MIDONET_REPOS_DIR=$TMP_MOUNT_PATH/etc/yum.repos.d
MIDONET_TARGET_FILE_NAME=midonet.repo
DATASTAX_REPOS_FILE=datastax
DATASTAX_TARGET_FILE_NAME=datastax.repo
ELASTICSEARCH_REPOS_FILE=elasticsearch
ELASTICSEARCH_TARGET_FILE_NAME=elasticsearch.repo
LOGSTASH_REPOS_FILE=logstash
LOGSTASH_TARGET_FILE_NAME=logstash.repo
LLPD_REPOS_FILE=vbernat-llpd
LLPD_TARGET_FILE_NAME=vbernat-llpd.repo
CURATOR_REPOS_FILE=curator
CURATOR_TARGET_FILE_NAME=curator.repo

mkdir -p $MIDONET_REPOS_DIR

# Replace the env vars of the MEM repo using env vars
# http://superuser.com/questions/235738/how-do-i-substitute-environment-variables-when-i-ouput-a-file
function subst() { eval echo -E "$2"; }
mapfile -c 1 -C subst < $TMP_HOOKS_PATH/extra-data.d/$MIDONET_REPOS_FILE > /tmp/repo-midonet
mapfile -c 1 -C subst < $TMP_HOOKS_PATH/extra-data.d/$DATASTAX_REPOS_FILE > /tmp/repo-cassandra
mapfile -c 1 -C subst < $TMP_HOOKS_PATH/extra-data.d/$ELASTICSEARCH_REPOS_FILE > /tmp/repo-elasticsearch
mapfile -c 1 -C subst < $TMP_HOOKS_PATH/extra-data.d/$LOGSTASH_REPOS_FILE > /tmp/repo-logstash
mapfile -c 1 -C subst < $TMP_HOOKS_PATH/extra-data.d/$LLPD_REPOS_FILE > /tmp/repo-llpd
mapfile -c 1 -C subst < $TMP_HOOKS_PATH/extra-data.d/$CURATOR_REPOS_FILE > /tmp/repo-curator

sudo mv /tmp/repo-midonet $MIDONET_REPOS_DIR/$MIDONET_TARGET_FILE_NAME
sudo mv /tmp/repo-cassandra $MIDONET_REPOS_DIR/$DATASTAX_TARGET_FILE_NAME
sudo mv /tmp/repo-elasticsearch $MIDONET_REPOS_DIR/$ELASTICSEARCH_TARGET_FILE_NAME
sudo mv /tmp/repo-logstash $MIDONET_REPOS_DIR/$LOGSTASH_TARGET_FILE_NAME
sudo mv /tmp/repo-llpd $MIDONET_REPOS_DIR/$LLPD_TARGET_FILE_NAME
sudo mv /tmp/repo-curator $MIDONET_REPOS_DIR/$CURATOR_TARGET_FILE_NAME

exit 0
