summaryrefslogtreecommitdiff
path: root/gitupdateunit.pas
blob: 94e0263c48eea4c0d1672aa8cb2a521d8e09670d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
unit gitupdateunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

implementation

uses
  baseunix, process, systemunit, lowlevelunit;

var
  updated:          boolean;
  myDir,baseDir,
    output,tmpFile: string;
  args:             array of string;

initialization
  updated:=false;

  myDir:=extractFilePath(paramstr(0));
  baseDir:=extractFilePath(leftStr(myDir,length(myDir)-1));

  tmpFile:='';

  output:='';
  setlength(args,1);
  args[0]:='pull';
  if runCommandInDir(myDir,'/usr/bin/git',args,output) then begin
    updated:=updated or (output<>'Already up-to-date.'#10);
    if runCommandInDir(baseDir+'units','/usr/bin/git',args,output) then begin
      updated:=updated or (output<>'Already up-to-date.'#10);
      if updated then begin
        setlength(args,15);
        args[0]:='-MObjFPC';
        args[1]:='-Scghi';
        args[2]:='-Cg';
        args[3]:='-CirotR';
        args[4]:='-O1';
        args[5]:='-g';
        args[6]:='-gl';
        args[7]:='-l';
        args[8]:='-vewnhibq';
        args[9]:='-Filib/x86_64-linux';
        args[10]:='-Fu../units';
        args[11]:='-Fu.';
        args[12]:='-FUlib/x86_64-linux';
        tmpFile:=myDir+extractFileName(paramstr(0));
        if fileexists(tmpFile+'.lpr') then
          args[13]:=tmpFile+'.lpr'
        else
          args[13]:=tmpFile+'.pas';
        tmpFile:=mkTemp('/tmp/fpc.XXXXXX');
        args[14]:='-o'+tmpFile;
        if runCommandInDir(myDir,'/usr/bin/fpc',args,output) then begin
          setlength(args,2);
          args[0]:=tmpFile;
          args[1]:=paramstr(0);
          if runCommand('/usr/bin/sha512sum',args,output) then begin
            baseDir:=erstesArgument(output,#10);
            if erstesArgument(baseDir)<>erstesArgument(output) then begin
              deletefile(paramstr(0));
              if runCommand('/usr/bin/mv',args,output) then
                fpExecVe(paramstr(0),argv,envp);
            end;
          end;
        end;
      end;
    end;
  end;
  if (tmpFile<>'') and fileexists(tmpFile) then
    deletefile(tmpFile);
  setlength(args,0);
end.