#!/usr/bin/env escript
%%!
%-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
% ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et:
%%%
%%%------------------------------------------------------------------------
%%% BSD LICENSE
%%% 
%%% Copyright (c) 2013-2016, Michael Truog <mjtruog at gmail dot com>
%%% All rights reserved.
%%% 
%%% Redistribution and use in source and binary forms, with or without
%%% modification, are permitted provided that the following conditions are met:
%%% 
%%%     * Redistributions of source code must retain the above copyright
%%%       notice, this list of conditions and the following disclaimer.
%%%     * Redistributions in binary form must reproduce the above copyright
%%%       notice, this list of conditions and the following disclaimer in
%%%       the documentation and/or other materials provided with the
%%%       distribution.
%%%     * All advertising materials mentioning features or use of this
%%%       software must display the following acknowledgment:
%%%         This product includes software developed by Michael Truog
%%%     * The name of the author may not be used to endorse or promote
%%%       products derived from this software without specific prior
%%%       written permission
%%% 
%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
%%% CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
%%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
%%% OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
%%% DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
%%% CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
%%% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
%%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
%%% SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
%%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
%%% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
%%% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
%%% DAMAGE.
%%%------------------------------------------------------------------------

-author('mjtruog [at] gmail (dot) com').

-mode(compile).

main(_) ->
    {ok,
     [{sys, _} = RelToolConfig,
      {target_dir, TargetDir},
      {overlay, OverlayConfig}]} = file:consult("reltool.config"),
    {ok, Spec} = reltool:get_target_spec([RelToolConfig]),
    case file:make_dir(TargetDir) of
        ok ->
            ok;
        {error, eexist} ->
            io:format("release already exists? (~p)~n", [TargetDir]),
            exit_code(1)
    end,
    ok = reltool:eval_target_spec(Spec, code:root_dir(), TargetDir),
    ok = process_overlay(RelToolConfig, TargetDir, OverlayConfig),
    exit_code(0).

shell(Command, Arguments) ->
    Shell = erlang:open_port({spawn_executable, "/bin/sh"},
                             [{args, ["-"]},
                              stream, binary, stderr_to_stdout, exit_status]),
    Exec = io_lib:format(Command, Arguments),
    true = erlang:port_command(Shell, ["exec ", Exec, "\n"]),
    case shell_output(Shell, []) of
        {0, _} ->
            ok;
        {Status, Output} ->
            io:format("~s\"~s\" failed! exit ~w~n", [Output, Exec, Status]),
            error
    end.

shell_output(Shell, Output) ->
    receive
        {Shell, {data, Data}} ->
            shell_output(Shell, [Data | Output]);
        {Shell, {exit_status, Status}} ->
            {Status, lists:reverse(Output)}
    end.

boot_rel_vsn({sys, Config} = _RelToolConfig) ->
    {rel, _Name, Ver, _} = proplists:lookup(rel, Config),
    Ver.

%% minimal parsing for handling mustache syntax
mustache(Body, Context) ->
    mustache(Body, "", Context).
mustache([], Result, _Context) ->
    lists:reverse(Result);
mustache([${, ${ | KeyStr], Result, Context) ->
    mustache_key(KeyStr, "", Result, Context);
mustache([C | Rest], Result, Context) ->
    mustache(Rest, [C | Result], Context).
mustache_key([$}, $} | Rest], KeyStr, Result, Context) ->
    Key = erlang:list_to_existing_atom(lists:reverse(KeyStr)),
    {ok, Value} = dict:find(Key, Context),
    mustache(Rest, lists:reverse(Value) ++ Result, Context);
mustache_key([C | Rest], KeyStr, Result, Context) ->
    mustache_key(Rest, [C | KeyStr], Result, Context).
    
%% support minimal overlay based on rebar overlays
process_overlay(RelToolConfig, TargetDir, OverlayConfig) ->
    BootRelVsn = boot_rel_vsn(RelToolConfig),
    OverlayVars =
        dict:from_list([{erts_vsn, "erts-" ++ erlang:system_info(version)},
                        {rel_vsn, BootRelVsn},
                        {target_dir, TargetDir},
                        {hostname, net_adm:localhost()}]),
    {ok, BaseDir} = file:get_cwd(),
    execute_overlay(OverlayConfig, OverlayVars, BaseDir, TargetDir).

execute_overlay([], _Vars, _BaseDir, _TargetDir) ->
    ok;
execute_overlay([{mkdir, Out} | Rest], Vars, BaseDir, TargetDir) ->
    OutDir = mustache(filename:join(TargetDir, Out), Vars),
    ok = shell("mkdir -p \"~s\"", [OutDir]),
    execute_overlay(Rest, Vars, BaseDir, TargetDir);
execute_overlay([{copy, In, Out} | Rest], Vars, BaseDir, TargetDir) ->
    InFile = mustache(filename:join(BaseDir, In), Vars),
    OutFile = mustache(filename:join(TargetDir, Out), Vars),
    true = filelib:is_file(InFile),
    ok = shell("cp -R \"~s\" \"~s\"", [InFile, OutFile]),
    execute_overlay(Rest, Vars, BaseDir, TargetDir).

exit_code(ExitCode) ->
    erlang:halt(ExitCode, [{flush, true}]).

