aboutsummaryrefslogtreecommitdiff
path: root/lpm.lua
blob: 2f03a4ecb14b8eb372b6e65f4107e0fb62c317da (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
setmetatable(_G, { __index = function(t, k) if not rawget(t, k) then error("cannot get undefined global variable: " .. k, 2) end end, __newindex = function(t, k) error("cannot set global variable: " .. k, 2) end  })

-- Begin rxi JSON library.
local json = { _version = "0.1.2" }
local encode
local escape_char_map = {
  [ "\\" ] = "\\",
  [ "\"" ] = "\"",
  [ "\b" ] = "b",
  [ "\f" ] = "f",
  [ "\n" ] = "n",
  [ "\r" ] = "r",
  [ "\t" ] = "t",
}

local escape_char_map_inv = { [ "/" ] = "/" }
for k, v in pairs(escape_char_map) do
  escape_char_map_inv[v] = k
end


local function escape_char(c)
  return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte()))
end


local function encode_nil(val)
  return "null"
end


local function encode_table(val, stack)
  local res = {}
  stack = stack or {}

  -- Circular reference?
  if stack[val] then error("circular reference") end

  stack[val] = true

  if rawget(val, 1) ~= nil or next(val) == nil then
    -- Treat as array -- check keys are valid and it is not sparse
    local n = 0
    for k in pairs(val) do
      if type(k) ~= "number" then
        error("invalid table: mixed or invalid key types")
      end
      n = n + 1
    end
    if n ~= #val then
      error("invalid table: sparse array")
    end
    -- Encode
    for i, v in ipairs(val) do
      table.insert(res, encode(v, stack))
    end
    stack[val] = nil
    return "[" .. table.concat(res, ",") .. "]"

  else
    -- Treat as an object
    for k, v in pairs(val) do
      if type(k) ~= "string" then
        error("invalid table: mixed or invalid key types")
      end
      table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
    end
    stack[val] = nil
    return "{" .. table.concat(res, ",") .. "}"
  end
end


local function encode_string(val)
  return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end


local function encode_number(val)
  -- Check for NaN, -inf and inf
  if val ~= val or val <= -math.huge or val >= math.huge then
    error("unexpected number value '" .. tostring(val) .. "'")
  end
  return string.format("%.14g", val)
end


local type_func_map = {
  [ "nil"     ] = encode_nil,
  [ "table"   ] = encode_table,
  [ "string"  ] = encode_string,
  [ "number"  ] = encode_number,
  [ "boolean" ] = tostring,
}


encode = function(val, stack)
  local t = type(val)
  local f = type_func_map[t]
  if f then
    return f(val, stack)
  end
  error("unexpected type '" .. t .. "'")
end


function json.encode(val)
  return ( encode(val) )
end

local parse

local function create_set(...)
  local res = {}
  for i = 1, select("#", ...) do
    res[ select(i, ...) ] = true
  end
  return res
end

local space_chars   = create_set(" ", "\t", "\r", "\n")
local delim_chars   = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
local escape_chars  = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
local literals      = create_set("true", "false", "null")

local literal_map = {
  [ "true"  ] = true,
  [ "false" ] = false,
  [ "null"  ] = nil,
}


local function next_char(str, idx, set, negate)
  for i = idx, #str do
    if set[str:sub(i, i)] ~= negate then
      return i
    end
  end
  return #str + 1
end


local function decode_error(str, idx, msg)
  local line_count = 1
  local col_count = 1
  for i = 1, idx - 1 do
    col_count = col_count + 1
    if str:sub(i, i) == "\n" then
      line_count = line_count + 1
      col_count = 1
    end
  end
  error( string.format("%s at line %d col %d", msg, line_count, col_count) )
end


local function codepoint_to_utf8(n)
  -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
  local f = math.floor
  if n <= 0x7f then
    return string.char(n)
  elseif n <= 0x7ff then
    return string.char(f(n / 64) + 192, n % 64 + 128)
  elseif n <= 0xffff then
    return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
  elseif n <= 0x10ffff then
    return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
                       f(n % 4096 / 64) + 128, n % 64 + 128)
  end
  error( string.format("invalid unicode codepoint '%x'", n) )
end


local function parse_unicode_escape(s)
  local n1 = tonumber( s:sub(1, 4),  16 )
  local n2 = tonumber( s:sub(7, 10), 16 )
   -- Surrogate pair?
  if n2 then
    return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
  else
    return codepoint_to_utf8(n1)
  end
end


local function parse_string(str, i)
  local res = ""
  local j = i + 1
  local k = j

  while j <= #str do
    local x = str:byte(j)

    if x < 32 then
      decode_error(str, j, "control character in string")

    elseif x == 92 then -- `\`: Escape
      res = res .. str:sub(k, j - 1)
      j = j + 1
      local c = str:sub(j, j)
      if c == "u" then
        local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
                 or str:match("^%x%x%x%x", j + 1)
                 or decode_error(str, j - 1, "invalid unicode escape in string")
        res = res .. parse_unicode_escape(hex)
        j = j + #hex
      else
        if not escape_chars[c] then
          decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
        end
        res = res .. escape_char_map_inv[c]
      end
      k = j + 1

    elseif x == 34 then -- `"`: End of string
      res = res .. str:sub(k, j - 1)
      return res, j + 1
    end

    j = j + 1
  end

  decode_error(str, i, "expected closing quote for string")
end


local function parse_number(str, i)
  local x = next_char(str, i, delim_chars)
  local s = str:sub(i, x - 1)
  local n = tonumber(s)
  if not n then
    decode_error(str, i, "invalid number '" .. s .. "'")
  end
  return n, x
end


local function parse_literal(str, i)
  local x = next_char(str, i, delim_chars)
  local word = str:sub(i, x - 1)
  if not literals[word] then
    decode_error(str, i, "invalid literal '" .. word .. "'")
  end
  return literal_map[word], x
end


local function parse_array(str, i)
  local res = {}
  local n = 1
  i = i + 1
  while 1 do
    local x
    i = next_char(str, i, space_chars, true)
    -- Empty / end of array?
    if str:sub(i, i) == "]" then
      i = i + 1
      break
    end
    -- Read token
    x, i = parse(str, i)
    res[n] = x
    n = n + 1
    -- Next token
    i = next_char(str, i, space_chars, true)
    local chr = str:sub(i, i)
    i = i + 1
    if chr == "]" then break end
    if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
  end
  return res, i
end


local function parse_object(str, i)
  local res = {}
  i = i + 1
  while 1 do
    local key, val
    i = next_char(str, i, space_chars, true)
    -- Empty / end of object?
    if str:sub(i, i) == "}" then
      i = i + 1
      break
    end
    -- Read key
    if str:sub(i, i) ~= '"' then
      decode_error(str, i, "expected string for key")
    end
    key, i = parse(str, i)
    -- Read ':' delimiter
    i = next_char(str, i, space_chars, true)
    if str:sub(i, i) ~= ":" then
      decode_error(str, i, "expected ':' after key")
    end
    i = next_char(str, i + 1, space_chars, true)
    -- Read value
    val, i = parse(str, i)
    -- Set
    res[key] = val
    -- Next token
    i = next_char(str, i, space_chars, true)
    local chr = str:sub(i, i)
    i = i + 1
    if chr == "}" then break end
    if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
  end
  return res, i
end


local char_func_map = {
  [ '"' ] = parse_string,
  [ "0" ] = parse_number,
  [ "1" ] = parse_number,
  [ "2" ] = parse_number,
  [ "3" ] = parse_number,
  [ "4" ] = parse_number,
  [ "5" ] = parse_number,
  [ "6" ] = parse_number,
  [ "7" ] = parse_number,
  [ "8" ] = parse_number,
  [ "9" ] = parse_number,
  [ "-" ] = parse_number,
  [ "t" ] = parse_literal,
  [ "f" ] = parse_literal,
  [ "n" ] = parse_literal,
  [ "[" ] = parse_array,
  [ "{" ] = parse_object,
}


