#!/bin/bash
set -e
set -o xtrace

if [ "$#" -ne 2 ]; then
  echo "
  Usage: <distro> <[script to run] OR build>
    distro: the distro to install the software for
    script_to_run: Execute the install script loacted in HIPSYCL_PKG_SCRIPT_DIR, ( by defauld install/scripts)
    build: Build the base image into the HIPSYCL_PKG_CONTAINER_DIR folder. the scripts that are necessary are copid to the image
        during the build time the definition file is located at: HIPSYCL_PKG_SCRIPT_DIR/base-<distro>.def

  Important ENV variables:
    - HIPSYCL_PKG_CONTAINER_DIR
  "
  exit -1
fi
distro=$1
candidate=$2

HIPSYCL_PKG_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
HIPSYCL_PKG_CONTAINER_DIR=${HIPSYCL_PKG_CONTAINER_DIR:-$HIPSYCL_PKG_SCRIPT_DIR/containers}
HIPSYCL_PKG_LLVM_REPO_BRANCH=${HIPSYCL_PKG_LLVM_REPO_BRANCH:-release/9.x}
echo $HIPSYCL_PKG_CONTAINER_DIR
HIPSYCL_PKG_BUILD_CUDA=${HIPSYCL_PKG_BUILD_CUDA:-ON}
HIPSYCL_PKG_BUILD_ROCM=${HIPSYCL_PKG_BUILD_ROCM:-ON}
HIPSYCL_PKG_BUILD_BASE=${HIPSYCL_PKG_BUILD_BASE:-ON}

SINGULARITYENV_HIPSYCL_PKG_BUILD_CUDA=$HIPSYCL_PKG_BUILD_CUDA
SINGULARITYENV_HIPSYCL_PKG_BUILD_ROCM=$HIPSYCL_PKG_BUILD_ROCM
SINGULARITYENV_HIPSYCL_PKG_BUILD_BASE=$HIPSYCL_PKG_BUILD_BASE
SINGULARITYENV_HIPSYCL_PKG_LLVM_REPO_BRANCH=$HIPSYCL_PKG_LLVM_REPO_BRANCH
SINGULARITYENV_HIPSYCL_PKG_LLVM_VERSION_MAJOR=$HIPSYCL_PKG_LLVM_VERSION_MAJOR
SINGULARITYENV_HIPSYCL_PKG_LLVM_VERSION_MINOR=$HIPSYCL_PKG_LLVM_VERSION_MINOR
SINGULARITYENV_HIPSYCL_PKG_LLVM_VERSION_PATCH=$HIPSYCL_PKG_LLVM_VERSION_PATCH
SINGULARITYENV_HIPSYCL_PKG_AOMP_RELEASE=$HIPSYCL_PKG_AOMP_RELEASE
SINGULARITYENV_HIPSYCL_PKG_AOMP_TAG=$HIPSYCL_PKG_AOMP_TAG
SINGULARITYENV_HIPSYCL_BUILD_DIR_PREFIX="/tmp/hipsycl-build-for-"
SINGULARITYENV_HIPSYCL_BUILD_DIR=$SINGULARITYENV_HIPSYCL_BUILD_DIR_PREFIX$distro

export SINGULARITYENV_HIPSYCL_PKG_BUILD_CUDA
export SINGULARITYENV_HIPSYCL_PKG_BUILD_ROCM
export SINGULARITYENV_HIPSYCL_PKG_BUILD_BASE
export SINGULARITYENV_HIPSYCL_PKG_LLVM_REPO_BRANCH 
export SINGULARITYENV_HIPSYCL_PKG_LLVM_VERSION_MAJOR
export SINGULARITYENV_HIPSYCL_PKG_LLVM_VERSION_MINOR 
export SINGULARITYENV_HIPSYCL_PKG_LLVM_VERSION_PATCH 
export SINGULARITYENV_HIPSYCL_PKG_AOMP_RELEASE
export SINGULARITYENV_HIPSYCL_PKG_AOMP_TAG
export SINGULARITYENV_HIPSYCL_BUILD_DIR


echo $HIPSYCL_PKG_CONTAINER_DIR
cd $HIPSYCL_PKG_SCRIPT_DIR
mkdir -p $HIPSYCL_PKG_CONTAINER_DIR
mkdir -p /tmp/hipsycl-pkg-builder

if [ "$candidate" = "build" ]; then
  echo "Building $distro image... with base pkgs"
  singularity build --fakeroot --sandbox -F $HIPSYCL_PKG_CONTAINER_DIR/hipsycl-$distro base-definitions/$distro.def
  echo "Building $distro hipSYCL base via spack"
elif [ "$candidate" = "cleanup" ]; then
  rm -rf $SINGULARITYENV_HIPSYCL_BUILD_DIR
else
  #echo $HIPSYCL_PKG_LLVM_VERSION_MAJOR
  tmpdir=$HIPSYCL_PKG_CONTAINER_DIR/tmp-$distro
  mkdir -p $tmpdir
  singularity -d exec --fakeroot --writable --no-home  -B $HIPSYCL_PKG_SCRIPT_DIR:/mnt \
  $HIPSYCL_PKG_CONTAINER_DIR/hipsycl-$distro \
  bash /mnt/$candidate.sh
fi
