aboutsummaryrefslogtreecommitdiff
path: root/lib/bisect.nim
blob: 55d5a6009024c124d57fb37dbd01b8e35be1ffbe (plain) (blame)
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
import future, os, osproc, re, strutils

{.passL: "-lalpm".}

proc vercmp(a: cstring, b: cstring): cint
  {.cdecl, importc: "alpm_pkg_vercmp".}

proc getSourceVersion(relativePath: string): seq[string] =
  let lines = execProcess("/bin/bash", ["-c",
    """source "$1/PKGBUILD" && echo "$epoch" && echo "$pkgver" && echo "$pkgrel"""",
    "bash", relativePath], options = {}).split("\n")
  if lines.len == 4:
    lines[0 .. 2]
  else:
    @[]

let params = commandLineParams()
let compareMethod = params[0]
let relativePath = params[1]
let compareVersion = params[2]

let (currentVersion, supported) = if compareMethod == "source":
    (getSourceVersion(relativePath), true)
  else:
    (@[], false)

if not supported:
  programResult = 255
elif currentVersion.len != 3:
  programResult = 125
else:
  let epoch = currentVersion[0].strip
  let pkgver = currentVersion[1].strip
  let pkgrel = currentVersion[2].strip

  if pkgver.len == 0 or pkgrel.len == 0:
    programResult = 125
  else:
    let version = if epoch.len > 0:
        epoch & ":" & pkgver & "-" & pkgrel
      else:
        pkgver & "-" & pkgrel

    # this output is required by main program
    echo(version)
    if vercmp(compareVersion, version) > 0:
      programResult = 0
    else:
      programResult = 1