function [ok,info,fid] = kmlWriteHeader(fName,varargin)
%KMLWRITEHEADER  Reads the header template file and writes it to a kml file
%
%   The function creates a kml file and writes the content of the header
%   file to that file. An icon is also written to the file: substitution of
%   ABCXYZ.
%
%   Syntax:
%   [ok,info,fid] = kmlWriteHeader(fName,varargin)
%
%   Input:
%     fName    = the filedescriptor of the kml file that must be created
%     varargin = key/value pairs  
%                 'icon' : the name of the icon filedescriptor
%
%   Output:
%     ok,info,fid =
%
%   Example
%   kmlWriteHeader
%
%   See also 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2010 <COMPANY>
%       Lou Verhage
%
%       <lou.verhage@deltares.nl>	
%
%       <ADDRESS>
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 of the License, or (at your option) any later version.
%
%   This library 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
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://OpenEarth.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and 
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute 
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 23 Mar 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

% $Id: kmlWriteHeader.m 4926 2011-04-18 13:00:40Z verhage $
% $Date: 2011-04-18 15:00:40 +0200 (Mon, 18 Apr 2011) $
% $Author: verhage $
% $Revision: 4926 $
% $HeadURL: https://svn.oss.deltares.nl/repos/openearthrawdata/trunk/rijkswaterstaat/kustlijnkaartenboek/scripts/kmlWriteHeader.m $
% $Keywords: $

%%initialize
OPT.icon  = '';
OPT       = setProperty(OPT,varargin{:});
if isempty(OPT.icon)
    OPT.icon = 'http://maps.google.com/mapfiles/kml/paddle/ltblu-ICONTYPE.png';
end
ok        = true;
info      = '';
fid       = -1;
fidHeader = -1;

%% handle files
% create the kml file
[PATHSTR,NAME,EXT,VERSN] = fileparts(fName);
if isempty(PATHSTR)
   fName = [NAME '.kml'];
else
   fName = [PATHSTR filesep NAME '.kml'];
end
fid = fopen(fName,'w');
if fid < 0
   ok = false;
   info = ['File ' fileName ' could not be opened.'];
   return
end
%  WRITE the header 
% open the header template file
fidHeader = fopen('templateHeader.kml','r');
if fidHeader < 0
   ok = false;
   info = ['File "templateHeader.kml" could not be found.'];
   return
end

while ~feof(fidHeader)
   line = fgetl(fidHeader);
   k = findstr(line, 'ABCXYZ');
   if ~isempty(k)
       s = strrep(line,'ABCXYZ',OPT.icon);
       k = findstr(s, 'ICONTYPE');
       if ~isempty(k)
           s = strrep(s,'ICONTYPE','blank');
       end
   else
       s = line;
   end
   k = findstr(s, 'DEFUVW');
   if ~isempty(k)
       s = strrep(s,'DEFUVW',OPT.icon);
       k = findstr(s, 'ICONTYPE');
       if ~isempty(k)
           s = strrep(s,'ICONTYPE','square');
       end
   end
   s = [strrep(s,'\','\\') ' \n'];
   fprintf(fid,s);
end

fclose(fidHeader);

