%%%----------------------------------------------------------------------
%%%
%%% ejabberd, Copyright (C) 2002-2024   ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------

%%%
%%% Dependencies
%%%

{deps, [{if_not_rebar3,
         {if_version_below, "24",
          {base64url, "~> 1.0", {git, "https://github.com/dvv/base64url", {tag, "1.0.1"}}}
        }},
        {cache_tab, "~> 1.0.30", {git, "https://github.com/processone/cache_tab", {tag, "1.0.31"}}},
        {eimp, "~> 1.0.22", {git, "https://github.com/processone/eimp", {tag, "1.0.23"}}},
        {if_var_true, pam,
         {epam, "~> 1.0.14", {git, "https://github.com/processone/epam", {tag, "1.0.14"}}}},
        {if_var_true, redis,
         {if_not_rebar3,
          {eredis, "~> 1.2.0", {git, "https://github.com/wooga/eredis/", {tag, "v1.2.0"}}}
        }},
        {if_var_true, redis,
         {if_rebar3,
          {if_version_below, "21",
           {eredis, "1.2.0", {git, "https://github.com/wooga/eredis/", {tag, "v1.2.0"}}},
           {eredis, "~> 1.7.1", {git, "https://github.com/Nordix/eredis/", {tag, "v1.7.1"}}}
        }}},
        {if_var_true, sip,
         {esip, "~> 1.0.52", {git, "https://github.com/processone/esip", {tag, "1.0.56"}}}},
        {if_var_true, zlib,
         {ezlib, "~> 1.0.12", {git, "https://github.com/processone/ezlib", {tag, "1.0.13"}}}},
        {fast_tls, "~> 1.1.19", {git, "https://github.com/processone/fast_tls", {tag, "1.1.22"}}},
        {fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.53"}}},
        {fast_yaml, "~> 1.0.36", {git, "https://github.com/processone/fast_yaml", {tag, "1.0.37"}}},
        {idna, "~> 6.0", {git, "https://github.com/benoitc/erlang-idna", {tag, "6.0.0"}}},
        {if_version_below, "27",
         {jiffy, "~> 1.1.1", {git, "https://github.com/davisp/jiffy", {tag, "1.1.1"}}}
        },
        {if_version_above, "23",
         {jose, "~> 1.11.10", {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.11.10"}}},
         {jose, "1.11.1", {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.11.1"}}}
        },
        {if_version_below, "22",
         {lager, "~> 3.9.1", {git, "https://github.com/erlang-lager/lager", {tag, "3.9.1"}}}
        },
        {if_var_true, lua,
         {if_version_below, "21",
          {luerl, "1.0.0", {git, "https://github.com/rvirding/luerl", {tag, "1.0"}}},
          {luerl, "~> 1.2.0", {git, "https://github.com/rvirding/luerl", {tag, "1.2"}}}
        }},
        {mqtree, "~> 1.0.16", {git, "https://github.com/processone/mqtree", {tag, "1.0.17"}}},
        {p1_acme, "~> 1.0.23", {git, "https://github.com/processone/p1_acme", {tag, "1.0.24"}}},
        {if_var_true, mysql,
         {p1_mysql, "~> 1.0.24", {git, "https://github.com/processone/p1_mysql", {tag, "1.0.25"}}}},
        {p1_oauth2, "~> 0.6.14", {git, "https://github.com/processone/p1_oauth2", {tag, "0.6.14"}}},
        {if_var_true, pgsql,
         {p1_pgsql, "~> 1.1.26", {git, "https://github.com/processone/p1_pgsql", {tag, "1.1.28"}}}},
        {p1_utils, "~> 1.0.25", {git, "https://github.com/processone/p1_utils", {tag, "1.0.26"}}},
        {pkix, "~> 1.0.10", {git, "https://github.com/processone/pkix", {tag, "1.0.10"}}},
        {if_var_true, sqlite,
         {sqlite3, "~> 1.1.14", {git, "https://github.com/processone/erlang-sqlite3", {tag, "1.1.15"}}}},
        {stringprep, "~> 1.0.29", {git, "https://github.com/processone/stringprep", {tag, "1.0.30"}}},
        {if_var_true, stun,
         {stun, "~> 1.2.12", {git, "https://github.com/processone/stun", {tag, "1.2.15"}}}},
        {xmpp, "~> 1.9.0", {git, "https://github.com/processone/xmpp", "855d974e122c8357d94c983b85342112c5f5531f"}},
        {yconf, ".*", {git, "https://github.com/processone/yconf", "9898754f16cbd4585a1c2061d72fa441ecb2e938"}}
       ]}.

{gitonly_deps, [ejabberd_po]}.

{if_var_true, latest_deps,
 {floating_deps, [cache_tab,
                  eimp,
                  epam,
                  esip,
                  ezlib,
                  fast_tls,
                  fast_xml,
                  fast_yaml,
                  mqtree,
                  p1_acme,
                  p1_mysql,
                  p1_oauth2,
                  p1_pgsql,
                  p1_utils,
                  pkix,
                  sqlite3,
                  stringprep,
                  stun,
                  xmpp,
                  yconf]}}.

%%%
%%% Compile
%%%

{recursive_cmds, ['configure-deps']}.

{post_hook_configure, [{"eimp", []},
                       {if_var_true, pam, {"epam", []}},
                       {if_var_true, sip, {"esip", []}},
                       {if_var_true, zlib, {"ezlib", []}},
                       {"fast_tls", []},
                       {"fast_xml", [{if_var_true, full_xml, "--enable-full-xml"}]},
                       {"fast_yaml", []},
                       {"stringprep", []}]}.

{erl_first_files, ["src/ejabberd_sql_pt.erl", "src/ejabberd_config.erl",
		   "src/gen_mod.erl", "src/mod_muc_room.erl",
		   "src/mod_push.erl", "src/xmpp_socket.erl"]}.

{erl_opts, [nowarn_deprecated_function,
            {i, "include"},
            {if_version_above, "20", {d, 'DEPRECATED_GET_STACKTRACE'}},
            {if_version_above, "20", {d, 'HAVE_ERL_ERROR'}},
            {if_version_above, "20", {d, 'HAVE_URI_STRING'}},
            {if_version_below, "21", {d, 'USE_OLD_HTTP_URI'}},
            {if_version_below, "22", {d, 'LAGER'}},
            {if_version_below, "21", {d, 'NO_CUSTOMIZE_HOSTNAME_CHECK'}},
            {if_version_below, "23", {d, 'USE_OLD_CRYPTO_HMAC'}},
            {if_version_below, "23", {d, 'USE_OLD_PG2'}},
            {if_version_below, "24", {d, 'COMPILER_REPORTS_ONLY_LINES'}},
            {if_version_below, "24", {d, 'SYSTOOLS_APP_DEF_WITHOUT_OPTIONAL'}},
            {if_version_below, "24", {d, 'OTP_BELOW_24'}},
            {if_version_below, "25", {d, 'OTP_BELOW_25'}},
            {if_version_below, "26", {d, 'OTP_BELOW_26'}},
            {if_version_below, "27", {d, 'OTP_BELOW_27'}},
            {if_var_false, debug, no_debug_info},
            {if_var_true, debug, debug_info},
            {if_var_true, elixir, {d, 'ELIXIR_ENABLED'}},
            {if_var_true, new_sql_schema, {d, 'NEW_SQL_SCHEMA'}},
            {if_var_true, roster_gateway_workaround, {d, 'ROSTER_GATEWAY_WORKAROUND'}},
            {if_var_true, sip, {d, 'SIP'}},
            {if_var_true, stun, {d, 'STUN'}},
            {if_type_exported, {odbc, {opaque, connection_reference, 0}}, {d, 'ODBC_HAS_TYPES'}},
            {src_dirs, [src,
                        {if_rebar3, sql},
                        {if_var_true, tools, tools}]}]}.

{if_rebar3, {plugins, [{if_version_below, "21", {rebar3_hex, "7.0.7"}},
                       {if_version_above, "20", {rebar3_hex, "~> 7.0.8"}},
                       {provider_asn1, "0.2.0"},
                       %% Protocol consolidation doesn't work correctly in upstream rebar_mix, see
                       %% https://github.com/Supersonido/rebar_mix/issues/27#issuecomment-894873335
                       %% Let's use this fixed rebar_mix fork, see its PR:
                       %% https://github.com/Supersonido/rebar_mix/pull/31
                       {if_var_true, elixir, {rebar_mix, ".*",
                                              {git, "https://github.com/bsanyi/rebar_mix.git",
                                               {branch, "consolidation_fix"}}}
                       }]}}.
{if_rebar3, {project_plugins, [configure_deps,
                               {if_var_true, tools, rebar3_format},
                               {if_var_true, tools, rebar3_lint}
                              ]}}.
{if_not_rebar3, {plugins, [
                           deps_erl_opts, override_deps_versions2, override_opts, configure_deps
                          ]}}.

{if_rebar3, {if_var_true, elixir,
    {provider_hooks, [
      {post, [{compile, {mix, consolidate_protocols}}]}
    ]}}}.

%% Compiling Jose 1.11.10 with Erlang/OTP 27.0 throws warnings on public_key deprecated functions
{if_rebar3, {overrides, [{del, jose, [{erl_opts, [warnings_as_errors]}]}]}}.

{sub_dirs, ["rel"]}.

{keep_build_info, true}.

%%%
%%% Test
%%%

{xref_warnings, false}.

{if_rebar3,
 {xref_checks,
  [deprecated_function_calls, deprecated_functions, locals_not_used,
   undefined_function_calls, undefined_functions]}
}.
{if_not_rebar3,
 {xref_checks,
  [deprecated_function_calls, deprecated_functions,
   undefined_function_calls, undefined_functions]}
}.

{xref_exclusions, [
                   "(\"gen_transport\":_/_)",
                   "(\"eprof\":_/_)",
                   {if_var_false, elixir, "(\"Elixir.*\":_/_)"},
                   {if_var_false, http, "(\"lhttpc\":_/_)"},
                   {if_var_false, mysql, "(\".*mysql.*\":_/_)"},
                   {if_var_false, odbc, "(\"odbc\":_/_)"},
                   {if_var_false, pam, "(\"epam\":_/_)"},
                   {if_var_false, pgsql, "(\".*pgsql.*\":_/_)"},
                   {if_var_false, redis, "(\"eredis\":_/_)"},
                   {if_var_false, sqlite, "(\"sqlite3\":_/_)"},
                   {if_var_false, zlib, "(\"ezlib\":_/_)"}]}.

{xref_ignores, [{eldap_filter_yecc, return_error, 2},
                {http_uri, encode, 1}]}.

{eunit_compile_opts, [{i, "tools"},
                      {i, "include"}]}.

{dialyzer, [{get_warnings, false}, % Show warnings of dependencies
            {if_version_above, "25",
             {plt_extra_apps,
              [asn1, odbc, public_key, stdlib, syntax_tools,
               idna, jose,
               cache_tab, eimp, fast_tls, fast_xml, fast_yaml,
               mqtree, p1_acme, p1_oauth2, p1_utils, pkix,
               stringprep, xmpp, yconf,
               {if_version_below, "27", jiffy},
               {if_var_true, pam, epam},
               {if_var_true, redis, eredis},
               {if_var_true, sip, esip},
               {if_var_true, zlib, ezlib},
               {if_var_true, lua, luerl},
               {if_var_true, mysql, p1_mysql},
               {if_var_true, pgsql, p1_pgsql},
               {if_var_true, stun, stun},
               {if_var_true, sqlite, sqlite3}]},
             {plt_extra_apps, % For Erlang/OTP 25 and older
              [cache_tab, eimp, fast_tls, fast_xml, fast_yaml,
               mqtree, p1_acme, p1_oauth2, p1_utils, pkix, stringprep, xmpp, yconf,
               {if_var_true, pam, epam},
               {if_var_true, redis, eredis},
               {if_var_true, sip, esip},
               {if_var_true, zlib, ezlib},
               {if_var_true, lua, luerl},
               {if_var_true, mysql, p1_mysql},
               {if_var_true, pgsql, p1_pgsql},
               {if_var_true, stun, stun},
               {if_var_true, sqlite, sqlite3}]}
            } ]}.

{ct_opts, [{keep_logs, 20}]}.

{cover_enabled, true}.
{cover_export_enabled, true}.
{coveralls_coverdata, "_build/test/cover/ct.coverdata"}.
{coveralls_service_name, "github"}.

%%%
%%% OTP Release
%%%

{relx, [{release, {ejabberd, {cmd, "grep {vsn, vars.config | sed 's|{vsn, \"||;s|\"}.||' | tr -d '\012'"}},
         [ejabberd]},
        {sys_config, "./rel/sys.config"},
        {vm_args, "./rel/vm.args"},
        {overlay_vars, "vars.config"},
        {overlay, [{mkdir, "logs"},
                   {mkdir, "database"},
                   {mkdir, "conf"},
                   {copy, "rel/files/erl", "erts-\{\{erts_vsn\}\}/bin/erl"},
                   {template, "ejabberdctl.template", "bin/ejabberdctl"},
                   {copy, "_build/default/lib/ejabberd/ebin/Elixir.*", "lib/ejabberd-{{release_version}}/ebin/"},
                   {copy, "{{base_dir}}/consolidated/*", "lib/ejabberd-{{release_version}}/ebin/"},
                   {copy, "rel/overlays/iex", "releases/{{release_version}}/"},
                   {if_var_true, elixir,
                    {template, "rel/overlays/elixir", "releases/{{release_version}}/elixir"}
                   },
                   {copy, "inetrc", "conf/inetrc"},
                   {copy, "tools/captcha*.sh", "lib/ejabberd-\{\{release_version\}\}/priv/bin/"},
                   {copy, "rel/files/install_upgrade.escript", "bin/install_upgrade.escript"}]}
        ]}.

{profiles, [{prod, [{relx, [{debug_info, strip},
                            {dev_mode, false},
                            {include_erts, true},
                            {include_src, true},
                            {generate_start_script, false},
                            {overlay, [{copy, "sql/*", "lib/ejabberd-\{\{release_version\}\}/priv/sql/"},
                                       {copy, "ejabberdctl.cfg.example", "conf/ejabberdctl.cfg"},
                                       {copy, "ejabberd.yml.example", "conf/ejabberd.yml"}]}]}]},
            {dev, [{post_hooks, [{release, "rel/setup-dev.sh rebar3"}]},
                   {deps, [{if_version_above, "20", sync}]},
                   {relx, [{debug_info, keep},
                           {dev_mode, true},
                           {include_erts, true},
                           {include_src, false},
                           {generate_start_script, true},
                           {extended_start_script, true},
                           {overlay, [{copy, "ejabberdctl.cfg.example", "conf/ejabberdctl.cfg.example"},
                                      {copy, "ejabberd.yml.example", "conf/ejabberd.yml.example"},
                                      {copy, "test/ejabberd_SUITE_data/ca.pem", "conf/"},
                                      {copy, "test/ejabberd_SUITE_data/cert.pem", "conf/"}]}]}]},
            {translations, [{deps, [{ejabberd_po, ".*", {git, "https://github.com/processone/ejabberd-po", {branch, "main"}}}]}]},
            {test, [{erl_opts, [nowarn_export_all]}]}]}.

{alias, [{relive, [{shell, "--apps ejabberd \
                            --config rel/relive.config \
                            --eval sync:go(). \
                            --script rel/relive.escript \
                            --name ejabberd@localhost"}]}
]}.

%% Local Variables:
%% mode: erlang
%% End:
%% vim: set filetype=erlang tabstop=8:
