#!/usr/bin/env escript
%% -*- erlang -*-
%%! -noinput +B

main(Argv) ->
    setup_code_path(),
    case gpb_compile:parse_opts_and_args(Argv) of
        {ok, {Opts, Files}} ->
            gpb_compile:c(Opts, Files); %% will halt
        {error, Reason} ->
            io:format("Error: ~s.~n", [Reason]),
            show_usage(),
            halt(1)
    end.

setup_code_path() ->
    BinDir = filename:dirname(escript:script_name()),
    EBinDir = filename:join([BinDir, "..", "ebin"]),
    %% add the gpb ebin path to we can have access to gpb_compile
    true = code:add_pathz(EBinDir).

show_usage() ->
    io:format("usage: ~s [options] X.proto [...]~n",
              [filename:basename(escript:script_name())]),
    gpb_compile:show_args().
