pipeline {
    agent { label 'macos' }
       environment {
        HOME="${WORKSPACE}"
        TEST_BIGDATA="https://bytesalad.stsci.edu/artifactory/romanisim"
        CRDS_SERVER_URL = "https://roman-crds.stsci.edu"
        CRDS_PATH = "${WORKSPACE}/crds_cache"
        PYSYN_CDBS="${WORKSPACE}/cdbs"
        PATH ="${WORKSPACE}/miniconda3/bin:${PATH}"
        TMPDIR="${WORKSPACE}/tmp"
        XDG_CACHE_HOME="${WORKSPACE}/tmp/.cache"
    }

    stages {
    	  stage('Setup') {
            steps {
                deleteDir()
                checkout scm
                sh("curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -o installer.sh")
                sh("bash installer.sh -b -p ${WORKSPACE}/miniconda3")
        		sh("conda init bash")
                sh("conda create -n romanisim python -y")
            }
        }
        stage('Install') {
            steps {
                sh("""#!/usr/bin/env bash
                      source $WORKSPACE/miniconda3/etc/profile.d/conda.sh
                      conda activate romanisim
                      pip install -e .[test] pytest-xdist
                   """
                )
            }
        }
        stage('Test') {
            steps {
                sh("mkdir ${WORKSPACE}/data")
                sh("curl -L https://stsci.box.com/shared/static/0ojjfg3cieqdpd18vl1bjnpe63r82dx8.gz -o ${WORKSPACE}/data/webbpsf-data.tar.gz")
                sh("tar -xzvf ${WORKSPACE}/data/webbpsf-data.tar.gz -C ${WORKSPACE}/data")
                sh("mkdir ${WORKSPACE}/data/galsim-data")
                sh("curl -L https://github.com/GalSim-developers/GalSim/raw/releases/2.4/examples/data/real_galaxy_catalog_23.5_example.fits -o ${WORKSPACE}/data/galsim-data/real_galaxy_catalog_23.5_example.fits")      
                sh("curl -L https://github.com/GalSim-developers/GalSim/raw/releases/2.4/examples/data/real_galaxy_catalog_23.5_example_selection.fits -o ${WORKSPACE}/data/galsim-data/real_galaxy_catalog_23.5_example_selection.fits")      
                sh("curl -L https://github.com/GalSim-developers/GalSim/raw/releases/2.4/examples/data/real_galaxy_catalog_23.5_example_fits.fits -o ${WORKSPACE}/data/galsim-data/real_galaxy_catalog_23.5_example_fits.fits")      
                sh("""#!/usr/bin/env bash
                      source ${WORKSPACE}/miniconda3/etc/profile.d/conda.sh
                      export WEBBPSF_PATH="${WORKSPACE}/data/webbpsf-data"
                      export GALSIM_CAT_PATH="${WORKSPACE}/data/galsim-data/real_galaxy_catalog_23.5_example.fits"
                      conda activate romanisim
                      pytest romanisim \
                        --bigdata --slow --basetemp=test_outputs \
                        -n 0  # -n 8 causes problems with CRDS runs?
                   """
                )
            }
        }
    }
}