#!/usr/bin/env bash
set -euo pipefail

PROJECT_DIR="${CI_PROJECT_DIR:-$PWD}"
PLATFORM="${PLATFORM_VER:?PLATFORM_VER must be set}"
ARTIFACT_DIR="${PROJECT_DIR}/data/mac_os_x/${PLATFORM}"

mkdir -p "${ARTIFACT_DIR}"

if ls "${PROJECT_DIR}"/data/cinc-workstation*dmg* >/dev/null 2>&1; then
  sudo mv -v "${PROJECT_DIR}"/data/cinc-workstation*dmg* "${ARTIFACT_DIR}/"
else
  echo "No macOS dmg artifacts found to move"
fi

sudo rm -rf \
  /opt/cinc-workstation/ \
  /var/cache/omnibus/* \
  "${PROJECT_DIR}"/data/cinc-workstation*pkg*
