unit runutils;

interface
uses {$IFDEF MSWINDOWS}
     windows,shellapi,
     {$ENDIF}
     {$IFDEF LINUX}
     libc,
     {$ENDIF}
     sysutils,shwmsg;

procedure run(const prog:string);overload;
procedure run(const prog,param:string);overload;
{$IFDEF MSWINDOWS}
procedure run(const prog:string;const priority:dword);overload;
procedure run(const prog,param:string;const priority:dword);overload;
{$ENDIF}
procedure open(const filename:string);

const ABOVE_NORMAL_PRIORITY_CLASS=$8000;
      {$EXTERNALSYM ABOVE_NORMAL_PRIORITY_CLASS}
      BELOW_NORMAL_PRIORITY_CLASS=$4000;
      {$EXTERNALSYM BELOW_NORMAL_PRIORITY_CLASS}

implementation

procedure open(const filename:string);
begin
{$IFDEF MSWINDOWS}
shellexecute(0,'open',pchar(filename),'',nil,sw_normal);
{$ENDIF}
{$IFDEF LINUX}
if libc.system(pchar('kfmclient exec "'+filename+'"'))=-1 then
   showmessagefmt('Failed to execute ''%s''',[filename]);
{$ENDIF}
end;

procedure run(const prog:string);
begin
run(prog,'');
end;

procedure run(const prog,param:string);
begin
{$IFDEF MSWINDOWS}
if shellexecute(0,'open',pchar(prog),pchar(param),pchar(extractfilepath(prog)),sw_normal)=ERROR_FILE_NOT_FOUND
   then showmessagefmt('File not found: "%s"',[prog]);
{$ENDIF}
{$IFDEF LINUX}
if libc.system(pchar('kfmclient exec "'+prog+' '+param+'"'))=-1 then
   showmessagefmt('Failed to execute ''%s''',[prog+' '+param]);
{$ENDIF}
end;

{$IFDEF MSWINDOWS}
procedure run(const prog:string;const priority:dword);
begin
run(prog,'',priority);
end;

procedure run(const prog,param:string;const priority:dword);
var pi:process_information;
    si:startupinfo;
begin
if comparetext(extractfileext(prog),'.bat')=0 then run(prog,param) else begin
if not fileexists(prog) then showmessagefmt('File not found: "%s"',[prog]);
si.lpReserved:=nil;
si.lpDesktop:=nil;
si.lpTitle:=nil;
si.dwFlags:=0;
si.cbReserved2:=0;
si.lpReserved2:=nil;
if createprocess(pchar(prog),pchar(param),nil,nil,true,priority,nil,pchar(extractfilepath(prog)),si,pi)
   =false then run(prog,param);
end;
end;
{$ENDIF}

end.

