function hgeo_collect_js_scripts(folder);

% Function hgeo_collect_js_scripts(folder);
% 
% collects the three.js library files and
% the "hgeo_subroutines.js" file, and puts
% them together in one file called 
% "hgeo_three_bundle.js", and stores this
% result in the actual directory, or, if 
% input variable folder is given, in the
% indicated directory.
% 
% 
% (c) 2026 fabien van mook
% 
% 2026.08.20 release of this file within package "fvm-hgeo" under GNU GPLv3+

% 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 3 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 can read the GNU General Public License on
% <https://www.gnu.org/copyleft/gpl.html>.


if (nargin < 1),
  folder = "";
else,
  if (length(folder) > 0),
    if (folder(end) != filesep),
      folder = [folder filesep];
    endif;
    mkdir(folder);
  endif;
endif;

%
% first, check whether "hgeo_three_bundle.js" already exists:
%

fn_bundle = "hgeo_three_bundle.js";
mt_bundle = NA;
if isfile([folder fn_bundle]),
  [~,mt_bundle] = filemtime([folder fn_bundle]);
endif;

% string to hold all the js scripts:
js_bundle = "";

% lastest modification time of the library files:
mt_latest = 0;

%
% secondly, find and collect the three.js library files:
%

% files = { <last_part_of_path>, <filename>; ...}:
%
files = {["share" filesep "20200827-threejs-r120-downloads" filesep "build" filesep], "three.js";
         ["share" filesep "20200827-threejs-r120-downloads" filesep "examples" filesep "js" filesep  "controls" filesep], "OrbitControls.js";
         ["share" filesep "20200827-threejs-r120-downloads" filesep "examples" filesep "js" filesep  "effects" filesep], "StereoEffect.js";
        };

for ifile = [1:size(files,1)],
  fn = [files{ifile,1} files{ifile,2}];
  found = 0;
  for ireldir = [1:4],
    reldir = repmat([".." filesep],1,ireldir);
    if isfile([reldir fn]),
      found = 1;
      break;
    endif;
    reldir = [reldir "fabien" filesep];
    if isfile([reldir fn]),
      found = 1;
      break;
    endif;
  endfor;
  if (found),
    js_bundle = [js_bundle ...
                 "\n\n// copy of \"" files{ifile,2} "\":\n\n" ...
                 fileread([reldir fn])];
    [~,mt] = filemtime([reldir fn]);
    mt_latest = max([mt, mt_latest]);
  else,
    warning("hgeo_collect_js_scripts(): cannot find three.js library file \"%s\".\n",files{ifile,2});
  endif;
endfor;

%
% thirdly, find and collect the hgeo_subroutines.js file:
%

fn_hgeo_subroutines = "hgeo_subroutines.js";
fn = file_in_loadpath(fn_hgeo_subroutines);
if (length(fn) > 0),
  js_bundle = [js_bundle ...
               "\n\n// copy of \"" fn_hgeo_subroutines "\":\n\n" ...
               fileread(fn)];
  [~,mt] = filemtime(fn);
  mt_latest = max([mt, mt_latest]);
else,
  warning("hgeo_collect_js_scripts(): cannot find file \"%s\".\n",fn_hgeo_subroutines);
endif;

%
% fourthly, write the bundle if it does not exist yet, or if the existing bundle is older:
%
if ( isna(mt_bundle) || (mt_bundle < mt_latest) ),
  % remove "history_begins_here ... history_ends_here":
  t1 = "// history_begins_here";
  t2 = "// history_ends_here";
  it1 = index(js_bundle,t1);
  it2 = index(js_bundle,t2);
  js_bundle = [js_bundle(1:(it1-1)) js_bundle((it2+length(t2)):end)];

  filewrite([folder fn_bundle],js_bundle);
else,
  printf("hgeo_collect_js_scripts(): file \"%s\" already exists and is new enough.\n",[folder fn_bundle]);
endif;

endfunction;
