#!/usr/bin/env escript
%% -*- erlang -*-



-mode(compile).
-define(NOT_IN_GIT_ERR_MSG,
    "ERROR: Building outside a git work tree is not supported,
           unless the source has first been exported.
           This is for the version number to get correct.
           To build outside a git work tree, first export it:
           Start with a git clone, then run helpers/export-from-git.
           For further info, see the README.md, the section section
           named Building outside of a git work tree
           Github automatically generates tar/zip files from tags,
           but these will unfortunately not do, since the version
           number does not get set correctly.").

file_err(FunName, FileName, Reason) ->
    io:format(standard_error, "file:~p(~s) excute failed!, Reason :~s ~n",
        [FunName, FileName, file:format_error(Reason)]),
    halt(1).


main([InFile, OutFile]) ->
    InGit = os:cmd("git rev-parse --is-inside-work-tree  2>&1"),
    case InGit of
        "true" ++ _ ->
            next;
        _ ->
            io:format(standard_error, "~s~n", [?NOT_IN_GIT_ERR_MSG]),
            halt(1)
    end,
    Vsn0 = os:cmd("git describe --always --tags --match [0-9]*.[0-9]*"),
    Vsn = lists:sublist(Vsn0, length(Vsn0) - 1),
    BaseStr = case file:read_file(InFile) of
                  {ok, S} ->
                      S;
                  {error, Reason} ->
                      file_err(read_file, InFile, Reason)
              end,
    SedList =
        [
            {<<"@vsn@">>, Vsn},
            {<<"is expected to be">>, <<"was">>},
            {<<"%% The version below">>, [<<"%% DO NOT EDIT -- generated ">>,
                <<"from gpb_version.hrl.in\n%% The version below">>]},
            {<<"%% NB: The build.mk_version_hrl depends.*">>, <<"">>}
        ],
    VersionStr = lists:foldl(fun({Patten, Replacement}, Acc) ->
        re:replace(Acc, Patten, Replacement, [{return, binary}])
                             end, BaseStr, SedList),
    case file:write_file(OutFile, VersionStr) of
        ok ->
            ok;
        {error, Reason2} ->
            file_err(write_file, OutFile, Reason2)
    end.
