{
********************************************************************************

SPackGui
Copyright (C) 2012-2013 Geoffray Levasseur <jeff.levasseur@free.fr>.
Copyright (C) <date> <add your name and mail address here>

http://jeff.levasseur.tuxfamily.org/
http://0.tuxfamilly.org/

This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software.  You can  use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability.

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and,  more generally, to use and operate it in the
same conditions as regards security.

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.

********************************************************************************

Description:
  main program

  Note: This program is intended for Unix OSes only. So all functions are Unix
  centric and references to any other OS have been removed. If you are
  interested in some more multiplatform functions for your own program, a more
  complete version for some of those may be available. To get it, please
  ask for it by mail to the maintenor or check the other projects of the
  author in the parent directory of the trunk subversion tree of that project.

********************************************************************************
}
program spackgui;

{$include ../common/defines.inc}

uses
  {$IFDEF UseCThreads}
  cthreads,
  cmem,
  {$ENDIF}
  Interfaces, IniFiles, // this includes the LCL widgetset
  sysutils, LCLType, Dialogs, LCLVersion, Forms,
  frmmain, uversion, frmabout, ucommon, udebug, uiconmanager, ustrings, uutils,
  frmprogress, uPackageManager, uDownload, blcksock, ftpsend, httpsend,
  frmLogFile, uDownloadManager, frmEnvironementSettings, frmAddChangeRepo,
  uSpackPackage, frmPackageProperty, frmsearch, frmDisplaySettings,
uListViewColors;

{$R *.res}

procedure CheckLocking; {$warning Use PID for a better instance checking}
var
  OtherComputer, OtherUser: string;
begin
  //we use a file for "instance checking" as this is commonly used on Unixes
  if FileExists(sLockFile) then
  begin
    Application.MessageBox(PChar(Format(rsWarningOtherInstance, [OtherUser,
      OtherComputer])), PChar(rsWarning), MB_ICONHAND + MB_OK);
    bChangeAllowed := False;
  end else
  begin
    bChangeAllowed := True;
    if not CreateLockFile(sLockFile) then
    begin
      PrintLnDbg(rsErrorUnableToLock, vlLow);
      bChangeAllowed := False;
    end;
  end;
end;

begin
  Application.Scaled:=True;
  if not InitDbg(sDefaultLogFileName) then
    InitDbg(GetUserDir + ExtractFileName(sDefaultLogFileName));
  Application.Title := 'SPackGui';
  Application.OnException := ExceptionProc;
  {$IF (lcl_major >= 1) or ((lcl_minor >= 9) and (lcl_release >= 31))}
    RequireDerivedFormResource := True;
  {$ENDIF}
  {$IFDEF TEST}
    PrintLnDbg(rsInfoRunningTest);
  {$ENDIF}
  {$IFDEF Debug}
    PrintLnDbg(rsInfoRunningDebug);
  {$ENDIF}
  InitConf;
  if bReadOnly then
  begin
    PrintLnDbg(rsErrorReadOnly, vlLow);
    PrintLnDbg(rsWarningConfWillBeLost, vlLow);
  end;
  if GetUserName <> sRootUserName then
  begin
    Application.MessageBox(PChar(rsWarningNotRoot), PChar(rsWarning),
      MB_ICONHAND + MB_OK);
    PrintlnDbg(rsWarningDbgNotRoot, vlLow);
  end;
  CheckLocking;
  fLogView := nil;
  LoadIconSet(sIconBaseDir);
  try
    Application.Initialize;
    Application.CreateForm(TfMain, fMain);
    Application.Run;
  finally
    if FileExists(sLockFile) then
    begin
      AssignFile(tLockFile, sLockFile);
      Erase(tLockFile);
    end;
    TerminateDbg;
  end;
end.