parse = function(str, idx)
  local chr = str:sub(idx, idx)
  local f = char_func_map[chr]
  if f then
    return f(str, idx)
  end
  decode_error(str, idx, "unexpected character '" .. chr .. "'")
end


function json.decode(str)
  if type(str) ~= "string" then
    error("expected argument of type string, got " .. type(str))
  end
  local res, idx = parse(str, next_char(str, 1, space_chars, true))
  idx = next_char(str, idx, space_chars, true)
  if idx <= #str then
    decode_error(str, idx, "trailing garbage")
  end
  return res
end

-- End JSON library.

local common = {}

function common.rmrf(root)
  if not root or root == "" then return end
  local info = system.stat(root)
  if not info then return end
  if info.type == "file" or info.symlink then return os.remove(root) end
  for i,v in ipairs(system.ls(root)) do common.rmrf(root .. PATHSEP .. v) end
  system.rmdir(root)
end

function common.mkdirp(path)
  local stat = system.stat(path)
  if stat and stat.type == "dir" then return true end
  if stat and stat.type == "file" then error("path " .. path .. " exists") end
  local subdirs = {}
  while path and path ~= "" do
    system.mkdir(path)
    local updir, basedir = path:match("(.*)[/\\](.+)$")
    table.insert(subdirs, 1, basedir or path)
    path = updir
  end
  for _, dirname in ipairs(subdirs) do
    path = path and path .. PATHSEP .. dirname or dirname
    system.mkdir(path)
  end
end


