summaryrefslogtreecommitdiffstats
path: root/packaging/installer/install-required-packages.sh
blob: 23cbe12d5e8a3ed60ecf2ce729c6ec05ffb41205 (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
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
#!/usr/bin/env bash
# shellcheck disable=SC2034
# We use lots of computed variable names in here, so we need to disable shellcheck 2034

export PATH="${PATH}:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
export LC_ALL=C

# Be nice on production environments
renice 19 $$ > /dev/null 2> /dev/null

ME="${0}"

if [ "${BASH_VERSINFO[0]}" -lt "4" ]; then
  echo >&2 "Sorry! This script needs BASH version 4+, but you have BASH version ${BASH_VERSION}"
  exit 1
fi

# These options control which packages we are going to install
# They can be pre-set, but also can be controlled with command line options
PACKAGES_NETDATA=${PACKAGES_NETDATA-0}
PACKAGES_NETDATA_NODEJS=${PACKAGES_NETDATA_NODEJS-0}
PACKAGES_NETDATA_PYTHON=${PACKAGES_NETDATA_PYTHON-0}
PACKAGES_NETDATA_PYTHON3=${PACKAGES_NETDATA_PYTHON3-0}
PACKAGES_NETDATA_PYTHON_MYSQL=${PACKAGES_NETDATA_PYTHON_MYSQL-0}
PACKAGES_NETDATA_PYTHON_POSTGRES=${PACKAGES_NETDATA_PYTHON_POSTGRES-0}
PACKAGES_NETDATA_PYTHON_MONGO=${PACKAGES_NETDATA_PYTHON_MONGO-0}
PACKAGES_DEBUG=${PACKAGES_DEBUG-0}
PACKAGES_IPRANGE=${PACKAGES_IPRANGE-0}
PACKAGES_FIREHOL=${PACKAGES_FIREHOL-0}
PACKAGES_FIREQOS=${PACKAGES_FIREQOS-0}
PACKAGES_UPDATE_IPSETS=${PACKAGES_UPDATE_IPSETS-0}
PACKAGES_NETDATA_DEMO_SITE=${PACKAGES_NETDATA_DEMO_SITE-0}
PACKAGES_NETDATA_SENSORS=${PACKAGES_NETDATA_SENSORS-0}
PACKAGES_NETDATA_DATABASE=${PACKAGES_NETDATA_DATABASE-0}
PACKAGES_NETDATA_EBPF=${PACKAGES_NETDATA_EBPF-0}

# needed commands
lsb_release=$(command -v lsb_release 2> /dev/null)

# Check which package managers are available
apk=$(command -v apk 2> /dev/null)
apt_get=$(command -v apt-get 2> /dev/null)
brew=$(command -v brew 2> /dev/null)
pkg=$(command -v pkg 2> /dev/null)
dnf=$(command -v dnf 2> /dev/null)
emerge=$(command -v emerge 2> /dev/null)
equo=$(command -v equo 2> /dev/null)
pacman=$(command -v pacman 2> /dev/null)
swupd=$(command -v swupd 2> /dev/null)
yum=$(command -v yum 2> /dev/null)
zypper=$(command -v zypper 2> /dev/null)

distribution=
release=
version=
codename=
package_installer=
tree=
detection=
NAME=
ID=
ID_LIKE=
VERSION=
VERSION_ID=

usage() {
  cat << EOF
OPTIONS:

${ME} [--dont-wait] [--non-interactive] \\
  [distribution DD [version VV] [codename CN]] [installer IN] [packages]

Supported distributions (DD):

    - arch           (all Arch Linux derivatives)
    - centos         (all CentOS derivatives)
    - gentoo         (all Gentoo Linux derivatives)
    - sabayon        (all Sabayon Linux derivatives)
    - debian, ubuntu (all Debian and Ubuntu derivatives)
    - redhat, fedora (all Red Hat and Fedora derivatives)
    - suse, opensuse (all SUSE and openSUSE derivatives)
    - clearlinux     (all Clear Linux derivatives)
    - macos          (Apple's macOS)

Supported installers (IN):

    - apt-get        all Debian / Ubuntu Linux derivatives
    - dnf            newer Red Hat / Fedora Linux
    - emerge         all Gentoo Linux derivatives
    - equo           all Sabayon Linux derivatives
    - pacman         all Arch Linux derivatives
    - yum            all Red Hat / Fedora / CentOS Linux derivatives
    - zypper         all SUSE Linux derivatives
    - apk            all Alpine derivatives
    - swupd          all Clear Linux derivatives
    - brew           macOS Homebrew
    - pkg            FreeBSD Ports

Supported packages (you can append many of them):

    - netdata-all    all packages required to install netdata
                     including mysql client, postgres client,
                     node.js, python, sensors, etc

    - netdata        minimum packages required to install netdata
                     (no mysql client, no nodejs, includes python)

    - nodejs         install nodejs
                     (required for monitoring named and SNMP)

    - python         install python

    - python3        install python3

    - python-mysql   install MySQLdb
                     (for monitoring mysql, will install python3 version
                     if python3 is enabled or detected)

    - python-postgres install psycopg2
                     (for monitoring postgres, will install python3 version
                     if python3 is enabled or detected)

    - python-pymongo install python-pymongo (or python3-pymongo for python3)

    - sensors        install lm_sensors for monitoring h/w sensors

    - firehol-all    packages required for FireHOL, FireQoS, update-ipsets
    - firehol        packages required for FireHOL
    - fireqos        packages required for FireQoS
    - update-ipsets  packages required for update-ipsets

    - demo           packages required for running a netdata demo site
                     (includes nginx and various debugging tools)


If you don't supply the --dont-wait option, the program
will ask you before touching your system.

EOF
}

release2lsb_release() {
  # loads the given /etc/x-release file
  # this file is normaly a single line containing something like
  #
  # X Linux release 1.2.3 (release-name)
  #
  # It attempts to parse it
  # If it succeeds, it returns 0
  # otherwise it returns 1

  local file="${1}" x DISTRIB_ID="" DISTRIB_RELEASE="" DISTRIB_CODENAME=""
  echo >&2 "Loading ${file} ..."

  x="$(grep -v "^$" "${file}" | head -n 1)"

  if [[ "${x}" =~ ^.*[[:space:]]+Linux[[:space:]]+release[[:space:]]+.*[[:space:]]+(.*)[[:space:]]*$ ]]; then
    eval "$(echo "${x}" | sed "s|^\(.*\)[[:space:]]\+Linux[[:space:]]\+release[[:space:]]\+\(.*\)[[:space:]]\+(\(.*\))[[:space:]]*$|DISTRIB_ID=\"\1\"\nDISTRIB_RELEASE=\"\2\"\nDISTRIB_CODENAME=\"\3\"|g" | grep "^DISTRIB")"
  elif [[ "${x}" =~ ^.*[[:space:]]+Linux[[:space:]]+release[[:space:]]+.*[[:space:]]+$ ]]; then
    eval "$(echo "${x}" | sed "s|^\(.*\)[[:space:]]\+Linux[[:space:]]\+release[[:space:]]\+\(.*\)[[:space:]]*$|DISTRIB_ID=\"\1\"\nDISTRIB_RELEASE=\"\2\"|g" | grep "^DISTRIB")"
  elif [[ "${x}" =~ ^.*[[:space:]]+release[[:space:]]+.*[[:space:]]+(.*)[[:space:]]*$ ]]; then
    eval "$(echo "${x}" | sed "s|^\(.*\)[[:space:]]\+release[[:space:]]\+\(.*\)[[:space:]]\+(\(.*\))[[:space:]]*$|DISTRIB_ID=\"\1\"\nDISTRIB_RELEASE=\"\2\"\nDISTRIB_CODENAME=\"\3\"|g" | grep "^DISTRIB")"
  elif [[ "${x}" =~ ^.*[[:space:]]+release[[:space:]]+.*[[:space:]]+$ ]]; then
    eval "$(echo "${x}" | sed "s|^\(.*\)[[:space:]]\+release[[:space:]]\+\(.*\)[[:space:]]*$|DISTRIB_ID=\"\1\"\nDISTRIB_RELEASE=\"\2\"|g" | grep "^DISTRIB")"
  fi

  distribution="${DISTRIB_ID}"
  version="${DISTRIB_RELEASE}"
  codename="${DISTRIB_CODENAME}"

  [ -z "${distribution}" ] && echo >&2 "Cannot parse this lsb-release: ${x}" && return 1
  detection="${file}"
  return 0
}

