function legacy_bayestopt_to_dprior(o, bayestopt_, PriorTrunc, Uniform)
% Populate an empty @dprior instance from a legacy bayestopt_-shaped struct.
%
% INPUTS
%   o           [@dprior]  empty prior object to populate in place
%   bayestopt_  [struct]   legacy prior specification (fields pshape, p1..p7, name)
%   PriorTrunc  [double]   prior truncation probability (default 0)
%   Uniform     [logical]  if true, all priors become uniform (default false)

% Copyright © 2023-2026 Dynare Team
%
% This file is part of Dynare.
%
% Dynare 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.
%
% Dynare 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 Dynare.  If not, see <https://www.gnu.org/licenses/>.

if nargin < 3, PriorTrunc = 0; end
if nargin < 4, Uniform = false; end

if isfield(bayestopt_, 'p1'),   o.p1   = bayestopt_.p1;   end
if isfield(bayestopt_, 'p2'),   o.p2   = bayestopt_.p2;   end
if isfield(bayestopt_, 'p3'),   o.p3   = bayestopt_.p3;   end
if isfield(bayestopt_, 'p4'),   o.p4   = bayestopt_.p4;   end
if isfield(bayestopt_, 'p5'),   o.p5   = bayestopt_.p5;   end
if isfield(bayestopt_, 'p6'),   o.p6   = bayestopt_.p6;   end
if isfield(bayestopt_, 'p7'),   o.p7   = bayestopt_.p7;   end
if isfield(bayestopt_, 'p11'),  o.p11  = bayestopt_.p11;  end
if isfield(bayestopt_, 'name'), o.name = bayestopt_.name; end

bounds = prior_bounds(bayestopt_, PriorTrunc);
o.lb = bounds.lb;
o.ub = bounds.ub;

if Uniform
    prior_shape = repmat(5, length(o.p6), 1);
else
    prior_shape = bayestopt_.pshape;
end
o.idbeta      = find(prior_shape == 1);
o.idgamma     = find(prior_shape == 2);
o.idgaussian  = find(prior_shape == 3);
o.idinvgamma1 = find(prior_shape == 4);
o.iduniform   = find(prior_shape == 5);
o.idinvgamma2 = find(prior_shape == 6);
o.idweibull   = find(prior_shape == 8);
