#!/usr/bin/env bash
# purpose: Create package and place it in /dock
# requires: /gridded_glm to be mounted with package contents (gridded_glm in repository)
set -ex

USAGE="Usage: package.sh vX.Y.Z [lmatools_ref glmtools_ref]"
if [ $# -eq 1 ]; then
    version=$1
    lmatools_ref=minvaluegrids
    glmtools_ref=ugf-newgrid
else if [ $# -eq 3 ]; then
    version=$1
    lmatools_ref=minvaluegrids
    glmtools_ref=ugf-newgrid
else
    echo $USAGE
    exit 1
fi

# conda activate build

# Debug Info
which python
conda info -a
conda list --export
echo "Version specified: ${version}"
echo "lmatools reference: ${lmatools_ref}"
echo "glmtools reference: ${glmtools_ref}"

# Install glmtools and related packages
# All dependencies should have been built with the buildbucket
# Careful: Could result in missing dependencies but we want reproducibility
pip install --no-deps git+https://github.com/deeplycloudy/lmatools.git@${lmatools_ref}
pip install --no-deps git+https://github.com/deeplycloudy/stormdrain.git
pip install --no-deps git+https://github.com/deeplycloudy/glmtools.git@${glmtools_ref}

# Build a tarball version of the current conda environment
conda_tb=conda_lmatools-${lmatools}_glmtools-${glmtools_ref}.tar.gz
conda pack -n build -o ${conda_tb}

# Build up our package directory
pkg_name=cspp_geo_gridded_glm-${version}
mkdir -p ${pkg_name}
cd ${pkg_name}
mkdir -p bin opt/conda
# Copy package scripts/data to package directory
cp /gridded_glm/PACKAGE_README.md ./README.md
cp /gridded_glm/bin/* ./bin/
# Untar the tarball so we can put things where we want
tar -xz -C ./opt/conda -f ../${conda_tb}
# Go back to original work directory
cd ..

# Create tarball of package directory
tar -czf ${pkg_name}.tar.gz ${pkg_name}
mv ${pkg_name}.tar.gz /dock/

