## -*- texinfo -*-
## @deftypefn {Function File} {@var{v} =} dsppack_version ()
## Return the dsppack version string (e.g. @code{"1.2.0"}).
##
## The value is read from the @file{Version:} line of the package
## @file{DESCRIPTION} file, so anything that stamps a version into its
## output (saved designs, reports) stays in step with the package itself.
## Both layouts are searched: @file{packinfo/DESCRIPTION} beside the
## installed functions, and @file{../DESCRIPTION} when running straight
## from a source checkout.  If neither can be read the compiled-in
## fallback below is returned.
## @end deftypefn

function v = dsppack_version()
    v = "1.2.0";   % fallback when DESCRIPTION is unavailable

    here      = fileparts(mfilename("fullpath"));
    candidates = {fullfile(here, "packinfo", "DESCRIPTION"), ...
                  fullfile(fileparts(here), "DESCRIPTION")};

    for k = 1:numel(candidates)
        fid = fopen(candidates{k}, "r");
        if fid == -1
            continue;
        end
        txt = fread(fid, Inf, "char=>char")';
        fclose(fid);
        tok = regexp(txt, '(?m)^\s*Version:\s*(\S+)', "tokens", "once");
        if ~isempty(tok)
            v = tok{1};
            return;
        end
    end
end
