| 12
 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
 2176
 2177
 2178
 2179
 2180
 2181
 2182
 2183
 2184
 2185
 2186
 2187
 2188
 2189
 2190
 2191
 2192
 2193
 2194
 2195
 2196
 2197
 2198
 2199
 2200
 2201
 2202
 2203
 2204
 2205
 2206
 2207
 2208
 2209
 2210
 2211
 2212
 2213
 2214
 2215
 2216
 2217
 2218
 2219
 2220
 2221
 2222
 2223
 2224
 2225
 2226
 2227
 2228
 2229
 2230
 2231
 2232
 2233
 2234
 2235
 2236
 2237
 2238
 2239
 2240
 2241
 2242
 2243
 2244
 2245
 2246
 2247
 2248
 2249
 2250
 2251
 2252
 2253
 2254
 2255
 2256
 2257
 2258
 2259
 2260
 2261
 2262
 2263
 2264
 2265
 2266
 2267
 2268
 2269
 2270
 2271
 2272
 2273
 2274
 2275
 2276
 2277
 2278
 2279
 2280
 2281
 2282
 2283
 2284
 2285
 2286
 2287
 2288
 2289
 2290
 2291
 2292
 2293
 2294
 2295
 2296
 2297
 2298
 2299
 2300
 2301
 2302
 2303
 2304
 2305
 2306
 2307
 2308
 2309
 2310
 2311
 2312
 2313
 2314
 2315
 2316
 2317
 2318
 2319
 2320
 2321
 2322
 2323
 2324
 2325
 2326
 2327
 2328
 2329
 2330
 2331
 2332
 2333
 2334
 2335
 2336
 2337
 2338
 2339
 2340
 2341
 2342
 2343
 2344
 2345
 2346
 2347
 2348
 2349
 2350
 2351
 2352
 2353
 2354
 2355
 2356
 2357
 2358
 2359
 2360
 2361
 2362
 2363
 2364
 2365
 2366
 2367
 2368
 2369
 2370
 2371
 2372
 2373
 2374
 2375
 2376
 2377
 2378
 2379
 2380
 2381
 2382
 2383
 2384
 2385
 2386
 2387
 2388
 2389
 2390
 2391
 2392
 2393
 2394
 2395
 2396
 2397
 2398
 2399
 2400
 2401
 2402
 2403
 2404
 2405
 2406
 2407
 2408
 2409
 2410
 2411
 2412
 2413
 2414
 2415
 2416
 2417
 2418
 2419
 2420
 2421
 2422
 2423
 2424
 2425
 2426
 2427
 2428
 2429
 2430
 2431
 2432
 2433
 2434
 2435
 2436
 2437
 2438
 2439
 2440
 2441
 2442
 2443
 2444
 2445
 2446
 2447
 2448
 2449
 2450
 2451
 2452
 2453
 2454
 2455
 2456
 2457
 2458
 2459
 2460
 2461
 2462
 2463
 2464
 2465
 2466
 2467
 2468
 2469
 2470
 2471
 2472
 2473
 2474
 2475
 2476
 2477
 2478
 2479
 2480
 2481
 2482
 2483
 2484
 2485
 2486
 2487
 2488
 2489
 2490
 2491
 2492
 2493
 2494
 2495
 2496
 2497
 2498
 2499
 2500
 2501
 2502
 2503
 2504
 2505
 2506
 2507
 2508
 2509
 2510
 2511
 2512
 2513
 2514
 2515
 2516
 2517
 2518
 2519
 2520
 2521
 2522
 2523
 2524
 2525
 2526
 2527
 2528
 2529
 2530
 2531
 2532
 2533
 2534
 2535
 2536
 2537
 2538
 2539
 2540
 2541
 2542
 2543
 2544
 2545
 2546
 2547
 2548
 2549
 2550
 2551
 2552
 2553
 2554
 2555
 2556
 2557
 2558
 2559
 2560
 2561
 2562
 2563
 2564
 2565
 2566
 2567
 2568
 2569
 2570
 2571
 2572
 2573
 2574
 2575
 2576
 2577
 2578
 2579
 2580
 2581
 2582
 2583
 2584
 2585
 2586
 2587
 2588
 2589
 2590
 2591
 2592
 2593
 2594
 2595
 2596
 2597
 2598
 2599
 2600
 2601
 2602
 2603
 2604
 2605
 2606
 2607
 2608
 2609
 2610
 2611
 2612
 2613
 2614
 2615
 2616
 2617
 2618
 2619
 2620
 2621
 2622
 2623
 2624
 2625
 2626
 2627
 2628
 2629
 2630
 2631
 2632
 2633
 2634
 2635
 2636
 2637
 2638
 2639
 2640
 2641
 2642
 2643
 2644
 2645
 2646
 2647
 2648
 2649
 2650
 2651
 2652
 2653
 2654
 2655
 2656
 2657
 2658
 2659
 2660
 2661
 2662
 2663
 2664
 2665
 2666
 2667
 2668
 2669
 2670
 2671
 2672
 2673
 2674
 2675
 2676
 2677
 2678
 2679
 2680
 2681
 2682
 2683
 2684
 2685
 2686
 2687
 2688
 2689
 2690
 2691
 2692
 2693
 2694
 2695
 2696
 2697
 2698
 2699
 2700
 2701
 2702
 2703
 2704
 2705
 2706
 2707
 2708
 2709
 2710
 2711
 2712
 2713
 2714
 2715
 2716
 2717
 2718
 2719
 2720
 2721
 2722
 2723
 2724
 2725
 2726
 2727
 2728
 2729
 2730
 2731
 2732
 2733
 2734
 2735
 2736
 2737
 2738
 2739
 2740
 2741
 2742
 2743
 2744
 2745
 2746
 2747
 2748
 2749
 2750
 2751
 2752
 2753
 2754
 2755
 2756
 2757
 2758
 2759
 2760
 2761
 2762
 2763
 2764
 2765
 2766
 2767
 2768
 2769
 2770
 2771
 2772
 2773
 2774
 2775
 2776
 2777
 2778
 2779
 2780
 2781
 2782
 2783
 2784
 2785
 2786
 2787
 2788
 2789
 2790
 2791
 2792
 2793
 2794
 2795
 2796
 2797
 2798
 2799
 2800
 2801
 2802
 2803
 2804
 2805
 2806
 2807
 2808
 2809
 2810
 2811
 2812
 2813
 2814
 2815
 2816
 2817
 2818
 2819
 2820
 2821
 2822
 2823
 2824
 2825
 2826
 2827
 2828
 2829
 2830
 2831
 2832
 2833
 2834
 2835
 2836
 2837
 2838
 2839
 2840
 2841
 2842
 2843
 2844
 2845
 2846
 2847
 
 | %%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2012-2018. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%
