zx/install.escript

139 lines
4.5 KiB
Erlang
Executable File

#! /usr/bin/env escript
%% ZX install script
-mode(compile).
-spec main(Argv :: [string()]) -> no_return().
main(_) ->
ok = io:setopts([{encoding, unicode}]),
OS = os:type(),
ZompDir = zomp_dir(OS),
case filelib:is_dir(ZompDir) of
true ->
Message = "Installation directory (~ts) already exists. Aborting.~n",
ok = io:format(Message, [ZompDir]),
halt(0);
false ->
unpack(OS, ZompDir)
end.
-spec unpack(OS, ZompDir) -> no_return()
when OS :: {Family :: unix | win32,
Name :: atom()},
ZompDir :: file:filename().
unpack(OS, ZompDir) ->
BaseDir = filename:dirname(ZompDir),
{ok, Files} = zip:extract("zomp.zip", [{cwd, BaseDir}]),
ok = io:format("Extracted: ~tp~n", [Files]),
add_launcher(OS, ZompDir).
-spec add_launcher(OS, ZompDir) -> no_return()
when OS :: {Family :: unix | win32,
Name :: atom()},
ZompDir :: file:filename().
add_launcher({unix, linux}, ZompDir) ->
ok = add_unix_link(ZompDir),
halt(0);
add_launcher({unix, _}, ZompDir) ->
ok = add_unix_link(ZompDir),
halt(0);
add_launcher({win32, nt}, ZompDir) ->
ok = add_windows_link(ZompDir),
halt(0).
-spec add_unix_link(ZompDir) -> ok
when ZompDir :: file:filename().
add_unix_link(ZompDir) ->
Home = filename:dirname(ZompDir),
Link = filename:join(Home, "bin/zx"),
LinkH = filename:join(Home, "bin/zxh"),
HomeBin = filename:dirname(Link),
Target = filename:join(ZompDir, "zx"),
TargetH = filename:join(ZompDir, "zxh"),
ok = filelib:ensure_dir(Link),
LinkCommand = "env LANG=en ln -s " ++ Target ++ " " ++ Link,
LinkCommandH = "env LANG=en ln -s " ++ TargetH ++ " " ++ LinkH,
ModeCommand = "env LANG=en chmod +x " ++ Target,
ModeCommandH = "env LANG=en chmod +x " ++ TargetH,
ok = os_cmd(LinkCommand),
ok = os_cmd(LinkCommandH),
ok = os_cmd(ModeCommand),
ok = os_cmd(ModeCommandH),
Path = os:getenv("PATH"),
Parts = string:lexemes(Path, ":"),
Message =
case lists:member(HomeBin, Parts) of
true ->
"A link to ~ts has been created at ~ts.~n"
"~ts seems to be in $PATH already.~n"
"You should be able to run any Zomp/ZX program as a normal shell~n"
"command by typing `zx`, or with an Erlang shell attached by~n"
"typing `zxh` from anywhere in your system.~n";
false ->
"A link to ~ts has been created at ~ts. "
"~ts seems to be NOT in your $PATH. "
"You will need to add that to $PATH if you want to be able to run~n"
"zx by simply typing `zx` (or zxh to attach an Erlang shell) from~n"
"anywhere in your system.~n"
end,
io:format(Message, [Target, Link, HomeBin]).
-spec add_windows_link(ZompDir) -> ok
when ZompDir :: file:filename().
add_windows_link(ZompDir) ->
Home = filename:join(os:getenv("HOMEDRIVE"), os:getenv("HOMEPATH")),
Launcher = filename:join(ZompDir, "zx.cmd"),
LauncherH = filename:join(ZompDir, "zxh.cmd"),
Vapor = filename:join(ZompDir, "vapor.cmd"),
Target = filename:join(Home, "zx.cmd"),
TargetH = filename:join(Home, "zxh.cmd"),
VaporL = filename:join([Home, "Desktop", "Vapor.cmd"]),
{ok, _} = file:copy(Launcher, Target),
{ok, _} = file:copy(LauncherH, TargetH),
{ok, _} = file:copy(Vapor, VaporL),
Message =
"`zx` and `zxh` launch scripts have been added to ~p~n"
"(available from cmd.exe in your home directory)~n"
"A GUI program launcher script has been added to the desktop.~n",
io:format(Message, [Home]).
-spec os_cmd(Command :: string()) -> ok.
os_cmd(Command) ->
case os:cmd(Command) of
"" ->
ok;
Output ->
ok = io:format("Output of: ~tp~n", [Command]),
io:format("~ts~n", [Output])
end.
-spec zomp_dir(OS) -> file:filename()
when OS :: {Family :: unix | win32,
Name :: atom()}.
%% @private
%% Check the host OS and return the absolute path to the zomp filesystem root.
zomp_dir({unix, _}) ->
Home = os:getenv("HOME"),
filename:join(Home, "zomp");
zomp_dir({win32, _}) ->
Path = os:getenv("LOCALAPPDATA"),
filename:join(Path, "zomp");
zomp_dir(Unknown) ->
Message = "zx_install: ERROR Unknown host system type: ~tp",
ok = io:format(Message, [Unknown]),
halt(1).