function common.basename(path)
  local s = path:reverse():find("[/\\]")
  if not s then return path end
  return path:sub(#path - s + 2)
end


function common.dirname(path)
  local s = path:reverse():find(PATHSEP)
  if not s then return path end
  return path:sub(1, #path - s)
end


function common.merge(src, merge)
  for k, v in pairs(merge) do src[k] = v end
  return src
end


function common.map(list, predicate)
  local t = {}
  for i, v in ipairs(list) do table.insert(t, predicate(v)) end
  return t
end


function common.grep(list, predicate)
  local t = {}
  for i, v in ipairs(list) do if predicate(v) then table.insert(t, v) end end
  return t
end


function common.join(joiner, list)
  local s = ""
  for i, v in ipairs(list) do if i > 1 then s = s .. joiner .. v else s = v end end
  return s
end

function common.split(splitter, str)
  local o = 1
  local res = {}
  while true do
      local s, e = str:find(splitter, o)
      table.insert(res, str:sub(o, s and (s - 1) or #str))
      if not s then break end
      o = e + 1
  end
  return table.unpack(res)
end


function common.slice(t, i, l)
  local n = {}
  for j = i, l ~= nil and (i - l) or #t do
    table.insert(n, t[j])
  end
  return n
end

function common.copy(src, dst)
  local src_stat, dst_stat = system.stat(src), system.stat(dst)
  if not src_stat then error("can't find " .. src) end
  if dst_stat and dst_stat.type == "dir" then return common.copy(src, dst .. PATHSEP .. common.basename(src)) end
  if src_stat.type == "dir" then
    common.mkdirp(dst)
    for i, file in ipairs(system.ls(src)) do common.copy(src .. PATHSEP .. file, dst .. PATHSEP .. file) end
  else
    local src_io, err1 = io.open(src, "rb")
    if err1 then error("can't open for reading " .. src .. ": " .. err1) end
    local dst_io, err2 = io.open(dst, "wb")
    if err2 then error("can't open for writing " .. src .. ": " .. err2) end
    while true do
      local chunk = src_io:read(16*1024)
      if not chunk then break end
      dst_io:write(chunk)
    end
    dst_io:flush()
  end
end

local function get_executable(name)
  return common.grep(common.map({ common.split(":", os.getenv("PATH")) }, function(e) return e .. PATHSEP .. name end), function(e) return system.stat(e) end)[1]
end


local function is_commit_hash(hash)
  return #hash == 40 and not hash:find("[^a-z0-9]")
end


local HOME, USERDIR, CACHEDIR, JSON, VERBOSE, MOD_VERSION, QUIET, FORCE, AUTO_PULL_REMOTES, ARCH, ASSUME_YES, NO_INSTALL_OPTIONAL, TMPDIR, repositories, lite_xls, system_bottle

local actions, warnings = {}, {}
local function log_action(message)
  if JSON then table.insert(actions, message) end
  if not QUIET then io.stderr:write(message .. "\n") end
end
local function log_warning(message)
  if JSON then table.insert(warnings, message) end
  if not QUIET then io.stderr:write("warning: " .. message .. "\n") end
end
local function fatal_warning(message)
  if not FORCE then error(message .. "; use --force to override") else log_warning(message) end
end
local function prompt(message)
  io.stderr:write(message .. " [Y/n]: ")
  if ASSUME_YES then io.stderr:write("Y\n") return true end
  local response = io.stdin:read("*line")
  return not response:find("%S") or response:find("^%s*[yY]%s*$")
end

local function compare_version(a, b) -- compares semver
  if not a or not b then return false end
  local _, _, majora, minora, revisiona = tostring(a):find("(%d+)%.?(%d*)%.?(%d*)")
  local _, _, majorb, minorb, revisionb = tostring(b):find("(%d+)%.?(%d*)%.?(%d*)")
  if majora == nil then error("can't parse version " .. a) end
  if majorb == nil then error("can't parse version " .. b) end
  majora, minora, revisiona = majora or 0, minora or 0, revisiona or 0
  majorb, minorb, revisionb = majorb or 0, minorb or 0, revisionb or 0
  if majora ~= majorb then return tonumber(majora) < tonumber(majorb) and -1 or 1 end
  if minora ~= minorb then return tonumber(minora) < tonumber(minorb) and -1 or 1 end
  if revisiona ~= revisionb then return tonumber(revisiona) < tonumber(revisionb) and -1 or 1 end
  return 0
end

local function match_version(version, pattern)
  if not pattern then return true end
  if pattern:find("^>=") then return compare_version(version, pattern:sub(3)) >= 0 end
  if pattern:find("^<=") then return compare_version(version, pattern:sub(3)) <= 0 end
  if pattern:find("^<") then return compare_version(version, pattern:sub(2)) == -1 end
  if pattern:find("^>") then return compare_version(version, pattern:sub(2)) == 1 end
  if pattern:find("^=") then return compare_version(version, pattern:sub(2)) == 0 end
  return version == pattern
end


local function get_all_plugins()
  local t = {}
  for i,r in ipairs(repositories) do
    for j,p in ipairs(r.plugins) do
      table.insert(t, p)
    end
  end
  return t
end

function common.get(source, target, checksum)
  if not checksum then return system.get(source, target) end
  if not system.stat(CACHEDIR .. PATHSEP .. "files") then common.mkdirp(CACHEDIR .. PATHSEP .. "files") end
  local cache_path = CACHEDIR .. PATHSEP .. "files" .. PATHSEP .. checksum
  if not system.stat(cache_path) then
    system.get(source, cache_path)
    if system.hash(cache_path, "file") ~= checksum then fatal_warning("checksum doesn't match for " .. source) end
  end
  common.copy(cache_path, target)
end

local Plugin = {}
function Plugin.__index(self, idx) return rawget(self, idx) or Plugin[idx] end
function Plugin.new(repository, metadata)
  local type = metadata.type or "plugin"
  local folder = metadata.type == "library" and "libraries" or "plugins"
  if metadata.path then metadata.path = metadata.path:gsub("/", PATHSEP) end
  local self = setmetatable(common.merge({
    repository = repository,
    tags = {},
    type = type,
    path = nil,
    remote = nil,
    version = "1.0",
    dependencies = {},
    local_path = repository.local_path .. PATHSEP .. (repository.commit or repository.branch) .. (metadata.path and (PATHSEP .. metadata.path:gsub("^/", "")) or ""),
  }, metadata), Plugin)
  -- Directory.
  self.organization = ((self.files and #self.files > 0) or self.remote or (not self.path and not self.url)) and "complex" or "singleton"
  return self
end

function Plugin:get_install_path(bottle)
  local folder = self.type == "library" and "libraries" or "plugins"
  local path = (bottle.local_path or USERDIR) .. PATHSEP .. folder .. PATHSEP .. (self.path and common.basename(self.path):gsub("%.lua$", "") or self.name)
  if self.organization == "singleton" then path = path .. ".lua" end
  return path
end


function Plugin:is_installed(bottle)
  return bottle.lite_xl:is_compatible(self) and system.stat(self:get_install_path(bottle))
end


function Plugin:is_incompatible(plugin)
  if self.dependencies[plugin.name] then
    if not match_version(plugin.version, dependencies[plugin.name]) then return true end
  end
  return false
end


local function get_plugin(name, version, filter)
  local candidates = {}
  filter = filter or {}
  for i,repo in ipairs(repositories) do
    for j,plugin in ipairs(repo.plugins) do
      if not version and plugin.provides then 
        for k, provides in ipairs(plugin.provides) do
          if provides == name then
            table.insert(candidates, plugin)
          end
        end
      end
      if plugin.name == name and match_version(plugin.version, version) then
        if (not filter.mod_version or not plugin.mod_version or tonumber(plugin.mod_version) == tonumber(filter.mod_version)) then
          table.insert(candidates, plugin)
        end
      end
    end
  end
  table.sort(candidates, function (a,b) return a.version < b.version end)
  return table.unpack(candidates)
end


function Plugin:get_compatibilities(bottle)
  local compatible_plugins = {}
  local incompatible_plugins = {}
  local installed_plugins = {}
  for i, repo in ipairs(repositories) do
    for j, plugin in ipairs(repo.plugins) do
      if plugin:is_installed(bottle) then
        table.insert(installed_plugins, plugin)
      end
    end
  end
  for plugin, v in pairs(self.dependencies) do
    local potential_plugins = { get_plugin(plugin, v.version, { mod_version = bottle.lite_xl.mod_version }) }
    local has_at_least_one = false
    local incomaptibilities = {}
    for i, potential_plugin in ipairs(potential_plugins) do
      for j, installed_plugin in ipairs(installed_plugins) do
        if installed_plugin:is_incompatible(potential_plugin) then
          table.insert(incomaptibilities, installed_plugin)
        end
      end
      if #incomaptibilities == 0 then
        if not compatible_plugins[plugin] or
          potential_plugin:is_installed(bottle) or
          (compare_version(compatible_plugins[plugin].version, potential_plugin.version) and not compatible_plugins[plugin]:is_installed(bottle))
        then
          compatible_plugins[plugin] = potential_plugin
        end
      else
        incompatible_plugins[plugin] = incompatibilities
      end
    end
  end
  return compatible_plugins, incompatible_plugins
end


local core_plugins = {
  autocomplete = true, autoreload = true, contextmenu = true, detectindent = true, drawwhitespace = true, language_c = true, language_cpp = true, language_css = true, language_html = true, language_js = true, language_lua = true, language_md = true, language_python = true, language_xml = true, lineguide = true, linewrapping = true, macro = true, projectsearch = true, quote = true, reflow = true, scale = true, tabularize = true, toolbarview = true, treeview = true, trimwhitespace = true, workspace = true
}

function Plugin:install(bottle, installing)
  if self:is_installed(bottle) then error("plugin " .. self.name .. " is already installed") end
  local install_path = self:get_install_path(bottle)
  local temporary_install_path = TMPDIR .. PATHSEP .. install_path:sub(#CACHEDIR)
  local status, err = pcall(function()
    installing = installing or {}
    installing[self.name] = true
    local compatible, incompatible = self:get_compatibilities(bottle)
    for plugin, version in pairs(self.dependencies) do
      if incompatible[plugin] then error("can't install " .. self.name .. ": incompatible with " .. incompatible[plugin][1].name .. ":" .. incompatible[plugin][1].version) end
    end
    for plugin, v in pairs(self.dependencies) do
      if not core_plugins[plugin] and not compatible[plugin] then error("can't find dependency " .. plugin .. (v.version and (":" .. v.version) or "")) end
    end
    for plugin, v in pairs(self.dependencies) do
      if not core_plugins[plugin] and not compatible[plugin]:is_installed(bottle) then
        if installing[plugin] then
          error("circular dependency detected in " .. self.name .. ": requires " .. plugin .. " but, " .. plugin .. " requires " .. self.name)
        end
        if not NO_INSTALL_OPTIONAL and (not v.optional or prompt(plugin .. " is an optional dependency of " .. self.name .. ". Should we install it?")) then
          compatible[plugin]:install(bottle, installing)
        end
      end
    end
    common.mkdirp(common.dirname(temporary_install_path))
    if self.status == "upgradable" then 
      log_action("Upgrading " .. self.organization .. "plugin located at " .. self.local_path .. " to " .. install_path)
      common.rmrf(install_path) 
    else
      log_action("Installing " .. self.organization .. " plugin located at " .. self.local_path .. " to " .. install_path)
    end

    if self.organization == "complex" and self.path and system.stat(self.local_path).type ~= "dir" then common.mkdirp(install_path) end  
    if self.url then
      log_action("Downloading file " .. self.url .. "...")
      local path = temporary_install_path .. (self.organization == 'complex' and self.path and system.stat(self.local_path).type ~= "dir" and (PATHSEP .. "init.lua") or "")
      common.get(self.url, path, self.checksum)
      log_action("Downloaded file " .. self.url .. " to " .. path)
      if system.hash(path, "file") ~= self.checksum then fatal_warning("checksum doesn't match for " .. path) end
    elseif self.remote then
      log_action("Cloning repository " .. self.remote .. " into " .. install_path)
      common.mkdirp(temporary_install_path)
      local _, _, url, branch = self.remote:find("^(.*):(.*)$")
      system.init(temporary_install_path, url)
      system.reset(temporary_install_path, branch)
    else
      local path = install_path .. (self.organization == 'complex' and self.path and system.stat(self.local_path).type ~= "dir" and (PATHSEP .. "init.lua") or "")
      local temporary_path = temporary_install_path .. (self.organization == 'complex' and self.path and system.stat(self.local_path).type ~= "dir" and (PATHSEP .. "init.lua") or "")
      log_action("Copying " .. self.local_path .. " to " .. path)
      common.copy(self.local_path, temporary_path)
    end
    for i,file in ipairs(self.files or {}) do
      if not file.arch or file.arch == ARCH then
        if not NO_INSTALL_OPTIONAL and (not file.optional or prompt(common.basename(file.url) .. " is an optional dependency of " .. self.name .. ". Should we install it?")) then
          if not file.checksum then error("requires a checksum") end
          local path = install_path .. PATHSEP .. (file.path or common.basename(file.url))
          local temporary_path = temporary_install_path .. PATHSEP .. (file.path or common.basename(file.url))
          log_action("Downloading file " .. file.url .. "...")
          common.get(file.url, temporary_path, file.checksum)
          log_action("Downloaded file " .. file.url .. " to " .. path)
          if system.hash(temporary_path, "file") ~= file.checksum then fatal_warning("checksum doesn't match for " .. path) end
        end
      end
    end
  end)
  if not status then
    common.rmrf(temporary_install_path)
    error(err)
  else
    common.rmrf(install_path)
    os.rename(temporary_install_path, install_path)
  end
end

function Plugin:depends_on(plugin)
  if self.dependencies[plugin.name] and self.dependencies[plugin.name].optional ~= true then return true end
  for i,v in ipairs(plugin.provides or {}) do if self.dependencies[v] and self.dependencies[v].optional ~= true then return true end end
  return false
end

function Plugin:uninstall(bottle)
  local install_path = self:get_install_path(bottle)
  log_action("Uninstalling plugin located at " .. install_path)
  local incompatible_plugins = common.grep(get_all_plugins(), function(p) return p:is_installed(bottle) and p:depends_on(self) end)
  if #incompatible_plugins == 0 or prompt(self.name .. " is depended upon by " .. common.join(", ", common.map(incompatible_plugins, function(p) return p.name end)) .. ". Remove as well?") then
    for i,plugin in ipairs(incompatible_plugins) do 
      if not plugin:uninstall(bottle) then return false end
    end
    common.rmrf(install_path)
    return true
  end
  return false
end


local Repository = {}
function Repository.__index(self, idx) return rawget(self, idx) or Repository[idx] end
function Repository.new(hash)
  if not hash.remote then error("requires a remote") end
  if not hash.remote:find("^https?:") and not hash.remote:find("^file:") then error("only repositories with http and file transports are supported") end
  local self = setmetatable({ 
    commit = hash.commit,
    remote = hash.remote,
    branch = hash.branch,
    plugins = nil,
    lite_xls = {},
    local_path = CACHEDIR .. PATHSEP .. "repos" .. PATHSEP .. system.hash(hash.remote),
    last_retrieval = nil 
  }, Repository)
  if system.stat(self.local_path) and not self.commit and not self.branch then
    -- In the case where we don't have a branch, and don't have a commit, check for the presence of `master` and `main`.
    if system.stat(self.local_path .. PATHSEP .. "master") then
      self.branch = "master"
    elseif system.stat(self.local_path .. PATHSEP .. PATHSEP .. "main") then
      self.branch = "main"
    else
      error("can't find branch for " .. self.remote)
    end
  end
  return self
end

function Repository.url(url)
  local e = url:reverse():find(":")
  local s = e and (#url - e + 1)
  local remote, branch_or_commit = url:sub(1, s and (s-1) or #url), s and url:sub(s+1)
  if remote == "https" or remote == "file" then remote, branch_or_commit = url, nil end
  if branch_or_commit and is_commit_hash(branch_or_commit) then
    return Repository.new({ remote = remote, commit = branch_or_commit })
  end
  return Repository.new({ remote = remote, branch = branch_or_commit })
end

function Repository:parse_manifest(already_pulling)
  if self.manifest then return self.manifest, self.remotes end
  if system.stat(self.local_path) and system.stat(self.local_path .. PATHSEP .. (self.commit or self.branch)) then
    self.manifest_path = self.local_path .. PATHSEP .. (self.commit or self.branch) .. PATHSEP .. "manifest.json"
    if not system.stat(self.manifest_path) then self:generate_manifest() end
    self.manifest = json.decode(io.open(self.manifest_path, "rb"):read("*all")) 
    self.plugins = {}
    self.remotes = {}
    for i, metadata in ipairs(self.manifest["plugins"] or {}) do
      if metadata.remote then
        local _, _, url, branch_or_commit = metadata.remote:find("^(.-):?(.*)?$")
        if branch_or_commit and is_commit_hash(branch_or_commit) then
          repo = Repository.new({ remote = url, commit = branch_or_commit })
          table.insert(remotes, repo)
          table.insert(self.plugins, Plugin.new(self, metadata))
        else
          -- log_warning("plugin " .. metadata.name .. " specifies remote as source, but isn't a commit")
        end
      else
        table.insert(self.plugins, Plugin.new(self, metadata))
      end
    end
    for i, metadata in ipairs(self.manifest["lite-xl"] or {}) do
      if metadata.remote then
        local _, _, url, branch_or_commit = metadata.remote:find("^(.-):?(.*)?$")
        if branch_or_commit and is_commit_hash(branch_or_commit) then
          repo = Repository.new({ remote = url, commit = branch_or_commit })
          table.insert(remotes, repo)
          table.insert(self.lite_xls, LiteXL.new(self, metadata))
        else
          -- log_warning("plugin " .. metadata.name .. " specifies remote as source, but isn't a commit")
        end
      else
        table.insert(self.lite_xls, LiteXL.new(self, metadata))
      end
    end
    for i, remote in ipairs(self.manifest["remotes"] or {}) do
      table.insert(self.remotes, Repository.url(remote))
    end
  end
  return self.manifest, self.remotes
end


-- in the cases where we don't have a manifest, assume generalized structure, take plugins folder, trawl through it, build manifest that way
-- assuming each .lua file under the `plugins` folder is a plugin. also parse the README, if present, and see if any of the plugins 
function Repository:generate_manifest()
  if not self.commit and not self.branch then error("requires an instantiation") end
  local path = self.local_path .. PATHSEP .. (self.commit or self.branch)
  local plugin_dir = system.stat(path .. PATHSEP .. "plugins") and PATHSEP .. "plugins" .. PATHSEP or PATHSEP
  local plugins, plugin_map = {}, {}
  if system.stat(path .. PATHSEP .. "README.md") then -- If there's a README, parse it for a table like in our primary repository.
    for line in io.lines(path .. PATHSEP .. "README.md") do
      local _, _, name, path, description = line:find("^%s*%|%s*%[`([%w_]+)%??.-`%]%((.-)%).-%|%s*(.-)%s*%|%s*$")
      if name then
        plugin_map[name] = { name = name, description = description, files = {} }
        if path:find("^http") then
          if path:find("%.lua") then
            plugin_map[name].url = path
            local file = common.get(path)
            plugin_map[name].checksum = system.hash(file)
          else
            plugin_map[name].remote = path
          end
        else
          plugin_map[name].path = path:gsub("%?.*$", "")
        end 
      end
    end
  end
  for i, file in ipairs(system.ls(path .. plugin_dir)) do
    if file:find("%.lua$") then
      local plugin = { description = nil, files = {}, name = common.basename(file):gsub("%.lua$", ""), dependencies = {}, mod_version = 3, version = "1.0", tags = {}, path = plugin_dir .. file  }
      for line in io.lines(path .. plugin_dir .. file) do
        local _, _, mod_version = line:find("%-%-.*mod%-version:%s*(%w+)")
        if mod_version then plugin.mod_version = mod_version end
        local _, _, required_plugin = line:find("require [\"']plugins.([%w_]+)")
        if required_plugin then if required_plugin ~= plugin.name then plugin.dependencies[required_plugin] = ">=1.0" end end
      end
      if plugin_map[plugin.name] then 
        plugin = common.merge(plugin, plugin_map[plugin.name])
        plugin_map[plugin.name].plugin = plugin 
      end
      table.insert(plugins, plugin)
    end
  end
  for k, v in pairs(plugin_map) do
    if not v.plugin then 
      table.insert(plugins, common.merge({ dependencies = {}, mod_version = self.branch == "master" and 2 or 3, version = "1.0", tags = {} }, v))
    end
  end
  io.open(path .. PATHSEP .. "manifest.json", "wb"):write(json.encode({ plugins = plugins })):flush()
end

function Repository:add()
  -- If neither specified then pull onto `master`, and check the main branch name, and move if necessary.
  if not self.branch and not self.commit then 
    local path = self.local_path .. PATHSEP .. "master"
    common.mkdirp(path)
    log_action("Retrieving " .. self.remote .. ":master/main...")
    system.init(path, self.remote)
    system.fetch(path)
    if not pcall(system.reset, path, "refs/remotes/origin/master", "hard") then
      if pcall(system.reset, path, "refs/remotes/origin/main", "hard") then
        os.rename(path, self.local_path .. PATHSEP .. "main")
        self.branch = "main"
      else
        error("can't find master or main.")
      end
    else
      self.branch = "master"
    end
    log_action("Retrieved " .. self.remote .. ":master/main.")
  else
    local path = self.local_path .. PATHSEP .. (self.commit or self.branch)
    common.mkdirp(path)
    log_action("Retrieving " .. self.remote .. ":master/main...")
    system.init(path, self.remote)
    system.fetch(path)
    system.reset(path, self.commit or ("refs/remotes/origin/" .. self.branch), "hard")
    log_action("Retrieved " .. self.remote .. ":" .. (self.commit or self.branch) .. "...")
    self.manifest = nil
  end
  local manifest, remotes = self:parse_manifest()
  if AUTO_PULL_REMOTES then -- any remotes we don't have in our listing, call add, and add into the list
    for i, remote in ipairs(remotes) do 
      local has = common.grep(repositories, function(repo) return repo.remote == remote.remote and repo.branch == remote.branch and repo.commit == remote.comit end) > 0
      if #has == 0 then
        remote:add() 
        table.insert(repositories, remote)
      end
    end
  end
  return self
end


function Repository:update()
  local manifest, remotes = self:parse_manifest()
  if self.branch then
    local path = self.local_path .. PATHSEP .. self.branch
    system.fetch(path)
    system.reset(path, "refs/remotes/origin/" .. self.branch, "hard")
    log_action("Updated " .. self.remote .. ":" .. (self.commit or self.branch))
    self.manifest = nil
    manifest, remotes = self:parse_manifest()
  end
  if AUTO_PULL_REMOTES then -- any remotes we don't have in our listing, call add, and add into the list
    for i, remote in ipairs(remotes) do 
      local has = #common.grep(repositories, function(repo) return repo.remote == remote.remote and repo.branch == remote.branch and repo.commit == remote.comit end)
      if #has == 0 then
        remote:add() 
        table.insert(repositories, remote)
      end
    end
  end
end


function Repository:remove()
  common.rmrf(self.local_path .. PATHSEP .. (self.commit or self.branch))
  if #system.ls(self.local_path) == 0 then common.rmrf(self.local_path) end
end


local LiteXL = {}
function LiteXL.__index(t, k) return LiteXL[k] end
function LiteXL.new(repository, metadata)
  if not metadata.version then error("lite-xl entry requires a version") end
  local self = setmetatable({
    repository = repository,
    version = metadata.version,
    remote = metadata.remote,
    tags = metadata.tags or {},
    mod_version = metadata.mod_version,
    path = metadata.path,
    hash = system.hash((repository and (repository.url .. ":" .. (repository.commit or repository.branch)) or "") .. "-" .. metadata.version),
    files = metadata.files or {}
  }, LiteXL)
  self.local_path = CACHEDIR .. PATHSEP .. "lite-xls" .. PATHSEP .. self.hash
  return self
end

function LiteXL:is_local()
  return not self.repository and self.path
end

function LiteXL:is_compatible(plugin)
  return compare_version(self.mod_version, plugin.mod_version) == 0
end

function LiteXL:is_installed() 
  return system.stat(self.local_path)
end

function LiteXL:install()
  if self:is_installed() then error("lite-xl " .. self.version .. " already installed") end
  common.mkdirp(self.local_path)
  if self.path and not self.repository then -- local repository
    system.symlink(self.local_path .. PATHSEP .. "lite_xl", self.path .. PATHSEP .. "lite_xl")
    system.symlink(self.local_path .. PATHSEP .. "data", self.path .. PATHSEP .. "data")
  else
    system.init(self.local_path, self.remote)
    system.reset(self.local_path, self.commit or self.branch)
    for i,file in ipairs(self.files or {}) do
      if file.arch and file.arch == ARCH then
        if not file.checksum then error("requires a checksum") end
        local path = self.local_path .. PATHSEP .. "lite-xl"
        log_action("Downloading file " .. file.url .. "...")
        common.get(file.url, path, file.checksum)
        log_action("Downloaded file " .. file.url .. " to " .. path)
        if system.hash(path, "file") ~= file.checksum then fatal_warning("checksum doesn't match for " .. path) end
      end
    end
  end
  if not system.stat(self.local_path .. PATHSEP .. "lite-xl") then error("can't find executable for lite-xl " .. self.version) end
end

function LiteXL:uninstall()
  if not system.stat(self.local_path) then error("lite-xl " .. self.version .. " not installed") end
  common.rmrf(self.local_path)
end


local Bottle = {}
function Bottle.__index(t, k) return Bottle[k] end
function Bottle.new(lite_xl, plugins, is_system)
  local self = setmetatable({
    lite_xl = lite_xl,
    plugins = plugins,
    is_system = is_system
  }, Bottle)
  if not is_system then 
    table.sort(self.plugins, function(a, b) return (a.name .. ":" .. a.version) < (b.name .. ":" .. b.version) end) end
    self.hash = system.hash(lite_xl.version .. " " .. common.join(" ", self.plugins))
    self.local_path = is_system and (CACHEDIR .. PATHSEP .. "bottles" .. PATHSEP .. self.hash)
  end
end

function Bottle:is_constructed() return system.stat(self.local_path) or self.is_system end

function Bottle:construct()
  if self.is_system then error("system bottle cannot be constructed") end
  if self:is_constructed() then error("bottle " .. self.hash .. " already constructed") end
  common.mkdirp(self.local_path)
  system.symlink(self.lite_xl.local_path .. PATHSEP .. "lite-xl", self.local_path .. PATHSEP .. "lite-xl")
  system.symlink(self.lite_xl.local_path .. PATHSEP .. "data", self.local_path .. PATHSEP .. "data")
  common.mkdirp(self.local_path .. PATHSEP .. "user")
  for i,plugin in ipairs(self.plugins) do plugin:install(self) end
end

function Bottle:destruct()
  if self.is_system then error("system bottle cannot be destructed") end
  if not self:is_constructed() then error("lite-xl " .. self.version .. " not constructed") end
  common.rmrf(self.local_path)
end

function Bottle:run(ARGS)
  if self.is_system then error("system bottle cannot be run") end
  if os.system(self.local_path, table.unpack(ARGS)) then error("can't execute " .. self.local_path) end
end


local function get_repository(url)
  if not url then error("requires a repository url") end
  local r = Repository.url(url)
  for i,v in ipairs(repositories) do
    if v.remote == r.remote and v.branch == r.branch and v.commit == r.commit then return i, v end
  end
  return nil
end



local function lpm_repo_init()
  if not system.stat(CACHEDIR) then
    for i, repository in ipairs({ Repository.url("https://github.com/lite-xl/lite-xl-plugins.git:master"), Repository.url("https://github.com/lite-xl/lite-xl-plugins.git:2.1") }) do
      if not system.stat(repository.local_path) or not system.stat(repository.local_path .. PATHSEP .. (repository.commit or repository.branch)) then 
        common.mkdirp(repository.local_path)
        table.insert(repositories, repository:add())
      end
    end
  end
end


local function lpm_repo_add(...)
  for i, url in ipairs({ ... }) do
    local idx, repo = get_repository(url)
    if repo then -- if we're alreayd a repo, put this at the head of the resolution list
      table.remove(repositories, idx)
    else
      repo = Repository.url(url):add()
    end
    table.insert(repositories, 1, repo)
    repo:update()
  end
end


local function lpm_repo_rm(...)
  for i, url in ipairs({ ... }) do
    local idx, repo = get_repository(url)
    if not repo then error("cannot find repository " .. url) end
    table.remove(repositories, idx)
    repo:remove()
  end
end


local function lpm_repo_update(...)
  local t = { ... }
  if #t == 0 then table.insert(t, false) end
  for i, url in ipairs(t) do
    local repo = url and get_repository(url)
    for i,v in ipairs(repositories) do 
      if not repo or v == repo then 
        v:update() 
      end 
    end
  end
end

local function get_lite_xl(version)
  for i,lite_xl in ipairs(lite_xls) do
    if lite_xl.version == version then return lite_xl end
  end
  for i,repo in ipairs(repositories) do
    for j,lite_xl in ipairs(repo.lite_xls) do
      if lite_xl.version == version then return lite_xl end
    end
  end
  return nil
end

local function lpm_lite_xl_save()
  io.open(CACHEDIR .. PATHSEP .. "lite_xls" .. PATHSEP .. "locals.json", "wb"):write(
    json.encode(common.map(common.grep(lite_xls, function(l) l:is_local() end), function(l) return { version = l.version, mod_version = l.mod_version, path = l.path } end))
  )
end

local function lpm_lite_xl_add(version, path)
  if not version then error("requires a version") end
  if not path then error("requires a path") end
  if not system.stat(path .. PATHSEP .. "lite-xl") then error("can't find " .. path .. PATHSEP .. "lite-xl") end
  if not system.stat(path .. PATHSEP .. "data") then error("can't find " .. path .. PATHSEP .. "data") end
  table.insert(lite_xls, LiteXL.new({ version = version, path = path, modversion = MOD_VERSION or 3 }))
  lpm_lite_xl_save()
end

local function lpm_lite_xl_rm(version)
  if not version then error("requires a version") end
  local lite_xl = get_lite_xl(version) or error("can't find lite_xl version " .. version)
  lite_xls = common.grep(lite_xls, function(l) return l ~= lite_xl end)
  lpm_lite_xl_save()
end

local function lpm_lite_xl_install(version)
  if not version then error("requires a version") end
  (get_lite_xl(version) or error("can't find lite-xl version " .. version)):install()
end


local function lpm_lite_xl_switch(version, target)
  if not version then error("requires a version") end
  target = target or get_executable("lite-xl")
  if not target then error("can't find installed lite-xl. please provide a target to install the symlink explicitly") end
  local lite_xl = get_lite_xl(version) or error("can't find lite-xl version " .. version)
  system.symlink(lite_xl.local_path .. PATHSEP .. "lite-xl", target)
end


local function lpm_lite_xl_uninstall(version)
  (get_lite_xl(version) or error("can't find lite-xl version " .. version)):uninstall()
end


local function lpm_lite_xl_list()
  local result = { ["lite-xl"] = { } }
  for i,repo in ipairs(repositories) do
    if not repo.lite-xls then error("can't find lite-xl for repo " .. repo.remote .. ":" .. (repo.commit or repo.branch or "master")) end
    for j, lite_xl in ipairs(repo.lite_xls) do
      table.insert(result["lite-xl"], {
        version = lite_xl.version,
        mod_version = lite_xl.mod_version,
        repository = repo.remote .. ":" .. (repo.commit or repo.branch)
      })
    end
  end
  if JSON then
    io.stdout:write(json.encode(result) .. "\n")
  else
    for i, plugin in ipairs(result.plugins) do
      if i ~= 0 then print("---------------------------") end
      print("Version:       " .. plugin.version)
      print("Mod-Version:   " .. (plugin.mod_version or "unknown"))
      print("Tags:          " .. common.join(", ", plugin.tags))
    end
  end
end

local function lpm_lite_xl_run(version, ...)
  if not version then error("requires a version") end
  local lite_xl = get_lite_xl(version) or error("can't find lite-xl version " .. version)
  local plugins = {}
  for i, str in ipairs({ ... }) do
    local name, version = common.split(":", str)
    local plugin = get_plugin(name, version, { mod_version = lite_xl.mod_version })
    table.insert(plugins, plugin)
  end
  local bottle = Bottle.new(lite_xl, plugins)
  if not bottle:is_constructed() then bottle:construct() end
  bottle:run()
end


local function lpm_install(...)
  for i, identifier in ipairs({ ... }) do
    local s = identifier:find(":")
    local name, version = (s and identifier:sub(1, s-1) or identifier), (s and identifier:sub(s+1) or nil)
    if not name then error('unrecognized identifier ' .. identifier) end
    if name == "lite-xl" then
      lpm_install_lite_xl(version)
    else
      local plugin = get_plugin(name, version, { mod_version = MOD_VERSION })
      if not plugin then error("can't find plugin " .. name .. " mod-version: " .. (MOD_VERSION or 'any')) end
      plugin:install(system_bottle)
    end
  end
end


local function lpm_plugin_uninstall(...)
  for i, name in ipairs({ ... }) do
    local plugins = { get_plugin(name) }
    if #plugins == 0 then error("can't find plugin " .. name) end
    local installed_plugins = common.grep(plugins, function(plugin) return plugin:is_installed() end)
    if #installed_plugins == 0 then error("plugin " .. name .. " not installed") end
    for i, plugin in ipairs(installed_plugins) do plugin:uninstall() end
  end
end


local function lpm_repo_list() 
  if JSON then
    io.stdout:write(json.encode({ repositories = common.map(repositories, function(repo) return { remote = repo.remote, commit = repo.commit, branch = repo.branch, path = repo.local_path .. PATHSEP .. (repo.commit or repo.branch), remotes = common.map(repo.remotes or {}, function(r) return remote.remote .. ":" .. (remote.commit or remote.branch) end)  } end) }) .. "\n")
  else
    for i, repository in ipairs(repositories) do
      local _, remotes = repository:parse_manifest()
      if i ~= 0 then print("---------------------------") end
      print("Remote :  " .. repository.remote .. ":" .. (repository.commit or repository.branch))
      print("Path   :  " .. repository.local_path .. PATHSEP .. (repository.commit or repository.branch))
      print("Remotes:  " .. json.encode(common.map(repository.remotes or {}, function(r) return remote.remote .. ":" .. (remote.commit or remote.branch) end)))
    end
  end
end

local function lpm_plugin_list() 
  local result = { plugins = { } }
  for i,repo in ipairs(repositories) do
    if not repo.plugins then error("can't find plugins for repo " .. repo.remote .. ":" .. (repo.commit or repo.branch or "master")) end
    for j,plugin in ipairs(repo.plugins) do
      table.insert(result.plugins, {
        name = plugin.name,
        status = plugin:is_installed() and "installed" or (system_bottle.lite_xl:is_compatible(plugin) and "available" or "incompatible"),
        version = "" .. plugin.version,
        dependencies = plugin.dependencies,
        description = plugin.description,
        mod_version = plugin.mod_version,
        tags = plugin.tags,
        type = plugin.type,
        organization = plugin.organization,
        repository = repo.remote .. ":" .. (repo.commit or repo.branch)
      })
    end
  end
  if JSON then
    io.stdout:write(json.encode(result) .. "\n")
  else
    for i, plugin in ipairs(result.plugins) do
      if i ~= 0 then print("---------------------------") end
      print("Name:          " .. plugin.name)
      print("Version:       " .. plugin.version)
      print("Status:        " .. plugin.status)
      print("Type:          " .. plugin.type)
      print("Orgnization:   " .. plugin.organization)
      print("Repository:    " .. plugin.repository)
      print("Description:   " .. (plugin.description or ""))
      print("Mod-Version:   " .. (plugin.mod_version or "unknown"))
      print("Dependencies:  " .. json.encode(plugin.dependencies))
      print("Tags:          " .. common.join(", ", plugin.tags))
    end
  end
end

local function lpm_plugin_upgrade()
  for i,repo in ipairs(repositories) do
    if not repo.plugins then error("can't find plugins for repo " .. repo.remote .. ":" .. (repo.commit or repo.branch or "master")) end
    for j,plugin in ipairs(repo.plugins) do
      if plugin:is_installed() then
        local compatibles = get_plugin(plugin.name, ">" .. plugin.version)
        table.sort(compatibles, function(a, b) return compare_version(b.version, a.version) end)
        compatibles[1]:install()
      end
    end
  end
end

local function lpm_purge()
  log_action("Removed " .. CACHEDIR .. ".")
  common.rmrf(CACHEDIR)
end

local function parse_arguments(arguments, options)
  local args = {}
  local i = 1
  while i <= #arguments do
    local s,e, option, value = arguments[i]:find("%-%-([^=]+)=?(.*)")
    if s then
      local flag_type = options[option]
      if not flag_type then error("unknown flag --" .. option) end
      if flag_type == "flag" then
        args[option] = true
      elseif flag_type == "string" or flag_type == "number" then
        if not value then
          if i < #arguments then error("option " .. option .. " requires a " .. flag_type) end
          value = arguments[i+1]
          i = i + 1
        end
        if flag_type == "number" and tonumber(flag_type) == nil then error("option " .. option .. " should be a number") end
        args[option] = value
      end
    else
      table.insert(args, arguments[i])
    end
    i = i + 1
  end
  return args
end

local status = 0
local function error_handler(err)
  local s, e = err:find(":%d+")
  local message = e and err:sub(e + 3) or err
  if JSON then
    if VERBOSE then 
      io.stderr:write(json.encode({ error = err, actions = actions, warnings = warnings, traceback = debug.traceback() }) .. "\n")
    else
      io.stderr:write(json.encode({ error = message or err, actions = actions, warnings = warnings }) .. "\n")
    end
  else
    io.stderr:write((not VERBOSE and message or err) .. "\n")
    if VERBOSE then io.stderr:write(debug.traceback() .. "\n") end
  end
  status = -1
end

local function run_command(ARGS)
  if not ARGS[2]:find("%S") then return end
  if ARGS[2] == "repo" and ARGV[3] == "add" then lpm_repo_add(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "repo" and ARGS[3] == "rm" then lpm_repo_rm(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "add" then lpm_repo_add(table.unpack(common.slice(ARGS, 3)))
  elseif ARGS[2] == "rm" then lpm_repo_rm(table.unpack(common.slice(ARGS, 3)))
  elseif ARGS[2] == "update" then lpm_repo_update(table.unpack(common.slice(ARGS, 3)))
  elseif ARGS[2] == "repo" and ARGS[3] == "update" then lpm_repo_update(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "repo" and ARGS[3] == "list" then return lpm_repo_list()
  elseif ARGS[2] == "plugin" and ARGS[3] == "install" then lpm_install(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "plugin" and ARGS[3] == "uninstall" then lpm_plugin_uninstall(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "plugin" and ARGS[3] == "list" then return lpm_plugin_list(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "plugin" and ARGS[3] == "upgrade" then return lpm_plugin_upgrade(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "upgrade" then return lpm_plugin_upgrade(table.unpack(common.slice(ARGS, 3)))
  elseif ARGS[2] == "install" then lpm_install(table.unpack(common.slice(ARGS, 3)))
  elseif ARGS[2] == "uninstall" then lpm_plugin_uninstall(table.unpack(common.slice(ARGS, 3)))
  elseif ARGS[2] == "list" then return lpm_plugin_list(table.unpack(common.slice(ARGS, 3)))
  elseif ARGS[2] == "lite-xl" and ARGS[3] == "list" then return lpm_lite_xl_list(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "lite-xl" and ARGS[3] == "uninstall" then return lpm_lite_xl_uninstall(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "lite-xl" and ARGS[3] == "install" then return lpm_install(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "lite-xl" and ARGS[3] == "switch" then return lpm_lite_xl_switch(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "lite-xl" and ARGS[3] == "run" then return lpm_lite_xl_run(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "lite-xl" and ARGS[3] == "add" then return lpm_lite_xl_add(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "lite-xl" and ARGS[3] == "rm" then return lpm_lite_xl_rm(table.unpack(common.slice(ARGS, 4)))
  elseif ARGS[2] == "run" then return lpm_lite_xl_run(table.unpack(common.slice(ARGS, 3)))
  elseif ARGS[2] == "switch" then return lpm_lite_xl_switch(table.unpack(common.slice(ARGS, 3)))
  elseif ARGS[2] == "purge" then lpm_purge()
  else error("unknown command: " .. ARGS[2]) end
  if JSON then
    io.stdout:write(json.encode({ actions = actions, warnings = warnings }))
  end
end


xpcall(function()
  local ARGS = parse_arguments(ARGV, { 
    json = "flag", userdir = "string", cachedir = "string", version = "flag", verbose = "flag", 
    quiet = "flag", version = "string", ["mod-version"] = "string", remotes = "flag", help = "flag",
    remotes = "flag", ssl_certs = "string", force = "flag", arch = "string", ["assume-yes"] = "flag",
    ["install-optional"] = "flag"
  })
  if ARGS["version"] then
    io.stdout:write(VERSION .. "\n")
    return 0
  end
  if ARGS["help"] or #ARGS == 1 or ARGS[2] == "help" then
    io.stderr:write([[
Usage: lpm COMMAND [...ARGUMENTS] [--json] [--userdir=directory] 
  [--cachedir=directory] [--quiet] [--version] [--help] [--remotes]
  [--ssl_certs=directory/file] [--force] [--arch=]] .. _G.ARCH .. [[]
  [--assume-yes] [--no-install-optional] [--verbose] [--mod-version=3]

LPM is a package manager for `lite-xl`, written in C (and packed-in lua).

It's designed to install packages from our central github repository (and
affiliated repositories), directly into your lite-xl user directory. It can
be called independently, for from the lite-xl `plugin_manager` plugin.

LPM will always use https://github.com/lite-xl/lite-xl-plugins as its base
repository, if none are present, and the cache directory does't exist,
but others can be added, and this base one can be removed.

It has the following commands:

  lpm repo list                            List all extant repos.
  lpm [repo] add <repository remote>       Add a source repository.
    [...<repository remote>] 
  lpm [repo] rm <repository remote>        Remove a source repository.
    [...<repository remote>]
  lpm [repo] update [<repository remote>]  Update all/the specified repos.
    [...<repository remote>]        
  lpm [plugin] install                     Install specific plugins.
    <plugin name>[:<version>]              If installed, upgrades.
    [...<plugin name>:<version>]                     
  lpm [plugin] uninstall <plugin name>     Uninstall the specific plugin.
    [...<plugin name>]
  lpm [plugin] list <repository remote>    List all/associated plugins.
    [...<repository remote>]    
  lpm [plugin] upgrade                     Upgrades all installed plugins 
                                           to new version if applicable.
  lpm [lite-xl] install <version>          Installs lite-xl. Infers the
    [binary] [datadir]                     paths on your system if not
                                           supplied. Automatically
                                           switches to be your system default
                                           if path auto inferred.
  lpm lite-xl add <version> <path>         Adds a local version of lite-xl to
                                           the managed list, allowing it to be
                                           easily bottled.
  lpm lite-xl remove <path>                Removes a local version of lite-xl
                                           from the managed list.
  lpm [lite-xl] switch <version> [<path>]  Sets the active version of lite-xl
                                           to be the specified version. Auto-detects
                                           current install of lite-xl; if none found
                                           path can be specifeid.
  lpm [lite-xl] list                       Lists all installed versions of
                                           lite-xl.
  lpm run <version> [...plugins]           Sets up a "bottle" to run the specified
                                           lite version, with the specified plugins
                                           and then opens it.
          
  lpm purge                                Completely purge all state for LPM.
  lpm -                                    Read these commands from stdin in
                                           an interactive print-eval loop.
  lpm help                                 Displays this help text.

Flags have the following effects:

  --json                   Performs all communication in JSON.
  --userdir=directory      Sets the lite-xl userdir manually.
                           If omitted, uses the normal lite-xl logic.
  --cachedir=directory     Sets the directory to store all repositories.
  --tmpdir=directory       During install, sets the staging area.
  --verbose                Spits out more information, including intermediate
                           steps to install and whatnot.
  --quiet                  Outputs nothing but explicit responses.
  --modversion             Sets the mod version of lite-xl to install plugins.
  --version                Returns version information.
  --remotes                Automatically adds any specified remotes in the 
                           repository to the end of the resolution list.
                           This is a potential security risk, so be careful.
  --help                   Displays this help text.
  --ssl_certs              Sets the SSL certificate store.
  --arch                   Sets the architecture (default: ]] .. _G.ARCH .. [[).
  --force                  Ignores checksum inconsitencies. 
                           Not recommended; security risk.
  --assume-yes             Ignores any prompts, and automatically answers yes
                           to all.
  --no-install-optional    On install, anything marked as optional
                           won't prompt.
]]
    )
    return 0
  end
  
  VERBOSE = ARGS["verbose"] or false
  JSON = ARGS["json"] or os.getenv("LPM_JSON")
  QUIET = ARGS["quiet"] or os.getenv("LPM_QUIET")
  FORCE = ARGS["force"]
  NO_INSTALL_OPTIONAL = ARGS["no-install-optional"]
  ARCH = ARGS["arch"] or _G.ARCH
  ASSUME_YES = ARGS["assume-yes"] or FORCE
  MOD_VERSION = ARGS["mod-version"] or os.getenv("LPM_MODVERSION")
  if MOD_VERSION == "any" then MOD_VERSION = nil end
  HOME = (os.getenv("USERPROFILE") or os.getenv("HOME")):gsub(PATHSEP .. "$", "")
  USERDIR = ARGS["userdir"] or os.getenv("LITE_USERDIR") or (os.getenv("XDG_CONFIG_HOME") and os.getenv("XDG_CONFIG_HOME") .. PATHSEP .. "lite-xl")
    or (HOME and (HOME .. PATHSEP .. '.config' .. PATHSEP .. 'lite-xl'))
  AUTO_PULL_REMOTES = ARGS["remotes"]
  if not system.stat(USERDIR) then error("can't find user directory " .. USERDIR) end
  CACHEDIR = ARGS["cachedir"] or os.getenv("LPM_CACHE") or USERDIR .. PATHSEP .. "lpm"
  TMPDIR = ARGS["tmpdir"] or CACHEDIR .. "/tmp"

  if ARGS[2] == "purge" then return lpm_purge() end
  if ARGS["ssl_certs"] then 
    local stat = system.stat(ARGS["ssl_certs"])
    if not stat then error("can't find " .. ARGS["ssl_certs"]) end
    system.certs(stat.type, ARGS["ssl_certs"])
  elseif not os.getenv("SSL_CERT_DIR") and not os.getenv("SSL_CERT_FILE") then
    local paths = { -- https://serverfault.com/questions/62496/ssl-certificate-location-on-unix-linux#comment1155804_62500
      "/etc/ssl/certs/ca-certificates.crt",                -- Debian/Ubuntu/Gentoo etc.
      "/etc/pki/tls/certs/ca-bundle.crt",                  -- Fedora/RHEL 6
      "/etc/ssl/ca-bundle.pem",                            -- OpenSUSE
      "/etc/pki/tls/cacert.pem",                           -- OpenELEC
      "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", -- CentOS/RHEL 7
      "/etc/ssl/cert.pem",                                 -- Alpine Linux
      "/etc/ssl/certs",                                    -- SLES10/SLES11, https://golang.org/issue/12139
      "/system/etc/security/cacerts",                      -- Android
      "/usr/local/share/certs",                            -- FreeBSD
      "/etc/pki/tls/certs",                                -- Fedora/RHEL
      "/etc/openssl/certs",                                -- NetBSD
      "/var/ssl/certs",                                    -- AIX
    }
    for i, path in ipairs(paths) do
      local stat = system.stat(path) 
      if stat then 
        system.certs(stat.type, path) 
        break 
      end
    end
  end


  -- Base setup; initialize default repos if applicable, read them in. Determine Lite XL system binary if not specified, and pull in a list of all local lite-xl's.
  repositories = {}
  lpm_repo_init()
  repositories = {}
  for i, remote_hash in ipairs(system.ls(CACHEDIR .. PATHSEP .. "repos")) do
    local remote
    for j, commit_or_branch in ipairs(system.ls(CACHEDIR .. PATHSEP .. "repos" .. PATHSEP .. remote_hash)) do
      if system.stat(CACHEDIR .. PATHSEP .. "repos" .. PATHSEP .. remote_hash .. PATHSEP  .. commit_or_branch .. PATHSEP .. ".git" .. PATHSEP .."config") then
        for line in io.lines(CACHEDIR .. PATHSEP .. "repos" .. PATHSEP .. remote_hash .. PATHSEP  .. commit_or_branch .. PATHSEP .. ".git" .. PATHSEP .."config") do
          local s,e = line:find("url = ") 
          if s then remote = line:sub(e+1) break end
        end
        if remote then
          table.insert(repositories, Repository.url(remote .. ":" .. commit_or_branch))
          repositories[#repositories]:parse_manifest()
        end
      end
    end
  end

  if not MOD_VERSION then
    local lite_xl_binary = get_executable("lite-xl")
    if lite_xl_binary then
      local hash = system.hash(lite_xl_binary, "file")
      for i,repo in ipairs(repositories) do
        for j,lite_xl in ipairs(repo.lite_xls) do
          local f = table.unpack(common.grep(lite_xl.files, function(f) return f.checksum == hash end))
          if f then system_bottle = Bottle.new(lite_xl, nil, true) end
        end
      end
    end
  end
  if not system_bottle then system_bottle = Bottle.new(LiteXL.new(nil, { mod_version = MOD_VERSION or 3, version = "system" }), nil, true) end
  if system.stat(CACHDIR .. PATHSEP .. "lite_xls" .. PATHSEP .. "locals.json") then
    lite_xls = { system_bottle.lite_xl }
    for i, lite_xl in ipairs(json.decode(io.open(CACHDIR .. PATHSEP .. "lite_xls" .. PATHSEP .. "locals.json", "rb"):read("*all"))) do
      table.insert(lite_xls, LiteXL.new(nil, { version = lite_xl.version, mod_version = lite_xl.mod_version, path = lite_xl.path }))
    end
  end
  
  if ARGS[2] ~= '-' then
    run_command(ARGS)
  else
    while true do
      local line = io.stdin:read("*line")
      if line == "quit" or line == "exit" then return 0 end
      local args = { ARGS[1] }
      local s = 1
      while true do
        local a,e = line:find("%s+", s)
        table.insert(args, line:sub(s, a and (a - 1) or #line))
        if not e then break end
        s = e + 1
      end
      xpcall(function()
        run_command(args)
      end, error_handler)
      actions, warnings = {}, {}
    end
  end

end, error_handler)


return status