Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 119 lines (109 sloc) 4.08 KB
#! /usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright (c) 2009 Hakan Mattsson
%%
%% See the file "LICENSE" for information on usage and redistribution
%% of this file, and for a DISCLAIMER OF ALL WARRANTIES.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
usage() ->
ThisEscript = filename:basename(resolved_script_name()),
io:format("Eflex - a flextime calculation tool implemented with Erlang/OTP \n", []),
io:format("\nusage:\n", []),
io:format(" ~s (run the graphical tool)\n", [ThisEscript]),
io:format(" ~s --help (display the users guide)\n", [ThisEscript]),
io:format(" ~s --make (build the tool) \n", [ThisEscript]),
io:format(" ~s --install (dry run of standalone installation)\n", [ThisEscript]),
io:format(" ~s --install TargetDir (create a standalone installation)\n", [ThisEscript]),
halt(1).
main([]) ->
ensure_app_dir(true),
eflex:start();
main(["--help" = Opt]) ->
AppDir = ensure_app_dir(true),
require_app(eflex, Opt),
File = filename:join([AppDir, "README.md"]),
case file:read_file(File) of
{ok, Bin} ->
io:format("~s\n", [binary_to_list(Bin)]);
{error, Reason} ->
fatal_error(3, "~s: ~s\n", [File, file:format_error(Reason)])
end;
main(["--make" = Opt]) ->
io:format("Making eflex...\n", []),
require_app(tools, Opt),
AppDir = ensure_app_dir(true),
SrcDir = filename:join([AppDir, "src"]),
ok = file:set_cwd(SrcDir),
make:all([debug_info,{outdir,"../ebin"}]);
main(["--install" = Opt| InstallDir]) ->
io:format("Installing eflex as standalone...\n", []),
require_app(reltool, Opt),
AppDir = ensure_app_dir(true),
LibDir = filename:dirname(AppDir),
ThisEscript = resolved_script_name(),
Sys = {sys, [{lib_dirs, [LibDir]},
{escript, ThisEscript, [{incl_cond, include}]},
{app, crypto, [{incl_cond, exclude}]},
{app, erts, [{incl_cond, exclude}]},
{app, hipe, [{incl_cond, exclude}]},
{app, reltool, [{incl_cond, exclude}]},
{app, tools, [{incl_cond, exclude}]},
{profile, standalone},
{excl_sys_filters,{add, ["^bin/(epmd|start.boot)(|\\.exe)$",
"^erts.*/bin/(beam|epmd|heart)(|\\.exe)$"]}}]},
{ok, Spec} = reltool:get_target_spec([Sys]),
case InstallDir of
[] ->
io:format("Spec: ~p\n", [Spec]),
timer:sleep(1);
_ ->
ok = reltool:eval_target_spec(Spec, code:root_dir(), InstallDir)
end,
ok;
main(_) ->
usage().
fatal_error(RetCode, Format, Args) ->
io:format(Format, Args),
halt(RetCode).
require_app(App, Opt) ->
case code:lib_dir(App) of
{error, _Reason} ->
fatal_error(4, "The application ~p is required for the option ~p.\n", [App, Opt]);
_ ->
ok
end.
resolved_script_name() ->
resolve_path(escript:script_name()).
ensure_app_dir(Retry) ->
case code:lib_dir(eflex) of
{error, _Reason} when Retry =:= true ->
%% Try to add eflex/ebin to the code path
ThisEscript = resolved_script_name(),
AppDir = filename:dirname(filename:dirname(ThisEscript)),
EbinDir = filename:join([AppDir, "ebin"]),
%% io:format("~s: Add directory ~p to code path\n", [Name, EbinDir]),
code:add_patha(EbinDir),
ensure_app_dir(false);
{error, Reason} ->
fatal_error(2, "The application eflex is required to run: ~p\n", [Reason]);
AppDir ->
AppDir
end.
resolve_path(Path) ->
do_resolve_path(filename:split(Path), []).
do_resolve_path([H | T], ResolvedPath) ->
ResolvedPath2 = case ResolvedPath of
[] -> filename:absname(H);
_ -> filename:join([ResolvedPath, H])
end,
case file:read_link(ResolvedPath2) of
{error, _} -> % Normal file
do_resolve_path(T, ResolvedPath2);
{ok, SymTarget} -> % Symbolic Link
ResolvedPath3 = filename:absname(filename:join([ResolvedPath, SymTarget])),
resolve_path(filename:join([ResolvedPath3] ++ T))
end;
do_resolve_path([], ResolvedPath) ->
ResolvedPath.
Something went wrong with that request. Please try again.