#!/usr/bin/env bash

BASE_DIR="$(pwd)"

CYAN="\033[96m"
RED="\033[91m"
RESET="\033[0m"
BOLD="\033[1m"

TMP=${BASE_DIR}/tmp
CHART_TEMPLATE=${TMP}/helm

PKG="${1}"
PKG_VERSION="${2}"
REGISTRY="${3}"
REGISTRY_ORG="${4}"
IMAGE_PREFIX="${5}"
PUSH="${6}"
PODMAN_CONTAINER="${7}"

PKG_NAME="${PKG}-${PKG_VERSION}"

# --------------------------------------------------------------------------------
# Create the container image
# --------------------------------------------------------------------------------
mkdir -p ${TMP}/container
cp -R ${CHART_TEMPLATE}/* ${TMP}/container/    

# Copy the plugin assets
mkdir -p ${TMP}/container/plugin

# Copy Helm charts and plugin assets
cp -R ${BASE_DIR}/assets/* ${TMP}/container/plugin
cp ${BASE_DIR}/package.json ${TMP}/container/plugin
cp ${BASE_DIR}/index.yaml ${TMP}/container/
cp ${BASE_DIR}/index.yaml ${TMP}/container/plugin
rm -f ${TMP}/container/plugin/report.html

# Add extension .tgz files with proper naming
echo -e "${CYAN}Copying extension archives...${RESET}"
for pkg_dir in ${BASE_DIR}/extensions/*/; do
  pkg=$(basename "${pkg_dir}")
  echo -e "Processing package: ${pkg}"
  
  # Find all .tgz files in the package directory
  for tgz_path in "${pkg_dir}"*.tgz; do
    if [ -f "${tgz_path}" ]; then
      # Extract version from filename
      version=$(basename "${tgz_path}" .tgz)
      compressed_package_name="${pkg}-${version}.tgz"
      
      echo "Copying ${tgz_path} to ${TMP}/container/plugin/${compressed_package_name}"
      cp "${tgz_path}" "${TMP}/container/plugin/${compressed_package_name}"
    fi
  done
done

# Generate files.txt for each pkg and move pkg files into relative plugin directories
for d in ${BASE_DIR}/dist-pkg/*; do
  pkg=$(basename $d)

  pushd ${BASE_DIR}/dist-pkg > /dev/null
  mkdir plugin && mv ./${pkg}/* ./plugin
  rm -rf ./${pkg}/* && mv ./plugin ./${pkg}

  find ${pkg} -type f | sed "s|^${pkg}/||" | sort > ./${pkg}/files.txt
  popd > /dev/null

  cp -R ${BASE_DIR}/dist-pkg/${pkg} ${TMP}/container/plugin
done

package() {
  if [ "${PODMAN_CONTAINER}" == "true" ]; then
    RUNTIME="podman"
  else
    RUNTIME="docker"
  fi
  echo -e "Container build: ${RUNTIME}"

  REGISTRY=${REGISTRY} ORG=${REGISTRY_ORG} REPO=${IMAGE_PREFIX}${PKG} TAG=${PKG_VERSION} RUNTIME=${RUNTIME} ./scripts/package

  if [ "${PUSH}" == "--push" ]; then
    echo -e "${CYAN}Pushing container image ...${RESET}"

    # Ensure that you do not overwrite production images
    if [[ "${REGISTRY_ORG}" == "rancher" ]]; then
      IMAGE=${REGISTRY}${REGISTRY_ORG}/${IMAGE_PREFIX}${PKG}:${PKG_VERSION}
      if ${RUNTIME} manifest inspect ${IMAGE} 2>&1 1>/dev/null; then
        echo -e "${RED}${BOLD}Cannot overwrite production image ${IMAGE_PREFIX}${PKG} since it already exists${RESET}"
        exit 1
      fi
    fi

    ${RUNTIME} push ${REGISTRY}${REGISTRY_ORG}/${IMAGE_PREFIX}${PKG}:${PKG_VERSION}
  fi
}

# Build the container image
pushd ${TMP}/container > /dev/null
echo -e "${CYAN}Building container image ...${RESET}"

if [ ! -z "${REGISTRY}" ]; then
  package
fi

popd > /dev/null