get_os_release() {
  # Loads the /etc/os-release or /usr/lib/os-release file(s)
  # Only the required fields are loaded
  #
  # If it manages to load a valid os-release, it returns 0
  # otherwise it returns 1
  #
  # It searches the ID_LIKE field for a compatible distribution

  os_release_file=
  if [ -s "/etc/os-release" ]; then
    os_release_file="/etc/os-release"
  elif [ -s "/usr/lib/os-release" ]; then
    os_release_file="/usr/lib/os-release"
  else
    echo >&2 "Cannot find an os-release file ..."
    return 1
  fi

  local x
  echo >&2 "Loading ${os_release_file} ..."

  eval "$(grep -E "^(NAME|ID|ID_LIKE|VERSION|VERSION_ID)=" "${os_release_file}")"
  for x in "${ID}" ${ID_LIKE}; do
    case "${x,,}" in
      alpine | arch | centos | clear-linux-os | debian | fedora | gentoo | manjaro | opensuse-leap | rhel | sabayon | sles | suse | ubuntu)
        distribution="${x}"
        version="${VERSION_ID}"
        codename="${VERSION}"
        detection="${os_release_file}"
        break
        ;;
      *)
        echo >&2 "Unknown distribution ID: ${x}"
        ;;
    esac
  done
  [ -z "${distribution}" ] && echo >&2 "Cannot find valid distribution in: ${ID} ${ID_LIKE}" && return 1

  [ -z "${distribution}" ] && return 1
  return 0
}

get_lsb_release() {
  # Loads the /etc/lsb-release file
  # If it fails, it attempts to run the command: lsb_release -a
  # and parse its output
  #
  # If it manages to find the lsb-release, it returns 0
  # otherwise it returns 1

  if [ -f "/etc/lsb-release" ]; then
    echo >&2 "Loading /etc/lsb-release ..."
    local DISTRIB_ID="" DISTRIB_RELEASE="" DISTRIB_CODENAME=""
    eval "$(grep -E "^(DISTRIB_ID|DISTRIB_RELEASE|DISTRIB_CODENAME)=" /etc/lsb-release)"
    distribution="${DISTRIB_ID}"
    version="${DISTRIB_RELEASE}"
    codename="${DISTRIB_CODENAME}"
    detection="/etc/lsb-release"
  fi

  if [ -z "${distribution}" ] && [ -n "${lsb_release}" ]; then
    echo >&2 "Cannot find distribution with /etc/lsb-release"
    echo >&2 "Running command: lsb_release ..."
    eval "declare -A release=( $(lsb_release -a 2> /dev/null | sed -e "s|^\(.*\):[[:space:]]*\(.*\)$|[\1]=\"\2\"|g") )"
    distribution="${release["Distributor ID"]}"
    version="${release[Release]}"
    codename="${release[Codename]}"
    detection="lsb_release"
  fi

  [ -z "${distribution}" ] && echo >&2 "Cannot find valid distribution with lsb-release" && return 1
  return 0
}

find_etc_any_release() {
  # Check for any of the known /etc/x-release files
  # If it finds one, it loads it and returns 0
  # otherwise it returns 1

  if [ -f "/etc/arch-release" ]; then
    release2lsb_release "/etc/arch-release" && return 0
  fi

  if [ -f "/etc/centos-release" ]; then
    release2lsb_release "/etc/centos-release" && return 0
  fi

  if [ -f "/etc/redhat-release" ]; then
    release2lsb_release "/etc/redhat-release" && return 0
  fi

  if [ -f "/etc/SuSe-release" ]; then
    release2lsb_release "/etc/SuSe-release" && return 0
  fi

  return 1
}

autodetect_distribution() {
  # autodetection of distribution/OS
  case "$(uname -s)" in
    "Linux")
      get_os_release || get_lsb_release || find_etc_any_release
      ;;
    "FreeBSD")
      distribution="freebsd"
      version="$(uname -r)"
      detection="uname"
      ;;
    "Darwin")
      distribution="macos"
      version="$(uname -r)"
      detection="uname"

      if [ ${EUID} -eq 0 ]; then
        echo >&2 "This script does not support running as EUID 0 on macOS. Please run it as a regular user."
        exit 1
      fi
      ;;
    *)
      return 1
      ;;
  esac
}

user_picks_distribution() {
  # let the user pick a distribution

  echo >&2
  echo >&2 "I NEED YOUR HELP"
  echo >&2 "It seems I cannot detect your system automatically."

  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    echo >&2 " > Bailing out..."
    exit 1
  fi

  if [ -z "${equo}" ] && [ -z "${emerge}" ] && [ -z "${apt_get}" ] && [ -z "${yum}" ] && [ -z "${dnf}" ] && [ -z "${pacman}" ] && [ -z "${apk}" ] && [ -z "${swupd}" ]; then
    echo >&2 "And it seems I cannot find a known package manager in this system."
    echo >&2 "Please open a github issue to help us support your system too."
    exit 1
  fi

  local opts=
  echo >&2 "I found though that the following installers are available:"
  echo >&2
  [ -n "${apt_get}" ] && echo >&2 " - Debian/Ubuntu based (installer is: apt-get)" && opts="apt-get ${opts}"
  [ -n "${yum}" ] && echo >&2 " - Redhat/Fedora/Centos based (installer is: yum)" && opts="yum ${opts}"
  [ -n "${dnf}" ] && echo >&2 " - Redhat/Fedora/Centos based (installer is: dnf)" && opts="dnf ${opts}"
  [ -n "${zypper}" ] && echo >&2 " - SuSe based (installer is: zypper)" && opts="zypper ${opts}"
  [ -n "${pacman}" ] && echo >&2 " - Arch Linux based (installer is: pacman)" && opts="pacman ${opts}"
  [ -n "${emerge}" ] && echo >&2 " - Gentoo based (installer is: emerge)" && opts="emerge ${opts}"
  [ -n "${equo}" ] && echo >&2 " - Sabayon based (installer is: equo)" && opts="equo ${opts}"
  [ -n "${apk}" ] && echo >&2 " - Alpine Linux based (installer is: apk)" && opts="apk ${opts}"
  [ -n "${swupd}" ] && echo >&2 " - Clear Linux based (installer is: swupd)" && opts="swupd ${opts}"
  [ -n "${brew}" ] && echo >&2 " - macOS based (installer is: brew)" && opts="brew ${opts}"
  # XXX: This is being removed in another PR.
  echo >&2

  REPLY=
  while [ -z "${REPLY}" ]; do
    echo "To proceed please write one of these:"
    echo "${opts// /, }"
    if ! read -r -p ">" REPLY; then
      continue
    fi

    if [ "${REPLY}" = "yum" ] && [ -z "${distribution}" ]; then
      REPLY=
      while [ -z "${REPLY}" ]; do
        if ! read -r -p "yum in centos, rhel or fedora? > "; then
          continue
        fi

        case "${REPLY,,}" in
          fedora | rhel)
            distribution="rhel"
            ;;
          centos)
            distribution="centos"
            ;;
          *)
            echo >&2 "Please enter 'centos', 'fedora' or 'rhel'."
            REPLY=
            ;;
        esac
      done
      REPLY="yum"
    fi
    check_package_manager "${REPLY}" || REPLY=
  done
}

detect_package_manager_from_distribution() {
  case "${1,,}" in
    arch* | manjaro*)
      package_installer="install_pacman"
      tree="arch"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${pacman}" ]; then
        echo >&2 "command 'pacman' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;

    sabayon*)
      package_installer="install_equo"
      tree="sabayon"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${equo}" ]; then
        echo >&2 "command 'equo' is required to install packages on a '${distribution} ${version}' system."
        # Maybe offer to fall back on emerge? Both installers exist in Sabayon...
        exit 1
      fi
      ;;

    alpine*)
      package_installer="install_apk"
      tree="alpine"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${apk}" ]; then
        echo >&2 "command 'apk' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;

    gentoo*)
      package_installer="install_emerge"
      tree="gentoo"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${emerge}" ]; then
        echo >&2 "command 'emerge' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;

    debian* | ubuntu*)
      package_installer="install_apt_get"
      tree="debian"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${apt_get}" ]; then
        echo >&2 "command 'apt-get' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;

    centos* | clearos*)
      echo >&2 "You should have EPEL enabled to install all the prerequisites."
      echo >&2 "Check: http://www.tecmint.com/how-to-enable-epel-repository-for-rhel-centos-6-5/"
      package_installer="install_yum"
      tree="centos"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${yum}" ]; then
        echo >&2 "command 'yum' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;

    fedora* | redhat* | red\ hat* | rhel*)
      package_installer=
      tree="rhel"
      [ -n "${yum}" ] && package_installer="install_yum"
      [ -n "${dnf}" ] && package_installer="install_dnf"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${package_installer}" ]; then
        echo >&2 "command 'yum' or 'dnf' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;

    suse* | opensuse* | sles*)
      package_installer="install_zypper"
      tree="suse"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${zypper}" ]; then
        echo >&2 "command 'zypper' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;

    clear-linux* | clearlinux*)
      package_installer="install_swupd"
      tree="clearlinux"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${swupd}" ]; then
        echo >&2 "command 'swupd' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;

    freebsd)
      package_installer="install_pkg"
      tree="freebsd"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${pkg}" ]; then
        echo >&2 "command 'pkg' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;
    macos)
      package_installer="install_brew"
      tree="macos"
      if [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${brew}" ]; then
        echo >&2 "command 'brew' is required to install packages on a '${distribution} ${version}' system."
        exit 1
      fi
      ;;

    *)
      # oops! unknown system
      user_picks_distribution
      ;;
  esac
}

