/*
 * Copyright (c) 1995 Gunther Schadow.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Files where objects can be found
 */
:- module(desc_files, [file/3,ifile/3,path/2,
	htell/3,hsuspend/0,hresume/1,htold/1,
	cctell/2,cctold/0,icctell/2,icctold/0,iccinclude/1,
	vcgtell/0,vcgtold/0,vcgnew/0,
	htmltell/1,htmltold/0]).

:- style_check(+string).

:- use_module(db-inter).
:- use_module(desc-names).

path(Type, P) :-
	path0(tree,P0),
	path1(Type, P1),
	concat(P0, P1, P).

path(_, P) :- path0(flat,P).

path1(message,'messages/').
path1(group,'messages/').
path1(segment,'segments/').
path1(ctype,'composites/').
path1(table,'tables/').
path1(code,'codes/').
path1(base,'basics/').
path1(vcg,'').
path1(html,'html/').

file(type,T,F) :-    
    data_type(T, _, _),
    uppercase(T,UT),
    concat(UT,'typ.h', F).

file(code,code(Num),F) :- !,
	cdetname(Num,Name),
	concat(Name,'.h',F).
file(code,Num,F) :-
	number(Num),
	file(code,code(Num),F).

file(segment,S,F) :-
	segment(S,_,_),
	uppercase(S,US),
	concat(US,'seg.h',F).

file(message,msg(M,EvC),F) :-
	message(M,EvC,_,_),
	evco(EvC,E),
	unimesg(M,E,Mu),
	uppercase(Mu,UMu),
	concat_atom([UMu,'msg.h'],F).

file(group,G,F) :-
	grptname(G,Gn),
	concat_atom([Gn,'.h'],F).

file(variant,VarName,FileName) :-
	concat(VarName,'.h',FileName).

file(W,S,F) :- nonnil(W), nonnil(S),
	sformat(F,"What file is ~w ~w ?",[W,S]),
	write(user,F), nl(user).

ifile(fld_mod,repfield,'repfield.h').
ifile(seg_mod,repstruc,'repstruc.h').
ifile(msg_mod,group,   'Group.h').

ifile(seg_mod,any,'hl7/ANYseg.h').
ifile(msg_mod,any,'hl7/ANYmsg.h').
ifile(seg_mod,repANYseg,'hl7/repANYseg.h').

ifile(type,T,F) :-    
	defined(P:data_type(T, _, _)),
	package(P,_,_,R,_),
	uppercase(T,UT),
	concat_atom([R,UT,'typ.h'], F).

ifile(code,code(Num),F) :- !,
	cdetname(Num,Name),
	defined(P:table(Num, _, _)),
	package(P,_,_,R,_),
	concat_atom([R,Name,'.h'],F).
file(code,Num,F) :-
	number(Num),
	file(code,code(Num),F).

ifile(segment,S,F) :-
	defined(P:segment(S,_,_)),
	package(P,_,_,R,_),
	uppercase(S,US),
	concat_atom([R,US,'seg.h'], F).

ifile(message,msg(M,EvC),F) :-
	defined(P:message(M,EvC,_,_)),
	package(P,_,_,R,_),
	evco(EvC,E),
	unimesg(M,E,Mu),
	uppercase(Mu,UMu),
	concat_atom([R,UMu,'msg.h'],F).

ifile(group,G,F) :-
	grptname(G,Gn),
	main_package(P),
	package(P,_,_,R,_),
	concat_atom([R,Gn,'.h'],F).

ifile(variant,VarName,FileName) :-
	main_package(P),
	package(P,_,_,R,_),	
	concat_atom([R,VarName,'.h'],FileName).

ifile(W,S,F) :-
	sformat(F,"What file is ~w ~w ?",[W,S]),
	write(user,F), nl(user).

htell(Fnh,Def,Type) :- 
%        format("<h"),flush,
	concat(Fn,'.h',Fnh),
	uppercase(Fn, UFn),
        main_package(P), uppercase(P,UP),
	concat_atom([UP,'_',UFn,'_H_'], Def),
	path(Type,Dir),
	concat_atom([Dir,Fn,'.h'],Pth),
	tell(Pth),
	copyright,
	format("#ifndef ~w~n", Def),
	format("#define ~w~n~n", Def),
	format("~n#pragma interface~n").

hsuspend :- told.
%        format("sh>"),flush.

hresume(Fnh) :-
%        format("<ha"),flush,
	append(Fnh).

