%%% Part 4 - Standalone application generation %%%

% A few simple support predicates to make standalone cgi-bin applications
save_cgi_bin(Name,EntryPoint) :-
        cgi_bin_dir(BinDir),
        atom_concat(BinDir,Name,FullName),
        save(FullName,Return),
        boot_form(Return,FullName,EntryPoint).

boot_form(0,FullName,_) :- 
        atom_concat('chmod ugo+rx ',FullName,Command),
        shell(Command).

boot_form(1,_,EntryPoint) :- 
        html_protect(EntryPoint),
        halt.

html_protect(Goal) :-
        user:asserta(
            (exception(E, C, fail) :-
               html_report_error([E,': ',C]),
               assert(error)
            ),
            Ref),
        call(Goal),
        erase(Ref),
        retractall(error).
html_protect(_) :-
        retract(error), !.
html_protect(_) :-
        html_report_error('Sorry, html_pl application failed.').

%%% Support predicates %%%

%% Concatenates two atoms
atom_concat(A,B,AB) :- concat(A,B,AB).
