#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname nitrogenshell
% Nitrogen Web Framework for Erlang
% Copyright (c) 2008 Rusty Klophaus
% Module by Jon Gretar Borgthorsson
% See MIT-LICENSE for licensing information.
-export([main/1]).
-include_lib("kernel/include/file.hrl").

%% External API
main(["create", Name]) ->
    ensure(),
    LowerName = string:to_lower(Name),
    DestDir = filename:absname(LowerName),
    ok = nitrogen_project:create_project(LowerName, DestDir);

main(["page", Url]) ->
    ensure_in_project(),
    DestDir = filename:absname("src/pages"),
    nitrogen_file:create_page(Url, DestDir);

main(_) ->
    usage().

%% Internal API

ensure() ->
    code:add_patha(filename:join(filename:dirname(escript:script_name()),
                                 "../ebin")).

ensure_in_project() ->
    ensure(),
    Dir = filename:absname("ebin"),
    case file:read_file_info(Dir) of
        {ok, _} ->
            ok;
        {error, _} ->
            io:format(" ERROR: You must be in a project root to use this command~n~n",[]),
            usage()
    end.

usage() ->
    io:format("Nitrogen Web Framework. ~n~nUsage: ~n"),
    io:format("  nitrogen create [PROJECT_NAME]  - Creates a project in a subdirectory ~n"),
    io:format("  nitrogen page [URL]             - Create a page for URL (EXAMPLE: 'nitrogen page web/blog/post') ~n"),
    halt(1).