# XXX: This is being removed in another PR.
check_package_manager() {
  # This is called only when the user is selecting a package manager
  # It is used to verify the user selection is right

  echo >&2 "Checking package manager: ${1}"

  case "${1}" in
    apt-get)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${apt_get}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_apt_get"
      tree="debian"
      detection="user-input"
      return 0
      ;;

    dnf)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${dnf}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_dnf"
      tree="rhel"
      detection="user-input"
      return 0
      ;;

    apk)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${apk}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_apk"
      tree="alpine"
      detection="user-input"
      return 0
      ;;

    equo)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${equo}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_equo"
      tree="sabayon"
      detection="user-input"
      return 0
      ;;

    emerge)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${emerge}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_emerge"
      tree="gentoo"
      detection="user-input"
      return 0
      ;;

    pacman)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${pacman}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_pacman"
      tree="arch"
      detection="user-input"

      return 0
      ;;

    zypper)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${zypper}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_zypper"
      tree="suse"
      detection="user-input"
      return 0
      ;;

    yum)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${yum}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_yum"
      if [ "${distribution}" = "centos" ]; then
        tree="centos"
      else
        tree="rhel"
      fi
      detection="user-input"
      return 0
      ;;

    swupd)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${swupd}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_swupd"
      tree="clear-linux"
      detection="user-input"
      return 0
      ;;

    brew)
      [ "${IGNORE_INSTALLED}" -eq 0 ] && [ -z "${brew}" ] && echo >&2 "${1} is not available." && return 1
      package_installer="install_brew"
      tree="macos"
      detection="user-input"

      return 0
      ;;

    *)
      echo >&2 "Invalid package manager: '${1}'."
      return 1
      ;;
  esac
}

require_cmd() {
  # check if any of the commands given as argument
  # are present on this system
  # If any of them is available, it returns 0
  # otherwise 1

  [ "${IGNORE_INSTALLED}" -eq 1 ] && return 1

  local wanted found
  for wanted in "${@}"; do
    if command -v "${wanted}" > /dev/null 2>&1; then
      found="$(command -v "$wanted" 2> /dev/null)"
    fi
    [ -n "${found}" ] && [ -x "${found}" ] && return 0
  done
  return 1
}

declare -A pkg_find=(
  ['gentoo']="sys-apps/findutils"
  ['fedora']="findutils"
  ['clearlinux']="findutils"
  ['macos']="NOTREQUIRED"
  ['freebsd']="NOTREQUIRED"
  ['default']="WARNING|"
)

declare -A pkg_distro_sdk=(
  ['alpine']="alpine-sdk"
  ['default']="NOTREQUIRED"
)

declare -A pkg_autoconf=(
  ['gentoo']="sys-devel/autoconf"
  ['clearlinux']="c-basic"
  ['default']="autoconf"
)

# required to compile netdata with --enable-sse
# https://github.com/firehol/netdata/pull/450
declare -A pkg_autoconf_archive=(
  ['gentoo']="sys-devel/autoconf-archive"
  ['clearlinux']="c-basic"
  ['alpine']="WARNING|"
  ['default']="autoconf-archive"

  # exceptions
  ['centos-6']="WARNING|"
  ['rhel-6']="WARNING|"
  ['rhel-7']="WARNING|"
)

declare -A pkg_autogen=(
  ['gentoo']="sys-devel/autogen"
  ['clearlinux']="c-basic"
  ['alpine']="WARNING|"
  ['default']="autogen"

  # exceptions
  ['centos-6']="WARNING|"
  ['rhel-6']="WARNING|"
)

declare -A pkg_automake=(
  ['gentoo']="sys-devel/automake"
  ['clearlinux']="c-basic"
  ['default']="automake"
)

# required to bundle libJudy
declare -A pkg_libtool=(
  ['gentoo']="sys-devel/libtool"
  ['clearlinux']="c-basic"
  ['default']="libtool"
)

# Required to build libwebsockets and libmosquitto on some systems.
declare -A pkg_cmake=(
  ['gentoo']="dev-util/cmake"
  ['clearlinux']="c-basic"
  ['default']="cmake"
)

declare -A pkg_json_c_dev=(
  ['alpine']="json-c-dev"
  ['arch']="json-c"
  ['clearlinux']="devpkg-json-c"
  ['debian']="libjson-c-dev"
  ['gentoo']="dev-libs/json-c"
  ['sabayon']="dev-libs/json-c"
  ['suse']="libjson-c-devel"
  ['freebsd']="json-c"
  ['default']="json-c-devel"
)

declare -A pkg_bridge_utils=(
  ['gentoo']="net-misc/bridge-utils"
  ['clearlinux']="network-basic"
  ['macos']="WARNING|"
  ['default']="bridge-utils"
)

declare -A pkg_chrony=(
  ['gentoo']="net-misc/chrony"
  ['clearlinux']="time-server-basic"
  ['macos']="WARNING|"
  ['default']="chrony"
)

declare -A pkg_curl=(
  ['gentoo']="net-misc/curl"
  ['sabayon']="net-misc/curl"
  ['default']="curl"
)

declare -A pkg_gzip=(
  ['gentoo']="app-arch/gzip"
  ['macos']="NOTREQUIRED"
  ['default']="gzip"
)

declare -A pkg_tar=(
  ['gentoo']="app-arch/tar"
  ['clearlinux']="os-core-update"
  ['macos']="NOTREQUIRED"
  ['default']="tar"
)

declare -A pkg_git=(
  ['gentoo']="dev-vcs/git"
  ['default']="git"
)

declare -A pkg_gcc=(
  ['gentoo']="sys-devel/gcc"
  ['clearlinux']="c-basic"
  ['macos']="NOTREQUIRED"
  ['default']="gcc"
)

declare -A pkg_gdb=(
  ['gentoo']="sys-devel/gdb"
  ['macos']="NOTREQUIRED"
  ['default']="gdb"
)

declare -A pkg_iotop=(
  ['gentoo']="sys-process/iotop"
  ['macos']="WARNING|"
  ['default']="iotop"
)

declare -A pkg_iproute2=(
  ['alpine']="iproute2"
  ['debian']="iproute2"
  ['gentoo']="sys-apps/iproute2"
  ['sabayon']="sys-apps/iproute2"
  ['clearlinux']="iproute2"
  ['macos']="WARNING|"
  ['default']="iproute"

  # exceptions
  ['ubuntu-12.04']="iproute"
)

declare -A pkg_ipset=(
  ['gentoo']="net-firewall/ipset"
  ['clearlinux']="network-basic"
  ['macos']="WARNING|"
  ['default']="ipset"
)

declare -A pkg_jq=(
  ['gentoo']="app-misc/jq"
  ['default']="jq"
)

declare -A pkg_iptables=(
  ['gentoo']="net-firewall/iptables"
  ['macos']="WARNING|"
  ['default']="iptables"
)

declare -A pkg_libz_dev=(
  ['alpine']="zlib-dev"
  ['arch']="zlib"
  ['centos']="zlib-devel"
  ['debian']="zlib1g-dev"
  ['gentoo']="sys-libs/zlib"
  ['sabayon']="sys-libs/zlib"
  ['rhel']="zlib-devel"
  ['suse']="zlib-devel"
  ['clearlinux']="devpkg-zlib"
  ['macos']="NOTREQUIRED"
  ['freebsd']="lzlib"
  ['default']=""
)

declare -A pkg_libuuid_dev=(
  ['alpine']="util-linux-dev"
  ['arch']="util-linux"
  ['centos']="libuuid-devel"
  ['clearlinux']="devpkg-util-linux"
  ['debian']="uuid-dev"
  ['gentoo']="sys-apps/util-linux"
  ['sabayon']="sys-apps/util-linux"
  ['rhel']="libuuid-devel"
  ['suse']="libuuid-devel"
  ['macos']="NOTREQUIRED"
  ['freebsd']="e2fsprogs-libuuid"
  ['default']=""
)

