%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ft=erlang ts=4 sw=4 et
%%
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

Cfg = case file:consult("../pkg.vars.config") of
          {ok, Terms} ->
              Terms;
          _Err ->
              []
      end,


%% get version infos
MajorVersion = integer_to_list(proplists:get_value(version_major, Cfg, 0)),
MinorVersion = integer_to_list(proplists:get_value(version_minor, Cfg, 0)),
RevVersion = integer_to_list(proplists:get_value(version_revision, Cfg, 0)),

%% build the version
BaseVersion = MajorVersion ++ "." ++ MinorVersion ++ "." ++ RevVersion,
Vsn = case os:getenv("RELEASE_TAG") of
    false ->
        BaseVersion;
    "git" ->
        {0, Ret} = eunit_lib:command("git describe --always"),
        Tag = [C||C <- Ret, C =/= $\n],
        BaseVersion ++ "+build." ++ Tag;
    Tag ->
        BaseVersion ++ "-" ++ Tag
end,

CouchJSName = proplists:get_value(couchjs_name, Cfg, "couchjs"),

[
    {sys, [
            {lib_dirs, ["../deps"]},
            {rel, "rcouch", Vsn,
                [
                    kernel,
                    stdlib,
                    lager,
                    sasl,
                    public_key,
                    ssl,
                    os_mon,
                    crypto,
                    inets,
                    runtime_tools,
                    mochiweb,
                    ibrowse,
                    hackney,
                    couch,
                    couch_httpd,
                    couch_replicator
                ]},
            {rel, "start_clean", "", [kernel, stdlib]},
            {boot_rel, "rcouch"},
            {profile, embedded},
            {relocatable, true},

            {excl_sys_filters, ["^bin/.*",
                                "^erts.*/bin/(dialyzer|typer)",
                                "^erts.*/doc",
                                "^erts.*/man"]},
            {excl_archive_filters, [".*"]},

            %% stdlib
            {app, asn1, [{incl_cond, include}]},
            {app, sasl, [{incl_cond, include}]},
            {app, crypto, [{incl_cond, include}]},
            {app, public_key, [{incl_cond, include}]},
            {app, ssl, [{incl_cond, include}]},
            {app, xmerl, [{incl_cond, include}]},
            {app, observer, [{incl_cond, include}]},


            {app, kernel, [{incl_cond, include}]},
            {app, stdlib, [{incl_cond, include}]},
            {app, inets, [{incl_cond, include}]},
            {app, os_mon, [{incl_cond, include}]},
            {app, compiler, [{incl_cond, include}]},
            {app, syntax_tools, [{incl_cond, include}]},
            {app, runtime_tools, [{incl_cond, include}]},

            %% dependencies
            {app, mochiweb, [{incl_cond, include}]},
            {app, oauth, [{incl_cond, include}]},
            {app, jiffy, [{incl_cond, include}]},
            {app, snappy, [{incl_cond, include}]},
            {app, ibrowse, [{incl_cond, include}]},
            {app, goldrush, [{incl_cond, include}]},
            {app, lager, [{incl_cond, include}]},
            {app, idna, [{incl_cond, include}]},
            {app, ssl_verify_hostname, [{incl_cond, include}]},
            {app, hackney, [{incl_cond, include}]},

            %% couchdb
            {app, couch, [{incl_cond, include}]},
            {app, ucol_nif, [{incl_cond, include}]},
            {app, couch_httpd, [{incl_cond, include}]},
            {app, couch_index, [{incl_cond, include}]},
            {app, couch_mrview, [{incl_cond, include}]},
            {app, couch_replicator, [{incl_cond, include}]},
            {app, couch_dbupdates, [{incl_cond, include}]},

            %% extras
            {app, couch_randomdoc, [{incl_cond, include}]},
            {app, geocouch, [{incl_cond, include}]}

        ]},

    {target_dir, "rcouch"},

    {overlay_vars, "vars.config"},

    {overlay, [
        {mkdir, "log"},
        {mkdir, "run"},
        {mkdir, "data"},

        %% keep empty files
        {copy, "../etc/rcouch/empty", "log/KEEP"},
        {copy, "../etc/rcouch/empty", "data/KEEP"},

        %% Copy base files for starting and interacting w/ node
        {copy, "../deps/node_package/priv/base/erl",
         "{{erts_vsn}}/bin/erl"},
        {copy, "../deps/node_package/priv/base/nodetool",
         "{{erts_vsn}}/bin/nodetool"},
        {template, "../deps/node_package/priv/base/runner",
         "bin/rcouch"},
        {template, "../deps/node_package/priv/base/env.sh",
         "lib/env.sh"},
        {template, "../deps/node_package/priv/base/app_epath.sh",
         "lib/app_epath.sh"},

        %% config files
        {template, "../etc/rcouch/app.config", "etc/app.config"},
        {template, "../etc/rcouch/vm.args", "etc/vm.args"},
        {template, "../etc/rcouch/couch.ini", "etc/couch.ini"},
        {template, "../etc/rcouch/{{node_config}}", "etc/local.ini"},

        %% couchdb data files
        {mkdir, "share"},
        {mkdir, "share/server"},
        {copy, "../share/server/main.js", "share/server"},
        {copy, "../share/server/main-coffee.js", "share/server"},
        {copy, "../share/www", "share"},
        {copy, "../deps/couch/priv/" ++ CouchJSName, "bin/" ++ CouchJSName},

        %% misc
        {mkdir, "lib/rcouch-patches"},
        {copy, "../support/couch_rel/ebin/etop_txt.beam", "lib/rcouch-patches"},

        %% geocouch
        {copy, "../deps/geocouch/share/www/script/test/*", "share/www/script/test/"}

    ]}
].
