unit Directories;

interface

var
  DataDir: string;
  LocaleCode: string = '';
  LocaleCodeAuto: string = '';

function LocalizedFilePath(const Path: string): string;
procedure UnitInit;
function GetSavedDir(Home: Boolean = False): string;
function GetMapsDir(Home: Boolean = False): string;
function GetGraphicsDir: string;
function GetSoundsDir: string;
function GetMusicDir: string;
function GetAiDir: string;
function GetAppSharePath(Path: string): string;


implementation

uses
  FileUtil, LCLIntf, LCLType, LCLProc, LazUTF8, SysUtils, LazFileUtils, Forms,
  Translations;

function GetAppSharePath(Path: string): string;
{$IFDEF UNIX}
var
  NewPath: string;
{$ENDIF}
begin
  Result := ExtractFileDir(Application.ExeName) + DirectorySeparator + Path;
  {$IFDEF UNIX}
  // If installed in Linux system then try to use different installation directory
  NewPath := ExtractFileDir(Application.ExeName) + DirectorySeparator + '..' +
    DirectorySeparator + 'share' + DirectorySeparator +
    ExtractFileNameOnly(Application.ExeName) + DirectorySeparator + Path;

  if not DirectoryExists(Result) then begin
    if DirectoryExists(NewPath) then begin
      Result := NewPath;
      Exit;
    end;
  end;

  if not FileExists(Result) then begin
    if FileExists(NewPath) then begin
      Result := NewPath;
      Exit;
    end;
  end;
  {$ENDIF}
end;

function GetLocale: string;
var
  I: Integer;
  LanguageID: TLanguageID;
begin
  // Win32 user may decide to override locale with LANG variable.
  Result := Copy(GetEnvironmentVariableUTF8('LANG'), 1, 2);

  if Result = '' then begin
    for I := 1 to ParamCount - 1 do
      if (ParamStrUTF8(I) = '--LANG') or (ParamStrUTF8(I) = '-l') or
        (ParamStrUTF8(I) = '--lang') then
          Result := ParamStrUTF8(I + 1);
  end;
  if Result = '' then begin
    LanguageID := GetLanguageID;
    Result := Copy(LanguageID.LanguageID, 1, 2);
  end;
end;

function LocalizedFilePath(const Path: string): string;
var
  LocaleCodeDir: string;
begin
  if LocaleCode = '' then begin
    if LocaleCodeAuto = '' then LocaleCodeAuto := GetLocale;
    LocaleCodeDir := LocaleCodeAuto;
  end else LocaleCodeDir := LocaleCode;

  if LocaleCode <> 'en' then begin
    Result := GetAppSharePath('Localization' + DirectorySeparator + LocaleCodeDir + DirectorySeparator + Path);
    if not DirectoryExists(Result) and not FileExists(Result) then
      Result := GetAppSharePath(Path);
  end else Result := GetAppSharePath(Path);
end;

procedure CopyDir(SourceDir, DestinationDir, Filter: string);
var
  Src, Dst: TSearchRec;
begin
  if not DirectoryExists(DestinationDir) then ForceDirectories(DestinationDir);
  if FindFirst(SourceDir + DirectorySeparator + Filter, $21, Src) = 0 then
    repeat
      if (FindFirst(DestinationDir + DirectorySeparator + Src.Name, $21, Dst) <> 0) or
        (Dst.Time < Src.Time) then
        CopyFile(SourceDir + DirectorySeparator + Src.Name,
          DestinationDir + DirectorySeparator + Src.Name, False);
      FindClose(Dst);
    until FindNext(Src) <> 0;
  FindClose(Src);
end;

procedure CopyFiles;
begin
  if DirectoryExists(GetSavedDir(True)) and not DirectoryExists(GetSavedDir(False)) then
    CopyDir(GetSavedDir(True), GetSavedDir(False), '*.*');
  if DirectoryExists(GetMapsDir(True)) and not DirectoryExists(GetMapsDir(False)) then
    CopyDir(GetMapsDir(True), GetMapsDir(False), '*.*');
end;

procedure UnitInit;
var
  AppDataDir: string;
begin
  LocaleCode := '';

  AppDataDir := GetAppConfigDir(False);
  if AppDataDir = '' then DataDir := GetAppSharePath('')
  else begin
    if not DirectoryExists(AppDataDir) then ForceDirectories(AppDataDir);
    DataDir := AppDataDir;
  end;
  CopyFiles;
end;

function GetSavedDir(Home: Boolean = False): string;
begin
  if Home then Result := GetAppSharePath('Saved')
    else Result := DataDir + 'Saved';
end;

function GetMapsDir(Home: Boolean = False): string;
begin
  if Home then Result := GetAppSharePath('Maps')
    else Result := DataDir + 'Maps';
end;

function GetGraphicsDir: string;
begin
  Result := GetAppSharePath('Graphics');
end;

function GetSoundsDir: string;
begin
  Result := GetAppSharePath('Sounds');
end;

function GetMusicDir: string;
begin
  Result := GetAppSharePath('Music');
end;

function GetAiDir: string;
begin
  Result := GetAppSharePath('AI');
end;

end.