declare -A pkg_libmnl_dev=(
  ['alpine']="libmnl-dev"
  ['arch']="libmnl"
  ['centos']="libmnl-devel"
  ['debian']="libmnl-dev"
  ['gentoo']="net-libs/libmnl"
  ['sabayon']="net-libs/libmnl"
  ['rhel']="libmnl-devel"
  ['suse']="libmnl-devel"
  ['clearlinux']="devpkg-libmnl"
  ['macos']="NOTREQUIRED"
  ['default']=""
)

declare -A pkg_lm_sensors=(
  ['alpine']="lm_sensors"
  ['arch']="lm_sensors"
  ['centos']="lm_sensors"
  ['debian']="lm-sensors"
  ['gentoo']="sys-apps/lm-sensors"
  ['sabayon']="sys-apps/lm_sensors"
  ['rhel']="lm_sensors"
  ['suse']="sensors"
  ['clearlinux']="lm-sensors"
  ['macos']="WARNING|"
  ['freebsd']="NOTREQUIRED"
  ['default']="lm_sensors"
)

declare -A pkg_logwatch=(
  ['gentoo']="sys-apps/logwatch"
  ['clearlinux']="WARNING|"
  ['macos']="WARNING|"
  ['default']="logwatch"
)

declare -A pkg_lxc=(
  ['gentoo']="app-emulation/lxc"
  ['clearlinux']="WARNING|"
  ['macos']="WARNING|"
  ['default']="lxc"
)

declare -A pkg_mailutils=(
  ['gentoo']="net-mail/mailutils"
  ['clearlinux']="WARNING|"
  ['macos']="WARNING|"
  ['default']="mailutils"
)

declare -A pkg_make=(
  ['gentoo']="sys-devel/make"
  ['macos']="NOTREQUIRED"
  ['freebsd']="gmake"
  ['default']="make"
)

declare -A pkg_netcat=(
  ['alpine']="netcat-openbsd"
  ['arch']="netcat"
  ['centos']="nmap-ncat"
  ['debian']="netcat"
  ['gentoo']="net-analyzer/netcat"
  ['sabayon']="net-analyzer/gnu-netcat"
  ['rhel']="nmap-ncat"
  ['suse']="netcat-openbsd"
  ['clearlinux']="sysadmin-basic"
  ['arch']="gnu-netcat"
  ['macos']="NOTREQUIRED"
  ['default']="netcat"

  # exceptions
  ['centos-6']="nc"
  ['rhel-6']="nc"
)

declare -A pkg_nginx=(
  ['gentoo']="www-servers/nginx"
  ['default']="nginx"
)

declare -A pkg_nodejs=(
  ['gentoo']="net-libs/nodejs"
  ['clearlinux']="nodejs-basic"
  ['freebsd']="node"
  ['default']="nodejs"

  # exceptions
  ['rhel-6']="WARNING|To install nodejs check: https://nodejs.org/en/download/package-manager/"
  ['rhel-7']="WARNING|To install nodejs check: https://nodejs.org/en/download/package-manager/"
  ['centos-6']="WARNING|To install nodejs check: https://nodejs.org/en/download/package-manager/"
  ['debian-6']="WARNING|To install nodejs check: https://nodejs.org/en/download/package-manager/"
  ['debian-7']="WARNING|To install nodejs check: https://nodejs.org/en/download/package-manager/"
)

declare -A pkg_postfix=(
  ['gentoo']="mail-mta/postfix"
  ['macos']="WARNING|"
  ['default']="postfix"
)

declare -A pkg_pkg_config=(
  ['alpine']="pkgconfig"
  ['arch']="pkgconfig"
  ['centos']="pkgconfig"
  ['debian']="pkg-config"
  ['gentoo']="virtual/pkgconfig"
  ['sabayon']="virtual/pkgconfig"
  ['rhel']="pkgconfig"
  ['suse']="pkg-config"
  ['freebsd']="pkgconf"
  ['clearlinux']="c-basic"
  ['default']="pkg-config"
)

declare -A pkg_python=(
  ['gentoo']="dev-lang/python"
  ['sabayon']="dev-lang/python:2.7"
  ['clearlinux']="python-basic"
  ['default']="python"

  # Exceptions
  ['macos']="WARNING|"
  ['centos-8']="python2"
)

declare -A pkg_python_mysqldb=(
  ['alpine']="py-mysqldb"
  ['arch']="mysql-python"
  ['centos']="MySQL-python"
  ['debian']="python-mysqldb"
  ['gentoo']="dev-python/mysqlclient"
  ['sabayon']="dev-python/mysqlclient"
  ['rhel']="MySQL-python"
  ['suse']="python-PyMySQL"
  ['clearlinux']="WARNING|"
  ['default']="python-mysql"

  # exceptions
  ['fedora-24']="python2-mysql"
)

declare -A pkg_python3_mysqldb=(
  ['alpine']="WARNING|"
  ['arch']="WARNING|"
  ['centos']="WARNING|"
  ['debian']="python3-mysqldb"
  ['gentoo']="dev-python/mysqlclient"
  ['sabayon']="dev-python/mysqlclient"
  ['rhel']="WARNING|"
  ['suse']="WARNING|"
  ['clearlinux']="WARNING|"
  ['macos']="WARNING|"
  ['default']="WARNING|"

  # exceptions
  ['debian-6']="WARNING|"
  ['debian-7']="WARNING|"
  ['debian-8']="WARNING|"
  ['ubuntu-12.04']="WARNING|"
  ['ubuntu-12.10']="WARNING|"
  ['ubuntu-13.04']="WARNING|"
  ['ubuntu-13.10']="WARNING|"
  ['ubuntu-14.04']="WARNING|"
  ['ubuntu-14.10']="WARNING|"
  ['ubuntu-15.04']="WARNING|"
  ['ubuntu-15.10']="WARNING|"
  ['centos-7']="python36-mysql"
  ['centos-8']="python38-mysql"
  ['rhel-7']="python36-mysql"
  ['rhel-8']="python38-mysql"
)

declare -A pkg_python_psycopg2=(
  ['alpine']="py-psycopg2"
  ['arch']="python2-psycopg2"
  ['centos']="python-psycopg2"
  ['debian']="python-psycopg2"
  ['gentoo']="dev-python/psycopg"
  ['sabayon']="dev-python/psycopg:2"
  ['rhel']="python-psycopg2"
  ['suse']="python-psycopg2"
  ['clearlinux']="WARNING|"
  ['macos']="WARNING|"
  ['default']="python-psycopg2"
)

declare -A pkg_python3_psycopg2=(
  ['alpine']="py3-psycopg2"
  ['arch']="python-psycopg2"
  ['centos']="WARNING|"
  ['debian']="WARNING|"
  ['gentoo']="dev-python/psycopg"
  ['sabayon']="dev-python/psycopg:2"
  ['rhel']="WARNING|"
  ['suse']="WARNING|"
  ['clearlinux']="WARNING|"
  ['macos']="WARNING|"
  ['default']="WARNING|"

  ['centos-7']="python3-psycopg2"
  ['centos-8']="python38-psycopg2"
  ['rhel-7']="python3-psycopg2"
  ['rhel-8']="python38-psycopg2"
)

declare -A pkg_python_pip=(
  ['alpine']="py-pip"
  ['gentoo']="dev-python/pip"
  ['sabayon']="dev-python/pip"
  ['clearlinux']="python-basic"
  ['macos']="WARNING|"
  ['default']="python-pip"
)

declare -A pkg_python3_pip=(
  ['alpine']="py3-pip"
  ['arch']="python-pip"
  ['gentoo']="dev-python/pip"
  ['sabayon']="dev-python/pip"
  ['clearlinux']="python3-basic"
  ['macos']="NOTREQUIRED"
  ['default']="python3-pip"
)

declare -A pkg_python_pymongo=(
  ['alpine']="WARNING|"
  ['arch']="python2-pymongo"
  ['centos']="WARNING|"
  ['debian']="python-pymongo"
  ['gentoo']="dev-python/pymongo"
  ['suse']="python-pymongo"
  ['clearlinux']="WARNING|"
  ['rhel']="WARNING|"
  ['macos']="WARNING|"
  ['default']="python-pymongo"
)

declare -A pkg_python3_pymongo=(
  ['alpine']="WARNING|"
  ['arch']="python-pymongo"
  ['centos']="WARNING|"
  ['debian']="python3-pymongo"
  ['gentoo']="dev-python/pymongo"
  ['suse']="python3-pymongo"
  ['clearlinux']="WARNING|"
  ['rhel']="WARNING|"
  ['freebsd']="py37-pymongo"
  ['macos']="WARNING|"
  ['default']="python3-pymongo"

  ['centos-7']="python36-pymongo"
  ['centos-8']="python3-pymongo"
  ['rhel-7']="python36-pymongo"
  ['rhel-8']="python3-pymongo"
)

