#!/bin/bash
# create_idr_venv 0.0.1
# Generated by dx-app-wizard.
#
# Basic execution pattern: Your app will run on a single machine from
# beginning to end.
#
# Your job's input variables (if any) will be loaded as environment
# variables before this script runs.  Any array inputs will be loaded
# as bash arrays.
#
# Any code outside of main() (or any entry point you may add) is
# ALWAYS executed, followed by running the entry point itself.
#
# See https://wiki.dnanexus.com/Developer-Portal for tutorials on how
# to modify this file.

main() {

    echo "Value of IDR_repo_tag: '$IDR_repo_tag'"

    # Fill in your application code here.
    #
    # To report any recognized errors in the correct format in
    # $HOME/job_error.json and exit this script, you can use the
    # dx-jobutil-report-error utility as follows:
    #
    #   dx-jobutil-report-error "My error message"
    #
    # Note however that this entire bash script is executed with -e
    # when running in the cloud, so any line which returns a nonzero
    # exit code will prematurely exit the script; if no error was
    # reported in the job_error.json file, then the failure reason
    # will be AppInternalError with a generic error message.

    # The following line(s) use the dx command-line tool to upload your file
    # outputs after you have created them on the local file system.  It assumes
    # that you have used the output field name for the filename for each output,
    # but you can change that behavior to suit your needs.  Run "dx upload -h"
    # to see more options to set metadata.

    sudo mv /etc/apt/apt.conf.d/99dnanexus /tmp
    sudo add-apt-repository -y ppa:deadsnakes/ppa
    sudo apt-get update
    sudo apt-get -y install python3.4-dev libfreetype6-dev

    python3.4 -m venv env3
    cd env3
    source bin/activate
    pip install --upgrade pip
    pip install scipy
    pip install numpy
    pip install matplotlib
    git clone --branch $IDR_repo_tag --recursive https://github.com/nboley/idr.git

    cd idr
    python setup.py install
    mkdir test
    cd test
    idr --plot --samples ../tests/data/peak1 ../tests/data/peak2
    ls -l
    deactivate

    cd ~
    venv_tarfile_name="idr_venv.tgz"
    tar -czvf $venv_tarfile_name /home/dnanexus/env3

    venv_tarfile=$(dx upload $venv_tarfile_name --brief)

    # The following line(s) use the utility dx-jobutil-add-output to format and
    # add output variables to your job's output as appropriate for the output
    # class.  Run "dx-jobutil-add-output -h" for more information on what it
    # does.

    dx-jobutil-add-output venv_tarfile "$venv_tarfile" --class=file
}