%% Test suite for the systools module.
%%
%%	The systools module is a wrapper for a number of modules that
%%	handle large parts of the release building functionality
%%	(e.g. checking app files, building a tar file, building
%%	release upgrad scripts.
%%
-module(systools_SUITE).
-compile(export_all).
%%-define(debug, true).
-include_lib("common_test/include/ct.hrl").
-define(format(S, A), ok).
-define(datadir, ?config(data_dir, Config)).
-define(privdir, ?config(priv_dir, Config)).
-define(copydir, ?config(copy_dir, Config)).
-include_lib("kernel/include/file.hrl").
-import(lists, [foldl/3]).
-define(default_timeout, ?t:minutes(20)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
suite() ->
    [{ct_hooks, [ts_install_cth]}].
all() -> 
    [{group, script}, {group, tar}, {group, relup}, {group, hybrid},
     {group, options}].
groups() -> 
    [{script, [],
      [script_options, normal_script, unicode_script, no_mod_vsn_script,
       wildcard_script, variable_script, abnormal_script,
       no_sasl_script, no_dot_erlang_script,
       src_tests_script, crazy_script,
       included_script, included_override_script,
       included_fail_script, included_bug_script, exref_script,
       duplicate_modules_script,
       otp_3065_circular_dependenies, included_and_used_sort_script]},
     {tar, [],
      [tar_options, normal_tar, no_mod_vsn_tar, system_files_tar,
       system_src_file_tar, invalid_system_files_tar, variable_tar,
       src_tests_tar, var_tar, exref_tar, link_tar, no_sasl_tar,
       otp_9507_path_ebin]},
     {relup, [],
      [normal_relup, restart_relup, abnormal_relup, no_sasl_relup,
       no_appup_relup, bad_appup_relup, app_start_type_relup, regexp_relup
      ]},
     {hybrid, [], [normal_hybrid,hybrid_no_old_sasl,hybrid_no_new_sasl]},
     {options, [], [otp_6226_outdir,app_file_defaults]}].
init_per_group(_GroupName, Config) ->
    Config.
end_per_group(_GroupName, Config) ->
    Config.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
init_per_suite(Config) when is_list(Config) ->
    %% To use in end_per_testcase
    Path = code:get_path(),
    {ok,Cwd} = file:get_cwd(),
    %% Make of copy of the data directory.
    DataDir = ?datadir,
    PrivDir = ?privdir,
    CopyDir = fname(PrivDir, "datacopy"),
    ok = file:make_dir(CopyDir),
    TarFile = fname(PrivDir, "datacopy.tgz"),
    ok = file:set_cwd(DataDir),
    ok = erl_tar:create(TarFile, ["."], [compressed]),
    ok = erl_tar:extract(TarFile, [compressed, {cwd,CopyDir}]),
    ok = file:delete(TarFile),
    %% Compile source files in the copy directory.
    Sources = filelib:wildcard(fname([CopyDir,'*','*','*','*','*.erl'])),
    lists:foreach(fun compile_source/1, Sources),
    [{copy_dir, CopyDir}, {cwd,Cwd}, {path,Path} | Config].
compile_source(File) ->
    %% The compiler will no longer create a Beam file
    %% with a module name that does not match the output
    %% file, so we must compile to a binary and write
    %% the output file ourselves.
    U = filename:dirname(filename:dirname(File)),
    Base = filename:rootname(filename:basename(File)),
    OutFile = filename:join([U,"ebin",Base++".beam"]),
    OutFileTemp = OutFile ++ "#",
    {ok,_,Code} = compile:file(File, [binary]),
    ok = file:write_file(OutFileTemp, Code),
    file:rename(OutFileTemp, OutFile).
end_per_suite(Config) when is_list(Config) ->
    rh_test_lib:clean_dir(?privdir),
    Config.
init_per_testcase(link_tar, Config) ->
    case os:type() of
	{unix, _} -> init_per_testcase(dummy, Config);
	{win32, _} -> {skip, "Skip on windows"}
    end;
init_per_testcase(_Case, Config) ->
    Dog = test_server:timetrap(?default_timeout),
    [{watchdog, Dog}|Config].
end_per_testcase(Case, Config) ->
    try apply(?MODULE,Case,[cleanup,Config])
    catch error:undef -> ok
    end,
    Dog=?config(watchdog, Config),
    test_server:timetrap_cancel(Dog),
    case {?config(path,Config),?config(cwd,Config)} of
	{undefined,undefined} ->
	    ok;
	{Path,Cwd} ->
	    true = code:set_path(Path),
	    ok = file:set_cwd(Cwd)
    end,
    ok.
%% Usage:
%%    systools:make_script("RelName")
%%                        Make a boot file from RelName.rel.
%%                        Generates RelName.{script,boot}
%%    systools:make_tar("RelName")
%%                        Make a release package from RelName.rel.
%%                        Generates RelName.tar,Z
%%    systools:script2boot(File)
%%                        File.script -> File.boot
%%    systools:make_relup("Target", ["UpFromRel"...], ["DownToRel"...], Opts)
%%			  Gather all appup scripts to the relup file
%%
%% make_script: Check illegal script options
script_options(Config) when is_list(Config) ->
    {'EXIT',{{badarg,[{path,["Path",12,"Another"]}]}, _}} =
	(catch systools:make_script("release", [{path,["Path",12,"Another"]}])),
    {'EXIT',{{badarg,[sillent]}, _}} =
	(catch systools:make_script("release",
				    [{path,["Path","Another"]},sillent])),
    {'EXIT',{{badarg,[locall]}, _}} =
	(catch systools:make_script("release",
				    [{path,["Path","Another"]},locall])),
    {'EXIT',{{badarg,[src_testsxx]}, _}} =
	(catch systools:make_script("release",
				    [{path,["Path"]},src_testsxx])),
    {'EXIT',{{badarg,[{variables, {"TEST", "/home/lib"}}]}, _}} =
	(catch systools:make_script("release",
				    [{variables, {"TEST", "/home/lib"}}])),
    {'EXIT',{{badarg,[{variables, [{a, b}, {"a", "b"}]}]}, _}} =
	(catch systools:make_script("release",
				    [{variables, [{a, b}, {"a", "b"}]}])),
    {'EXIT',{{badarg,[exreff]}, _}} =
	(catch systools:make_script("release",
				    [{path,["Path","Another"]},exreff])),
    {'EXIT',{{badarg,[{exref,["appl"]}]}, _}} =
	(catch systools:make_script("release", [{exref,["appl"]}])),
    {'EXIT',{{badarg,[{machine, "appl"}]}, _}} =
	(catch systools:make_script("release", [{machine,"appl"}])),
    ok.
%% make_script: Check that normal case
normal_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    PSAVE = code:get_path(),		% Save path
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P1 = fname([LibDir, 'db-2.1', ebin]),
    P2 = fname([LibDir, 'fe-3.1', ebin]),
    true = code:add_patha(P1),
    true = code:add_patha(P2),
    ok = file:set_cwd(LatestDir),
    ok = systools:make_script(filename:basename(LatestName)),
    {ok, _} = read_script_file(LatestName),	% Check readabillity
    %% Check the same but w. silent flag
    {ok, _, []} = systools:make_script(LatestName, [silent]),
    {ok, _, []} = systools:make_script(LatestName, [silent,warnings_as_errors]),
    %% Use the local option
    ok = systools:make_script(LatestName, [local]),
    ok = check_script_path(LatestName),
    %% use the path option
    code:set_path(PSAVE),			% Restore path
    %% Mess up std path:
    true = code:add_patha(fname([LibDir, 'db-1.0', ebin])),
    true = code:add_patha(fname([LibDir, 'fe-2.1', ebin])),
    error = systools:make_script(LatestName),	%should fail
    ok = systools:make_script(LatestName,[{path, [P1, P2]}]),
    ok = file:set_cwd(OldDir),
    code:set_path(PSAVE),			% Restore path
    ok.
%% make_script: Test make_script with unicode .app file
unicode_script(Config) when is_list(Config) ->
    UnicodeStr = [945,946], % alhpa beta in greek letters
    {LatestDir, LatestName} = create_script({unicode,UnicodeStr},Config),
    DataDir = filename:absname(?copydir),
    UnicodeApp = fname([DataDir, "d_unicode", "lib", "ua-1.0"]),
    TarFile = fname(?privdir, "unicode_app.tgz"),
    {ok, Tar} = erl_tar:open(TarFile, [write, compressed]),
    ok = erl_tar:add(Tar, UnicodeApp, "ua-1.0", [compressed]),
    ok = erl_tar:close(Tar),
    UnicodeLibDir = fname([DataDir, "d_unicode", UnicodeStr]),
    P1 = fname([UnicodeLibDir, "ua-1.0", "ebin"]),
    %% Need to do this on a separate node to make sure it has unicode
    %% filename mode (+fnu*)
    {ok,HostStr} = inet:gethostname(),
    Host = list_to_atom(HostStr),
    {ok,Node} = ct_slave:start(Host,unicode_script_node,[{erl_flags,"+fnui"}]),
    ok = rpc:call(Node,erl_tar,extract,
		  [TarFile, [{cwd,UnicodeLibDir},compressed]]),
    true = rpc:call(Node,code,add_patha,[P1]),
    ok = rpc:call(Node,file,set_cwd,[LatestDir]),
    ok = rpc:call(Node,systools,make_script,[filename:basename(LatestName),
					     [local]]),
    {ok, Script} = rpc:call(Node,file,consult,[LatestName++".script"]),
    %% For debug purpose - print script to log
    io:format("~tp~n",[Script]),
    %% check that script contains unicode strings in
    %% 1. release version (set in ?MODULE:do_create_script)
    [{script,{"Test release",UnicodeStr},Instr}] = Script,
    %% 2. application description (set in ua.app in data dir)
    [AppInfo] = [X || {apply,{application,load,[{application,ua,X}]}} <- Instr],
    {description,UnicodeStr} = lists:keyfind(description,1,AppInfo),
    %% 3. path (directory name where unicode_app.tgz is extracted)
    true = lists:member({path,[P1]},Instr),
    %% If all is good, delete the unicode dir to avoid lingering files
    %% on windows.
    rpc:call(Node,code,add_pathz,[filename:dirname(code:which(?MODULE))]),
    rpc:call(Node,?MODULE,delete_tree,[UnicodeLibDir]),
    ok.
unicode_script(cleanup,Config) ->
    _ = ct_slave:stop(unicode_script_node),
    file:delete(fname(?privdir, "unicode_app.tgz")),
    ok.
%% make_script:
%% Modules specified without version in .app file (db-3.1).
%% Note that this is now the normal way - i.e. systools now ignores
%% the module versions in the .app file.
no_mod_vsn_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    PSAVE = code:get_path(),		% Save path
    {LatestDir, LatestName} = create_script(latest_no_mod_vsn,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P1 = fname([LibDir, 'db-3.1', ebin]),
    P2 = fname([LibDir, 'fe-3.1', ebin]),
    true = code:add_patha(P1),
    true = code:add_patha(P2),
    ok = file:set_cwd(LatestDir),
    ok = systools:make_script(filename:basename(LatestName)),
    {ok, _} = read_script_file(LatestName),	% Check readabillity
    %% Check the same but w. silent flag
    {ok, _, []} = systools:make_script(LatestName, [silent]),
    %% Use the local option
    ok = systools:make_script(LatestName, [local]),
    ok = check_script_path(LatestName),
    %% use the path option
    code:set_path(PSAVE),			% Restore path
    %% Mess up std path:
    true = code:add_patha(fname([LibDir, 'db-1.0', ebin])),
    true = code:add_patha(fname([LibDir, 'fe-2.1', ebin])),
    error = systools:make_script(LatestName),	%should fail
    ok = systools:make_script(LatestName,
			      [{path, [P1, P2]}]),
    ok = file:set_cwd(OldDir),
    code:set_path(PSAVE),			% Restore path
    ok.
%% make_script: Check that make_script handles wildcards in path.
wildcard_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    WildDir = fname([LibDir, '*', ebin]),
    ok = file:set_cwd(LatestDir),
    error = systools:make_script(filename:basename(LatestName)),
    ok = systools:make_script(LatestName,
			      [{path, [WildDir]}]),
    {ok, _} = read_script_file(LatestName),	% Check readabillity
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Add own installation dependent variable in script.
variable_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    ok = systools:make_script(LatestName,
			      [{path, P},
			       {variables, [{"TEST", LibDir}]}]),
    %% Check variables
    ok = check_var_script_file([fname(['$TEST', 'db-2.1', ebin]),
				fname(['$TEST', 'fe-3.1', ebin])],
			       P,
			       LatestName),
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Abnormal cases.
abnormal_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    ok = file:set_cwd(LatestDir),
    LibDir = fname([DataDir, d_bad_app_vsn, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    %% Check wrong app vsn
    error = systools:make_script(LatestName, [{path, P}]),
    {error,
     systools_make,
     [{error_reading, {db, {no_valid_version,
			    {{"should be","2.1"},
			     {"found file", _, "2.0"}}}}}]} =
	systools:make_script(LatestName, [silent, {path, P}]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Create script without sasl appl. Check warning.
no_sasl_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest1_no_sasl,Config),
    DataDir = filename:absname(?copydir),
    LibDir = [fname([DataDir, d_normal, lib])],
    P = [fname([LibDir, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _ , [{warning,missing_sasl}]} =
	systools:make_script(LatestName,[{path, P},silent]),
    {error, systools_make, {warnings_treated_as_errors,[missing_sasl]}} =
	systools:make_script(LatestName,[{path, P},silent,warnings_as_errors]),
    {ok, _ , []} =
	systools:make_script(LatestName,[{path, P},silent, no_warn_sasl]),
    {ok, _ , []} =
	systools:make_script(LatestName,[{path, P},silent, no_warn_sasl,
                                         warnings_as_errors]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Create script with no_dot_erlang. Check script contents.
no_dot_erlang_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest1_no_sasl,Config),
    DataDir = filename:absname(?copydir),
    LibDir = [fname([DataDir, d_normal, lib])],
    P = [fname([LibDir, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _ , []} =
	systools:make_script(LatestName,[{path, P},silent, no_warn_sasl]),
    {ok, [{_, _, LoadDotErlang}]} = read_script_file(LatestName),
    [erlangrc] = [E || {apply, {c, E, []}} <- LoadDotErlang],
    {ok, _ , []} =
	systools:make_script(LatestName,[{path, P},silent, no_warn_sasl, no_dot_erlang]),
    {ok, [{_, _, DoNotLoadDotErlang}]} = read_script_file(LatestName),
    [] = [E || {apply, {c, E, []}} <- DoNotLoadDotErlang],
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Do not check date of object file or that source code
%% can be found.
src_tests_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    PSAVE = code:get_path(),		% Save path
    {LatestDir, LatestName} = create_script(latest,Config),
    BootFile = LatestName ++ ".boot",
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_missing_src, lib]),
    P1 = fname([LibDir, 'db-2.1', ebin]),
    P2 = fname([LibDir, 'fe-3.1', ebin]),
    N = [P1, P2],
    ok = file:set_cwd(LatestDir),
    %% Manipulate the modification date of a beam file so it seems
    %% older than its .erl file
    Erl = filename:join([P1,"..","src","db1.erl"]),
    {ok, FileInfo=#file_info{mtime={{Y,M,D},T}}} = file:read_file_info(Erl),
    Beam = filename:join(P1,"db1.beam"),
    ok=file:write_file_info(Beam, FileInfo#file_info{mtime={{Y-1,M,D},T}}),
    %% Remove a .erl file
    Erl2 = filename:join([P1,"..","src","db2.erl"]),
    file:delete(Erl2),
    %% Then make script
    %% .boot file should not exist
    ok = file:delete(BootFile),
    false = filelib:is_regular(BootFile),
    %% With warnings_as_errors and src_tests option, an error should be issued
    {error, systools_make,
     {warnings_treated_as_errors, [{obj_out_of_date,_},
                                   {source_not_found,_}]}} =
	systools:make_script(LatestName, [silent, {path, N}, src_tests,
					  warnings_as_errors]),
    error =
	systools:make_script(LatestName, [{path, N}, src_tests,
					  warnings_as_errors]),
    %% due to warnings_as_errors .boot file should still not exist
    false = filelib:is_regular(BootFile),
    %% Two warnings should be issued when src_tests is given
    %% 1. old object code for db1.beam
    %% 2. missing source code for db2.beam
    {ok, _, [{warning,{obj_out_of_date,_}},
	     {warning,{source_not_found,_}}]} =
	systools:make_script(LatestName, [silent, {path, N}, src_tests]),
    %% .boot file should exist now
    true = filelib:is_regular(BootFile),
    %% Without the src_tests option, no warning should be issued
    {ok, _, []} =
	systools:make_script(LatestName, [silent, {path, N}]),
    %% Check that the old no_module_tests option (from the time when
    %% it was default to do the src_test) is ignored
    {ok, _, [{warning,{obj_out_of_date,_}},
	     {warning,{source_not_found,_}}]} =
	systools:make_script(LatestName, [silent,
					  {path, N},
					  no_module_tests,
					  src_tests]),
    ok = file:set_cwd(OldDir),
    code:set_path(PSAVE),
    ok.
%% make_script: Do the crazy cases.
crazy_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest, Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    %% Run with bad path
    error = systools:make_script(LatestName),
    {error, _, [{error_reading, _}, {error_reading, _}]} =
	systools:make_script(LatestName, [silent]),
    %% Run with .rel file lacking kernel
    {LatestDir2, LatestName2} = create_script(latest_nokernel, Config),
    ok = file:set_cwd(LatestDir2),
    error = systools:make_script(LatestName2),
    {error, _, {missing_mandatory_app,kernel}} =
	systools:make_script(LatestName2, [silent,{path,P}]),
    %% Run with .rel file with non-permanent kernel
    {LatestDir3, LatestName3} = create_script(latest_kernel_start_type, Config),
    ok = file:set_cwd(LatestDir3),
    error = systools:make_script(LatestName3),
    {error, _, {mandatory_app,kernel,load}} =
	systools:make_script(LatestName3, [silent,{path,P}]),
    %% Run with .rel file with non-permanent stdlib
    {LatestDir4, LatestName4} = create_script(latest_stdlib_start_type, Config),
    ok = file:set_cwd(LatestDir4),
    error = systools:make_script(LatestName4),
    {error, _, {mandatory_app,stdlib,load}} =
	systools:make_script(LatestName4, [silent,{path,P}]),
    %% Run with .rel file lacking stdlib
    {LatestDir5, LatestName5} = create_script(latest_no_stdlib, Config),
    ok = file:set_cwd(LatestDir5),
    error = systools:make_script(LatestName5),
    {error, _, {missing_mandatory_app,stdlib}} =
	systools:make_script(LatestName5, [silent,{path,P}]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Check that make_script handles generation of script
%% for applications with included applications.
included_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_include_files(inc1, Config),
    ok = file:set_cwd(LatestDir),
    ok = systools:make_script(LatestName),
    ok = check_include_script(LatestName,
			      [t1, t2, t3, t5, t4, t6],
			      [t1, t3, t6]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Check that make_script handles generation of script
%% for applications with included applications which are override by
%% the .rel file.
included_override_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_include_files(inc2, Config),
    ok = file:set_cwd(LatestDir),
    ok = systools:make_script(LatestName),
    ok = check_include_script(LatestName,
			      [t1, t2, t3, t4, t6, t5],
			      [t1, t3, t6, t5]),
    {_, LatestName1} = create_include_files(inc3, Config),
    ok = systools:make_script(LatestName1),
    ok = check_include_script(LatestName1,
			      [t3, t5, t4, t6, t1, t2],
			      [t3, t6, t1, t2]),
    {_, LatestName2} = create_include_files(inc4, Config),
    ok = systools:make_script(LatestName2),
    ok = check_include_script(LatestName2,
			      [t3, t4, t6, t5, t1, t2],
			      [t3, t6, t5, t1, t2]),
    {_, LatestName3} = create_include_files(inc5, Config),
    ok = systools:make_script(LatestName3),
    ok = check_include_script(LatestName3,
			      [t3, t4, t6, t1, t2],
			      [t3, t6, t1, t2]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Check that make_script handles errors then generating
%% script with included applications.
included_fail_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_include_files(inc6, Config),
    ok = file:set_cwd(LatestDir),
    {error, _, {undefined_applications,[t2]}} =
	systools:make_script(LatestName, [silent]),
    {_, LatestName1} = create_include_files(inc7, Config),
    {error, _, {duplicate_include,[{{t5,t7,_,_},{t5,t6,_,_}}]}} =
	systools:make_script(LatestName1, [silent]),
    {_, LatestName3} = create_include_files(inc9, Config),
    {error, _, {circular_dependencies,[{t10,_},{t8,_}]}} =
	systools:make_script(LatestName3, [silent]),
    {_, LatestName4} = create_include_files(inc10, Config),
    {error, _, [{error_reading,{t9,{override_include,[t7]}}}]} =
	systools:make_script(LatestName4, [silent]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Check that make_script handles generation of script
%% with difficult dependency for included applications.
included_bug_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_include_files(inc11, Config),
    ok = file:set_cwd(LatestDir),
    ok = systools:make_script(LatestName),
    ok = check_include_script(LatestName,
			      [t13, t11, t12],
			      [t11, t12]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Circular dependencies in systools:make_script().
otp_3065_circular_dependenies(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} =
	create_include_files(otp_3065_circular_dependenies, Config),
    ok = file:set_cwd(LatestDir),
    ok = systools:make_script(LatestName),
    ok = check_include_script(LatestName,
			      [aa12, chAts, chTraffic],
			      [chTraffic]),
    ok = file:set_cwd(OldDir),
    ok.
%% Test sorting of included applications and used applications
included_and_used_sort_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir1, LatestName1} = create_include_files(sort_apps, Config),
    ok = file:set_cwd(LatestDir1),
    ok = systools:make_script(LatestName1),
    ok = check_include_script(LatestName1,
			      [t20,t19,t18,t17,t16,t15,t14],[t20,t19,t18,t14]),
    {LatestDir2, LatestName2} = create_include_files(sort_apps_rev, Config),
    ok = file:set_cwd(LatestDir2),
    ok = systools:make_script(LatestName2),
    ok = check_include_script(LatestName2,
			      [t18,t19,t20,t15,t16,t17,t14],[t18,t19,t20,t14]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_script: Check that make_script exref option works.
exref_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    PSAVE = code:get_path(),		% Save path
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _, []} = systools:make_script(LatestName, [{path,P}, silent]),
    %% Complete exref
    {ok, _, W1} =
	systools:make_script(LatestName, [exref, {path,P}, silent]),
    check_exref_warnings(with_db1, W1),
    {ok, _} = read_script_file(LatestName),	% Check readabillity
    %% Only exref the db application.
    {ok, _, W2} =
	systools:make_script(LatestName, [{exref,[db]}, {path,P}, silent]),
    check_exref_warnings(with_db1, W2),
    {ok, _} = read_script_file(LatestName),	% Check readabillity
    %% Only exref the fe application.
    {ok, _, W3} =
	systools:make_script(LatestName, [{exref,[fe]}, {path,P}, silent]),
    check_exref_warnings(without_db1, W3),
    {ok, _} = read_script_file(LatestName),	% Check readabillity
    %% exref the db and stdlib applications.
    {ok, _, W4} =
	systools:make_script(LatestName, [{exref,[db,stdlib]}, {path,P}, silent]),
    check_exref_warnings(with_db1, W4),
    {ok, _} = read_script_file(LatestName),	% Check readabillity
    ok = file:set_cwd(OldDir),
    code:set_path(PSAVE),			% Restore path
    ok.
check_exref_warnings(with_db1, W) ->
    case get_exref(undef, W) of
	{ok, [{db2,non_existing_func,0},
	      {fe2,non_existing_func,0},
	      {lists,non_existing_func,1}]} ->
	    ok;
	{ok, L} ->
	    test_server:fail({exref_warning_undef, L});
	_E ->
	    test_server:fail({bad_undef,_E})
    end;
check_exref_warnings(without_db1, W) ->
    case get_exref(undef, W) of
	false ->
	    ok;
	{ok, L} ->
	    test_server:fail({exref_warning_undef, L})
    end.
get_exref(undef, W)   -> filter(no_hipe(get_exref1(exref_undef, W))).
filter(false) ->
    false;
filter({ok, W}) ->
    {ok, filter(W)};
filter(L) ->
    lists:filter(fun%({hipe_consttab,_,_}) -> false;
		     ({int,_,_}) -> false;
		     ({i,_,_}) -> false;
		     ({crypto,_,_}) -> false;
		     (_) -> true
		end,
		 L).
get_exref1(T, [{warning, {T, Value}}|_]) -> {ok, Value};
get_exref1(T, [_|W])                     -> get_exref1(T, W);
get_exref1(_, [])                        -> false.
no_hipe(false) ->
    false;
no_hipe({ok, Value}) ->
    case erlang:system_info(hipe_architecture) of
	undefined ->
	    Hipe = "hipe",
	    Fun = fun({M,_,_}) -> not lists:prefix(Hipe, atom_to_list(M)) end,
	    NewValue = lists:filter(Fun, Value),
	    {ok, NewValue};
	_Arch ->
	    {ok, Value}
    end.
%% duplicate_modules_script: Check that make_script rejects two
%% applications providing the same module.
duplicate_modules_script(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(duplicate_modules,Config),
    DataDir = filename:absname(?copydir),
    ok = file:set_cwd(LatestDir),
    LibDir = fname([DataDir, d_duplicate_modules, lib]),
    P = [fname([LibDir, 'app1-1.0', ebin]),
	 fname([LibDir, 'app2-1.0', ebin])],
    %% Check wrong app vsn
    error = systools:make_script(LatestName, [{path, P}]),
    {error,
      systools_make,
      {duplicate_modules, [
          {{myapp,app1,_}, {myapp,app2,_}}
        ]
      }
    } = systools:make_script(LatestName, [silent, {path, P}]),
    ok = file:set_cwd(OldDir),
    ok.
%% tar_options: Check illegal tar options.
tar_options(Config) when is_list(Config) ->
    {'EXIT',{{badarg,[{path,["Path",12,"Another"]}]}, _}} =
	(catch systools:make_tar("release", [{path,["Path",12,"Another"]}])),
    {'EXIT',{{badarg,[sillent]}, _}} =
	(catch systools:make_tar("release",
				 [{path,["Path","Another"]},sillent])),
    {'EXIT',{{badarg,[{dirs,["dirs"]}]}, _}} =
	(catch systools:make_tar("release", [{dirs, ["dirs"]}])),
    {'EXIT',{{badarg,[{erts, illegal}]}, _}} =
	(catch systools:make_tar("release", [{erts, illegal}])),
    {'EXIT',{{badarg,[src_testsxx]}, _}} =
	(catch systools:make_tar("release",
				 [{path,["Path"]},src_testsxx])),
    {'EXIT',{{badarg,[{variables, [{a, b}, {"a", "b"}]}]}, _}} =
	(catch systools:make_tar("release",
				 [{variables, [{a, b}, {"a", "b"}]}])),
    {'EXIT',{{badarg,[{var_tar, illegal}]}, _}} =
	(catch systools:make_tar("release", [{var_tar, illegal}])),
    {'EXIT',{{badarg,[exreff]}, _}} =
	(catch systools:make_tar("release",
				 [{path,["Path","Another"]},exreff])),
    {'EXIT',{{badarg,[{exref,["appl"]}]}, _}} =
	(catch systools:make_tar("release", [{exref,["appl"]}])),
    {'EXIT',{{badarg,[{machine, "appl"}]}, _}} =
	(catch systools:make_tar("release", [{machine,"appl"}])),
    ok.
%% make_tar: Check normal case
normal_tar(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _, []} = systools:make_script(LatestName, [silent, {path, P}]),
    ok = systools:make_tar(LatestName, [{path, P}]),
    ok = check_tar(fname([lib,'db-2.1',ebin,'db.app']), LatestName),
    {ok, _, []} = systools:make_tar(LatestName, [{path, P}, silent]),
    ok = check_tar(fname([lib,'fe-3.1',ebin,'fe.app']), LatestName),
    ok = file:set_cwd(OldDir),
    ok.
%% make_tar: Modules specified without version in .app file (db-3.1).
%% Note that this is now the normal way - i.e. systools now ignores
%% the module versions in the .app file.
no_mod_vsn_tar(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest_no_mod_vsn,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-3.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _, []} = systools:make_script(LatestName, [silent, {path, P}]),
    ok = systools:make_tar(LatestName, [{path, P}]),
    ok = check_tar(fname([lib,'db-3.1',ebin,'db.app']), LatestName),
    {ok, _, []} = systools:make_tar(LatestName, [{path, P}, silent]),
    ok = check_tar(fname([lib,'fe-3.1',ebin,'fe.app']), LatestName),
    ok = file:set_cwd(OldDir),
    ok.
%% make_tar: Check that relup or sys.config are included if they exist
system_files_tar(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    %% Add dummy relup and sys.config
    ok = file:write_file("sys.config","[].\n"),
    ok = file:write_file("relup","{\"LATEST\",[],[]}.\n"),
    {ok, _, []} = systools:make_script(LatestName, [silent, {path, P}]),
    ok = systools:make_tar(LatestName, [{path, P}]),
    ok = check_tar(fname(["releases","LATEST","sys.config"]), LatestName),
    ok = check_tar(fname(["releases","LATEST","relup"]), LatestName),
    {ok, _, []} = systools:make_tar(LatestName, [{path, P}, silent]),
    ok = check_tar(fname(["releases","LATEST","sys.config"]), LatestName),
    ok = check_tar(fname(["releases","LATEST","relup"]), LatestName),
    ok = file:set_cwd(OldDir),
    ok.
system_files_tar(cleanup,Config) ->
    Dir = ?privdir,
    file:delete(filename:join(Dir,"sys.config")),
    file:delete(filename:join(Dir,"relup")),
    ok.
%% make_tar: Check that sys.config.src and not sys.config is included
system_src_file_tar(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
         fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    %% Add dummy sys.config and sys.config.src
    ok = file:write_file("sys.config.src","[${SOMETHING}].\n"),
    ok = file:write_file("sys.config","[].\n"),
    {ok, _, _} = systools:make_script(LatestName, [silent, {path, P}]),
    ok = systools:make_tar(LatestName, [{path, P}]),
    ok = check_tar(fname(["releases","LATEST","sys.config.src"]), LatestName),
    {error, _} = check_tar(fname(["releases","LATEST","sys.config"]), LatestName),
    {ok, _, _} = systools:make_tar(LatestName, [{path, P}, silent]),
    ok = check_tar(fname(["releases","LATEST","sys.config.src"]), LatestName),
    {error, _} = check_tar(fname(["releases","LATEST","sys.config"]), LatestName),
    ok = file:set_cwd(OldDir),
    ok.
system_src_file_tar(cleanup,Config) ->
    Dir = ?privdir,
    file:delete(filename:join(Dir,"sys.config")),
    file:delete(filename:join(Dir,"sys.config.src")),
    ok.
%% make_tar: Check that make_tar fails if relup or sys.config exist
%% but do not have valid content
invalid_system_files_tar(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _, []} = systools:make_script(LatestName, [silent, {path, P}]),
    %% Add dummy relup and sys.config - faulty sys.config
    ok = file:write_file("sys.config","[]\n"), %!!! syntax error - missing '.'
    ok = file:write_file("relup","{\"LATEST\",[],[]}.\n"),
    error = systools:make_tar(LatestName, [{path, P}]),
    {error,_,{tar_error,{add,"sys.config",[{error,_}]}}} =
	systools:make_tar(LatestName, [{path, P}, silent]),
    %% Add dummy relup and sys.config - faulty sys.config
    ok = file:write_file("sys.config","[x,y].\n"), %!!! faulty format
    ok = file:write_file("relup","{\"LATEST\",[],[]}.\n"),
    error = systools:make_tar(LatestName, [{path, P}]),
    {error,_,{tar_error,{add,"sys.config",[invalid_format]}}} =
	systools:make_tar(LatestName, [{path, P}, silent]),
    %% Add dummy relup and sys.config - faulty relup
    ok = file:write_file("sys.config","[]\n"),
    ok = file:write_file("relup","{\"LATEST\"\n"), %!!! syntax error - truncated
    error = systools:make_tar(LatestName, [{path, P}]),
    {error,_,{tar_error,{add,"relup",[{error,_}]}}} =
	systools:make_tar(LatestName, [{path, P}, silent]),
    %% Add dummy relup and sys.config - faulty relup
    ok = file:write_file("sys.config","[]\n"),
    ok = file:write_file("relup","[].\n"), %!!! faulty format
    error = systools:make_tar(LatestName, [{path, P}]),
    {error,_,{tar_error,{add,"relup",[invalid_format]}}} =
	systools:make_tar(LatestName, [{path, P}, silent]),
    ok = file:set_cwd(OldDir),
    ok.
invalid_system_files_tar(cleanup,Config) ->
    Dir = ?privdir,
    file:delete(filename:join(Dir,"sys.config")),
    file:delete(filename:join(Dir,"relup")),
    ok.
%% make_tar: Use variable and create separate tar (included in generated tar).
variable_tar(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _, []} = systools:make_script(LatestName,
				      [silent,
				       {path, P},
				       {variables,[{"TEST", LibDir}]}]),
    ok = systools:make_tar(LatestName, [{path, P},
					{variables,[{"TEST", LibDir}]}]),
    ok = check_var_tar("TEST", LatestName),
    {ok, _, []} = systools:make_tar(LatestName,
				   [{path, P}, silent,
				    {variables,[{"TEST", LibDir}]}]),
    ok = check_var_tar("TEST", LatestName),
    ok = file:set_cwd(OldDir),
    ok.
%% make_tar: Check that symlinks in applications are handled correctly.
link_tar(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_links, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    %% Make some links
    Db1Erl = fname(['db-2.1',src,'db1.erl']),
    NormalDb1Erl = fname([DataDir,d_normal,lib,Db1Erl]),
    LinkDb1Erl = fname([LibDir, Db1Erl]),
    ok = file:make_symlink(NormalDb1Erl, LinkDb1Erl),
    Db1Beam = fname(['db-2.1',ebin,'db1.beam']),
    NormalDb1Beam = fname([DataDir,d_normal,lib,Db1Beam]),
    LinkDb1Beam = fname([LibDir, Db1Beam]),
    ok = file:make_symlink(NormalDb1Beam, LinkDb1Beam),
    FeApp = fname(['fe-3.1',ebin,'fe.app']),
    NormalFeApp = fname([DataDir,d_normal,lib,FeApp]),
    LinkFeApp = fname([LibDir, FeApp]),
    ok = file:make_symlink(NormalFeApp, LinkFeApp),
    %% Create the tar and check that the linked files are included as
    %% regular files
    ok = file:set_cwd(LatestDir),
    {ok,_,[]} = systools:make_script(LatestName, [{path, P},silent]),
    {ok, _, []} = systools:make_tar(LatestName, [{path, P}, silent]),
    ok = check_tar_regular(?privdir,
			   [fname([lib,FeApp]),
			    fname([lib,Db1Beam])],
			   LatestName),
    {ok, _, []} = systools:make_tar(LatestName, [{path, P}, silent,
						 {dirs, [src]}]),
    ok = check_tar_regular(?privdir,
			   [fname([lib,FeApp]),
			    fname([lib,Db1Beam]),
			    fname([lib,Db1Erl])],
			   LatestName),
    ok = file:set_cwd(OldDir),
    ok.
%% make_tar: Do not check date of object file or that source code can be found.
src_tests_tar(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_missing_src, lib]),
    P1 = fname([LibDir, 'db-2.1', ebin]),
    P2 = fname([LibDir, 'fe-3.1', ebin]),
    P = [P1, P2],
    ok = file:set_cwd(LatestDir),
    %% Manipulate the modification date of a beam file so it seems
    %% older than the .erl file
    Erl = filename:join([P1,"..","src","db1.erl"]),
    {ok, FileInfo=#file_info{mtime={{Y,M,D},T}}} = file:read_file_info(Erl),
    Beam = filename:join(P1,"db1.beam"),
    ok = file:write_file_info(Beam, FileInfo#file_info{mtime={{Y-1,M,D},T}}),
    %% Remove a .erl file
    Erl2 = filename:join([P1,"..","src","db2.erl"]),
    file:delete(Erl2),
    ok = systools:make_script(LatestName, [{path, P}]),
    %% Then make tar - two warnings should be issued when
    %% src_tests is given
    %% 1. old object code for db1.beam
    %% 2. missing source code for db2.beam
    {ok, _, [{warning,{obj_out_of_date,_}},
	     {warning,{source_not_found,_}}]} =
	systools:make_tar(LatestName, [{path, P}, silent,
				       {dirs, [src]},
				       src_tests]),
    ok = check_tar(fname([lib,'db-2.1',src,'db1.erl']), LatestName),
    %% Without the src_tests option, no warning should be issued
    {ok, _, []} = systools:make_tar(LatestName, [{path, P}, silent,
						 {dirs, [src]}]),
    ok = check_tar(fname([lib,'db-2.1',src,'db1.erl']), LatestName),
    %% Check that the old no_module_tests option (from the time when
    %% it was default to do the src_test) is ignored
    {ok, _, [{warning,{obj_out_of_date,_}},
	     {warning,{source_not_found,_}}]} =
	systools:make_tar(LatestName, [{path, P}, silent,
				       {dirs, [src]},
				       no_module_tests,
				       src_tests]),
    ok = check_tar(fname([lib,'db-2.1',src,'db1.erl']), LatestName),
    ok = file:set_cwd(OldDir),
    ok.
%% make_tar: Check that make_tar handles generation and placement of
%% tar files for variables outside the main tar file.
%% Test the {var_tar, include | ownfile | omit} optio.
var_tar(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    PSAVE = code:get_path(),		% Save path
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _, []} = systools:make_script(LatestName,
				      [silent,
				       {path, P},
				       {variables,[{"TEST", LibDir}]}]),
    ok = systools:make_tar(LatestName, [{path, P},
					{var_tar, ownfile},
					{variables,[{"TEST", LibDir}]}]),
    true = exists_tar_file("TEST"), %% Also removes the file !
    {error, {not_generated, _}} = check_var_tar("TEST", LatestName),
    ok = systools:make_tar(LatestName, [{path, P},
					{var_tar, omit},
					{variables,[{"TEST", LibDir}]}]),
    {error, {not_generated, _}} = check_var_tar("TEST", LatestName),
    false = exists_tar_file("TEST"),
    ok = systools:make_tar(LatestName, [{path, P},
					{var_tar, include},
					{variables,[{"TEST", LibDir}]}]),
    ok = check_var_tar("TEST", LatestName),
    false = exists_tar_file("TEST"),
    ok = file:set_cwd(OldDir),
    code:set_path(PSAVE),
    ok.
%% make_tar: Check exref option.
exref_tar(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'fe-3.1', ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _, []} = systools:make_script(LatestName, [silent, {path, P}]),
    %% Complete exref
    {ok, _, W1} =
	systools:make_tar(LatestName, [exref, {path, P}, silent]),
    check_exref_warnings(with_db1, W1),
    ok = check_tar(fname([lib,'db-2.1',ebin,'db.app']), LatestName),
    %% Only exref the db application.
    {ok, _, W2} =
	systools:make_tar(LatestName, [{exref, [db]}, {path, P}, silent]),
    check_exref_warnings(with_db1, W2),
    ok = check_tar(fname([lib,'fe-3.1',ebin,'fe.app']), LatestName),
    %% Only exref the fe application.
    {ok, _, W3} =
	systools:make_tar(LatestName, [{exref, [fe]}, {path, P}, silent]),
    check_exref_warnings(without_db1, W3),
    ok = check_tar(fname([lib,'db-2.1',ebin,'db.app']), LatestName),
    %% exref the db and stdlib applications.
    {ok, _, W4} =
	systools:make_tar(LatestName, [{exref, [db, stdlib]},
				       {path, P}, silent]),
    check_exref_warnings(with_db1, W4),
    ok = check_tar(fname([lib,'fe-3.1',ebin,'fe.app']), LatestName),
    ok = file:set_cwd(OldDir),
    ok.
%% make_tar:  Create tar without sasl appl. Check warning.
no_sasl_tar(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest1_no_sasl,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(LatestDir),
    {ok, _, _} = systools:make_script(LatestName, [silent, {path, P}]),
    ok = systools:make_tar(LatestName, [{path, P}]),
    {ok, _, [{warning,missing_sasl}]} =
        systools:make_tar(LatestName, [{path, P}, silent]),
    {ok, _, []} =
        systools:make_tar(LatestName, [{path, P}, silent, no_warn_sasl]),
    {ok, _, []} =
        systools:make_tar(LatestName, [{path, P}, silent, no_warn_sasl,
                                       warnings_as_errors]),
    TarFile = LatestName ++ ".tar.gz",
    true = filelib:is_regular(TarFile),
    ok = file:delete(TarFile),
    {error, systools_make, {warnings_treated_as_errors,[missing_sasl]}} =
        systools:make_tar(LatestName, [{path, P}, silent, warnings_as_errors]),
    error =
        systools:make_tar(LatestName, [{path, P}, warnings_as_errors]),
    false = filelib:is_regular(TarFile),
    ok = file:set_cwd(OldDir),
    ok.
%% make_tar: OTP-9507 - make_tar failed when path given as just 'ebin'.
otp_9507_path_ebin(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest_small,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    FeDir = fname([LibDir, 'fe-3.1']),
    ok = file:set_cwd(FeDir),
    RelName = fname([LatestDir,LatestName]),
    P1 = ["./ebin",
	  fname([DataDir, lib, kernel, ebin]),
	  fname([DataDir, lib, stdlib, ebin]),
	  fname([DataDir, lib, sasl, ebin])],
    {ok, _, []} = systools:make_script(RelName, [silent, {path, P1}]),
    ok = systools:make_tar(RelName, [{path, P1}]),
    Content1 = tar_contents(RelName),
    P2 = ["ebin",
	  fname([DataDir, lib, kernel, ebin]),
	  fname([DataDir, lib, stdlib, ebin]),
	  fname([DataDir, lib, sasl, ebin])],
    %% Tickets solves the following line - it used to fail with
    %% {function_clause,[{filename,join,[[]]},...}
    ok = systools:make_tar(RelName, [{path, P2}]),
    Content2 = tar_contents(RelName),
    true = (Content1 == Content2),
    ok = file:set_cwd(OldDir),
    ok.
%% make_relup: Check normal case
normal_relup(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir,LatestName}   = create_script(latest0,Config),
    {_LatestDir1,LatestName1} = create_script(latest1,Config),
    {_LatestDir2,LatestName2} = create_script(latest2,Config),
    DataDir = filename:absname(?copydir),
    LibDir = [fname([DataDir, d_normal, lib])],
    P = [fname([LibDir, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(LatestDir),
    %% This is the ultra normal case
    ok = systools:make_relup(LatestName, [LatestName1], [LatestName1],
			     [{path, P}]),
    ok = check_relup([{db, "2.1"}], [{db, "1.0"}]),
    {ok, Relup, _, []} =
	systools:make_relup(LatestName, [LatestName1], [LatestName1],
			    [{path, P}, silent]),
    ok = check_relup([{db, "2.1"}], [{db, "1.0"}]),
    %% file should not be written if warnings_as_errors is enabled.
    %% delete before running tests.
    ok = file:delete("relup"),
    %% Check that warnings are treated as errors
    error =
	systools:make_relup(LatestName, [LatestName2], [LatestName1],
			    [{path, P}, warnings_as_errors]),
    {error, systools_relup,
     {warnings_treated_as_errors,[pre_R15_emulator_upgrade,
                                  {erts_vsn_changed, _}]}} =
	systools:make_relup(LatestName, [LatestName2], [LatestName1],
			    [{path, P}, silent, warnings_as_errors]),
    %% relup file should not exist
    false = filelib:is_regular("relup"),
    %% Check that warnings get through
    ok = systools:make_relup(LatestName, [LatestName2], [LatestName1],
			     [{path, P}]),
    ok = check_relup([{fe, "3.1"}, {db, "2.1"}], [{db, "1.0"}]),
    {ok, _, _, [pre_R15_emulator_upgrade,{erts_vsn_changed, _}]} =
	systools:make_relup(LatestName, [LatestName2], [LatestName1],
			    [{path, P}, silent]),
    ok = check_relup([{fe, "3.1"}, {db, "2.1"}], [{db, "1.0"}]),
    %% relup file should exist now
    true = filelib:is_regular("relup"),
    %% file should not be written if noexec option is used.
    %% delete before running tests.
    ok = file:delete("relup"),
    {ok,Relup,_,[]} =
        systools:make_relup(LatestName, [LatestName1], [LatestName1],
                            [{path, P}, noexec]),
    false = filelib:is_regular("relup"),
    ok = file:set_cwd(OldDir),
    ok.
%% make_relup: Test relup which includes emulator restart.
restart_relup(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir,LatestName}   = create_script(latest0,Config),
    {_LatestDir1,LatestName1} = create_script(latest1,Config),
    {_LatestDir0CurrErts,LatestName0CurrErts} =
	create_script(latest0_current_erts,Config),
    {_CurrentAllDir,CurrentAllName} = create_script(current_all,Config),
    {_CurrentAllFutErtsDir,CurrentAllFutErtsName} =
	create_script(current_all_future_erts,Config),
    {_CurrentAllFutSaslDir,CurrentAllFutSaslName} =
	create_script(current_all_future_sasl,Config),
    DataDir = filename:absname(?copydir),
    LibDir = [fname([DataDir, d_normal, lib])],
    P = [fname([LibDir, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin]),
	 fname([DataDir, lib, 'sasl-9.9', ebin])],
    ok = file:set_cwd(LatestDir),
    %% OTP-2561: Check that the option 'restart_emulator' generates a
    %% "restart_emulator" instruction.
    {ok, _ , _, []} =
	systools:make_relup(LatestName, [LatestName1], [LatestName1],
			    [{path, P},restart_emulator,silent]),
    ok = check_relup([{db, "2.1"}], [{db, "1.0"}]),
    ok = check_restart_emulator(),
    %% Pre-R15 to Post-R15 upgrade
    {ok, _ , _, Ws} =
	systools:make_relup(LatestName0CurrErts,
			    [LatestName1],
			    [LatestName1],
			    [{path, P},silent]),
    ok = check_relup([{db,"2.1"}], [{db, "1.0"}]),
    ok = check_pre_to_post_r15_restart_emulator(),
    ok = check_pre_to_post_r15_warnings(Ws),
    %% Check that new sasl version generates a restart_new_emulator
    %% instruction
    {ok, _ , _, []} =
	systools:make_relup(CurrentAllFutSaslName,
			    [CurrentAllName],
			    [CurrentAllName],
			    [{path, P},silent]),
    ok = check_relup([{fe, "3.1"}], []),
    ok = check_restart_emulator_diff_coreapp(),
    %% Check that new erts version generates a restart_new_emulator
    %% instruction, if FromSaslVsn >= R15SaslVsn
    %% (One erts_vsn_changed warning for upgrade and one for downgrade)
    {ok, _ , _, [{erts_vsn_changed,_},{erts_vsn_changed,_}]} =
	systools:make_relup(CurrentAllFutErtsName,
			    [CurrentAllName],
			    [CurrentAllName],
			    [{path, P},silent]),
    ok = check_relup([{fe, "3.1"}], []),
    ok = check_restart_emulator_diff_coreapp(),
    %% Check that new erts version generates a restart_new_emulator
    %% instruction, and can be combined with restart_emulator opt.
    %% (One erts_vsn_changed warning for upgrade and one for downgrade)
    {ok, _ , _, [{erts_vsn_changed,_},{erts_vsn_changed,_}]} =
	systools:make_relup(CurrentAllFutErtsName,
			    [CurrentAllName],
			    [CurrentAllName],
			    [{path, P},restart_emulator,silent]),
    ok = check_relup([{fe, "3.1"}], []),
    ok = check_restart_emulator(),
    ok = check_restart_emulator_diff_coreapp(),
    ok = file:set_cwd(OldDir),
    ok.
%% This test fails if wrong version numbers are seen in the relup file
%% or if any application is missing. This was triggered by OTP-1360.
check_relup(UpVsnL, DnVsnL) ->
    {ok, [{_V1, [{_, _, Up}], [{_, _, Dn}]}]} = file:consult(relup),
    [] = foldl(fun(X, Acc) ->
		       true = lists:member(X, Acc),
		       lists:delete(X, Acc) end,
	       UpVsnL,
	       [{App, Vsn} || {load_object_code,{App,Vsn,_}} <- Up]),
    [] = foldl(fun(X, Acc) ->
		       true = lists:member(X, Acc),
		       lists:delete(X, Acc) end,
	       DnVsnL,
	       [{App, Vsn} || {load_object_code,{App,Vsn,_}} <- Dn]),
    ok.
check_relup_up_only(UpVsnL) ->
    {ok, [{_V1, [{_, _, Up}], []}]} = file:consult(relup),
    [] = foldl(fun(X, Acc) ->
		       true = lists:member(X, Acc),
		       lists:delete(X, Acc) end,
	       UpVsnL,
	       [{App, Vsn} || {load_object_code,{App,Vsn,_}} <- Up]),
    ok.
check_restart_emulator() ->
    {ok, [{_V1, [{_, _, Up}], [{_, _, Dn}]}]} = file:consult(relup),
    restart_emulator = lists:last(Up),
    restart_emulator = lists:last(Dn),
    ok.
check_restart_emulator_up_only() ->
    {ok, [{_V1, [{_, _, Up}], []}]} = file:consult(relup),
    restart_emulator = lists:last(Up),
    ok.
check_restart_emulator_diff_coreapp() ->
    {ok, [{_V1, [{_, _, Up}], [{_, _, Dn}]}]} = file:consult(relup),
    [restart_new_emulator|_] = Up,
    restart_emulator = lists:last(Dn),
    ok.
check_pre_to_post_r15_restart_emulator() ->
    {ok, [{_V1, [{_, _, Up}], [{_, _, Dn}]}]} = file:consult(relup),
    restart_new_emulator = lists:last(Up),
    restart_emulator = lists:last(Dn),
    ok.
check_pre_to_post_r15_warnings(Ws) ->
    true = lists:member(pre_R15_emulator_upgrade,Ws),
    ok.
%% make_relup: Check that appup files may be missing, but only if we
%% don't need them.
no_appup_relup(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir,LatestName}   = create_script(latest_small,Config),
    {_LatestDir0,LatestName0} = create_script(latest_small0,Config),
    {_LatestDir1,LatestName1} = create_script(latest_small1,Config),
    DataDir = filename:absname(?copydir),
    ok = file:set_cwd(LatestDir),
    %% Check that appup might be missing
    P1 = [fname([DataDir, d_no_appup, lib, 'fe-3.1', ebin]),
	  fname([DataDir, lib, kernel, ebin]),
	  fname([DataDir, lib, stdlib, ebin]),
	  fname([DataDir, lib, sasl, ebin])],
    ok =
	systools:make_relup(LatestName, [LatestName], [], [{path, P1}]),
    {ok,_, _, []} =
	systools:make_relup(LatestName, [LatestName], [],
			    [silent, {path, P1}]),
    %% Check that appup might NOT be missing when we need it
    P2 = [fname([DataDir, d_no_appup, lib, 'fe-3.1', ebin]),
	  fname([DataDir, d_no_appup, lib, 'fe-2.1', ebin]),
	  fname([DataDir, lib, kernel, ebin]),
	  fname([DataDir, lib, stdlib, ebin]),
	  fname([DataDir, lib, sasl, ebin])],
    error =
	systools:make_relup(LatestName, [LatestName0], [], [{path, P2}]),
    {error,_,{file_problem, {_,{error,{open,_,_}}}}} =
	systools:make_relup(LatestName, [], [LatestName0],
			    [silent, {path, P2}]),
    %% Check that appups missing vsn traps
    P3 = [fname([DataDir, d_no_appup, lib, 'fe-2.1', ebin]),
	  fname([DataDir, d_no_appup, lib, 'fe-500.18.7', ebin]),
	  fname([DataDir, lib, kernel, ebin]),
	  fname([DataDir, lib, stdlib, ebin]),
	  fname([DataDir, lib, sasl, ebin])],
    error =
	systools:make_relup(LatestName0, [LatestName1], [], [{path, P3}]),
    {error,_,{no_relup, _, _, _}} =
	systools:make_relup(LatestName0, [], [LatestName1],
			    [silent, {path, P3}]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_relup: Check that badly written appup files are detected.
bad_appup_relup(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir,LatestName}   = create_script(latest_small,Config),
    {_LatestDir0,LatestName0} = create_script(latest_small0,Config),
    DataDir = filename:absname(?copydir),
    N2 = [fname([DataDir, d_bad_appup, lib, 'fe-3.1', ebin]),
	  fname([DataDir, d_bad_appup, lib, 'fe-2.1', ebin]),
	  fname([DataDir, lib, kernel, ebin]),
	  fname([DataDir, lib, stdlib, ebin]),
	  fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(LatestDir),
    %% Check that bad appup is trapped
    error =
	systools:make_relup(LatestName, [LatestName0], [], [{path, N2}]),
    {error,_,{file_problem, {_, {error, {parse,_, _}}}}} =
	systools:make_relup(LatestName, [], [LatestName0],
			    [silent, {path, N2}]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_relup: Check some abnormal cases.
abnormal_relup(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir,LatestName}   = create_script(latest0,Config),
    {_LatestDir1,LatestName1} = create_script(latest1,Config),
    %% Check wrong app vsn
    DataDir = filename:absname(?copydir),
    P = [fname([DataDir, d_bad_app_vsn, lib, 'db-2.1', ebin]),
	 fname([DataDir, d_bad_app_vsn, lib, 'fe-3.1', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(LatestDir),
    error = systools:make_relup(LatestName, [LatestName1], [LatestName1],
				[{path, P}]),
    R0 = systools:make_relup(LatestName, [LatestName1], [LatestName1],
			     [silent, {path, P}]),
    {error,systools_make,
     [{error_reading,{db,{no_valid_version,
			  {{"should be","2.1"},
			   {"found file", _, "2.0"}}}}}]} = R0,
    ok = file:set_cwd(OldDir),
    ok.
%% make_relup: Check relup can not be created is sasl is not in rel file.
no_sasl_relup(Config) when is_list(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {Dir1,Name1} = create_script(latest1_no_sasl,Config),
    {_Dir2,Name2} = create_script(latest1,Config),
    DataDir = filename:absname(?copydir),
    LibDir = [fname([DataDir, d_normal, lib])],
    P = [fname([LibDir, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(Dir1),
    error = systools:make_relup(Name2, [Name1], [Name1], [{path, P}]),
    R1 = systools:make_relup(Name2, [Name1], [Name1],[silent, {path, P}]),
    {error,systools_relup,{missing_sasl,_}} = R1,
    error = systools:make_relup(Name1, [Name2], [Name2], [{path, P}]),
    R2 = systools:make_relup(Name1, [Name2], [Name2],[silent, {path, P}]),
    {error,systools_relup,{missing_sasl,_}} = R2,
    ok = file:set_cwd(OldDir),
    ok.
%% make_relup: Check that application start type is used in relup
app_start_type_relup(Config) when is_list(Config) ->
    %% This might fail if some applications are not available, if so
    %% skip the test case.
    try create_script(latest_app_start_type2,Config) of
	{Dir2,Name2} ->
	    app_start_type_relup(Dir2,Name2,Config)
    catch throw:{error,Reason} ->
	    {skip,Reason}
    end.
app_start_type_relup(Dir2,Name2,Config) ->
    PrivDir = ?config(priv_dir, Config),
    {Dir1,Name1} = create_script(latest_app_start_type1,Config),
    Release1 = filename:join(Dir1,Name1),
    Release2 = filename:join(Dir2,Name2),
    {ok, Release2Relup, systools_relup, []} = systools:make_relup(Release2, [Release1], [Release1], [{outdir, PrivDir}, silent]),
    {"LATEST_APP_START_TYPE2",
     [{"LATEST_APP_START_TYPE1",[], UpInstructions}],
     [{"LATEST_APP_START_TYPE1",[], DownInstructions}]} = Release2Relup,
    %% ?t:format("Up: ~p",[UpInstructions]),
    %% ?t:format("Dn: ~p",[DownInstructions]),
    [{load_object_code, {mnesia, _, _}},
     {load_object_code, {runtime_tools, _, _}},
     {load_object_code, {snmp, _, _}},
     {load_object_code, {xmerl, _, _}},
     point_of_no_return
     | UpInstructionsT] = UpInstructions,
    true = lists:member({apply,{application,start,[mnesia,permanent]}}, UpInstructionsT),
    true = lists:member({apply,{application,start,[runtime_tools,transient]}}, UpInstructionsT),
    true = lists:member({apply,{application,load,[snmp]}}, UpInstructionsT),
    false = lists:any(fun({apply,{application,_,[xmerl|_]}}) -> true; (_) -> false end, UpInstructionsT),
    [point_of_no_return | DownInstructionsT] = DownInstructions,
    true = lists:member({apply,{application,stop,[mnesia]}}, DownInstructionsT),
    true = lists:member({apply,{application,stop,[runtime_tools]}}, DownInstructionsT),
    true = lists:member({apply,{application,stop,[snmp]}}, DownInstructionsT),
    true = lists:member({apply,{application,stop,[xmerl]}}, DownInstructionsT),
    true = lists:member({apply,{application,unload,[mnesia]}}, DownInstructionsT),
    true = lists:member({apply,{application,unload,[runtime_tools]}}, DownInstructionsT),
    true = lists:member({apply,{application,unload,[snmp]}}, DownInstructionsT),
    true = lists:member({apply,{application,unload,[xmerl]}}, DownInstructionsT),
    ok.
%% make_relup: Check that regexp can be used in .appup for UpFromVsn
%% and DownToVsn.
regexp_relup(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {LatestDir,LatestName}   = create_script(latest_small,Config),
    {_LatestDir0,LatestName0} = create_script(latest_small0,Config),
    {_LatestDir1,LatestName1} = create_script(latest_small2,Config),
    DataDir = filename:absname(?copydir),
    P = [fname([DataDir, d_regexp_appup, lib, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(LatestDir),
    %% Upgrade fe 2.1 -> 3.1, and downgrade 2.1 -> 3.1
    %% Shall match the first entry if fe-3.1 appup.
    {ok, _, _, []} =
	systools:make_relup(LatestName, [LatestName0], [LatestName0],
			    [{path, P}, silent]),
    ok = check_relup([{fe, "3.1"}], [{fe, "2.1"}]),
    %% Upgrade fe 2.1.1 -> 3.1
    %% Shall match the second entry in fe-3.1 appup. Have added a
    %% restart_emulator instruction there to distinguish it from
    %% the first entry...
    {ok, _, _, []} =
	systools:make_relup(LatestName, [LatestName1], [], [{path, P}, silent]),
    ok = check_relup_up_only([{fe, "3.1"}]),
    ok = check_restart_emulator_up_only(),
    %% Attempt downgrade fe 3.1 -> 2.1.1
    %% Shall not match any entry!!
    {error,systools_relup,{no_relup,_,_,_}} =
	systools:make_relup(LatestName, [], [LatestName1], [{path, P}, silent]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_hybrid_boot: Normal case.
%% For upgrade of erts - create a boot file which is a hybrid between
%% old and new release - i.e. starts erts, kernel, stdlib, sasl from
%% new release, all other apps from old release.
normal_hybrid(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {Dir1,Name1} = create_script(latest1,Config),
    {_Dir2,Name2} = create_script(current_all,Config),
    DataDir = filename:absname(?copydir),
    LibDir = [fname([DataDir, d_normal, lib])],
    P = [fname([LibDir, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(Dir1),
    {ok, _ , []} = systools:make_script(Name1,[{path, P},silent]),
    {ok, _ , []} = systools:make_script(Name2,[{path, P},silent]),
    {ok,Boot1} = file:read_file(Name1 ++ ".boot"),
    {ok,Boot2} = file:read_file(Name2 ++ ".boot"),
    ok = file:set_cwd(OldDir),
    {ok,Hybrid} = systools_make:make_hybrid_boot("tmp_vsn",Boot1,Boot2,
                                                 [dummy,args]),
    {script,{"Test release","tmp_vsn"},Script} = binary_to_term(Hybrid),
    ct:log("~p.~n",[Script]),
    %% Check that all paths to base apps are replaced by paths from BaseLib
    Boot1Str = io_lib:format("~p~n",[binary_to_term(Boot1)]),
    Boot2Str = io_lib:format("~p~n",[binary_to_term(Boot2)]),
    HybridStr = io_lib:format("~p~n",[binary_to_term(Hybrid)]),
    ReOpts = [global,{capture,first,list},unicode],
    {match,OldKernelMatch} = re:run(Boot1Str,"kernel-[0-9\.]+",ReOpts),
    {match,OldStdlibMatch} = re:run(Boot1Str,"stdlib-[0-9\.]+",ReOpts),
    {match,OldSaslMatch} = re:run(Boot1Str,"sasl-[0-9\.]+",ReOpts),
    {match,NewKernelMatch} = re:run(Boot2Str,"kernel-[0-9\.]+",ReOpts),
    {match,NewStdlibMatch} = re:run(Boot2Str,"stdlib-[0-9\.]+",ReOpts),
    {match,NewSaslMatch} = re:run(Boot2Str,"sasl-[0-9\.]+",ReOpts),
    {match,NewKernelMatch} = re:run(HybridStr,"kernel-[0-9\.]+",ReOpts),
    {match,NewStdlibMatch} = re:run(HybridStr,"stdlib-[0-9\.]+",ReOpts),
    {match,NewSaslMatch} = re:run(HybridStr,"sasl-[0-9\.]+",ReOpts),
    NewKernelN = length(NewKernelMatch),
    NewKernelN = length(OldKernelMatch),
    NewStdlibN = length(NewStdlibMatch),
    NewStdlibN = length(OldStdlibMatch),
    NewSaslN = length(NewSaslMatch),
    NewSaslN = length(OldSaslMatch),
    %% Check that kernelProcesses are taken from new boot script
    {script,_,Script2} = binary_to_term(Boot2),
    NewKernelProcs = [KP || KP={kernelProcess,_,_} <- Script2],
    NewKernelProcs = [KP || KP={kernelProcess,_,_} <- Script],
    %% Check that application load instruction has correct versions
    Apps = application:loaded_applications(),
    {_,_,KernelVsn} = lists:keyfind(kernel,1,Apps),
    {_,_,StdlibVsn} = lists:keyfind(stdlib,1,Apps),
    {_,_,SaslVsn} = lists:keyfind(sasl,1,Apps),
    [KernelInfo] = [I || {kernelProcess,application_controller,
			  {application_controller,start,
			   [{application,kernel,I}]}} <- Script],
    [StdlibInfo] = [I || {apply,
			  {application,load,
			   [{application,stdlib,I}]}} <- Script],
    [SaslInfo] = [I || {apply,
			{application,load,
			 [{application,sasl,I}]}} <- Script],
    {vsn,KernelVsn} = lists:keyfind(vsn,1,KernelInfo),
    {vsn,StdlibVsn} = lists:keyfind(vsn,1,StdlibInfo),
    {vsn,SaslVsn} = lists:keyfind(vsn,1,SaslInfo),
    %% Check that new_emulator_upgrade call is added
    [_,{apply,{release_handler,new_emulator_upgrade,[dummy,args]}}|_] =
	lists:reverse(Script),
    %% Check that db-1.0 and fe-3.1 are used (i.e. vsns from old release)
    %% And that fe is in there (it exists in old rel but not in new)
    {match,DbMatch} = re:run(HybridStr,"db-[0-9\.]+",ReOpts),
    {match,[_|_]=FeMatch} = re:run(HybridStr,"fe-[0-9\.]+",ReOpts),
    true = lists:all(fun(["db-1.0"]) -> true;
			(_)  -> false
		     end,
		     DbMatch),
    true = lists:all(fun(["fe-3.1"]) -> true;
			(_)  -> false
		     end,
		     FeMatch),
    %% Check that script has same length as old script, plus one (the
    %% new_emulator_upgrade apply)
    {_,_,Old} = binary_to_term(Boot1),
    OldLength = length(Old),
    NewLength = length(Script),
    NewLength = OldLength + 1,
    ok.
%% make_hybrid_boot: No sasl in from-release.
%% Check that systools_make:make_hybrid_boot fails with a meaningful
%% error message if the FromBoot does not include the sasl
%% application.
hybrid_no_old_sasl(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {Dir1,Name1} = create_script(latest1_no_sasl,Config),
    {_Dir2,Name2} = create_script(current_all,Config),
    DataDir = filename:absname(?copydir),
    LibDir = [fname([DataDir, d_normal, lib])],
    P = [fname([LibDir, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(Dir1),
    {ok, _ , [{warning,missing_sasl}]} =
	systools:make_script(Name1,[{path, P},silent]),
    {ok, _ , []} = systools:make_script(Name2,[{path, P},silent]),
    {ok,Boot1} = file:read_file(Name1 ++ ".boot"),
    {ok,Boot2} = file:read_file(Name2 ++ ".boot"),
    {error,{app_not_replaced,sasl}} =
	systools_make:make_hybrid_boot("tmp_vsn",Boot1,Boot2,[dummy,args]),
    ok = file:set_cwd(OldDir),
    ok.
%% make_hybrid_boot: No sasl in to-release.
%% Check that systools_make:make_hybrid_boot fails with a meaningful
%% error message if the ToBoot does not include the sasl
%% application.
hybrid_no_new_sasl(Config) ->
    {ok, OldDir} = file:get_cwd(),
    {Dir1,Name1} = create_script(latest1,Config),
    {_Dir2,Name2} = create_script(current_all_no_sasl,Config),
    DataDir = filename:absname(?copydir),
    LibDir = [fname([DataDir, d_normal, lib])],
    P = [fname([LibDir, '*', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(Dir1),
    {ok, _ , []} = systools:make_script(Name1,[{path, P},silent]),
    {ok, _ , [{warning,missing_sasl}]} =
	systools:make_script(Name2,[{path, P},silent]),
    {ok,Boot1} = file:read_file(Name1 ++ ".boot"),
    {ok,Boot2} = file:read_file(Name2 ++ ".boot"),
    {error,{app_not_found,sasl}} =
	systools_make:make_hybrid_boot("tmp_vsn",Boot1,Boot2,[dummy,args]),
    ok = file:set_cwd(OldDir),
    ok.
%% options: {outdir,Dir} option
otp_6226_outdir(Config) when is_list(Config) ->
    PrivDir = ?privdir,
    {ok, OldDir} = file:get_cwd(),
    {LatestDir, LatestName} = create_script(latest0,Config),
    {_LatestDir, LatestName1} = create_script(latest1,Config),
    DataDir = filename:absname(?copydir),
    LibDir = fname([DataDir, d_normal, lib]),
    P = [fname([LibDir, 'db-2.1', ebin]),
	 fname([LibDir, 'db-1.0', ebin]),
	 fname([LibDir, 'fe-3.1', ebin]),
	 fname([DataDir, lib, kernel, ebin]),
	 fname([DataDir, lib, stdlib, ebin]),
	 fname([DataDir, lib, sasl, ebin])],
    ok = file:set_cwd(LatestDir),
    %% Create an outdir1 directory
    ok = file:make_dir("outdir1"),
    %% ==== Now test systools:make_script ====
    %% a) badarg
    {'EXIT', {{badarg,[{outdir,outdir1}]}, _}} =
	(catch systools:make_script(LatestName, [{outdir,outdir1},
						 {path,P},silent])),
    %% b) absolute path
    Outdir1 = filename:join(PrivDir, "outdir1"),
    {ok,_,[]} = systools:make_script(LatestName, [{outdir,Outdir1},
						  {path,P},silent]),
    Script1 = filename:join(Outdir1, LatestName ++ ".script"),
    Boot1 = filename:join(Outdir1, LatestName ++ ".boot"),
    true = filelib:is_file(Script1),
    true = filelib:is_file(Boot1),
    ok = file:delete(Script1),
    ok = file:delete(Boot1),
    %% c) relative path
    {ok,_,[]} = systools:make_script(LatestName, [{outdir,"./outdir1"},
						  {path,P},silent]),
    true = filelib:is_file(Script1),
    true = filelib:is_file(Boot1),
    ok = file:delete(Script1),
    ok = file:delete(Boot1),
    %% d) absolute but incorrect path
    Outdir2 = filename:join(PrivDir, "outdir2"),
    Script2 = filename:join(Outdir2, LatestName ++ ".script"),
    {error,_,{open,Script2,_}} =
	systools:make_script(LatestName, [{outdir,Outdir2},{path,P},silent]),
    %% e) relative but incorrect path
    {error,_,{open,_,_}} =
	systools:make_script(LatestName, [{outdir,"./outdir2"},{path,P},silent]),
    %% f) with .rel in another directory than cwd
    ok = file:set_cwd(Outdir1),
    {ok,_,[]} = systools:make_script(filename:join(PrivDir, LatestName),
				     [{outdir,"."},{path,P},silent]),
    true = filelib:is_file(LatestName ++ ".script"),
    true = filelib:is_file(LatestName ++ ".boot"),
    ok = file:delete(LatestName ++ ".script"),
    ok = file:delete(LatestName ++ ".boot"),
    ok = file:set_cwd(LatestDir),
    %% ==== Now test systools:make_tar =====
    {ok,_,[]} = systools:make_script(LatestName, [{path,P},silent]),
    %% a) badarg
    {'EXIT', {{badarg, [{outdir,outdir1}]}, _}} =
    	(catch systools:make_tar(LatestName,[{outdir,outdir1},{path,P},silent])),
    %% b) absolute path
    {ok,_,[]} = systools:make_tar(LatestName, [{outdir,Outdir1},
					       {path,P},silent]),
    Tar1 = filename:join(Outdir1,LatestName++".tar.gz"),
    true = filelib:is_file(Tar1),
    ok = file:delete(Tar1),
    %% c) relative path
    {ok,_,[]} = systools:make_tar(LatestName, [{outdir,"./outdir1"},
					       {path,P},silent]),
    true = filelib:is_file(Tar1),
    ok = file:delete(Tar1),
    %% d) absolute but incorrect path
    Tar2 = filename:join(Outdir2,LatestName++".tar.gz"),
    {error,_,{tar_error,{open,Tar2,{Tar2,enoent}}}} =
	systools:make_tar(LatestName, [{outdir,Outdir2},{path,P},silent]),
    %% e) relative but incorrect path
    {error,_,{tar_error,{open,_,_}}} =
	systools:make_tar(LatestName, [{outdir,"./outdir2"},{path,P},silent]),
    %% f) with .rel in another directory than cwd
    ok = file:set_cwd(Outdir1),
    {ok,_,[]} = systools:make_tar(filename:join(PrivDir, LatestName),
				  [{outdir,"."},{path,P},silent]),
    true = filelib:is_file(Tar1),
    ok = file:set_cwd(LatestDir),
    %% ===== Now test systools:make_relup =====
    %% a) badarg
    {'EXIT', {{badarg, [{outdir,outdir1}]}, _}} =
    	(catch systools:make_relup(LatestName,[LatestName1],[LatestName1],
    				   [{outdir,outdir1}, 
				    {path,P},silent])),
    %% b) absolute path
    Relup = filename:join(Outdir1, "relup"),
    {ok,_,_,[]} = systools:make_relup(LatestName,[LatestName1],[LatestName1],
				      [{outdir,Outdir1},
				       {path,P},silent]),
    true = filelib:is_file(Relup),
    ok = file:delete(Relup),
    %% c) relative path
    {ok,_,_,[]} = systools:make_relup(LatestName,[LatestName1],[LatestName1],
				      [{outdir,"./outdir1"},
				       {path,P},silent]),
    true = filelib:is_file(Relup),
    ok = file:delete(Relup),
    %% d) absolute but incorrect path
    {error,_,{file_problem,{"relup",{open,enoent}}}} =
	systools:make_relup(LatestName,[LatestName1],[LatestName1],
			    [{outdir,Outdir2},{path,P},silent]),
    %% e) relative but incorrect path
    {error,_,{file_problem,{"relup",{open,enoent}}}} =
	systools:make_relup(LatestName,[LatestName1],[LatestName1],
			    [{outdir,"./outdir2"},{path,P},silent]),
    %% f) with .rel in another directory than cwd
    %% -- not necessary to test since relup by default is placed in
    %%    cwd, not in the same directory as the .rel file --
    %% Change back to previous working directory
    ok = file:set_cwd(OldDir),
    ok.
%% Test that all default values can be used as values in the .app file
app_file_defaults(Config) ->
    PrivDir = ?config(priv_dir,Config),
    Name = app1,
    NameStr = atom_to_list(Name),
    Vsn = "1.0",
    AppSpec = app_spec(Name,#{vsn=>"1.0"}),
    ok = file:write_file(filename:join(PrivDir,NameStr ++ ".app"),
			 io_lib:format("~p.~n",[AppSpec])),
    {ok,_} = systools_make:read_application(NameStr,Vsn,[PrivDir],[]),
    ok.
app_spec(Name,New) ->
    {application,Name,app_spec(New)}.
app_spec(New) ->
    Default = #{description => "",
		id => "",
		vsn => "",
		modules => [],
		maxP => infinity,
		maxT => infinity,
		registered => [],
		included_applications => [],
		applications => [],
		env => [],
		mod => [],
		start_phases => undefined,
		runtime_dependencies => []},
    maps:to_list(maps:merge(Default,New)).
%%%%%%
%%%%%% Utilities
%%%%%%
check_script_path(RelName) ->
    {ok, [Conts]} = read_script_file(RelName),
    {script, {_, _}, ListOfThings} = Conts,
    case lists:keysearch(path, 1, ListOfThings) of
	{value, {path, [$$,$R,$O,$O,$T | _]}} -> %"$ROOT..."
	    false;
	_ -> ok
    end.
check_var_script_file(VarDirs, NoExistDirs, RelName) ->
    {ok, [Conts]} = read_script_file(RelName),
    {script, {_, _}, ListOfThings} = Conts,
    AllPaths = lists:append(lists:map(fun({path, P}) -> P;
					 (_)         -> []
				      end,
				      ListOfThings)),
    case lists:filter(fun(VarDir) -> lists:member(VarDir, AllPaths) end,
		      VarDirs) of
	VarDirs ->
	    ok;
	_ ->
	    test_server:fail("All variable dirs not in generated script")
    end,
    case lists:filter(fun(NoExistDir) -> lists:member(NoExistDir, AllPaths) end,
		      NoExistDirs) of
	[] ->
	    ok;
	_ ->
	    test_server:fail("Unexpected dirs in generated script")
    end.
check_include_script(RelName, ExpectedLoad, ExpectedStart) ->
    {ok, [Conts]} = read_script_file(RelName),
    {script, {_, _}, ListOfThings} = Conts,
    %% Check that the applications are loaded in given order !
    ActualLoad = 
	[App || {apply,{application,load,[{application,App,_}]}} <- ListOfThings,
		App=/=kernel,
		App=/=stdlib],
    if ActualLoad =:= ExpectedLoad -> ok;
       true -> test_server:fail({bad_load_order, ActualLoad, ExpectedLoad})
    end,
    %% Check that applications are started in given order !
    ActualStart =
	[App || {apply,{application,start_boot,[App|_]}} <- ListOfThings,
		App =/= kernel,
		App =/= stdlib],
    if ActualStart =:= ExpectedStart -> ok;
       true -> test_server:fail({bad_start_order, ActualStart,ExpectedStart})
    end,
    ok.
read_script_file(RelName) ->
    file:consult(RelName ++ ".script").
check_var_tar(Variable, RelName) ->
    Expected = tar_name(Variable),
    case check_tar(Expected,RelName) of
	ok ->
	    ok;
	{error, {erroneous_tar_file, _, missing, _}} ->
	    {error, {not_generated, Expected}}
    end.
exists_tar_file(Name) ->
    File = tar_name(Name),
    case filelib:is_regular(File) of
	true ->
	    ok = file:delete(File),
	    true;
	_ ->
	    false
    end.
%% Take a snap of the generated tar file and check if a certain
%% file is included.
%% This ensures at least that the tar file is generated.
check_tar(File, RelName) ->
    TarContents = tar_contents(RelName),
    case lists:member(File,TarContents) of
	true -> ok;
	_    -> {error, {erroneous_tar_file, tar_name(RelName), missing, File}}
    end.
%% Check that the given files exist in the tar file, and that they are
%% not symlinks
check_tar_regular(PrivDir, Files, RelName) ->
    TmpDir = fname(PrivDir,tmp),
    ok = file:make_dir(TmpDir),
    ok = erl_tar:extract(tar_name(RelName),
			 [{files,Files},{cwd,TmpDir},compressed]),
    R = lists:foldl(fun(File,Acc) ->
			    case file:read_link_info(fname(TmpDir,File)) of 
				{ok,#file_info{type=regular}} ->
				    Acc;
				{ok,#file_info{type=Other}} ->
				    [{File,Other}|Acc];
				_ ->
				    [{File,missing}|Acc]
			    end
		    end,
		    [],
		    Files),
    delete_tree(TmpDir),
    case R of
	[] ->
	    ok;
	NotThere ->
	    {error,{erroneous_tar_file,tar_name(RelName),NotThere}}
    end.
delete_tree(Dir) ->
    case filelib:is_dir(Dir) of
	true ->
	    {ok,Files} = file:list_dir(Dir),
	    lists:foreach(fun(File) -> delete_tree(filename:join(Dir,File)) end, 
			  Files),
	    file:del_dir(Dir);
	false ->
	    ok = file:delete(Dir)
    end.
tar_contents(Name) ->
    {ok, Cont} = erl_tar:table(Name ++ ".tar.gz", [compressed]),
    Cont.
tar_name(Name) ->
    Name ++ ".tar.gz".
create_script(latest,Config) ->
    Apps = core_apps(current) ++ [{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest,Config,"4.4",Apps);
create_script(latest_no_mod_vsn,Config) ->
    Apps = core_apps(current) ++ [{db,"3.1"},{fe,"3.1"}],
    do_create_script(latest_no_mod_vsn,Config,"4.4",Apps);
create_script(latest0,Config) ->
    Apps = core_apps("1.0") ++ [{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest0,Config,"4.4",Apps);
create_script(latest0_current_erts,Config) ->
    Apps = core_apps("1.0") ++ [{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest0_current_erts,Config,current,Apps);
create_script(latest1,Config) ->
    Apps = core_apps("1.0") ++ [{db,"1.0"},{fe,"3.1"}],
    do_create_script(latest1,Config,"4.4",Apps);
create_script(latest1_no_sasl,Config) ->
    Apps = [{kernel,"1.0"},{stdlib,"1.0"},{db,"1.0"},{fe,"3.1"}],
    do_create_script(latest1_no_sasl,Config,"4.4",Apps);
create_script(latest2,Config) ->
    Apps = core_apps("1.0") ++ [{db,"1.0"},{fe,"2.1"}],
    do_create_script(latest2,Config,"4.3",Apps);
create_script(latest_small,Config) ->
    Apps = core_apps("1.0") ++ [{fe,"3.1"}],
    do_create_script(latest_small,Config,"4.4",Apps);
create_script(latest_small0,Config) ->		%Differs in fe vsn
    Apps = core_apps("1.0") ++ [{fe,"2.1"}],
    do_create_script(latest_small0,Config,"4.4",Apps);
create_script(latest_small1,Config) ->
    Apps = core_apps("1.0") ++ [{fe,"500.18.7"}],
    do_create_script(latest_small1,Config,"4.4",Apps);
create_script(latest_small2,Config) ->
    Apps = core_apps("1.0") ++ [{fe,"2.1.1"}],
    do_create_script(latest_small2,Config,"4.4",Apps);
create_script(latest_nokernel,Config) ->
    Apps = [{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest_nokernel,Config,"4.4",Apps);
create_script(latest_kernel_start_type,Config) ->
    Apps = [{kernel,"1.0",load},{stdlib,"1.0"},{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest_kernel_start_type,Config,"4.4",Apps);
create_script(latest_stdlib_start_type,Config) ->
    Apps = [{kernel,"1.0"},{stdlib,"1.0",load},{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest_stdlib_start_type,Config,"4.4",Apps);
create_script(latest_no_stdlib,Config) ->
    Apps = [{kernel,"1.0"},{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest_no_stdlib,Config,"4.4",Apps);
create_script(latest_app_start_type1,Config) ->
    Apps = core_apps(current),
    do_create_script(latest_app_start_type1,Config,current,Apps);
create_script(latest_app_start_type2,Config) ->
    OtherApps = [{mnesia,current,permanent},
		 {runtime_tools,current,transient},
		 {snmp,current,load},
		 {xmerl,current,none}],
    Apps = core_apps(current) ++ OtherApps,
    do_create_script(latest_app_start_type2,Config,current,Apps);
create_script(current_all_no_sasl,Config) ->
    Apps = [{kernel,current},{stdlib,current},{db,"2.1"},{fe,"3.1"}],
    do_create_script(current_all_no_sasl,Config,current,Apps);
create_script(current_all,Config) ->
    Apps = core_apps(current) ++ [{db,"2.1"}],
    do_create_script(current_all,Config,current,Apps);
create_script(current_all_future_erts,Config) ->
    Apps = core_apps(current) ++ [{db,"2.1"},{fe,"3.1"}],
    do_create_script(current_all_future_erts,Config,"99.99",Apps);
create_script(current_all_future_sasl,Config) ->
    Apps = [{kernel,current},{stdlib,current},{sasl,"9.9"},{db,"2.1"},{fe,"3.1"}],
    do_create_script(current_all_future_sasl,Config,current,Apps);
create_script({unicode,RelVsn},Config) ->
    Apps = core_apps(current) ++ [{ua,"1.0"}],
    do_create_script(unicode,RelVsn,Config,current,Apps);
create_script(duplicate_modules,Config) ->
    Apps = core_apps(current) ++ [{app1,"1.0"},{app2,"1.0"}],
    do_create_script(duplicate_modules,Config,current,Apps).
do_create_script(Id,Config,ErtsVsn,AppVsns) ->
    do_create_script(Id,string:to_upper(atom_to_list(Id)),Config,ErtsVsn,AppVsns).
do_create_script(Id,RelVsn,Config,ErtsVsn,AppVsns) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, Id),
    {ok,Fd} = file:open(Name++".rel",write),
    RelfileContent =
	{release,{"Test release", RelVsn},
	 {erts,erts_vsn(ErtsVsn)},
	 app_vsns(AppVsns)},
    io:format(Fd,"~p.~n",[RelfileContent]),
    ok = file:close(Fd),
    {filename:dirname(Name), filename:basename(Name)}.
core_apps(Vsn) ->
    [{App,Vsn} || App <- [kernel,stdlib,sasl]].
app_vsns(AppVsns) ->
    [{App,app_vsn(App,Vsn)} || {App,Vsn} <- AppVsns] ++
	[{App,app_vsn(App,Vsn),Type} || {App,Vsn,Type} <- AppVsns].
app_vsn(App,current) ->
    case application:load(App) of
	Ok when Ok==ok; Ok=={error,{already_loaded,App}} ->
	    {ok,Vsn} = application:get_key(App,vsn),
	    Vsn;
	Error ->
	    throw(Error)
    end;
app_vsn(_App,Vsn) ->
    Vsn.
erts_vsn(current) -> erlang:system_info(version);
erts_vsn(Vsn) -> Vsn.
create_include_files(inc1, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc1),
    create_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t6, \"1.0\"}, {t5, \"1.0\"}, \n"
	"  {t4, \"1.0\"}, {t3, \"1.0\"}, {t2, \"1.0\"}, \n"
	"  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc2, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc2),
    create_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    %% t6 does not include t5 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t6, \"1.0\", [t4]}, {t5, \"1.0\"}, \n"
	"  {t4, \"1.0\"}, {t3, \"1.0\"}, {t2, \"1.0\"}, \n"
	"  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc3, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc3),
    create_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    %% t3 does not include t2 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t6, \"1.0\"}, {t5, \"1.0\"}, \n"
	"  {t4, \"1.0\"}, {t3, \"1.0\", []}, {t2, \"1.0\"}, \n"
	"  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc4, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc4),
    create_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    %% t3 does not include t2 !
    %% t6 does not include t5 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t6, \"1.0\", [t4]}, {t5, \"1.0\"}, \n"
	"  {t4, \"1.0\"}, {t3, \"1.0\", []}, {t2, \"1.0\"}, \n"
	"  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc5, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc5),
    create_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    %% t6 does not include t5 !
    %% exclude t5.
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t6, \"1.0\", [t4]}, \n"
	"  {t4, \"1.0\"}, {t3, \"1.0\", []}, {t2, \"1.0\"}, \n"
	"  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc6, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc6),
    create_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    %% t3 does include non existing t2 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t6, \"1.0\"}, {t5, \"1.0\"}, \n"
	"  {t4, \"1.0\"}, {t3, \"1.0\"}, \n"
	"  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc7, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc7),
    create_apps(PrivDir),
    create_app(t7, PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    %% t7 and t6 does include t5 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t7, \"1.0\"}, {t6, \"1.0\"}, {t5, \"1.0\"}, \n"
	"  {t4, \"1.0\"}, {t3, \"1.0\"}, {t2, \"1.0\"}, \n"
	"  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc8, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc8),
    create_circular_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    %% t8 uses t9 and t10 includes t9 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t8, \"1.0\"}, {t9, \"1.0\"}, {t10, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc9, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc9),
    create_circular_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    %% t8 uses t9, t9 uses t10 and t10 includes t8 ==> circular !!
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t8, \"1.0\"}, {t9, \"1.0\"}, {t10, \"1.0\", [t8]}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc10, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc10),
    create_circular_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    %% t9 tries to include not specified (in .app file) application !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t8, \"1.0\"}, {t9, \"1.0\", [t7]}, {t10, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(inc11, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, inc11),
    create_apps2(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t11, \"1.0\"}, \n"
	"  {t12, \"1.0\"}, \n"
	"  {t13, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(otp_3065_circular_dependenies, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, otp_3065_circular_dependenies),
    create_apps_3065(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {chAts, \"1.0\"}, {aa12, \"1.0\"}, \n"
	"  {chTraffic, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(sort_apps, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, sort_apps),
    create_sort_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t14, \"1.0\"}, \n"
	"  {t20, \"1.0\"}, \n"
	"  {t19, \"1.0\"}, \n"
	"  {t18, \"1.0\"}, \n"
	"  {t17, \"1.0\"}, \n"
	"  {t16, \"1.0\"}, \n"
	"  {t15, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};
create_include_files(sort_apps_rev, Config) ->
    PrivDir = ?privdir,
    Name = fname(PrivDir, sort_apps_rev),
    create_sort_apps(PrivDir),
    Apps = application_controller:which_applications(),
    {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
	" [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
	++ StdlibVer ++ "\"},\n"
	"  {t14, \"1.0\"}, \n"
	"  {t18, \"1.0\"}, \n"
	"  {t19, \"1.0\"}, \n"
	"  {t20, \"1.0\"}, \n"
	"  {t15, \"1.0\"}, \n"
	"  {t16, \"1.0\"}, \n"
	"  {t17, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)}.
create_apps(Dir) ->
    T1 = "{application, t1,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, [kernel, stdlib]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't1.app'), list_to_binary(T1)),
    T2 = "{application, t2,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, [t1]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't2.app'), list_to_binary(T2)),
    T3 = "{application, t3,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, [t2]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't3.app'), list_to_binary(T3)),
    T4 = "{application, t4,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, [t3]},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't4.app'), list_to_binary(T4)),
    T5 = "{application, t5,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, [t3]},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't5.app'), list_to_binary(T5)),
    T6 = "{application, t6,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, [t4, t5]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't6.app'), list_to_binary(T6)).
create_app(t7, Dir) ->
    T7 = "{application, t7,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, [t5]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't7.app'), list_to_binary(T7)).
create_circular_apps(Dir) ->
    T8 = "{application, t8,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, [t9]},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't8.app'), list_to_binary(T8)),
    T9 = "{application, t9,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, [t10]},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't9.app'), list_to_binary(T9)),
    T10 = "{application, t10,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, [t8, t9]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't10.app'), list_to_binary(T10)).
create_apps2(Dir) ->
    T11 = "{application, t11,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, [t13]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't11.app'), list_to_binary(T11)),
    T12 = "{application, t12,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, [t11]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't12.app'), list_to_binary(T12)),
    T13 = "{application, t13,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't13.app'), list_to_binary(T13)).
create_apps_3065(Dir) ->
    T11 = "{application, chTraffic,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, [chAts]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 'chTraffic.app'), list_to_binary(T11)),
    T12 = "{application, chAts,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, [aa12]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 'chAts.app'), list_to_binary(T12)),
    T13 = "{application, aa12,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, [chAts]},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 'aa12.app'), list_to_binary(T13)).
create_sort_apps(Dir) ->
    T14 = "{application, t14,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, [t18,t20,t19]},\n"
	"  {included_applications, [t15,t17,t16]},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't14.app'), list_to_binary(T14)),
    T15 = "{application, t15,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't15.app'), list_to_binary(T15)),
    T16 = "{application, t16,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't16.app'), list_to_binary(T16)),
    T17 = "{application, t17,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't17.app'), list_to_binary(T17)),
    T18 = "{application, t18,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't18.app'), list_to_binary(T18)),
    T19 = "{application, t19,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't19.app'), list_to_binary(T19)),
    T20 = "{application, t20,\n"
	" [{vsn, \"1.0\"},\n"
	"  {description, \"test\"},\n"
	"  {modules, []},\n"
	"  {applications, []},\n"
	"  {included_applications, []},\n"
	"  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't20.app'), list_to_binary(T20)).
fname(N) ->
    filename:join(N).
fname(Dir, Basename) ->
    filename:join(Dir, Basename).
 |