declare -A pkg_python_requests=(
  ['alpine']="py-requests"
  ['arch']="python2-requests"
  ['centos']="python-requests"
  ['debian']="python-requests"
  ['gentoo']="dev-python/requests"
  ['sabayon']="dev-python/requests"
  ['rhel']="python-requests"
  ['suse']="python-requests"
  ['clearlinux']="python-extras"
  ['macos']="WARNING|"
  ['default']="python-requests"
  ['alpine-3.1.4']="WARNING|"
  ['alpine-3.2.3']="WARNING|"
)

declare -A pkg_python3_requests=(
  ['alpine']="py3-requests"
  ['arch']="python-requests"
  ['centos']="WARNING|"
  ['debian']="WARNING|"
  ['gentoo']="dev-python/requests"
  ['sabayon']="dev-python/requests"
  ['rhel']="WARNING|"
  ['suse']="WARNING|"
  ['clearlinux']="python-extras"
  ['macos']="WARNING|"
  ['default']="WARNING|"

  ['centos-7']="python36-requests"
  ['centos-8']="python3-requests"
  ['rhel-7']="python36-requests"
  ['rhel-8']="python3-requests"
)

declare -A pkg_lz4=(
  ['alpine']="lz4-dev"
  ['debian']="liblz4-dev"
  ['ubuntu']="liblz4-dev"
  ['suse']="liblz4-devel"
  ['gentoo']="app-arch/lz4"
  ['clearlinux']="devpkg-lz4"
  ['arch']="lz4"
  ['macos']="lz4"
  ['freebsd']="liblz4"
  ['default']="lz4-devel"
)

declare -A pkg_libuv=(
  ['alpine']="libuv-dev"
  ['debian']="libuv1-dev"
  ['ubuntu']="libuv1-dev"
  ['gentoo']="dev-libs/libuv"
  ['arch']="libuv"
  ['clearlinux']="devpkg-libuv"
  ['macos']="libuv"
  ['freebsd']="libuv"
  ['default']="libuv-devel"
)

declare -A pkg_openssl=(
  ['alpine']="openssl-dev"
  ['debian']="libssl-dev"
  ['ubuntu']="libssl-dev"
  ['suse']="libopenssl-devel"
  ['clearlinux']="devpkg-openssl"
  ['gentoo']="dev-libs/openssl"
  ['arch']="openssl"
  ['freebsd']="openssl"
  ['macos']="openssl@1.1"
  ['default']="openssl-devel"
)

declare -A pkg_judy=(
  ['debian']="libjudy-dev"
  ['ubuntu']="libjudy-dev"
  ['suse']="judy-devel"
  ['gentoo']="dev-libs/judy"
  ['arch']="judy"
  ['freebsd']="Judy"
  ['fedora']="Judy-devel"
  ['default']="NOTREQUIRED"
)

declare -A pkg_python3=(
  ['gentoo']="dev-lang/python"
  ['sabayon']="dev-lang/python:3.4"
  ['clearlinux']="python3-basic"
  ['macos']="python"
  ['default']="python3"

  # exceptions
  ['centos-6']="WARNING|"
)

declare -A pkg_screen=(
  ['gentoo']="app-misc/screen"
  ['sabayon']="app-misc/screen"
  ['clearlinux']="sysadmin-basic"
  ['default']="screen"
)

declare -A pkg_sudo=(
  ['gentoo']="app-admin/sudo"
  ['macos']="NOTREQUIRED"
  ['default']="sudo"
)

declare -A pkg_sysstat=(
  ['gentoo']="app-admin/sysstat"
  ['macos']="WARNING|"
  ['default']="sysstat"
)

declare -A pkg_tcpdump=(
  ['gentoo']="net-analyzer/tcpdump"
  ['clearlinux']="network-basic"
  ['default']="tcpdump"
)

declare -A pkg_traceroute=(
  ['alpine']=" "
  ['gentoo']="net-analyzer/traceroute"
  ['clearlinux']="network-basic"
  ['macos']="NOTREQUIRED"
  ['default']="traceroute"
)

declare -A pkg_valgrind=(
  ['gentoo']="dev-util/valgrind"
  ['default']="valgrind"
)

declare -A pkg_ulogd=(
  ['centos']="WARNING|"
  ['rhel']="WARNING|"
  ['clearlinux']="WARNING|"
  ['gentoo']="app-admin/ulogd"
  ['arch']="ulogd"
  ['macos']="WARNING|"
  ['default']="ulogd2"
)

declare -A pkg_unzip=(
  ['gentoo']="app-arch/unzip"
  ['macos']="NOTREQUIRED"
  ['default']="unzip"
)

declare -A pkg_zip=(
  ['gentoo']="app-arch/zip"
  ['macos']="NOTREQUIRED"
  ['default']="zip"
)

declare -A pkg_libelf=(
  ['alpine']="elfutils-dev"
  ['arch']="libelf"
  ['gentoo']="virtual/libelf"
  ['sabayon']="virtual/libelf"
  ['debian']="libelf-dev"
  ['ubuntu']="libelf-dev"
  ['fedora']="elfutils-libelf-devel"
  ['centos']="elfutils-libelf-devel"
  ['rhel']="elfutils-libelf-devel"
  ['clearlinux']="devpkg-elfutils"
  ['suse']="libelf-devel"
  ['macos']="NOTREQUIRED"
  ['freebsd']="NOTREQUIRED"
  ['default']="libelf-devel"

  # exceptions
  ['alpine-3.5']="libelf-dev"
  ['alpine-3.4']="libelf-dev"
  ['alpine-3.3']="libelf-dev"
)

validate_package_trees() {
  if type -t validate_tree_${tree} > /dev/null; then
    validate_tree_${tree}
  fi
}

validate_installed_package() {
  validate_${package_installer} "${p}"
}

suitable_package() {
  local package="${1//-/_}" p="" v="${version//.*/}"

  echo >&2 "Searching for ${package} ..."

  eval "p=\${pkg_${package}['${distribution,,}-${version,,}']}"
  [ -z "${p}" ] && eval "p=\${pkg_${package}['${distribution,,}-${v,,}']}"
  [ -z "${p}" ] && eval "p=\${pkg_${package}['${distribution,,}']}"
  [ -z "${p}" ] && eval "p=\${pkg_${package}['${tree}-${version}']}"
  [ -z "${p}" ] && eval "p=\${pkg_${package}['${tree}-${v}']}"
  [ -z "${p}" ] && eval "p=\${pkg_${package}['${tree}']}"
  [ -z "${p}" ] && eval "p=\${pkg_${package}['default']}"

  if [[ "${p/|*/}" =~ ^(ERROR|WARNING|INFO)$ ]]; then
    echo >&2 "${p/|*/}"
    echo >&2 "package ${1} is not available in this system."
    if [ -z "${p/*|/}" ]; then
      echo >&2 "You may try to install without it."
    else
      echo >&2 "${p/*|/}"
    fi
    echo >&2
    return 1
  elif [ "${p}" = "NOTREQUIRED" ]; then
    return 0
  elif [ -z "${p}" ]; then
    echo >&2 "WARNING"
    echo >&2 "package ${1} is not availabe in this system."
    echo >&2
    return 1
  else
    if [ "${IGNORE_INSTALLED}" -eq 0 ]; then
      validate_installed_package "${p}"
    else
      echo "${p}"
    fi
    return 0
  fi
}

