%%% 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),
        unix(system(Command)).

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

html_protect(Goal) :-
        on_exception(E,Goal,html_report_error(E)).
html_protect(_) :-
        html_report_error('Sorry, html_pl application failed.').

%%% Support predicates %%%

%% Concatenates two atoms
atom_concat(A,B,AB) :-
        name(A,AS),
        name(B,BS),
        append(AS,BS,ABS),
        name(AB,ABS).

append([],X,X).
append([H|T1],L,[H|T2]) :- append(T1,L,T2).

%% Get Unix environment variable
getenv(Var,Val) :-
        name(Var,VS),
        append("echo $",VS,SCommand),
        name(Command,SCommand),
        unix(popen(Command,read,S)),
        read_command_output(S,SInput),
        SInput = [_|_], % not null
        atom_chars(Val,SInput).

%% For reading output from a unix command
read_command_output(S,List) :- 
        get0(S,Char),
        check_char(Char,S,List).

check_char(-1,_S,[]) :- !. % EOF
check_char(10,_S,[]) :- !. % Newline
check_char(Ch,S,[Ch|Rest]) :- 
        get0(S,Ch1),
        check_char(Ch1,S,Rest).