htold(Def):-
	format("~n#endif /* ! ~w */~n", Def),
        told.
%        format("h>"),flush.

cctell(Fnh,Type) :- 
%        format("<c"),flush,
	concat(Fn,'.h',Fnh),
	concat(Fn,'.cc',Fncc),
	path(Type,Dir),
	concat(Dir,Fncc,Ptcc),
	tell(Ptcc),
	copyright,
        format("#pragma implementation ~S~w~S~n", [Fnh]),
        format("#include ~S~w~S~n~n", [Fnh]).

cctold :- told.
%    format("c>"),flush.

vcgtell :- 
%    format("<v"),flush,
    path(vcg,Dir),
    concat(Dir,'graph.vcg',Ptvcg),
    append(Ptvcg).

vcgtold :- told.
%    format("v>"),flush.

vcgnew :- delete_file('graph.vcg'); true.

htmltell(Fnh) :-
%    format("<t"),flush,
    path(html,Dir),
    concat(Title,'.h',Fnh),
    concat(Fnh,'tml',Fn),
    concat(Dir,Fn,Pn),
    tell(Pn),
    main_package(P),
    uppercase(P,UP),
    format("<HTML>~n<TITLE>~w ~w</TITLE>~n", [UP, Title]),
    format("<BODY>~n"),
    format("<EM>Created by "), banner, format("</EM><BR>~n"),
    format("<A HREF=~'messages.html~'>MESSAGES</A>~n"),
    format("<A HREF=~'variants.html~'>VARIANTS</A>~n"),
    format("<A HREF=~'groups.html~'>GROUPS</A>~n"),
    format("<A HREF=~'segments.html~'>SEGMENTS</A>~n"),
    format("<A HREF=~'composites.html~'>COMPOSITES</A>~n"),
    format("<A HREF=~'tables.html~'>TABLES</A><HR>~n").
        
htmltold :-
    format("<HR><A HREF=~'messages.html~'>MESSAGES</A>~n"),
    format("<A HREF=~'variants.html~'>VARIANTS</A>~n"),
    format("<A HREF=~'groups.html~'>GROUPS</A>~n"),
    format("<A HREF=~'segments.html~'>SEGMENTS</A>~n"),
    format("<A HREF=~'composites.html~'>COMPOSITES</A>~n"),
    format("<A HREF=~'tables.html~'>TABLES</A><BR>~n"),
    format("<EM>Created by "), banner, format("</EM>~n"),
    format("</BODY></HTML>"),
    told.
%    format("t>"),flush.

icctell(Fnh,Type) :- 
%        format("<i"),flush,
	concat(Fn,'.h',Fnh),
	concat(Fn,'.icc',Fncc),
	path(Type,Dir),
	concat(Dir,Fncc,Ptcc),
	tell(Ptcc),
	copyright,
	format("#ifdef OUTLINE~n"),
	format("# define inline~n"),
	format("#endif~n").

icctold :-
	format("#ifdef OUTLINE~n"),
	format("# undef inline~n"),
	format("#endif~n"),
	told.
%        format("i>"),flush.

iccinclude(Fnh) :-
	main_package(P),
	package(P,_,_,R,_),
	concat(Fn,'.h',Fnh),
	format("~n#ifndef OUTLINE~n"),
	format("# include <~w~w.icc>~n" ,[R,Fn]),
	format("#endif~n").


copyright :-
	protogen_release(R),
	format("/*~n"),
	format(" * This file is automatically Generated by ProtoGen r~w.~n", [R]),
	format(" * Do not edit it.~n"),
	format(" *~n"),
	format(" * Copyright (c) 1995, 1996 Gunther Schadow.  All rights reserved.~n"),
	format(" *~n"),
	format(" * This program is free software; you can redistribute it and/or modify~n"),
	format(" * it under the terms of the GNU General Public License as published by~n"),
	format(" * the Free Software Foundation; either version 2 of the License, or~n"),
	format(" * (at your option) any later version.~n"),
	format(" * ~n"),
	format(" * This program is distributed in the hope that it will be useful,~n"),
	format(" * but WITHOUT ANY WARRANTY; without even the implied warranty of~n"),
	format(" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the~n"),
	format(" * GNU General Public License for more details.~n"),
	format(" * ~n"),
	format(" * You should have received a copy of the GNU General Public License~n"),
	format(" * along with this program; if not, write to the Free Software~n"),
	format(" * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.~n"),
	format(" */~n~n").
