// Reconcile DEP-5 debian/copyright to licensecheck
//
// Copyright : 2024 P Blackman
// License   : BSD-2-clause
//
// Process command line options

unit options;
{$mode delphi}

interface

var
    Option_Version : Boolean = False;
    Option_Help    : Boolean = False;
    Option_Struct  : Boolean = False;
    Option_Long    : Boolean = False;
    Option_Terse   : Boolean = False;

    ReadMeFile : Text;

	FallbackLang,
	Lang5	: Ansistring;
	Lang2   : string[2];

Procedure ShowVersions;
Procedure ShowHelp;
procedure GetOptions;


implementation uses Process, SysUtils, gettext, support;

// Get debian package version from it's changelog
function GetVersion (Package : String) : String;
var OK : Boolean;
    S1 : String;

begin
    OK := RunCommand('/usr/bin/dpkg-parsechangelog', ['-l', '/usr/share/doc/' + Package, '--show-field', 'Version'],
        S1,  [poUsePipes, poWaitOnExit]);

    If OK then
        result := TrimRight(S1)
    else
        result := '';
end;

Procedure ShowVersions;
begin
    Writeln (Lang2 + ': Versions: recon ' + (GetVersion('licenserecon/changelog.gz'))
          + '  check ' + (GetVersion('licensecheck/changelog.Debian.gz')));
end;


Procedure ShowHelp;
var line   : String;
begin
	if not OpenFile ('/usr/share/doc/licenserecon/ReadMe.'+ Lang2, ReadMeFile) then
		OpenFile ('/usr/share/doc/licenserecon/ReadMe', ReadMeFile);

    While not EOF (ReadMeFile) Do
    begin
        Readln (ReadMeFile, Line);
        Writeln (Line);
    end;
end;


procedure CheckParam (P : String);
begin
    if SameText (P, '-v') or SameText (P, '--version') then
        Option_Version := true
    else if SameText (P, '-h') or SameText (P, '-?') or SameText (P, '--help') then
        Option_Help := True
    else if SameText (P, '-s') or SameText (P, '--struct') then
        Option_Struct := true
    else if SameText (P, '-t') or SameText (P, '--terse') then
        Option_Terse   := true
    else if SameText (P, '-l') or SameText (P, '--long') then
        Option_Long := true
    else
        Writeln ('Ignoring unknown option ', P);
end;


procedure GetOptions;
var P : Integer;
begin
    for P := 1 to ParamCount do
        CheckParam (ParamStr(P));

    // This combination make no sense
    if Option_Terse then
        Option_Long := False;
        
    // Setup language id
    GetLanguageIDs(Lang5, FallbackLang);
    Lang2 := LeftStr(Lang5,2);
end;

end.
