{%- set is_cmd_exe = build_platform == "windows" -%}
{%- macro env(key) -%}
{%- if is_cmd_exe %}{{ "%" ~ key ~ "%" }}{% else %}{{ "$" ~ key }}{% endif -%}
{% endmacro -%}

{# Platform-specific variables #}
{%- if is_cmd_exe -%}
{%- set R_CMD = "R.exe CMD" -%}
{%- set LIBRARY_DIR = "%LIBRARY_PREFIX%\\R\\library" -%}
{%- set LINE_CONT = "^" -%}
{%- else -%}
{%- set R_CMD = "R CMD" -%}
{%- set LIBRARY_DIR = "$PREFIX/lib/R/library" -%}
{%- set LINE_CONT = "\\" -%}
{%- endif -%}

{# Set up R CMD INSTALL arguments #}
{%- set install_args = [
    "--library=" ~ LIBRARY_DIR,
    "--no-lock",
] + extra_args
-%}

{# Output version information for debugging #}
{% if is_cmd_exe -%}
R.exe --version
@if errorlevel 1 exit 1
{% else -%}
R --version
{% endif %}

{# Create library directory if it doesn't exist #}
{% if is_cmd_exe -%}
if not exist "{{ LIBRARY_DIR }}" mkdir "{{ LIBRARY_DIR }}"
@if errorlevel 1 exit 1
{% else -%}
mkdir -p "{{ LIBRARY_DIR }}"
{% endif %}

{# Run R CMD INSTALL #}
{{ R_CMD }} INSTALL {{ install_args | join(" " ~ LINE_CONT ~ "\n    ") }} "{{ source_dir }}"

{# Check for errors #}
{% if is_cmd_exe -%}
@if errorlevel 1 exit 1
{% else -%}
if [ $? -ne 0 ]; then
    exit 1
fi
{% endif %}
