distributable:
  url: https://github.com/erlang/otp/releases/download/OTP-{{ version.raw }}/otp_src_{{ version.raw }}.tar.gz
  strip-components: 1

provides:
  - bin/ct_run
  - bin/dialyzer
  - bin/epmd
  - bin/erl
  - bin/erlc
  - bin/escript
  - bin/run_erl
  - bin/to_erl
  - bin/typer

interprets:
  extensions: erl
  args: escript

versions:
  github: erlang/otp/releases
  strip: /^OTP /

dependencies:
  openssl.org: ^1.1
  invisible-island.net/ncurses: '*'
  linux/x86-64:
    gnu.org/gcc/libstdcxx: '*'

runtime:
  env:
    ERL_ROOTDIR: ${{prefix}}/lib/erlang

build:
  dependencies:
    perl.org: '>=5'
    linux/x86-64:
      gnu.org/gcc: '*'
  script:
    - ./configure $ARGS
    - make -j {{hw.concurrency}}
    - make install
  env:
    CC: cc
    CXX: c++
    LD: ld
    CFLAGS: -O2 -g $CFLAGS
    ERL_TOP: $SRCROOT
    ARGS:
      - --disable-debug
      - --disable-silent-rules
      - --prefix={{prefix}}
      - --enable-dynamic-ssl-lib
      - --enable-hipe
      - --enable-smp-support
      - --enable-threads
      - --enable-pie
      - --with-ssl={{deps.openssl.org.prefix}}
      - --without-javac
    linux/x86-64:
      CC: gcc
      CXX: g++
      LD: gcc
    darwin:
      ARGS:
        - --enable-darwin-64bit
        - --enable-kernel-poll
        - --with-dynamic-trace=dtrace

test:
  dependencies:
    pkgx.sh: 1
  script:
    - epmd -kill || true
    - epmd -daemon -address 127.0.0.1 -relaxed_command_check
    - run: test "$(escript $FIXTURE 10)" = "factorial 10 = 3628800"
      fixture:
        extname: erl
        content: |
          #!/usr/bin/env escript
          %% -*- erlang -*-
          %%! -smp enable -sname factorial -mnesia debug verbose
          main([String]) ->
              try
                  N = list_to_integer(String),
                  F = fac(N),
                  io:format("factorial ~w = ~w\n", [N,F])
              catch
                  _:_ ->
                      usage()
              end;
          main(_) ->
              usage().

          usage() ->
              io:format("usage: factorial integer\n").

          fac(0) -> 1;
          fac(N) -> N * fac(N-1).
    - epmd -kill || true
  env:
    ERL_DIST_PORT: $(pkgx get-port | tail -n1)