packages() {
  # detect the packages we need to install on this system

  # -------------------------------------------------------------------------
  # basic build environment

  suitable_package distro-sdk

  require_cmd git || suitable_package git
  require_cmd find || suitable_package find

  require_cmd gcc ||
    require_cmd gcc-multilib || suitable_package gcc

  require_cmd make || suitable_package make
  require_cmd autoconf || suitable_package autoconf
  suitable_package autoconf-archive
  require_cmd autogen || suitable_package autogen
  require_cmd automake || suitable_package automake
  require_cmd libtoolize || suitable_package libtool
  require_cmd pkg-config || suitable_package pkg-config
  require_cmd cmake || suitable_package cmake

  # -------------------------------------------------------------------------
  # debugging tools for development

  if [ "${PACKAGES_DEBUG}" -ne 0 ]; then
    require_cmd traceroute || suitable_package traceroute
    require_cmd tcpdump || suitable_package tcpdump
    require_cmd screen || suitable_package screen

    if [ "${PACKAGES_NETDATA}" -ne 0 ]; then
      require_cmd gdb || suitable_package gdb
      require_cmd valgrind || suitable_package valgrind
    fi
  fi

  # -------------------------------------------------------------------------
  # common command line tools

  if [ "${PACKAGES_NETDATA}" -ne 0 ]; then
    require_cmd tar || suitable_package tar
    require_cmd curl || suitable_package curl
    require_cmd gzip || suitable_package gzip
    require_cmd nc || suitable_package netcat
  fi

  # -------------------------------------------------------------------------
  # firehol/fireqos/update-ipsets command line tools

  if [ "${PACKAGES_FIREQOS}" -ne 0 ]; then
    require_cmd ip || suitable_package iproute2
  fi

  if [ "${PACKAGES_FIREHOL}" -ne 0 ]; then
    require_cmd iptables || suitable_package iptables
    require_cmd ipset || suitable_package ipset
    require_cmd ulogd ulogd2 || suitable_package ulogd
    require_cmd traceroute || suitable_package traceroute
    require_cmd bridge || suitable_package bridge-utils
  fi

  if [ "${PACKAGES_UPDATE_IPSETS}" -ne 0 ]; then
    require_cmd ipset || suitable_package ipset
    require_cmd zip || suitable_package zip
    require_cmd funzip || suitable_package unzip
  fi

  # -------------------------------------------------------------------------
  # netdata libraries

  if [ "${PACKAGES_NETDATA}" -ne 0 ]; then
    suitable_package libz-dev
    suitable_package libuuid-dev
    suitable_package libmnl-dev
    suitable_package json-c-dev
  fi

  # -------------------------------------------------------------------------
  # sensors

  if [ "${PACKAGES_NETDATA_SENSORS}" -ne 0 ]; then
    require_cmd sensors || suitable_package lm_sensors
  fi

  # -------------------------------------------------------------------------
  # netdata database
  if [ "${PACKAGES_NETDATA_DATABASE}" -ne 0 ]; then
    suitable_package libuv
    suitable_package lz4
    suitable_package openssl
    suitable_package judy
  fi

  # -------------------------------------------------------------------------
  # ebpf plugin
  if [ "${PACKAGES_NETDATA_EBPF}" -ne 0 ]; then
    suitable_package libelf
  fi

  # -------------------------------------------------------------------------
  # scripting interpreters for netdata plugins

  if [ "${PACKAGES_NETDATA_NODEJS}" -ne 0 ]; then
    require_cmd nodejs node js || suitable_package nodejs
  fi

  # -------------------------------------------------------------------------
  # python2

  if [ "${PACKAGES_NETDATA_PYTHON}" -ne 0 ]; then
    require_cmd python || suitable_package python

    [ "${PACKAGES_NETDATA_PYTHON_MONGO}" -ne 0 ] && suitable_package python-pymongo
    # suitable_package python-requests
    # suitable_package python-pip

    [ "${PACKAGES_NETDATA_PYTHON_MYSQL}" -ne 0 ] && suitable_package python-mysqldb
    [ "${PACKAGES_NETDATA_PYTHON_POSTGRES}" -ne 0 ] && suitable_package python-psycopg2
  fi

  # -------------------------------------------------------------------------
  # python3

  if [ "${PACKAGES_NETDATA_PYTHON3}" -ne 0 ]; then
    require_cmd python3 || suitable_package python3

    [ "${PACKAGES_NETDATA_PYTHON_MONGO}" -ne 0 ] && suitable_package python3-pymongo
    # suitable_package python3-requests
    # suitable_package python3-pip

    [ "${PACKAGES_NETDATA_PYTHON_MYSQL}" -ne 0 ] && suitable_package python3-mysqldb
    [ "${PACKAGES_NETDATA_PYTHON_POSTGRES}" -ne 0 ] && suitable_package python3-psycopg2
  fi

  # -------------------------------------------------------------------------
  # applications needed for the netdata demo sites

  if [ "${PACKAGES_NETDATA_DEMO_SITE}" -ne 0 ]; then
    require_cmd sudo || suitable_package sudo
    require_cmd jq || suitable_package jq
    require_cmd nginx || suitable_package nginx
    require_cmd postconf || suitable_package postfix
    require_cmd lxc-create || suitable_package lxc
    require_cmd logwatch || suitable_package logwatch
    require_cmd mail || suitable_package mailutils
    require_cmd iostat || suitable_package sysstat
    require_cmd iotop || suitable_package iotop
  fi
}

DRYRUN=0
run() {

  printf >&2 "%q " "${@}"
  printf >&2 "\n"

  if [ ! "${DRYRUN}" -eq 1 ]; then
    "${@}"
    return $?
  fi
  return 0
}

sudo=
if [ ${UID} -ne 0 ]; then
  sudo="sudo"
fi

# -----------------------------------------------------------------------------
# debian / ubuntu

validate_install_apt_get() {
  echo >&2 " > Checking if package '${*}' is installed..."
  [ "$(dpkg-query -W --showformat='${Status}\n' "${*}")" = "install ok installed" ] || echo "${*}"
}

