const shell = require("shelljs");
const getVersion = require("./services/version");

module.exports = async (options, logger) => {
  let {
    namespace = process.env.NAMESPACE,
    build_id = process.env.BUILD_ID,
    version = process.env.VERSION,
    assembly_version = process.env.ASSEMBLY_VERSION,
    branch_slug
  } = getVersion(options, logger);

  let suffix = process.env.BITBUCKET_REPO_SLUG ? " >> ~/.bashrc" : "";

  shell.env["NAMESPACE"] = namespace;
  shell.exec(`echo 'export NAMESPACE=${namespace}' ${suffix}`);
  shell.exec("echo NAMESPACE = $NAMESPACE");

  shell.env["BUILD_ID"] = build_id;
  shell.exec(`echo 'export BUILD_ID=${build_id}' ${suffix}`);
  shell.exec("echo BUILD_ID = $BUILD_ID");

  shell.env["VERSION"] = version;
  shell.exec(`echo 'export VERSION=${version}' ${suffix}`);
  shell.exec("echo VERSION = $VERSION");

  shell.env["ASSEMBLY_VERSION"] = assembly_version;
  shell.exec(`echo 'export ASSEMBLY_VERSION=${assembly_version}' ${suffix}`);
  shell.exec("echo ASSEMBLY_VERSION = $ASSEMBLY_VERSION");

  shell.env["BRANCH_SLUG"] = branch_slug;
  shell.exec(`echo 'export BRANCH_SLUG=${branch_slug}' ${suffix}`);
  shell.exec("echo BRANCH_SLUG = $BRANCH_SLUG");
};
