// Test for false positive on GPL later than licences
//
// Copyright : 2023 P Blackman
// License   : BSD-2-clause
//


var  lfile : Text; // Get uninitialised data warning if this is local

// Extract license short string, expecting a GPL license
function GetFileLicense (Fname : AnsiString) : String;
const maxlines : Integer = 100;
var lines,
    lpos : Integer;
    L1,L2 : String;
    line : AnsiString;

begin
    result := '';
    lines := 0;
    if OpenFile (FName, lfile) then
    begin
        while not EOF (lfile) and (lines < maxlines) do
        begin
            readln (lfile, line);
            lpos := NPos ('GPL', line, 1);

            if lpos > 0 then
            begin
                L1 := ExtractSubstr(line, lpos, WhiteSpace);
                Result := L1;

                if L1 = 'GPL' then
                begin
                    L2 := ExtractSubstr(line, lpos, WhiteSpace + ['"']);
                    if L2 = 'V3+' then
                        result := L1 + L2;
                end;

                lines := maxlines // terminate loop
            end
            else
                lines := lines + 1;
        end
    end;
end;


// Return true if Actual is 3+ as shown in d/copright
function CheckGPL (Fname, Dep5, Actual : AnsiString) : Boolean;
var License : String;

begin
    result := false;

    if (Dep5 = 'GPL-3+') and (Actual = 'GPL-3') then
    begin
        License := GetFileLicense (FName);
        if (License  <> '') and (license [length(License)] = '+') then
            result := true;
    end;
end;
