## Installation

### Requirements

This package has been tested on Linux (openSUSE Leap 15.0 x64), Windows (7 and 10) and macOS (10.15 Catalina). It is developed with cross-platform languages (Rust and R) so it is supposed to work on other platforms. Package is available as binaries for Windows and macOS and as sources, that need to be compiled, for other operating systems.

Software requirements for the binary installation are the following:

* **R** (package is developed with version 4.0.2)
* **airGR** R package (available on CRAN or [here](https://webgr.irstea.fr/airGR-website))

For the source compilation, in addition to the requirements above, the following are needed:

* **Rtools** for Windows (available [here](https://cran.r-project.org/bin/windows/Rtools/)) or a building environmment on other platforms, generally installed with R on linux distributions. At least, a C/C++ compiler like a recent version of `gcc`
* **cargo** Rust compilation platform, version 1.42 or above. Detailed installation instructions are given below.

### Rust platform

General instructions to install the Rust compilation platform are given on the website [RustUp](https://rustup.rs/). It can be installed without administrator privileges - you do not need to ask your local computer technician that does not know Rust.

#### Prior notice: PATH environment variable modification

By default, the Rust compilation plaform is installed in `$HOME/.cargo` on Unix platforms and `%USERPROFILE%/.cargo` on Windows. This directory is then added to the PATH environment variable for the user to be able to use the toolchain without refering to the installation directory. 

If you plan to use Rust for other purpose than installing `baseflow`, it is recommanded to set the *modify PATH variable* to *yes*. But if you only want to build and install *baseflow*, to avoid any side-effect of the installation, you should set this option to *no* as explained below.
CRAN policy prevents published R packages to alter the user environment or filespace apart from the R session. Therefore, you should only change your PATH global variable if you know what you are doing.

#### Windows

On Windows, download and lauch the rustup-init.exe script (64 bits version is [here](https://win.rustup.rs/x86_64), 32 bits is [here](https://win.rustup.rs/i686)). In the command prompt window, choose the second option `Customize installation` and set configuration as below.

```
default host triple: i686-pc-windows-gnu
default toolchain: stable
modify PATH variable: no
```

Then, choose the first option `Proceed with installation`.

On 64-bits Windows systems, you also need to install the x64 target. After installation, open a terminal and type:

```
%USERPROFILE%\cargo\.bin\rustup.exe target add x86_64-pc-windows-gnu
```

#### Unix systems (GNU/Linux and macOS)

On GNU/Linux platforms and other Unix systems, launch the following command into a terminal:

```
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
Here again, chose the second option `Customize installation` and set configuration as below.

For GNU/Linux 64 bits systems:

```
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable
modify PATH variable: no
```

For GNU/Linux 32 bits systems:

```
default host triple: i686-unknown-linux-gnu
default toolchain: stable
modify PATH variable: no
```

For macOS with Intel x86-64 architecture:

```
default host triple: x86_64-apple-darwin
default toolchain: stable
modify PATH variable: no
```

For macOS with ARM64 architecture (Apple Silicon):

```
default host triple: aarch64-apple-darwin
default toolchain: stable
modify PATH variable: no
```

For other platforms, detailed information about Rust support can be found [here](https://doc.rust-lang.org/rustc/platform-support.html).
  
After modifying configuration, choose option 1 `Proceed with installation`.

### Package compilation and installation

Download the source package as a `.tar.gz` file. Start R and run the following command in the console, where `path_to_package` is the path of the downloaded `.tar.gz` file of the package.

```
install.packages("path_to_package", repos = NULL, type = "source")
```  

On Windows, you can directly download the binary `.zip` file, and run the following command, where `path_to_binary_package` is the path of the downloaded `.zip` file of the package.

```
install.packages("path_to_binary_package", repos = NULL, type = "win.binary")
```
  
After installation, try to load package using command

```
library(baseflow)
```  

### Uninstalling Rust platform after installation

To uninstall Rust toolchain after installing the package, use the following commands:

On Windows:

```
%USERPROFILE%\cargo\.bin\rustup.exe self uninstall
```

On Unix-based systems (macOS and GNU/Linux):
  
```
~/cargo/.bin/rustup self uninstall
```
