#!/bin/bash
dockerfile_directory=${ZOOX_WORKSPACE_ROOT}/pipedream/slurm
webapp_dir="pipedream/slurm"
webapp_name=pipedream_slurm
bazel_target=pipedream_slurm
workspace_dir=$(pwd)

webapp_dir=pipedream/slurm

bazel build //${webapp_dir}:${bazel_target}

bin_dir="bazel-bin/"
docker_workspace="${webapp_name}-docker"
docker_image="docker-registry.zooxlabs.com/${webapp_name}"
tar_name="${webapp_name}.tar"
tar_path="${docker_workspace}/${tar_name}"
docker_sock=${DOCKER_SOCK:-''}

# Automatically set docker sock to default path if DOCKER_SOCK is not provided and default docker sock exists
if [[ -z "${docker_sock}" && -e "/var/run/docker.sock" ]]; then
    docker_sock="unix:///var/run/docker.sock"
else
    docker_sock=${DOCKER_SOCK:-"unix:///var/run/ci_docker.sock"}
fi

cd ${ZOOX_WORKSPACE_ROOT}/bazel-bin/

mkdir -p ${docker_workspace}

filelist="Dockerfile entrypoint.sh munge.key etc-default-munge poll_and_set.py poll_and_set_loop.sh app.py slurm.conf mysql_connection.py __init__.py"

for f in $filelist; do cp ${webapp_dir}/${bazel_target}.runfiles/zoox/${webapp_dir}/$f ${docker_workspace}; done;
cp ${ZOOX_WORKSPACE_ROOT}/infra/ansible/roles/apt_sources/files/llvm-snapshot.gpg.key ${docker_workspace}
cp ${ZOOX_WORKSPACE_ROOT}/infra/ansible/roles/apt_sources/files/ubuntu-toolchain-r-test.asc ${docker_workspace}
cp ${ZOOX_WORKSPACE_ROOT}/scripts/setup/get-pip.py ${docker_workspace}
cp ${ZOOX_WORKSPACE_ROOT}/scripts/ansible/roles/python_pip/files/pip.conf ${docker_workspace}

ls -l ${docker_workspace}

cp -r ${dockerfile_directory}/pyslurm_src ${docker_workspace}

tar --dereference -cf "$tar_path" ./${webapp_dir}

cd "${docker_workspace}"
docker --host "${docker_sock}" build . -t ${docker_image}

cd "${workspace_dir}"