install_apt_get() {
  local opts=""
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    # http://serverfault.com/questions/227190/how-do-i-ask-apt-get-to-skip-any-interactive-post-install-configuration-steps
    export DEBIAN_FRONTEND="noninteractive"
    opts="${opts} -yq"
  fi

  read -r -a apt_opts <<< "$opts"

  # update apt repository caches

  echo >&2 "NOTE: Running apt-get update and updating your APT caches ..."
  if [ "${version}" = 8 ]; then
    echo >&2 "WARNING: You seem to be on Debian 8 (jessie) which is old enough we have to disable Check-Valid-Until checks"
    if ! cat /etc/apt/sources.list /etc/apt/sources.list.d/* 2> /dev/null | grep -q jessie-backports; then
      echo >&2 "We also have to enable the jessie-backports repository"
      if prompt "Is this okay?"; then
        ${sudo} /bin/sh -c 'echo "deb http://archive.debian.org/debian/ jessie-backports main contrib non-free" >> /etc/apt/sources.list.d/99-archived.list'
      fi
    fi
    run ${sudo} apt-get "${apt_opts[@]}" -o Acquire::Check-Valid-Until=false update
  else
    run ${sudo} apt-get "${apt_opts[@]}" update
  fi

  # install the required packages
  run ${sudo} apt-get "${apt_opts[@]}" install "${@}"
}

# -----------------------------------------------------------------------------
# centos / rhel

prompt() {
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode, assuming yes (y)"
    echo >&2 " > Would have promptedfor ${1} ..."
    return 0
  fi

  while true; do
    read -r -p "${1} [y/n] " yn
    case $yn in
      [Yy]*) return 0 ;;
      [Nn]*) return 1 ;;
      *) echo >&2 "Please answer with yes (y) or no (n)." ;;
    esac
  done
}

validate_tree_freebsd() {
  local opts=
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    opts="-y"
  fi

  echo >&2 " > FreeBSD Version: ${version} ..."

  make="make"
  echo >&2 " > Checking for gmake ..."
  if ! pkg query %n-%v | grep -q gmake; then
    if prompt "gmake is required to build on FreeBSD and is not installed. Shall I install it?"; then
      run ${sudo} pkg install ${opts} gmake
    fi
  fi
}

validate_tree_centos() {
  local opts=
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    opts="-y"
  fi

  echo >&2 " > CentOS Version: ${version} ..."

  echo >&2 " > Checking for epel ..."
  if ! rpm -qa | grep epel > /dev/null; then
    if prompt "epel not found, shall I install it?"; then
      run ${sudo} yum ${opts} install epel-release
    fi
  fi

  if [[ "${version}" =~ ^8(\..*)?$ ]]; then
    echo >&2 " > Checking for config-manager ..."
    if ! run yum ${sudo} config-manager; then
      if prompt "config-manager not found, shall I install it?"; then
        run ${sudo} yum ${opts} install 'dnf-command(config-manager)'
      fi
    fi

    echo >&2 " > Checking for PowerTools ..."
    if ! run yum ${sudo} repolist | grep PowerTools; then
      if prompt "PowerTools not found, shall I install it?"; then
        run ${sudo} yum ${opts} config-manager --set-enabled powertools
      fi
    fi

    echo >&2 " > Checking for Okay ..."
    if ! rpm -qa | grep okay > /dev/null; then
      if prompt "okay not found, shall I install it?"; then
        run ${sudo} yum ${opts} install http://repo.okay.com.mx/centos/8/x86_64/release/okay-release-1-3.el8.noarch.rpm
      fi
    fi

    echo >&2 " > Installing Judy-devel directly ..."
    run ${sudo} yum ${opts} install http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/Judy-devel-1.0.5-18.module_el8.1.0+217+4d875839.x86_64.rpm

  elif [[ "${version}" =~ ^6\..*$ ]]; then
    echo >&2 " > Detected CentOS 6.x ..."
    echo >&2 " > Checking for Okay ..."
    if ! rpm -qa | grep okay > /dev/null; then
      if prompt "okay not found, shall I install it?"; then
        run ${sudo} yum ${opts} install http://repo.okay.com.mx/centos/6/x86_64/release/okay-release-1-3.el6.noarch.rpm
      fi
    fi

  fi
}

validate_install_yum() {
  echo >&2 " > Checking if package '${*}' is installed..."
  yum list installed "${*}" > /dev/null 2>&1 || echo "${*}"
}

install_yum() {
  # download the latest package info
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  ${sudo} yum update  "
    echo >&2
  fi

  local opts=
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    # http://unix.stackexchange.com/questions/87822/does-yum-have-an-equivalent-to-apt-aptitudes-debian-frontend-noninteractive
    opts="-y"
  fi

  read -r -a yum_opts <<< "${opts}"

  # install the required packages
  run ${sudo} yum "${yum_opts[@]}" install "${@}" # --enablerepo=epel-testing
}

# -----------------------------------------------------------------------------
# fedora

validate_install_dnf() {
  echo >&2 " > Checking if package '${*}' is installed..."
  dnf list installed "${*}" > /dev/null 2>&1 || echo "${*}"
}

install_dnf() {
  # download the latest package info
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  ${sudo} dnf update  "
    echo >&2
  fi

  local opts=
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    # man dnf
    opts="-y"
  fi

  # install the required packages
  # --setopt=strict=0 allows dnf to proceed
  # installing whatever is available
  # even if a package is not found
  opts="$opts --setopt=strict=0"
  read -r -a dnf_opts <<< "$opts"
  run ${sudo} dnf "${dnf_opts[@]}" install "${@}"
}

# -----------------------------------------------------------------------------
# gentoo

validate_install_emerge() {
  echo "${*}"
}

install_emerge() {
  # download the latest package info
  # we don't do this for emerge - it is very slow
  # and most users are expected to do this daily
  # emerge --sync
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  ${sudo} emerge --sync  or  ${sudo} eix-sync  "
    echo >&2
  fi

  local opts="--ask"
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    opts=""
  fi

  read -r -a emerge_opts <<< "$opts"

  # install the required packages
  run ${sudo} emerge "${emerge_opts[@]}" -v --noreplace "${@}"
}

# -----------------------------------------------------------------------------
# alpine

validate_install_apk() {
  echo "${*}"
}

install_apk() {
  # download the latest package info
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  ${sudo} apk update  "
    echo >&2
  fi

  local opts="--force-broken-world"
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
  else
    opts="${opts} -i"
  fi

  read -r -a apk_opts <<< "$opts"

  # install the required packages
  run ${sudo} apk add "${apk_opts[@]}" "${@}"
}

# -----------------------------------------------------------------------------
# sabayon

validate_install_equo() {
  echo >&2 " > Checking if package '${*}' is installed..."
  equo s --installed "${*}" > /dev/null 2>&1 || echo "${*}"
}

install_equo() {
  # download the latest package info
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  ${sudo} equo up  "
    echo >&2
  fi

  local opts="-av"
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    opts="-v"
  fi

  read -r -a equo_opts <<< "$opts"

  # install the required packages
  run ${sudo} equo i "${equo_opts[@]}" "${@}"
}

# -----------------------------------------------------------------------------
# arch

PACMAN_DB_SYNCED=0
validate_install_pacman() {

  if [ ${PACMAN_DB_SYNCED} -eq 0 ]; then
    echo >&2 " > Running pacman -Sy to sync the database"
    local x
    x=$(pacman -Sy)
    [ -z "${x}" ] && echo "${*}"
    PACMAN_DB_SYNCED=1
  fi
  echo >&2 " > Checking if package '${*}' is installed..."

  # In pacman, you can utilize alternative flags to exactly match package names,
  # but is highly likely we require pattern matching too in this so we keep -s and match
  # the exceptional cases like so
  local x=""
  case "${package}" in
    "gcc")
      # Temporary workaround: In archlinux, default installation includes runtime libs under package "gcc"
      # These are not sufficient for netdata install, so we need to make sure that the appropriate libraries are there
      # by ensuring devel libs are available
      x=$(pacman -Qs "${*}" | grep "base-devel")
      ;;
    "tar")
      x=$(pacman -Qs "${*}" | grep "local/tar")
      ;;
    "make")
      x=$(pacman -Qs "${*}" | grep "local/make ")
      ;;
    *)
      x=$(pacman -Qs "${*}")
      ;;
  esac

  [ -z "${x}" ] && echo "${*}"
}

install_pacman() {
  # download the latest package info
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  ${sudo} pacman -Syu  "
    echo >&2
  fi

  # install the required packages
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    # http://unix.stackexchange.com/questions/52277/pacman-option-to-assume-yes-to-every-question/52278
    # Try the noconfirm option, if that fails, go with the legacy way for non-interactive
    run ${sudo} pacman --noconfirm --needed -S "${@}" || yes | run ${sudo} pacman --needed -S "${@}"
  else
    run ${sudo} pacman --needed -S "${@}"
  fi
}

# -----------------------------------------------------------------------------
# suse / opensuse

validate_install_zypper() {
  rpm -q "${*}" > /dev/null 2>&1 || echo "${*}"
}

install_zypper() {
  # download the latest package info
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  ${sudo} zypper update  "
    echo >&2
  fi

  local opts="--ignore-unknown"
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    # http://unix.stackexchange.com/questions/82016/how-to-use-zypper-in-bash-scripts-for-someone-coming-from-apt-get
    opts="${opts} --non-interactive"
  fi

  read -r -a zypper_opts <<< "$opts"

  # install the required packages
  run ${sudo} zypper "${zypper_opts[@]}" install "${@}"
}

# -----------------------------------------------------------------------------
# clearlinux

validate_install_swupd() {
  swupd bundle-list | grep -q "${*}" || echo "${*}"
}

install_swupd() {
  # download the latest package info
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  ${sudo} swupd update  "
    echo >&2
  fi

  run ${sudo} swupd bundle-add "${@}"
}

# -----------------------------------------------------------------------------
# macOS

validate_install_pkg() {
  pkg query %n-%v | grep -q "${*}" || echo "${*}"
}

validate_install_brew() {
  brew list | grep -q "${*}" || echo "${*}"
}

install_pkg() {
  # download the latest package info
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  pkg update "
    echo >&2
  fi

  local opts=
  if [ "${NON_INTERACTIVE}" -eq 1 ]; then
    echo >&2 "Running in non-interactive mode"
    opts="-y"
  fi

  read -r -a pkg_opts <<< "${opts}"

  run ${sudo} pkg install "${pkg_opts[@]}" "${@}"
}

install_brew() {
  # download the latest package info
  if [ "${DRYRUN}" -eq 1 ]; then
    echo >&2 " >> IMPORTANT << "
    echo >&2 "    Please make sure your system is up to date"
    echo >&2 "    by running:  brew upgrade "
    echo >&2
  fi

  run brew install "${@}"
}

# -----------------------------------------------------------------------------

install_failed() {
  local ret="${1}"
  cat << EOF



We are very sorry!

Installation of required packages failed.

What to do now:

  1. Make sure your system is updated.
     Most of the times, updating your system will resolve the issue.

  2. If the error message is about a specific package, try removing
     that package from the command and run it again.
     Depending on the broken package, you may be able to continue.

  3. Let us know. We may be able to help.
     Open a github issue with the above log, at:

           https://github.com/netdata/netdata/issues


EOF
  remote_log "FAILED" "${ret}"
  exit 1
}

remote_log() {
  # log success or failure on our system
  # to help us solve installation issues
  curl > /dev/null 2>&1 -Ss --max-time 3 "https://registry.my-netdata.io/log/installer?status=${1}&error=${2}&distribution=${distribution}&version=${version}&installer=${package_installer}&tree=${tree}&detection=${detection}&netdata=${PACKAGES_NETDATA}&nodejs=${PACKAGES_NETDATA_NODEJS}&python=${PACKAGES_NETDATA_PYTHON}&python3=${PACKAGES_NETDATA_PYTHON3}&mysql=${PACKAGES_NETDATA_PYTHON_MYSQL}&postgres=${PACKAGES_NETDATA_PYTHON_POSTGRES}&pymongo=${PACKAGES_NETDATA_PYTHON_MONGO}&sensors=${PACKAGES_NETDATA_SENSORS}&database=${PACKAGES_NETDATA_DATABASE}&ebpf=${PACKAGES_NETDATA_EBPF}&firehol=${PACKAGES_FIREHOL}&fireqos=${PACKAGES_FIREQOS}&iprange=${PACKAGES_IPRANGE}&update_ipsets=${PACKAGES_UPDATE_IPSETS}&demo=${PACKAGES_NETDATA_DEMO_SITE}"
}

if [ -z "${1}" ]; then
  usage
  exit 1
fi

pv=$(python --version 2>&1)
if [ "${tree}" = macos ]; then
  pv=3
elif [[ "${pv}" =~ ^Python\ 2.* ]]; then
  pv=2
elif [[ "${pv}" =~ ^Python\ 3.* ]]; then
  pv=3
elif [[ "${tree}" == "centos" ]] && [ "${version}" -lt 8 ]; then
  pv=2
else
  pv=3
fi

# parse command line arguments
DONT_WAIT=0
NON_INTERACTIVE=0
IGNORE_INSTALLED=0
while [ -n "${1}" ]; do
  case "${1}" in
    distribution)
      distribution="${2}"
      shift
      ;;

    version)
      version="${2}"
      shift
      ;;

    codename)
      codename="${2}"
      shift
      ;;

    installer)
      check_package_manager "${2}" || exit 1
      shift
      ;;

    dont-wait | --dont-wait | -n)
      DONT_WAIT=1
      ;;

    non-interactive | --non-interactive | -y)
      NON_INTERACTIVE=1
      ;;

    ignore-installed | --ignore-installed | -i)
      IGNORE_INSTALLED=1
      ;;

    netdata-all)
      PACKAGES_NETDATA=1
      PACKAGES_NETDATA_NODEJS=1
      if [ "${pv}" -eq 2 ]; then
        PACKAGES_NETDATA_PYTHON=1
        PACKAGES_NETDATA_PYTHON_MYSQL=1
        PACKAGES_NETDATA_PYTHON_POSTGRES=1
        PACKAGES_NETDATA_PYTHON_MONGO=1
      else
        PACKAGES_NETDATA_PYTHON3=1
        PACKAGES_NETDATA_PYTHON3_MYSQL=1
        PACKAGES_NETDATA_PYTHON3_POSTGRES=1
        PACKAGES_NETDATA_PYTHON3_MONGO=1
      fi
      PACKAGES_NETDATA_SENSORS=1
      PACKAGES_NETDATA_DATABASE=1
      PACKAGES_NETDATA_EBPF=1
      ;;

    netdata)
      PACKAGES_NETDATA=1
      PACKAGES_NETDATA_PYTHON3=1
      PACKAGES_NETDATA_DATABASE=1
      PACKAGES_NETDATA_EBPF=1
      ;;

    python | netdata-python)
      PACKAGES_NETDATA_PYTHON=1
      ;;

    python3 | netdata-python3)
      PACKAGES_NETDATA_PYTHON3=1
      ;;

    python-mysql | mysql-python | mysqldb | netdata-mysql)
      if [ "${pv}" -eq 2 ]; then
        PACKAGES_NETDATA_PYTHON=1
        PACKAGES_NETDATA_PYTHON_MYSQL=1
      else
        PACKAGES_NETDATA_PYTHON3=1
        PACKAGES_NETDATA_PYTHON3_MYSQL=1
      fi
      ;;

    python-postgres | postgres-python | psycopg2 | netdata-postgres)
      if [ "${pv}" -eq 2 ]; then
        PACKAGES_NETDATA_PYTHON=1
        PACKAGES_NETDATA_PYTHON_POSTGRES=1
      else
        PACKAGES_NETDATA_PYTHON3=1
        PACKAGES_NETDATA_PYTHON3_POSTGRES=1
      fi
      ;;

    python-pymongo)
      if [ "${pv}" -eq 2 ]; then
        PACKAGES_NETDATA_PYTHON=1
        PACKAGES_NETDATA_PYTHON_MONGO=1
      else
        PACKAGES_NETDATA_PYTHON3=1
        PACKAGES_NETDATA_PYTHON3_MONGO=1
      fi
      ;;

    nodejs | netdata-nodejs)
      PACKAGES_NETDATA=1
      PACKAGES_NETDATA_NODEJS=1
      PACKAGES_NETDATA_DATABASE=1
      ;;

    sensors | netdata-sensors)
      PACKAGES_NETDATA=1
      PACKAGES_NETDATA_PYTHON3=1
      PACKAGES_NETDATA_SENSORS=1
      PACKAGES_NETDATA_DATABASE=1
      ;;

    firehol | update-ipsets | firehol-all | fireqos)
      PACKAGES_IPRANGE=1
      PACKAGES_FIREHOL=1
      PACKAGES_FIREQOS=1
      PACKAGES_IPRANGE=1
      PACKAGES_UPDATE_IPSETS=1
      ;;

    demo | all)
      PACKAGES_NETDATA=1
      PACKAGES_NETDATA_NODEJS=1
      if [ "${pv}" -eq 2 ]; then
        PACKAGES_NETDATA_PYTHON=1
        PACKAGES_NETDATA_PYTHON_MYSQL=1
        PACKAGES_NETDATA_PYTHON_POSTGRES=1
        PACKAGES_NETDATA_PYTHON_MONGO=1
      else
        PACKAGES_NETDATA_PYTHON3=1
        PACKAGES_NETDATA_PYTHON3_MYSQL=1
        PACKAGES_NETDATA_PYTHON3_POSTGRES=1
        PACKAGES_NETDATA_PYTHON3_MONGO=1
      fi
      PACKAGES_DEBUG=1
      PACKAGES_IPRANGE=1
      PACKAGES_FIREHOL=1
      PACKAGES_FIREQOS=1
      PACKAGES_UPDATE_IPSETS=1
      PACKAGES_NETDATA_DEMO_SITE=1
      PACKAGES_NETDATA_DATABASE=1
      PACKAGES_NETDATA_EBPF=1
      ;;

    help | -h | --help)
      usage
      exit 1
      ;;

    *)
      echo >&2 "ERROR: Cannot understand option '${1}'"
      echo >&2
      usage
      exit 1
      ;;
  esac
  shift
done

# Check for missing core commands like grep, warn the user to install it and bail out cleanly
if ! command -v grep > /dev/null 2>&1; then
  echo >&2
  echo >&2 "ERROR: 'grep' is required for the install to run correctly and was not found on the system."
  echo >&2 "Please install grep and run the installer again."
  echo >&2
  exit 1
fi

if [ -z "${package_installer}" ] || [ -z "${tree}" ]; then
  if [ -z "${distribution}" ]; then
    # we dont know the distribution
    autodetect_distribution || user_picks_distribution
  fi

  # When no package installer is detected, try again from distro info if any
  if [ -z "${package_installer}" ]; then
    detect_package_manager_from_distribution "${distribution}"
  fi

  # Validate package manager trees
  validate_package_trees
fi

[ "${detection}" = "/etc/os-release" ] && cat << EOF

/etc/os-release information:
NAME            : ${NAME}
VERSION         : ${VERSION}
ID              : ${ID}
ID_LIKE         : ${ID_LIKE}
VERSION_ID      : ${VERSION_ID}
EOF

cat << EOF

We detected these:
Distribution    : ${distribution}
Version         : ${version}
Codename        : ${codename}
Package Manager : ${package_installer}
Packages Tree   : ${tree}
Detection Method: ${detection}
Default Python v: ${pv} $([ ${pv} -eq 2 ] && [ "${PACKAGES_NETDATA_PYTHON3}" -eq 1 ] && echo "(will install python3 too)")

EOF

mapfile -t PACKAGES_TO_INSTALL < <(packages | sort -u)

if [ ${#PACKAGES_TO_INSTALL[@]} -gt 0 ]; then
  echo >&2
  echo >&2 "The following command will be run:"
  echo >&2
  DRYRUN=1
  "${package_installer}" "${PACKAGES_TO_INSTALL[@]}"
  DRYRUN=0
  echo >&2
  echo >&2

  if [ "${DONT_WAIT}" -eq 0 ] && [ "${NON_INTERACTIVE}" -eq 0 ]; then
    read -r -p "Press ENTER to run it > " || exit 1
  fi

  "${package_installer}" "${PACKAGES_TO_INSTALL[@]}" || install_failed $?

  echo >&2
  echo >&2 "All Done! - Now proceed to the next step."
  echo >&2

else
  echo >&2
  echo >&2 "All required packages are already installed. Now proceed to the next step."
  echo >&2
fi

remote_log "OK"

exit 0