#!/usr/bin/env modbuild

pbuild::add_to_group 'MX'


pbuild::prep() {
    :
}

pbuild::configure() {
    #BUILD CONDA ENV with miniconda installation:
    #
    #MINICONDA INSTALL
    mkdir "$PREFIX/miniconda"
    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O "$PREFIX/miniconda/miniconda.sh"
    bash "$PREFIX/miniconda/miniconda.sh" -b -u -p "$PREFIX/miniconda/"

    #FOR DEBUGGING AND CHECKING MINICONDA INSTALLATION
    "$PREFIX/miniconda/condabin/conda" config --show

    #CREATE ENV , make sure to source the "correct" conda.sh
    "$PREFIX/miniconda/condabin/conda" env create --name "alphafold_$V" -f "$BUILDBLOCK_DIR/environment.yml"
    source "$PREFIX/miniconda/etc/profile.d/conda.sh"
    conda activate  "alphafold_$V"
    pip3 install --upgrade --no-cache-dir  jax==0.3.25  jaxlib==0.3.25+cuda11.cudnn805  -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html
    conda deactivate 
}

pbuild::compile() {
    
    ALPHAFOLD_HOME="$PREFIX/alphafold"
    #local BRANCH
    if [[ "${#V_RELEASE}" -eq 7 ]]; then
    #Release looks like a git hash
       BRANCH="${V_RELEASE}"
    else
       #choose the  given version(e.g. a tag)  as branch or choose main
       #BRANCH="v${V_PKG}"
       BRANCH="main"
    fi
  
    git clone --depth=1 -b "$BRANCH" https://github.com/deepmind/alphafold.git "$ALPHAFOLD_HOME" || return $?

    if ! [ -f "$ALPHAFOLD_HOME/alphafold/common/stereo_chemical_props.txt" ]; then
        
    curl -fLsS -o "$ALPHAFOLD_HOME/alphafold/common/stereo_chemical_props.txt" \
         https://git.scicore.unibas.ch/schwede/openstructure/-/raw/7102c63615b64735c4941278d92b554ec94415f8/modules/mol/alg/src/stereo_chemical_props.txt
    fi
        
}

pbuild::install() {
    
    cp -r "$BUILDBLOCK_DIR/bin" "$PREFIX/"
    sed -i "s/ALPHAFOLD_VERSION/$V/g" "$PREFIX/bin/"*
}

