summaryrefslogtreecommitdiffstats
path: root/make_slackware_live.sh
blob: b51e573cbfe2605b23ae4ffef3674d91537103d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
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
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
#!/bin/bash

# Copyright 2014, 2015, 2016, 2017, 2018, 2019, 2020  Eric Hameleers, Eindhoven, NL 
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
# -----------------------------------------------------------------------------
#
# This script creates a live image for a Slackware OS.
# Features:
# - boots using isolinux/extlinux on BIOS, or grub on UEFI.
# - requires kernel >= 4.0 which supports multiple lower layers in overlay
# - uses squashfs to create compressed modules out of directory trees
# - uses overlayfs to bind multiple squashfs modules together
# - you can add your own modules into ./addons/ or ./optional subdirectories.
# - persistence is enabled when writing the ISO to USB stick using iso2usb.sh.
# - LUKS encrypted homedirectory is optional on USB stick using iso2usb.sh.
#
# -----------------------------------------------------------------------------

# Version of the Live OS generator:
VERSION="1.3.8.4"

# Timestamp:
THEDATE=$(date +%Y%m%d)

# Directory where our live tools are stored:
LIVE_TOOLDIR=${LIVE_TOOLDIR:-"$(cd $(dirname $0); pwd)"}

# Load the optional configuration file:
CONFFILE=${CONFFILE:-"${LIVE_TOOLDIR}/$(basename $0 .sh).conf"}
if [ -f ${CONFFILE} ]; then
  echo "-- Loading configuration file."
  . ${CONFFILE}
fi

# Set to "YES" to send error output to the console:
DEBUG=${DEBUG:-"NO"}

# Set to "YES" in order to delete everything we have,
# and rebuild any pre-existing .sxz modules from scratch:
FORCE=${FORCE:-"NO"}

# Set to 32 to be more compatible with the specs. Slackware uses 4 by default:
BOOTLOADSIZE=${BOOTLOADSIZE:-4}

# If you want to include an EFI boot image for 32bit Slackware then you
# need a recompiled grub which supports 32bit EFI (Slackware's grub will not).
# A patch for grub.SlackBuild to enable this feature can be found
# in the source directory. Works for both the 32bit and the 64bit grub package.
# Therefore we disable 32bit EFI by default. Enable at your own peril:
EFI32=${EFI32:-"NO"}

# Set to YES if you want to use the SMP kernel on 32bit Slackware:
SMP32=${SMP32:-"NO"}

# Include support for NFS root (PXE boot), will increase size of the initrd:
NFSROOTSUP=${NFSROOTSUP:-"YES"}

# This variable can be set to a comma-separated list of package series.
# The squashfs module(s) for these package series will then be re-generated.
# Example commandline parameter: "-r l,kde,kdei"
REFRESH=""

# Use xorriso instead of mkisofs/isohybrid to create the ISO:
USEXORR=${USEXORR:-"NO"}

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

# Distribution name:
DISTRO=${DISTRO:-"slackware"}

# What type of Live image?
# Choices are: SLACKWARE, XFCE, LEAN, DAW, KTOWN, MATE, CINNAMON, DLACK, STUDIOWARE
LIVEDE=${LIVEDE:-"SLACKWARE"}

# The live username of the image:
LIVEUID=${LIVEUID:-"live"}

# The number of the live account in the image:
LIVEUIDNR=${LIVEUIDNR:-"1000"}

# The full name of the live account in the image can be set per Live variant,
# and will always be overridden by a LIVEUIDFN definition in the .conf file.
# The LIVEUIDFN defaults to '${DISTRO^} Live User' if not set explicitly:
LIVEUIDFN_DAW="${DISTRO^} Live Musician"

# The root and live user passwords of the image:
ROOTPW=${ROOTPW:-"root"}
LIVEPW=${LIVEPW:-"live"}

# The nvidia persistence account:
NVUID=${NVUID:-"nvidia"}
NVUIDNR=${NVUIDNR:-"365"}
NVGRP=${NVFRP:-"nvidia"}
NVGRPNR=${NVUIDNR:-"365"}

# Custom name for the host:
LIVE_HOSTNAME=${LIVE_HOSTNAME:-"darkstar"}

# What runlevel to use if adding a DE like: XFCE, DAW, KTOWN etc...
RUNLEVEL=${RUNLEVEL:-4}

# Use the graphical syslinux menu (YES or NO)?
SYSMENU=${SYSMENU:-"YES"}

# The amount of seconds we want the init script to wait to give the kernel's
# USB subsystem time to settle. The default value of mkinitrd is "1" which
# is too short for use with USB sticks but "1" is fine for CDROM/DVD.
WAIT=${WAIT:-"5"}

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

# Who built the live image:
BUILDER=${BUILDER:-"Alien BOB"}

# Console font to use with syslinux for better language support:
CONSFONT=${CONSFONT:-"ter-i16v.psf"}

# The ISO main directory:
LIVEMAIN=${LIVEMAIN:-"liveslak"}

# Marker used for finding the Slackware Live files:
MARKER=${MARKER:-"SLACKWARELIVE"}

# The filesystem label we will be giving our ISO:
MEDIALABEL=${MEDIALABEL:-"LIVESLAK"}

# The name of the custom package list containing the generic kernel.
# This package list is special because the script will also take care of
# the ISO boot setup when processing the MINLIST package list:
MINLIST=${MINLIST:-"min"}

# For x86_64 you can add multilib:
MULTILIB=${MULTILIB:-"NO"}

# Use the '-G' parameter to generate the ISO from a pre-populated directory
# containing the live OS files:
ONLY_ISO="NO"

# The name of the directory used for storing persistence data:
PERSISTENCE=${PERSISTENCE:-"persistence"}

# Slackware version to use (note: this won't work for Slackware <= 14.1):
SL_VERSION=${SL_VERSION:-"current"}

# Slackware architecture to install:
SL_ARCH=${SL_ARCH:-"x86_64"}

# Root directory of a Slackware local mirror tree;
# You can define custom repository location (must be in local filesystem)
# for any module in the file ./pkglists/<module>.conf:
SL_REPO=${SL_REPO:-"/var/cache/liveslak/Slackware"}
DEF_SL_REPO=${SL_REPO}

# The rsync URI of our default Slackware mirror server:
SL_REPO_URL=${SL_REPO_URL:-"rsync.osuosl.org::slackware"}
DEF_SL_REPO_URL=${SL_REPO_URL}

# List of Slackware package series - each will become a squashfs module:
SEQ_SLACKWARE="tagfile:a,ap,d,e,f,k,kde,l,n,t,tcl,x,xap,xfce,y pkglist:slackextra"

# Stripped-down Slackware with XFCE as the Desktop Environment:
# - each series will become a squashfs module:
SEQ_XFCEBASE="${MINLIST},noxbase,x_base,xapbase,xfcebase local:mcpp"

# Stripped-down Base Slackware:
SEQ_LEAN="pkglist:${MINLIST},noxbase,x_base,xapbase,xfcebase,slackpkgplus,z00_plasma5supp,z01_plasma5base"

# Stripped-down Slackware DAW with Plasma5 as the Desktop Environment:
# - each series will become a squashfs module.
# Note that loading the modules needs a specific order, which we force:
SEQ_DAW="pkglist:${MINLIST},noxbase,x_base,xapbase,slackpkgplus,z00_plasma5supp,z01_plasma5base,z01_plasma5extra,z01_swdev,z02_alien4daw,z02_alienrest4daw,z03_daw"

# Slackware with 'ktown' Plasma5 instead of its own KDE (full install):
# - each will become a squashfs module:
SEQ_KTOWN="tagfile:a,ap,d,e,f,k,l,n,t,tcl,x,xap,xfce,y pkglist:ktown,ktownalien,slackextra,slackpkgplus"

# List of Slackware package series with MSB instead of KDE (full install):
# - each will become a squashfs module:
SEQ_MSB="tagfile:a,ap,d,e,f,k,l,n,t,tcl,x,xap,xfce,y pkglist:mate,slackextra,slackpkgplus"

# List of Slackware package series with Cinnamon instead of KDE (full install):
# - each will become a squashfs module:
SEQ_CIN="tagfile:a,ap,d,e,f,k,l,n,t,tcl,x,xap,xfce,y pkglist:cinnamon,slackextra,slackpkgplus"

# Slackware package series with Gnome3/systemd instead of KDE (full install):
# - each will become a squashfs module:
SEQ_DLACK="tagfile:a,ap,d,e,f,k,l,n,t,tcl,x,xap pkglist:dlackware,slackextra,systemd"

# List of Slackware package series with StudioWare (full install):
# - each will become a squashfs module:
SEQ_STUDW="tagfile:a,ap,d,e,f,k,kde,l,n,t,tcl,x,xap,xfce,y pkglist:slackextra,slackpkgplus,studioware"

# Package blacklists for variants:
#BLACKLIST_KTOWN="scim scim-tables scim-pinyin scim-m17n scim-hangul scim-anthy scim-input-pad"

# -- START: Used verbatim in upslak.sh -- #
# List of kernel modules required for a live medium to boot properly;
# Lots of HID modules added to support keyboard input for LUKS password entry:
KMODS=${KMODS:-"squashfs:overlay:loop:xhci-pci:ohci-pci:ehci-pci:xhci-hcd:uhci-hcd:ehci-hcd:mmc-core:mmc-block:sdhci:sdhci-pci:sdhci-acpi:usb-storage:hid:usbhid:i2c-hid:hid-generic:hid-apple:hid-cherry:hid-logitech:hid-logitech-dj:hid-logitech-hidpp:hid-lenovo:hid-microsoft:hid_multitouch:jbd:mbcache:ext3:ext4:isofs:fat:nls_cp437:nls_iso8859-1:msdos:vfat:ntfs"}

# Network kernel modules to include for NFS root support:
NETMODS="kernel/drivers/net kernel/drivers/virtio"

# Network kernel modules to exclude from above list:
NETEXCL="appletalk arcnet bonding can dummy.ko hamradio hippi ifb.ko irda macvlan.ko macvtap.ko pcmcia sb1000.ko team tokenring tun.ko usb veth.ko wan wimax wireless xen-netback.ko"
# -- END: Used verbatim in upslak.sh -- #

# Firmware for wired network cards required for NFS root support:
NETFIRMWARE="3com acenic adaptec bnx tigon e100 sun kaweth tr_smctr cxgb3 rtl_nic"

# If any Live variant needs additional 'append' parameters, define them here,
# either using a variable name 'KAPPEND_<LIVEDE>', or by defining 'KAPPEND' in the .conf file:
KAPPEND_SLACKWARE=""
KAPPEND_KTOWN="threadirqs"
KAPPEND_DAW="threadirqs"
KAPPEND_STUDIOWARE="threadirqs"

# Add CACert root certificates yes/no?
ADD_CACERT=${ADD_CACERT:-"NO"}

# Default language selection for the Live OS; 'en' means generic English.
# This can be changed with the commandline switch "-l":
DEF_LANG="en"

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

# What compression to use for the initrd?
# Default is xz with CRC32 (the kernel's XZ decoder does not support CRC64),
# the alternative is gzip (which adds  ~30% to the initrd size).
COMPR=${COMPR:-"xz --check=crc32"}

# What compressors are available?
SQ_COMP_AVAIL="gzip lzma lzo xz zstd"

# What module exttensions do we accept:
SQ_EXT_AVAIL="sxz sfz szs xzm"

# Compressor optimizations:
declare -A SQ_COMP_PARAMS_DEF
SQ_COMP_PARAMS_DEF[gzip]=""
SQ_COMP_PARAMS_DEF[lzma]=""
SQ_COMP_PARAMS_DEF[lzo]=""
SQ_COMP_PARAMS_DEF[xz]="-b 512k -Xdict-size 100%"
SQ_COMP_PARAMS_DEF[zstd]="-b 512k -Xcompression-level 16"
declare -A SQ_COMP_PARAMS_OPT
SQ_COMP_PARAMS_OPT[gzip]=""
SQ_COMP_PARAMS_OPT[lzma]=""
SQ_COMP_PARAMS_OPT[lzo]=""
SQ_COMP_PARAMS_OPT[xz]="-b 1M"
SQ_COMP_PARAMS_OPT[zstd]="-b 1M -Xcompression-level 19"

# What compression to use for the squashfs modules?
# Default is xz, alternatives are gzip, lzma, lzo, zstd:
SQ_COMP=${SQ_COMP:-"xz"}

# Mount point where Live filesystem is assembled (no storage requirements):
LIVE_ROOTDIR=${LIVE_ROOTDIR:-"/mnt/slackwarelive"}

# Directory where the live ISO image will be written:
OUTPUT=${OUTPUT:-"/tmp"}

# Directory where we create the staging directory:
TMP=${TMP:-"/tmp"}

# Toplevel directory of our staging area (this needs sufficient storage):
LIVE_STAGING=${LIVE_STAGING:-"${TMP}/slackwarelive_staging"}

# Work directory where we will create all the temporary stuff:
LIVE_WORK=${LIVE_WORK:-"${LIVE_STAGING}/temp"}

# Directory to be used by overlayfs for data manipulation,
# needs to be a directory in the same filesystem as ${LIVE_WORK}:
LIVE_OVLDIR=${LIVE_OVLDIR:-"${LIVE_WORK}/.ovlwork"}

# Directory where we will move the kernel and create the initrd;
# note that a ./boot directory will be created in here by installpkg:
LIVE_BOOT=${LIVE_BOOT:-"${LIVE_STAGING}/${LIVEMAIN}/bootinst"}

# Directories where the squashfs modules will be created:
LIVE_MOD_SYS=${LIVE_MOD_SYS:-"${LIVE_STAGING}/${LIVEMAIN}/system"}
LIVE_MOD_ADD=${LIVE_MOD_ADD:-"${LIVE_STAGING}/${LIVEMAIN}/addons"}
LIVE_MOD_OPT=${LIVE_MOD_OPT:-"${LIVE_STAGING}/${LIVEMAIN}/optional"}

# ---------------------------------------------------------------------------
# Define some functions.
# ---------------------------------------------------------------------------

# Clean up in case of failure:
function cleanup() {
  # Clean up by unmounting our loopmounts, deleting tempfiles:
  echo "--- Cleaning up the staging area..."
  sync
  umount ${LIVE_ROOTDIR}/sys 2>${DBGOUT} || true
  umount ${LIVE_ROOTDIR}/proc 2>${DBGOUT} || true
  umount ${LIVE_ROOTDIR}/dev 2>${DBGOUT} || true
  umount ${LIVE_ROOTDIR} 2>${DBGOUT} || true
  # Need to umount the squashfs modules too:
  umount ${LIVE_WORK}/*_$$ 2>${DBGOUT} || true

  rmdir ${LIVE_ROOTDIR} 2>${DBGOUT}
  rmdir ${LIVE_WORK}/*_$$ 2>${DBGOUT}
  rm ${LIVE_MOD_OPT}/* 2>${DBGOUT} || true
  rm ${LIVE_MOD_ADD}/* 2>${DBGOUT} || true
} # End of cleanup()

trap 'echo "*** $0 FAILED at line $LINENO ***"; cleanup; exit 1' ERR INT TERM

# Uncompress the initrd based on the compression algorithm used:
function uncompressfs() {
  if $(file "${1}" | grep -qi ": gzip"); then
    gzip -cd "${1}"
  elif $(file "${1}" | grep -qi ": XZ"); then
    xz -cd "${1}"
  fi
} # End of uncompressfs()

#
# Return the full pathname of first package found below $2 matching exactly $1:
#
function full_pkgname() {
  PACK=$1
  if [ -e $2 ]; then
    TOPDIR=$2
    # Perhaps I will use this more readable code in future:
    #for FL in $(find ${TOPDIR} -name "${PACK}-*.t?z" 2>/dev/null) ; do
    #  # Weed out package names starting with "$PACK"; we want exactly "$PACK":
    #  if [ "$(echo $FL |rev|cut -d- -f4-|cut -d/ -f1|rev)" != "$PACK" ]; then
    #    continue
    #  else
    #    break
    #  fi
    #done
    #echo "$FL"
    echo "$(find ${TOPDIR}/ -name "${PACK}-*.t?z" 2>/dev/null |grep -E "\<${PACK//+/\\+}-[^-]+-[^-]+-[^-]+.t?z" |head -1)"
  else
    echo ""
  fi
} # End of full_pkgname()

#
# Find packages and install them into the temporary root:
#
function install_pkgs() {
  if [ -z "$1" ]; then
    echo "-- function install_pkgs: Missing module name."
    cleanup
    exit 1
  fi
  if [ ! -d "$2" ]; then
    echo "-- function install_pkgs: Target directory '$2' does not exist!"
    cleanup
    exit 1
  elif [ ! -f "$2/${MARKER}" ]; then
    echo "-- function install_pkgs: Target '$2' does not contain '${MARKER}' file."
    echo "-- Did you choose the right installation directory?"
    cleanup
    exit 1
  fi

  # Define the default Slackware repository, can be overridden here:
  SL_REPO_URL="${DEF_SL_REPO_URL}"
  SL_REPO="${DEF_SL_REPO}"
  SL_PKGROOT="${DEF_SL_PKGROOT}"
  SL_PATCHROOT="${DEF_SL_PATCHROOT}"

  if [ "$3" = "local" -a -d ${LIVE_TOOLDIR}/local${DIRSUFFIX}/$1 ]; then
    echo "-- Installing local packages from subdir 'local${DIRSUFFIX}/$1'."
    #installpkg --terse --root "$2" "${LIVE_TOOLDIR}/local${DIRSUFFIX}/$1/*.t?z"
    ROOT="$2" upgradepkg --install-new --reinstall "${LIVE_TOOLDIR}"/local${DIRSUFFIX}/"$1"/*.t?z
  else
    # Load package list and (optional) custom repo info:
    if [ "$3" = "tagfile" ]; then
      PKGCONF="__tagfile__"
      PKGFILE=${SL_PKGROOT}/${1}/tagfile
    else
      PKGCONF=${LIVE_TOOLDIR}/pkglists/$(echo $1 |tr [A-Z] [a-z]).conf
      PKGFILE=${LIVE_TOOLDIR}/pkglists/$(echo $1 |tr [A-Z] [a-z]).lst
      if [ -f ${PKGCONF} ]; then
        echo "-- Loading repo info for '$1'."
        . ${PKGCONF}
      fi
    fi

    if [ "${SL_REPO}" = "${DEF_SL_REPO}" ]; then
      # We need only one release from the Slackware package mirror;
      # This must *not* end with a '/' :
      SELECTION="${DISTRO}${DIRSUFFIX}-${SL_VERSION}"
    else
      SELECTION=""
    fi
    if [ ! -d ${SL_REPO} -o -z "$(find ${SL_PKGROOT}/ -type f 2>/dev/null)" ]; then
      # Oops... empty local repository. Let's see if we can rsync from remote:
      echo "** Slackware package repository root '${SL_REPO}' does not exist or is empty!"
      RRES=1
      if [ -n "${SL_REPO_URL}" ]; then
        mkdir -p ${SL_REPO}
        # Must be a rsync URL!
        echo "-- Rsync-ing repository content from '${SL_REPO_URL}' to local directory '${SL_REPO}'..."
        echo "-- This can be time-consuming.  Please wait."
        rsync -rlptD --no-motd --exclude=source ${RSYNCREP} ${SL_REPO_URL}/${SELECTION} ${SL_REPO}/
        RRES=$?
        echo "-- Done rsync-ing from '${SL_REPO_URL}'."
      fi
      if [ $RRES -ne 0 ]; then
        echo "** Exiting."
        cleanup
        exit 1
      fi
    fi

    if [ -f ${PKGFILE} ]; then
      echo "-- Loading package list '$PKGFILE'."
    else
      echo "-- Mandatory package list file '$PKGFILE' is missing! Exiting."
      cleanup
      exit 1
    fi

    for PKGPAT in $(cat ${PKGFILE} |grep -v -E '^ *#|^$' |cut -d: -f1); do
      # Extract the name of the package to install:
      PKG=$(echo $PKGPAT |cut -d% -f2)
      # Extract the name of the potential package to replace/remove:
      # - If there is no package to replace then the 'cut' will make
      #   REP equal to PKG.
      # - If PKG is empty then this is a request to remove the package.
      REP=$(echo $PKGPAT |cut -d% -f1)
      # Skip installation on detecting a blacklisted package:
      for BLST in ${BLACKLIST} BLNONE; do
        if [ "$PKG" == "$BLST" ]; then
          # Found a blacklisted package.
          break
        fi
      done
      # Now decide what to do:
      if [ -z "${PKG}" ]; then
        # Package removal:
        ROOT="$2" removepkg "${REP}"
      elif [ "${PKG}" == "${BLST}" ]; then
        echo "-- Not installing blacklisted package '$PKG'."
      else
        # Package install/upgrade:
        # Look in ./patches ; then ./${DISTRO}$DIRSUFFIX ; then ./extra
        # Need to escape any '+' in package names such a 'gtk+2'.
        if [ ! -z "${SL_PATCHROOT}" ]; then
          FULLPKG=$(full_pkgname ${PKG} ${SL_PATCHROOT})
        else
          FULLPKG=""
        fi
        if [ "x${FULLPKG}" = "x" ]; then
          FULLPKG=$(full_pkgname ${PKG} ${SL_PKGROOT})
        else
          echo "-- $PKG found in patches"
        fi
        if [ "x${FULLPKG}" = "x" ]; then
          # One last attempt: look in ./extra
          FULLPKG=$(full_pkgname ${PKG} $(dirname ${SL_PKGROOT})/extra)
        fi

        if [ "x${FULLPKG}" = "x" ]; then
          echo "-- Package $PKG was not found in $(dirname ${SL_REPO}) !"
        else
          # Determine if we need to install or upgrade a package:
          for INSTPKG in $(ls -1 "$2"/var/log/packages/${REP}-* 2>/dev/null |rev |cut -d/ -f1 |cut -d- -f4- |rev) ; do
            if [ "$INSTPKG" = "$REP" ]; then
              break
            fi
          done
          if [ "$INSTPKG" = "$REP" ]; then
            if [ "$PKG" = "$REP" ]; then
              ROOT="$2" upgradepkg --reinstall "${FULLPKG}"
            else
              # We need to replace one package (REP) with another (FULLPKG):
              ROOT="$2" upgradepkg "${REP}%${FULLPKG}"
            fi
          else
            installpkg --terse --root "$2" "${FULLPKG}"
          fi
        fi
      fi
    done
  fi

  if [ "$TRIM" = "doc" -o "$TRIM" = "mandoc" -o "$TRIM" = "bloat" ]; then
    # Remove undesired (too big for a live OS) document subdirectories,
    # but leave cups alone because it contains the CUPS service's web page:
    (cd "${2}/usr/doc" && find . -type d -mindepth 2 -maxdepth 2 |grep -v /cups- |xargs rm -rf)
    rm -rf "$2"/usr/share/gtk-doc
    rm -rf "$2"/usr/share/help
    find "$2"/usr/share/ -type d -name doc |xargs rm -rf
    # Remove residual bloat:
    rm -rf "${2}"/usr/doc/*/html
    rm -f "${2}"/usr/doc/*/*.{pdf,db,gz,bz2,xz,txt,TXT}
    # This will remove more bloat but won't touch the license texts:
    find "${2}"/usr/doc/ -type f -size +50k |grep -v /cups- |xargs rm -f
    # Remove info pages:
    rm -rf "$2"/usr/info
  fi
  if [ "$TRIM" = "mandoc" -o "$TRIM" = "bloat" ]; then
    # Also remove man pages:
    rm -rf "$2"/usr/man
  fi
  if [ "$TRIM" = "bloat" ]; then
    # By pruning stuff that no one likely needs anyway,
    # we make room for packages we would otherwise not be able to add.
    # MySQL embedded is only used by Amarok:
    rm -f "$2"/usr/bin/mysql*embedded*
    # Also remove the big unused/esoteric static libraries:
    rm -f "$2"/usr/lib${DIRSUFFIX}/*.a
    # This was inadvertantly left in the gcc package:
    rm -f "$2"/usr/libexec/gcc/*/*/cc1objplus
    # From samba we mostly want the shared runtime libraries:
    rm -rf "$2"/usr/share/samba
    rm -rf "$2"/usr/lib${DIRSUFFIX}/python*/site-packages/samba
    # Guile library is all we need to satisfy make:
    rm -f "$2"/usr/bin/guil*
    rm -rf "$2"/usr/include/guile
    rm -rf "$2"/usr/lib64/guile
    rm -rf "$2"/usr/share/guile
    # I am against torture:
    rm -f "$2"/usr/bin/smbtorture
    # From llvm we only want the shared runtime libraries so wipe the rest:
    rm -f "$2"/usr/lib${DIRSUFFIX}/lib{LLVM,lldb}*.a
    rm -rf "$2"/usr/lib${DIRSUFFIX}/libclang*
    rm -rf "$2"/usr/include/{c++/v1,clang,clang-c,lldb,llvm,llvm-c}
    rm -rf "$2"/usr/share/{clang,opt-viewer,scan-build,scan-view}
    rm -rf "$2"/usr/lib${DIRSUFFIX}/cmake/{clang,llvm}
    rm -rf "$2"/usr/lib${DIRSUFFIX}/clang
    rm -rf "$2"/usr/lib${DIRSUFFIX}/python*/site-packages/{clang,lldb}
    if [ -e "$2"/var/log/packages/llvm-[0-9]* ]; then
      for BINFILE in $(grep /bin/. "$2"/var/log/packages/llvm-[0-9]*) ; do
         rm -f "$2"/$BINFILE ; 
      done
    fi
    # Bye llvm; on with the rest:
    rm -rf "$2"/usr/lib${DIRSUFFIX}/d3d
    rm -rf "$2"/usr/lib${DIRSUFFIX}/guile
    rm -rf "$2"/usr/share/icons/HighContrast
    # Nor these datacenter NIC firmwares and drivers:
    rm -rf "$2"/lib/firmware/{bnx*,cxgb4,libertas,liquidio,mellanox,netronome,qed}
    rm -rf "$2"/lib/modules/*/kernel/drivers/infiniband
    rm -rf "$2"/lib/modules/*/kernel/drivers/net/ethernet/{broadcom/bnx*,chelsio,mellanox,netronome,qlogic}
    # Old wireless cards that eat space:
    rm -rf "$2"/lib/firmware/mrvl
    rm -rf "$2"/lib/modules/*/kernel/drivers/net/wireless/marvell
    # Qualcomm GPU firmware (Android phone/tablet)
    rm -rf "$2"/lib/firmware/qcom
    # Texas Instruments ARM firmware:
    rm -rf "$2"/lib/firmware/ti-connectivity
    # Mediatek ARM firmware:
    rm -rf "$2"/lib/firmware/mediatek
    rm -rf "$2"/lib/firmware/vpu*.bin
    # Firmware for Data Path Acceleration Architecture NICs:
    rm -rf "$2"/lib/firmware/dpaa2
    # Not needed:
    rm -rf "$2"/boot/System.map*
    # Depends on Qt:
    rm -f "$2"/usr/bin/wpa_gui "$2"/usr/share/applications/wpa_gui.desktop
    # Replace 3.2 MB splash with a symlink to a 33 kB file:
    if [ -e "$2"/usr/share/gimp/2.0/images/gimp-splash.png -a ! -L "$2"/usr/share/gimp/2.0/images/gimp-splash.png ]; then
      rm -rf "$2"/usr/share/gimp/2.0/images/gimp-splash.png
      ln -sf wilber.png "$2"/usr/share/gimp/2.0/images/gimp-splash.png
    fi
    # Replace big watch cursors with simpler ones:
    if [ -e "$2"/usr/share/icons/Adwaita/cursors/watch -a ! -L "$2"/usr/share/icons/Adwaita/cursors/watch ]; then
      rm -rf "$2"/usr/share/icons/Adwaita/cursors/{watch,left_ptr_watch}
      ln -sf left_ptr "$2"/usr/share/icons/Adwaita/cursors/watch
      ln -sf left_ptr "$2"/usr/share/icons/Adwaita/cursors/left_ptr_watch
    fi
    # Remove 9+ MB of brushes:
    rm -rf "$2"/usr/share/gimp/2.0/brushes/Fun
    # Get rid of useless documentation:
    rm -rf "$2"/usr/share/ghostscript/*/doc/
    # We don't need tests or examples:
    find "$2"/usr/ -type d -iname test |xargs rm -rf
    find "$2"/usr/ -type d -iname "example*" |xargs rm -rf
    # Get rid of most of the screensavers:
    KEEPXSCR="julia|xflame|xjack"
    if [ -d "${2}"/usr/libexec/xscreensaver ]; then
      cd "${2}"/usr/libexec/xscreensaver
        find . -type f | grep -Ev "($KEEPXSCR)" |xargs rm -f
      cd - 1>/dev/null
    fi
    if [ "$3" != "local" ] && echo "$(cat ${PKGFILE} |grep -v -E '^ *#|^$' |cut -d: -f1)" |grep -wq glibc ; then
      # Remove unneeded languages from glibc:
      KEEPLANG="$(cat ${LIVE_TOOLDIR}/languages|grep -Ev "(^ *#|^$)"|cut -d: -f5)"
      for LOCALEDIR in /usr/lib${DIRSUFFIX}/locale /usr/share/i18n/locales /usr/share/locale ; do
        if [ -d "${2}"/${LOCALEDIR} ]; then
          cd "${2}"/${LOCALEDIR}
          mkdir ${LIVE_WORK}/.keep
          for KL in C ${KEEPLANG} ; do
            # en_US.utf8 -> en_US*
            mv ${KL%%.utf8}* ${LIVE_WORK}/.keep/ 2>/dev/null
            # en_US.utf8 -> en
            mv ${KL%%_*} ${LIVE_WORK}/.keep/ 2>/dev/null
          done
          rm -rf [A-Za-z]*
          mv ${LIVE_WORK}/.keep/* . 2>/dev/null
          rm -rf ${LIVE_WORK}/.keep
          cd - 1>/dev/null
        fi
      done
    fi
    # Remove big old ICU libraries that are not needed for the XFCE image:
    if [ -e "$2"/var/log/packages/aaa_elflibs-[0-9]* ]; then
      for ICUFILE in $(grep /libicu "$2"/var/log/packages/aaa_elflibs-[0-9]*) ; do
         rm -f "$2"/$ICUFILE ;
      done
    fi
  fi

} # End install_pkgs()


#
# Create the graphical multi-language syslinux boot menu:
#
function gen_bootmenu() {

  MENUROOTDIR="$1/menu"

  # Generate vesamenu structure - many files because of the selection tree.
  mkdir -p ${MENUROOTDIR}

  # Initialize an empty keyboard selection and language menu:
  rm -f ${MENUROOTDIR}/kbd.cfg
  rm -f ${MENUROOTDIR}/lang*.cfg

  # Generate main (EN) vesamenu.cfg:
  cat ${LIVE_TOOLDIR}/menu.tpl | sed \
    -e "s/@KBD@/${DEF_KBD}/g" \
    -e "s/@LANG@/${DEF_LANG}/g" \
    -e "s/@ULANG@/${DEF_LANG^^}/g" \
    -e "s,@LOCALE@,${DEF_LOCALE},g" \
    -e "s,@TZ@,${DEF_TZ},g" \
    -e "s/@CONSFONT@/$CONSFONT/g" \
    -e "s/@DIRSUFFIX@/$DIRSUFFIX/g" \
    -e "s/@DISTRO@/$DISTRO/g" \
    -e "s/@CDISTRO@/${DISTRO^}/g" \
    -e "s/@UDISTRO@/${DISTRO^^}/g" \
    -e "s/@KVER@/$KVER/g" \
    -e "s/@LIVEMAIN@/$LIVEMAIN/g" \
    -e "s/@MEDIALABEL@/$MEDIALABEL/g" \
    -e "s/@LIVEDE@/$(echo $LIVEDE |sed 's/BASE//')/g" \
    -e "s/@SL_VERSION@/$SL_VERSION/g" \
    -e "s/@VERSION@/$VERSION/g" \
    -e "s/@KAPPEND@/$KAPPEND/g" \
    > ${MENUROOTDIR}/vesamenu.cfg

  for LANCOD in $(cat ${LIVE_TOOLDIR}/languages |grep -Ev "(^ *#|^$)" |cut -d: -f1)
  do
    LANDSC=$(cat ${LIVE_TOOLDIR}/languages |grep "^$LANCOD:" |cut -d: -f2)
    KBD=$(cat ${LIVE_TOOLDIR}/languages |grep "^$LANCOD:" |cut -d: -f3)
    # First, create keytab files if they are missing:
    if [ ! -f ${MENUROOTDIR}/${KBD}.ktl ]; then
      keytab-lilo $(find /usr/share/kbd/keymaps/i386 -name "us.map.gz") $(find /usr/share/kbd/keymaps/i386 -name "${KBD}.map.gz") > ${MENUROOTDIR}/${KBD}.ktl
    fi
    # Add this keyboard to the keyboard selection menu:
    cat <<EOL >> ${MENUROOTDIR}/kbd.cfg
label ${LANCOD}
  menu label ${LANDSC}
EOL
    if [ "${KBD}" == "${DEF_KBD}" ]; then
      echo "  menu default" >> ${MENUROOTDIR}/kbd.cfg
    fi
    cat <<EOL >> ${MENUROOTDIR}/kbd.cfg
  kbdmap menu/${KBD}.ktl
  kernel vesamenu.c32
  append menu/menu_${LANCOD}.cfg

EOL

    # Generate custom vesamenu.cfg for selected keyboard:
    cat ${LIVE_TOOLDIR}/menu.tpl | sed \
      -e "s/@KBD@/$KBD/g" \
      -e "s/@LANG@/$LANCOD/g" \
      -e "s/@ULANG@/${DEF_LANG^^}/g" \
      -e "s,@LOCALE@,${DEF_LOCALE},g" \
      -e "s,@TZ@,${DEF_TZ},g" \
      -e "s/@CONSFONT@/$CONSFONT/g" \
      -e "s/@DIRSUFFIX@/$DIRSUFFIX/g" \
      -e "s/@DISTRO@/$DISTRO/g" \
      -e "s/@CDISTRO@/${DISTRO^}/g" \
      -e "s/@UDISTRO@/${DISTRO^^}/g" \
      -e "s/@KVER@/$KVER/g" \
      -e "s/@LIVEMAIN@/$LIVEMAIN/g" \
      -e "s/@MEDIALABEL@/$MEDIALABEL/g" \
      -e "s/@LIVEDE@/$(echo $LIVEDE |sed 's/BASE//')/g" \
      -e "s/@SL_VERSION@/$SL_VERSION/g" \
      -e "s/@VERSION@/$VERSION/g" \
      -e "s/@KAPPEND@/$KAPPEND/g" \
      > ${MENUROOTDIR}/menu_${LANCOD}.cfg

    # Generate custom language selection submenu for selected keyboard:
    for SUBCOD in $(cat ${LIVE_TOOLDIR}/languages |grep -Ev "(^ *#|^$)" |cut -d: -f1) ; do
      SUBKBD=$(cat ${LIVE_TOOLDIR}/languages |grep "^$SUBCOD:" |cut -d: -f3)
      cat <<EOL >> ${MENUROOTDIR}/lang_${LANCOD}.cfg
label $(cat ${LIVE_TOOLDIR}/languages |grep "^$SUBCOD:" |cut -d: -f1)
  menu label $(cat ${LIVE_TOOLDIR}/languages |grep "^$SUBCOD:" |cut -d: -f2)
EOL
      if [ "$SUBKBD" = "$KBD" ]; then
        echo "  menu default" >> ${MENUROOTDIR}/lang_${LANCOD}.cfg
      fi
      cat <<EOL >> ${MENUROOTDIR}/lang_${LANCOD}.cfg
  kernel /boot/generic
  append initrd=/boot/initrd.img $KAPPEND load_ramdisk=1 prompt_ramdisk=0 rw printk.time=0 kbd=$KBD tz=$(cat ${LIVE_TOOLDIR}/languages |grep "^$SUBCOD:" |cut -d: -f4) locale=$(cat ${LIVE_TOOLDIR}/languages |grep "^$SUBCOD:" |cut -d: -f5) xkb=$(cat ${LIVE_TOOLDIR}/languages |grep "^$SUBCOD:" |cut -d: -f6)

EOL
    done

  done

} # End of gen_bootmenu()

#
# Create the grub menu file for UEFI boot:
#
function gen_uefimenu() {

  GRUBDIR="$1"

  # Generate the grub menu structure - many files because of the selection tree.
  # I expect the directory to exist... but you never know.
  mkdir -p ${GRUBDIR}

  # Initialize an empty keyboard, language and timezone selection menu:
  rm -f ${GRUBDIR}/kbd.cfg
  rm -f ${GRUBDIR}/lang.cfg
  rm -f ${GRUBDIR}/tz.cfg

  # Generate main grub.cfg:
  cat ${LIVE_TOOLDIR}/grub.tpl | sed \
    -e "s/@KBD@/${DEF_KBD}/g" \
    -e "s,@TZ@,${DEF_TZ},g" \
    -e "s/@LANG@/${DEF_LANG}/g" \
    -e "s/@ULANG@/${DEF_LANG^^}/g" \
    -e "s/@LANDSC@/${DEF_LANDSC}/g" \
    -e "s/@LOCALE@/${DEF_LOCALE}/g" \
    -e "s/@CONSFONT@/$CONSFONT/g" \
    -e "s/@DIRSUFFIX@/$DIRSUFFIX/g" \
    -e "s/@DISTRO@/$DISTRO/g" \
    -e "s/@CDISTRO@/${DISTRO^}/g" \
    -e "s/@UDISTRO@/${DISTRO^^}/g" \
    -e "s/@KVER@/$KVER/g" \
    -e "s/@LIVEMAIN@/$LIVEMAIN/g" \
    -e "s/@MEDIALABEL@/$MEDIALABEL/g" \
    -e "s/@LIVEDE@/$(echo $LIVEDE |sed 's/BASE//')/g" \
    -e "s/@SL_VERSION@/$SL_VERSION/g" \
    -e "s/@VERSION@/$VERSION/g" \
    -e "s/@KAPPEND@/$KAPPEND/g" \
    > ${GRUBDIR}/grub.cfg

  # Set a default keyboard selection:
  cat <<EOL > ${GRUBDIR}/kbd.cfg
# Keyboard selection:
set default = $sl_lang

EOL

  # Set a default language selection:
  cat <<EOL > ${GRUBDIR}/lang.cfg
# Language selection:
set default = $sl_lang

EOL

  # Create the remainder of the selection menus:
  for LANCOD in $(cat languages |grep -Ev "(^ *#|^$)" |cut -d: -f1) ; do
    LANDSC=$(cat languages |grep "^$LANCOD:" |cut -d: -f2)
    KBD=$(cat languages |grep "^$LANCOD:" |cut -d: -f3)
    XKB=$(cat languages |grep "^$LANCOD:" |cut -d: -f6)
    LANLOC=$(cat languages |grep "^$LANCOD:" |cut -d: -f5)
    # Add this entry to the keyboard selection menu:
    cat <<EOL >> ${GRUBDIR}/kbd.cfg
menuentry "${LANDSC}" {
  set sl_kbd="$KBD"
  set sl_xkb="$XKB"
  set sl_lang="$LANDSC"
  export sl_kbd
  export sl_xkb
  export sl_lang
  configfile \$prefix/grub.cfg
}

EOL

    # Add this entry to the language selection menu:
    cat <<EOL >> ${GRUBDIR}/lang.cfg
menuentry "${LANDSC}" {
  set sl_locale="$LANLOC"
  set sl_lang="$LANDSC"
  export sl_locale
  export sl_lang
  configfile \$prefix/grub.cfg
}

EOL

  done

  # Create the timezone selection menu:
  TZDIR="/usr/share/zoneinfo"
  TZLIST=$(mktemp -t alientz.XXXXXX)
  if [ ! -f $TZLIST ]; then
    echo "*** Failed to create a temporary file!"
    cleanup
    exit 1
  fi
  # First, create a list of timezones:
  # This code taken from Slackware script:
  # source/a/glibc-zoneinfo/timezone-scripts/output-updated-timeconfig.sh
  # Author: Patrick Volkerding <volkerdi@slackware.com>
  # US/ first:
  ( cd $TZDIR
    find . -type f | xargs file | grep "timezone data" | cut -f 1 -d : | cut -f 2- -d / | sort | grep "^US/" | while read zone ; do
      echo "${zone}" >> $TZLIST
    done
  )
  # Don't list right/ and posix/ zones:
  ( cd $TZDIR
    find . -type f | xargs file | grep "timezone data" | cut -f 1 -d : | cut -f 2- -d / | sort | grep -v "^US/" | grep -v "^posix/" | grep -v "^right/" | while read zone ; do
      echo "${zone}" >> $TZLIST
    done
  )
  for TZ in $(cat $TZLIST); do
    # Add this entry to the keyboard selection menu:
    cat <<EOL >> ${GRUBDIR}/tz.cfg
menuentry "${TZ}" {
  set sl_tz="$TZ"
  export sl_tz
  configfile \$prefix/grub.cfg
}

EOL
  rm -f $TZLIST

  done

} # End of gen_uefimenu()

#
# Create an ISO file from a directory's content:
#
function create_iso() {
  TOPDIR=${1:-"${LIVE_STAGING}"}

  cd "$TOPDIR"

  # Tag the type of live environment to the ISO filename:
  if [ "$LIVEDE" = "SLACKWARE" ]; then
    ISOTAG=""
  else
    ISOTAG="-$(echo $LIVEDE |tr A-Z a-z)"
  fi

  # Determine whether we add UEFI boot capabilities to the ISO:
  if [ -f boot/syslinux/efiboot.img -a "$USEXORR" = "NO" ]; then
    UEFI_OPTS="-eltorito-alt-boot -no-emul-boot -eltorito-platform 0xEF -eltorito-boot boot/syslinux/efiboot.img"
  elif [ -f boot/syslinux/efiboot.img -a "$USEXORR" = "YES" ]; then
    UEFI_OPTS="-eltorito-alt-boot -e boot/syslinux/efiboot.img -no-emul-boot"
  else
    UEFI_OPTS=""
  fi

  # Time to determine the output filename, now that we know all the variables
  # and ensured that the OUTPUT directory exists:
  OUTFILE=${OUTFILE:-"${OUTPUT}/${DISTRO}${DIRSUFFIX}-live${ISOTAG}-${SL_VERSION}.iso"}
  if [ "$USEXORR" = "NO" ]; then
    mkisofs -o "${OUTFILE}" \
      -V "${MEDIALABEL}" \
      -R -J \
      -hide-rr-moved \
      -v -d -N \
      -no-emul-boot -boot-load-size ${BOOTLOADSIZE} -boot-info-table \
      -sort boot/syslinux/iso.sort \
      -b boot/syslinux/isolinux.bin \
      -c boot/syslinux/isolinux.boot \
      ${UEFI_OPTS} \
      -preparer "$(echo $LIVEDE |sed 's/BASE//') Live built by ${BUILDER}" \
      -publisher "The Slackware Linux Project - http://www.slackware.com/" \
      -A "${DISTRO^}-${SL_VERSION} for ${SL_ARCH} ($(echo $LIVEDE |sed 's/BASE//') Live $VERSION)" \
      -x ./$(basename ${LIVE_WORK}) \
      -x ./${LIVEMAIN}/bootinst \
      -x boot/syslinux/testing \
      .

    if [ "$SL_ARCH" = "x86_64" -o "$EFI32" = "YES" ]; then
      # Make this a hybrid ISO with UEFI boot support on x86_64.
      # On 32bit, the variable EFI32 must be explicitly enabled.
      isohybrid -u "${OUTFILE}"
    else
      isohybrid "${OUTFILE}"
    fi # End UEFI hybrid ISO.
  else
    echo "-- Using xorriso to generate the ISO and make it hybrid."
    xorriso -as mkisofs -o "${OUTFILE}" \
      -V "${MEDIALABEL}" \
      -J -joliet-long -r \
      -hide-rr-moved \
      -v -d -N \
      -b boot/syslinux/isolinux.bin \
      -c boot/syslinux/isolinux.boot \
      -boot-load-size ${BOOTLOADSIZE} -boot-info-table -no-emul-boot \
      ${UEFI_OPTS} \
      -isohybrid-mbr /usr/share/syslinux/isohdpfx.bin \
      -isohybrid-gpt-basdat \
      -preparer "$(echo $LIVEDE |sed 's/BASE//') Live built by ${BUILDER}" \
      -publisher "The Slackware Linux Project - http://www.slackware.com/" \
      -A "${DISTRO^}-${SL_VERSION} for ${SL_ARCH} ($(echo $LIVEDE |sed 's/BASE//') Live $VERSION)" \
      -x ./$(basename ${LIVE_WORK}) \
      -x ./${LIVEMAIN}/bootinst \
      -x boot/syslinux/testing \
      .
  fi

  # Return to original directory:
  cd - 1>/dev/null

  cd "${OUTPUT}"
    md5sum "$(basename "${OUTFILE}")" \
      > "$(basename "${OUTFILE}")".md5
  cd - 1>/dev/null
  echo "-- Live ISO image created:"
  echo "   - CDROM max size is 737.280.000 bytes (703 MB)"
  echo "   - DVD max size is 4.706.074.624 bytes (4.38 GB aka 4.7 GiB)"
  ls -l "${OUTFILE}"*

} # End of create_iso()

#
# Configure a custom background image in Plasma5:
#
function plasma5_custom_bg() {
  # The function expects a background image file of JPG or PNG format.
  # The bitmap must be present in the liveslak source tree as:
  # media/<variant>/bg/background.png or media/<variant>/bg/background.jpg ,
  # where <variant> is the lowercase name of the liveslak variant (such as
  # cinnamon, daw, slackware, etc).
  # The file background.{jpg,png} can be a symlink to an actual JPG or PNG.
  # Aspect ratio of the image *must* be 16:9 (1920x1080 px or higher res).

  # Exit immediately if the image file is not found:
  if ! readlink -f ${LIVE_TOOLDIR}/media/${LIVEDE,,}/bg/background.* 1>/dev/null 2>&1 ; then
    echo "-- No ${LIVEDE} custom wallpaper image."
    return
  fi

  echo "-- Configuring ${LIVEDE} custom background image."
  # First convert our image into a JPG in the liveslak directory:
  mkdir -p ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${LIVEDE,,}
  convert ${LIVE_TOOLDIR}/media/${LIVEDE,,}/bg/background.* ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${LIVEDE,,}/background.jpg

  # Create a Plasma5 desktop wallpaper set with a lowercase LIVEDE name:
  mkdir -p ${LIVE_ROOTDIR}/usr/share/wallpapers/${LIVEDE,,}/contents/images

  # Create set of images for common aspect ratios like 16:9, 16:10 and 4:3:
  # Aspect Ratio 16:9 :
  convert ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${LIVEDE,,}/background.jpg \
    -resize 1920x1080 \
    ${LIVE_ROOTDIR}/usr/share/wallpapers/${LIVEDE,,}/contents/images/1920x1080.jpg
  convert ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${LIVEDE,,}/background.jpg \
    -resize 5120x2880 \
    ${LIVE_ROOTDIR}/usr/share/wallpapers/${LIVEDE,,}/contents/images/5120x2880.jpg
  # Aspect Ratio 16:10 :
  convert  ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${LIVEDE,,}/background.jpg \
    -resize 5120x - | \
    convert - -geometry 1920x1200^ -gravity center -crop 1920x1200+0+0 \
    ${LIVE_ROOTDIR}/usr/share/wallpapers/${LIVEDE,,}/contents/images/1920x1200.jpg
  convert  ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${LIVEDE,,}/background.jpg \
    -resize 5120x - | \
    convert - -geometry 1280x800^ -gravity center -crop 1280x800+0+0 \
    ${LIVE_ROOTDIR}/usr/share/wallpapers/${LIVEDE,,}/contents/images/1280x800.jpg
  # Aspect Ratio 4:3 :
  convert  ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${LIVEDE,,}/background.jpg \
    -resize 5120x - | \
    convert - -geometry 1024x768^ -gravity center -crop 1024x768+0+0 \
    ${LIVE_ROOTDIR}/usr/share/wallpapers/${LIVEDE,,}/contents/images/1024x768.jpg

  # Create the required wallpaper screenshot of 400x225 px (16:9 aspect ratio):
  convert ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${LIVEDE,,}/background.jpg \
    -resize 400x225 \
    ${LIVE_ROOTDIR}/usr/share/wallpapers/${LIVEDE,,}/contents/screenshot.png

  # Add wallpaper description:
  cat <<EOT >${LIVE_ROOTDIR}/usr/share/wallpapers/${LIVEDE,,}/metadata.desktop
[Desktop Entry]
Name=${DISTRO^} Live

X-KDE-PluginInfo-Name=${LIVEDE,,}
X-KDE-PluginInfo-Author=Eric Hameleers
X-KDE-PluginInfo-Email=alien@slackware.com
X-KDE-PluginInfo-License=CC-BY-SA-4.0
EOT

  # Now set our wallpaper to be the default. For this to work, we need to link
  # the name of the default theme to ours, so find out what the default is:
  DEF_THEME="$(grep ^defaultWallpaperTheme ${LIVE_ROOTDIR}/usr/share/plasma/desktoptheme/default/metadata.desktop |cut -d= -f2-)"
  mv ${LIVE_ROOTDIR}/usr/share/wallpapers/${DEF_THEME}{,.orig}
  ln -s ${LIVEDE,,} ${LIVE_ROOTDIR}/usr/share/wallpapers/${DEF_THEME}

  # Custom background for the SDDM login greeter:
  mkdir -p ${LIVE_ROOTDIR}/usr/share/sddm/themes/breeze
  cp ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${LIVEDE,,}/background.jpg ${LIVE_ROOTDIR}/usr/share/sddm/themes/breeze/${LIVEDE,,}_background.jpg
  cat <<EOT > ${LIVE_ROOTDIR}/usr/share/sddm/themes/breeze/theme.conf.user
[General]
background=${LIVEDE,,}_background.jpg
EOT

  # Screenlocker:
  mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/.config
cat <<EOT > ${LIVE_ROOTDIR}/home/${LIVEUID}/.config/kscreenlockerrc 
[$Version]
update_info=kscreenlocker.upd:0.1-autolock

[Greeter]
WallpaperPlugin=org.kde.image
[Greeter][Wallpaper][org.kde.image][General]
FillMode=2
Image=file:///usr/share/${LIVEMAIN}/${LIVEDE,,}/background.jpg
EOT

} # End of plasma5_custom_bg()

# ---------------------------------------------------------------------------
# Action!
# ---------------------------------------------------------------------------

while getopts "a:c:d:efhl:m:r:s:t:vz:GH:MO:R:X" Option
do
  case $Option in
    h )
	echo "----------------------------------------------------------------"
	echo "make_slackware_live.sh $VERSION"
	echo "----------------------------------------------------------------"
        echo "Usage:"
        echo "  $0 [OPTION] ..."
        echo "or:"
        echo "  SL_REPO=/your/repository/dir $0 [OPTION] ..."
        echo ""
        echo "The SL_REPO is the directory that contains the directory"
        echo "  ${DISTRO}-<RELEASE> or ${DISTRO}64-<RELEASE>"
        echo "Current value of SL_REPO : $SL_REPO"
        echo ""
        echo "The script's parameters are:"
        echo " -h                 This help."
        echo " -a arch            Machine architecture (default: ${SL_ARCH})."
        echo "                    Use i586 for a 32bit ISO, x86_64 for 64bit."
        echo " -c comp            Squashfs compression (default: ${SQ_COMP})."
        echo "                    Can be any of '${SQ_COMP_AVAIL}'."
        echo " -d desktoptype     SLACKWARE (full Slack),XFCE basic, LEAN, DAW,"
        echo "                    KTOWN, MATE, CINNAMON, DLACK, STUDIOWARE."
        echo " -e                 Use ISO boot-load-size of 32 for computers."
        echo "                    where the ISO won't boot otherwise."
        echo " -f                 Forced re-generation of all squashfs modules,"
        echo "                    custom configurations and new initrd.img."
        echo " -l <localization>  Enable a different default localization"
        echo "                    (script-default is '${DEF_LANG}')."
        echo " -m pkglst[,pkglst] Add modules defined by pkglists/<pkglst>,..."
        echo " -r series[,series] Refresh only one or a few package series."
        echo " -s slackrepo_dir   Directory containing ${DISTRO^} repository."
        echo " -t <none|doc|mandoc|bloat>"
        echo "                    Trim the ISO (remove man and/or doc and/or bloat)."
        echo " -v                 Show debug/error output."
        echo " -z version         Define your ${DISTRO^} version (default: $SL_VERSION)."
        echo " -G                 Generate ISO file from existing directory tree"
        echo " -H hostname        Hostname of the Live OS (default: $LIVE_HOSTNAME)."
        echo " -M                 Add multilib (x86_64 only)."
        echo " -O outfile         Custom filename for the ISO."
        echo " -R runlevel        Runlevel to boot into (default: $RUNLEVEL)."
        echo " -X                 Use xorriso instead of mkisofs/isohybrid."
        exit
        ;;
    a ) SL_ARCH="${OPTARG}"
        ;;
    c ) SQ_COMP="${OPTARG}"
        ;;
    d ) LIVEDE="$(echo ${OPTARG} |tr a-z A-Z)"
        ;;
    e ) BOOTLOADSIZE=32
        ;;
    f ) FORCE="YES"
        ;;
    l ) DEF_LANG="${OPTARG}"
        ;;
    m ) SEQ_ADDMOD="${OPTARG}"
        ;;
    r ) REFRESH="${OPTARG}"
        ;;
    s ) SL_REPO="${OPTARG}"
        ;;
    t ) TRIM="${OPTARG}"
        ;;
    v ) DEBUG="YES"
        ;;
    z ) SL_VERSION="${OPTARG}"
        ;;
    G ) ONLY_ISO="YES"
        ;;
    H ) LIVE_HOSTNAME="${OPTARG}"
        ;;
    M ) MULTILIB="YES"
        ;;
    O ) OUTFILE="${OPTARG}"
        OUTPUT="$(cd $(dirname "${OUTFILE}"); pwd)"
        ;;
    R ) RUNLEVEL=${OPTARG}
        ;;
    X ) USEXORR="YES"
        ;;
    * ) echo "You passed an illegal switch to the program!"
        echo "Run '$0 -h' for more help."
        exit
        ;;   # DEFAULT
  esac
done

# End of option parsing.
shift $(($OPTIND - 1))

#  $1 now references the first non option item supplied on the command line
#  if one exists.
# ---------------------------------------------------------------------------

[ "$DEBUG" = "NO" ] && DBGOUT="/dev/null" || DBGOUT="/dev/stderr"

# -----------------------------------------------------------------------------
# Some sanity checks first.
# -----------------------------------------------------------------------------

if [ -n "$REFRESH" -a "$FORCE" = "YES" ]; then
  echo ">> Please use only _one_ of the switches '-f' or '-r'!"
  echo ">> Run '$0 -h' for more help."
  exit 1
fi

if [ "$ONLY_ISO" = "YES" -a "$FORCE" = "YES" ]; then
  echo ">> Please use only _one_ of the switches '-f' or '-G'!"
  echo ">> Run '$0 -h' for more help."
  exit 1
fi

if [ $RUNLEVEL -ne 3 -a $RUNLEVEL -ne 4 ]; then
  echo ">> Default runlevel other than 3 or 4 is not supported."
  exit 1
fi

if [ "$SL_ARCH" != "x86_64" -a "$MULTILIB" = "YES" ]; then
  echo ">> Multilib is only supported on x86_64 architecture."
  exit 1
fi

if ! cat ${LIVE_TOOLDIR}/languages |grep -Ev '(^ *#|^$)' |grep -q ^${DEF_LANG}:
then
  echo ">> Unsupported language '${DEF_LANG}', select a supported language:"
  echo ">> $(cat ${LIVE_TOOLDIR}/languages |grep -Ev '(^ *#|^$)' |cut -d: -f1)."
  exit 1
else
  # Default locale, timezone and keyboard layout based on language choice:
  DEF_LANDSC="$(cat ${LIVE_TOOLDIR}/languages |grep ^${DEF_LANG}: |cut -d: -f2)"
  DEF_KBD="$(cat ${LIVE_TOOLDIR}/languages |grep ^${DEF_LANG}: |cut -d: -f3)"
  DEF_TZ="$(cat ${LIVE_TOOLDIR}/languages |grep ^${DEF_LANG}: |cut -d: -f4)"
  DEF_LOCALE="$(cat ${LIVE_TOOLDIR}/languages |grep ^${DEF_LANG}: |cut -d: -f5)"
  # Select sane defaults in case the language file lacks info:
  DEF_LANDSC="${DEF_LANDSC:-'us american'}"
  DEF_KBD="${DEF_KBD:-'us'}"
  DEF_TZ="${DEF_TZ:-'UTC'}"
  DEF_LOCALE="${DEF_LOCALE:-'en_US.utf8'}"
fi

# Directory suffix, arch dependent:
if [ "$SL_ARCH" = "x86_64" ]; then
  DIRSUFFIX="64"
  EFIFORM="x86_64"
  EFISUFF="x64"
else
  DIRSUFFIX=""
  EFIFORM="i386"
  EFISUFF="ia32"
fi

# Package root directory, arch dependent:
SL_PKGROOT=${SL_REPO}/${DISTRO}${DIRSUFFIX}-${SL_VERSION}/${DISTRO}${DIRSUFFIX}
DEF_SL_PKGROOT=${SL_PKGROOT}

# Patches root directory, arch dependent:
SL_PATCHROOT=${SL_REPO}/${DISTRO}${DIRSUFFIX}-${SL_VERSION}/patches/packages
DEF_SL_PATCHROOT=${SL_PATCHROOT}

# Are all the required add-on tools present?
[ "$USEXORR" = "NO" ] && ISOGEN="mkisofs isohybrid" || ISOGEN="xorriso"
PROG_MISSING=""
for PROGN in mksquashfs unsquashfs grub-mkfont grub-mkimage syslinux $ISOGEN installpkg upgradepkg keytab-lilo rsync mkdosfs ; do
  if ! which $PROGN 1>/dev/null 2>/dev/null ; then
    PROG_MISSING="${PROG_MISSING}--   $PROGN\n"
  fi
done
if [ ! -z "$PROG_MISSING" ] ; then
  echo "-- Required program(s) not found in PATH!"
  echo -e ${PROG_MISSING}
  echo "-- Exiting."
  exit 1
fi

# Test whether the compressor of choice is supported by the script:
if ! echo ${SQ_COMP_AVAIL} | grep -wq ${SQ_COMP} ; then
  echo "-- Compressor '${SQ_COMP}' not supported by liveslak!"
  echo "-- Select one of '${SQ_COMP_AVAIL}'"
  exit 1
else
  # Test whether the local squashfs-tools support the compressor:
  if ! mksquashfs 2>&1 | grep -Ewq "^[[:space:]]*${SQ_COMP}" ; then
    echo "-- Compressor '${SQ_COMP}' not supported by your 'mksquashfs'!"
    echo "-- Select another one from '${SQ_COMP_AVAIL}'"
    exit 1
  fi
fi
 
# What compression parameters to use?
# For our lean XFCE image we try to achieve max compression,
# at the expense of runtime latency:
if [ "$LIVEDE" = "XFCE" ] ; then
  SQ_COMP_PARAMS=${SQ_COMP_PARAMS:-"${SQ_COMP_PARAMS_OPT[${SQ_COMP}]}"}
else
  SQ_COMP_PARAMS=${SQ_COMP_PARAMS:-"${SQ_COMP_PARAMS_DEF[${SQ_COMP}]}"}
fi

# Check rsync progress report capability:
if [ -z "$(rsync  --info=progress2 2>&1 |grep "unknown option")" ]; then
  # Use recent rsync to display some progress:
  RSYNCREP="--no-inc-recursive --info=progress2"
else
  # Remain silent if we have an older rsync:
  RSYNCREP=" "
fi

# What to trim from the ISO file (none, doc, mandoc, bloat):
if [ "${LIVEDE}" == "XFCE" ] ; then
  TRIM=${TRIM:-"bloat"}
elif [ "${LIVEDE}" == "LEAN" ] ; then
  TRIM=${TRIM:-"doc"}
else
  TRIM=${TRIM:-"none"}
fi

# Determine possible blacklist to use:
if [ -z "${BLACKLIST}" ]; then
  eval BLACKLIST=\$BLACKLIST_${LIVEDE}
fi

# Create output directory for image file:
mkdir -p "${OUTPUT}"
if [ $? -ne 0 ]; then
  echo "-- Creation of output directory '${OUTPUT}' failed! Exiting."
  exit 1
fi

# If so requested, we generate the ISO image and immediately exit.
if [ "$ONLY_ISO" = "YES" -a -n "${LIVE_STAGING}" ]; then
  create_iso ${LIVE_STAGING}
  cleanup
  exit 0
else
  # Remove ./boot - it will be created from scratch later:
  rm -rf ${LIVE_STAGING}/boot
fi

# Cleanup if we are FORCEd to rebuild from scratch:
if [ "$FORCE" = "YES" ]; then
  echo "-- Removing old files and directories!"
  umount ${LIVE_ROOTDIR}/{proc,sys,dev} 2>${DBGOUT} || true
  umount ${LIVE_ROOTDIR} 2>${DBGOUT} || true
  rm -rf ${LIVE_STAGING}/${LIVEMAIN} ${LIVE_WORK} ${LIVE_ROOTDIR}
fi

# Create temporary directories for building the live filesystem:
for LTEMP in $LIVE_OVLDIR $LIVE_BOOT $LIVE_MOD_SYS $LIVE_MOD_ADD $LIVE_MOD_OPT ; do
  umount ${LTEMP} 2>${DBGOUT} || true
  mkdir -p ${LTEMP}
  if [ $? -ne 0 ]; then
    echo "-- Creation of temporary directory '${LTEMP}' failed! Exiting."
    exit 1
  fi
done

# Create the mount point for our Slackware filesystem:
if [ ! -d ${LIVE_ROOTDIR} ]; then
  mkdir -p ${LIVE_ROOTDIR}
  if [ $? -ne 0 ]; then
    echo "-- Creation of moint point '${LIVE_ROOTDIR}' failed! Exiting."
    exit 1
  fi
  chmod 775 ${LIVE_ROOTDIR}
else
  echo "-- Found an existing live root directory at '${LIVE_ROOTDIR}'".
  echo "-- Check the content and deal with it, then remove that directory."
  echo "-- Exiting now."
  exit 1
fi

# ----------------------------------------------------------------------------
# Install package series:
# ----------------------------------------------------------------------------

unset INSTDIR
RODIRS="${LIVE_BOOT}"
# Create the verification file for the install_pkgs function:
echo "${THEDATE} (${BUILDER})" > ${LIVE_BOOT}/${MARKER}

# Determine which module sequence we have to build:
case "$LIVEDE" in
  SLACKWARE) MSEQ="${SEQ_SLACKWARE}" ;;
       XFCE) MSEQ="${SEQ_XFCEBASE}" ;;
       LEAN) MSEQ="${SEQ_LEAN}" ;;
        DAW) MSEQ="${SEQ_DAW}" ;;
      KTOWN) MSEQ="${SEQ_KTOWN}" ;;
       MATE) MSEQ="${SEQ_MSB}" ;;
   CINNAMON) MSEQ="${SEQ_CIN}" ;;
      DLACK) MSEQ="${SEQ_DLACK}" ;;
 STUDIOWARE) MSEQ="${SEQ_STUDW}" ;;
          *) if [ -n "${SEQ_CUSTOM}" ]; then
               # Custom distribution with a predefined package list:
               MSEQ="${SEQ_CUSTOM}"              
             else
               echo "** Unsupported configuration '$LIVEDE'"; exit 1
             fi
             ;;
esac

# Do we need to create/include additional module(s) defined by a pkglist:
if [ -n "$SEQ_ADDMOD" ]; then
  echo "-- Adding ${SEQ_ADDMOD}."
  MSEQ="${MSEQ} ${SEQ_ADDMOD}"
fi

# Do we need to include multilib?
# Add these last so we can easily distribute the module separately.
if [ "$MULTILIB" = "YES" ]; then
  if ! echo ${MSEQ} |grep -qw multilib ; then
    echo "-- Adding multilib."
    MSEQ="${MSEQ} pkglist:multilib"
  fi
fi

echo "-- Creating liveslak ${VERSION} '${LIVEDE}' image (based on ${DISTRO^}-${SL_VERSION} ${SL_ARCH})."

# Module sequence can be composed of multiple sub-sequences:
for MSUBSEQ in ${MSEQ} ; do

  SL_SERIES="$(echo ${MSUBSEQ} |cut -d: -f2 |tr , ' ')"
  # MTYPE can be "tagfile", "local" or "pkglist"
  # If MTYPE was not specified, by default it is "pkglist":
  MTYPE="$(echo ${MSUBSEQ} |cut -d: -f1 |tr , ' ')"
  if [ "${MTYPE}" = "${SL_SERIES}" ]; then MTYPE="pkglist" ; fi

  # We prefix our own modules based on the source of the package list:
  case "$MTYPE" in
    tagfile) MNUM="0010" ;;
    pkglist) MNUM="0020" ;;
      local) MNUM="0030" ;;
          *) echo "** Unknown package source '$MTYPE'"; exit 1 ;;
  esac

for SPS in ${SL_SERIES} ; do

  INSTDIR=${LIVE_WORK}/${SPS}_$$
  mkdir -p ${INSTDIR}

  if [ "$FORCE" = "YES" -o $(echo ${REFRESH} |grep -wq ${SPS} ; echo $?) -eq 0 -o ! -f ${LIVE_MOD_SYS}/${MNUM}-${DISTRO}_${SPS}-${SL_VERSION}-${SL_ARCH}.sxz ]; then

    # Following conditions trigger creation of the squashed module:
    # - commandline switch '-f' was used, or;
    # - the module was mentioned in the '-r' commandline switch, or;
    # - the module does not yet exist.

    # Create the verification file for the install_pkgs function:
    echo "${THEDATE} (${BUILDER})" > ${INSTDIR}/${MARKER}

    echo "-- Installing the '${SPS}' series."
    umount ${LIVE_ROOTDIR} 2>${DBGOUT} || true
    mount -t overlay -o lowerdir=${RODIRS},upperdir=${INSTDIR},workdir=${LIVE_OVLDIR} overlay ${LIVE_ROOTDIR}

    # Install the package series:
    install_pkgs ${SPS} ${LIVE_ROOTDIR} ${MTYPE}
    umount ${LIVE_ROOTDIR} || true

    if [ "$SPS" = "a" -o "$SPS" = "${MINLIST}" ]; then

      # We need to take care of a few things first:
      if [ "$SL_ARCH" = "x86_64" -o "$SMP32" = "NO" ]; then
        KGEN=$(ls --indicator-style=none ${INSTDIR}/var/log/packages/kernel*modules* |grep -v smp |head -1 |rev | cut -d- -f3 |tr _ - |rev)
        KVER=$(ls --indicator-style=none ${INSTDIR}/lib/modules/ |grep -v smp |head -1)
      else
        KGEN=$(ls --indicator-style=none ${INSTDIR}/var/log/packages/kernel*modules* |grep smp |head -1 |rev | cut -d- -f3 |tr _ - |rev)
        KVER=$(ls --indicator-style=none ${INSTDIR}/lib/modules/ |grep smp |head -1)
      fi
      if [ -z "$KVER" ]; then
        echo "-- Could not find installed kernel in '${INSTDIR}'! Exiting."
        cleanup
        exit 1
      else
        # Move the content of the /boot directory out of the minimal system,
        # this will be joined again using overlay:
        rm -rf ${LIVE_BOOT}/boot
        mv ${INSTDIR}/boot ${LIVE_BOOT}/
        # Squash the boot files into a module as a safeguard:
        mksquashfs ${LIVE_BOOT} ${LIVE_MOD_SYS}/0000-${DISTRO}_boot-${SL_VERSION}-${SL_ARCH}.sxz -noappend -comp ${SQ_COMP} ${SQ_COMP_PARAMS}
      fi

    fi

    # Squash the installed package series into a module:
    mksquashfs ${INSTDIR} ${LIVE_MOD_SYS}/${MNUM}-${DISTRO}_${SPS}-${SL_VERSION}-${SL_ARCH}.sxz -noappend -comp ${SQ_COMP} ${SQ_COMP_PARAMS}
    rm -rf ${INSTDIR}/*

    # End result: we have our .sxz file and the INSTDIR is empty again,
    # Next step is to loop-mount the squashfs file onto INSTDIR.

  elif [ "$SPS" = "a" -o "$SPS" = "${MINLIST}" ]; then

    # We need to do a bit more if we skipped creation of 'a' or 'min' module:
    # Extract the content of the /boot directory out of the boot module,
    # else we don't have a /boot ready when we create the ISO.
    # We can not just loop-mount it because we need to write into /boot later:
    rm -rf ${LIVE_BOOT}/boot
    unsquashfs -dest ${LIVE_BOOT}/boottemp ${LIVE_MOD_SYS}/0000-${DISTRO}_boot-${SL_VERSION}-${SL_ARCH}.sxz
    mv ${LIVE_BOOT}/boottemp/* ${LIVE_BOOT}/
    rmdir ${LIVE_BOOT}/boottemp

  fi

  # Add the package series tree to the readonly lowerdirs for the overlay:
  RODIRS="${INSTDIR}:${RODIRS}"

  # Mount the modules for use in the final assembly of the ISO:
  mount -t squashfs -o loop ${LIVE_MOD_SYS}/${MNUM}-${DISTRO}_${SPS}-${SL_VERSION}-${SL_ARCH}.sxz ${INSTDIR}

done
done

# ----------------------------------------------------------------------------
# Modules for all package series are created and loop-mounted.
# Next: system configuration.
# ----------------------------------------------------------------------------

# Configuration mudule will always be created from scratch:
INSTDIR=${LIVE_WORK}/zzzconf_$$
mkdir -p ${INSTDIR}

# -------------------------------------------------------------------------- #
echo "-- Configuring the base system."
# -------------------------------------------------------------------------- #

umount ${LIVE_ROOTDIR} 2>${DBGOUT} || true
mount -t overlay -o lowerdir=${RODIRS},upperdir=${INSTDIR},workdir=${LIVE_OVLDIR} overlay ${LIVE_ROOTDIR}

# Determine the kernel version in the Live OS:
if [ "$SL_ARCH" = "x86_64" -o "$SMP32" = "NO" ]; then
  KGEN=$(ls --indicator-style=none ${LIVE_ROOTDIR}/var/log/packages/kernel*modules* |grep -v smp |head -1 |rev | cut -d- -f3 |tr _ - |rev)
  KVER=$(ls --indicator-style=none ${LIVE_ROOTDIR}/lib/modules/ |grep -v smp |head -1)
else
  KGEN=$(ls --indicator-style=none ${LIVE_ROOTDIR}/var/log/packages/kernel*modules* |grep smp |head -1 |rev | cut -d- -f3 |tr _ - |rev)
  KVER=$(ls --indicator-style=none ${LIVE_ROOTDIR}/lib/modules/ |grep smp |head -1)
fi

# Configure hostname and network:
echo "${LIVE_HOSTNAME}.example.net" > ${LIVE_ROOTDIR}/etc/HOSTNAME
if [ -f ${LIVE_ROOTDIR}/etc/NetworkManager/NetworkManager.conf ]; then
  sed -i -e "s/^hostname=.*/hostname=${LIVE_HOSTNAME}/" \
    ${LIVE_ROOTDIR}/etc/NetworkManager/NetworkManager.conf
fi
sed -e "s/^\(127.0.0.1\t*\)darkstar.*/\1${LIVE_HOSTNAME}.example.net ${LIVE_HOSTNAME}/" \
  -i ${LIVE_ROOTDIR}/etc/hosts

# Make sure we can access DNS straight away:
cat <<EOT >> ${LIVE_ROOTDIR}/etc/resolv.conf
nameserver 8.8.4.4
nameserver 8.8.8.8

EOT

# Configure default locale (script-default is 'en_US.utf8). Note that there
# is 'UTF-8' versus 'utf8' and while the former has a preference, there is
# no functional difference between the two when using Linux glibc.
# This setting can be overridden on boot:
if grep -q "^ *export LANG=" ${LIVE_ROOTDIR}/etc/profile.d/lang.sh ; then
  sed -e "s/^ *export LANG=.*/export LANG=${DEF_LOCALE}/" -i ${LIVE_ROOTDIR}/etc/profile.d/lang.sh
else
  echo "export LANG=${DEF_LOCALE}" >> ${LIVE_ROOTDIR}/etc/profile.d/lang.sh
fi
# Does not hurt to also add systemd compatible configuration:
echo "LANG=${DEF_LOCALE}" > ${LIVE_ROOTDIR}/etc/locale.conf
echo "KEYMAP=${DEF_KBD}" > ${LIVE_ROOTDIR}/etc/vconsole.conf

# Set timezone to UTC, mimicking the 'timeconfig' script in Slackware:
ln -s /usr/share/zoneinfo/UTC ${LIVE_ROOTDIR}/etc/localtime
# Could be absent so 'rm -f' to avoid script aborts:
rm -f ${LIVE_ROOTDIR}/etc/localtime-copied-from

# Qt5 expects '/etc/localtime' to be a symlink. If this is a real file,
# it causes Qt5 timezone detection to fail so that "UTC" will be returned
# always.  However if a file '/etc/timezone' exists, Qt5 will use that.
# We add the file and update the 'timeconfig' script accordingly:
echo "UTC" > ${LIVE_ROOTDIR}/etc/timezone
sed -i -n "p;s/^\( *\)rm -f localtime$/\1echo \$TZ > timezone/p" \
  ${LIVE_ROOTDIR}/usr/sbin/timeconfig

# Configure the hardware clock to be interpreted as UTC as well:
cat <<EOT > ${LIVE_ROOTDIR}/etc/hardwareclock 
# /etc/hardwareclock
#
# Tells how the hardware clock time is stored.
# You should run timeconfig to edit this file.

UTC
EOT

# Configure a nice default console font that can handle Unicode:
cat <<EOT >${LIVE_ROOTDIR}/etc/rc.d/rc.font
#!/bin/sh
#
# This selects your default screen font from among the ones in
# /usr/share/kbd/consolefonts.
#
#setfont -v

# Use Terminus font to work better with the Unicode-enabled console
# (configured in /etc/lilo.conf)
setfont -v ter-120b
EOT
chmod +x ${LIVE_ROOTDIR}/etc/rc.d/rc.font

# Enable mouse support in runlevel 3:
cat <<"EOM" > ${LIVE_ROOTDIR}/etc/rc.d/rc.gpm
#!/bin/sh
# Start/stop/restart the GPM mouse server:
if [ -x /usr/sbin/gpm ]; then
  MTYPE="imps2"
  if [ "$1" = "stop" ]; then
    echo "Stopping gpm..."
    /usr/sbin/gpm -k
  elif [ "$1" = "restart" ]; then
    echo "Restarting gpm..."
    /usr/sbin/gpm -k
    sleep 1
    /usr/sbin/gpm -m /dev/mouse -t ${MTYPE}
  else # assume $1 = start:
    echo "Starting gpm:  /usr/sbin/gpm -m /dev/mouse -t ${MTYPE}"
    /usr/sbin/gpm -m /dev/mouse -t ${MTYPE}
  fi
fi
EOM
chmod +x ${LIVE_ROOTDIR}/etc/rc.d/rc.gpm

# Remove ssh server keys - new unique keys will be generated
# at first boot of the live system: 
rm -f ${LIVE_ROOTDIR}/etc/ssh/*key*

# Sanitize /etc/fstab :
cat <<EOT > ${LIVE_ROOTDIR}/etc/fstab
proc      /proc       proc        defaults   0   0
sysfs     /sys        sysfs       defaults   0   0
tmpfs     /tmp        tmpfs       defaults,nodev,nosuid,mode=1777  0   0
tmpfs     /var/tmp    tmpfs       defaults,nodev,nosuid,mode=1777  0   0
tmpfs     /dev/shm    tmpfs       defaults,nodev,nosuid,mode=1777  0   0
devpts    /dev/pts    devpts      gid=5,mode=620   0   0
none      /           tmpfs       defaults   1   1

EOT

# Prevent loop devices (sxz modules) from appearing in filemanagers:
mkdir -p ${LIVE_ROOTDIR}/etc/udev/rules.d
cat <<EOL > ${LIVE_ROOTDIR}/etc/udev/rules.d/11-local.rules
# Prevent loop devices (mounted sxz modules) from appearing in
# filemanager panels - http://www.seguridadwireless.net

# Hidden loops for udisks:
KERNEL=="loop*", ENV{UDISKS_PRESENTATION_HIDE}="1"

# Hidden loops for udisks2:
KERNEL=="loop*", ENV{UDISKS_IGNORE}="1"
EOL

# Set a root password.
echo "root:${ROOTPW}" | chroot ${LIVE_ROOTDIR} /usr/sbin/chpasswd

# Create group and user for the nvidia persistence daemon:
if ! chroot ${LIVE_ROOTDIR} /usr/bin/getent passwd ${NVUID} > /dev/null 2>&1 ;
then
  chroot ${LIVE_ROOTDIR} /usr/sbin/groupadd -g ${NVGRPNR} ${NVGRP}
  chroot ${LIVE_ROOTDIR} /usr/sbin/useradd -c "Nvidia persistence" -u ${NVUIDNR} -g ${NVGRPNR} -d /dev/null -s /bin/false ${NVUID}
  echo "${NVUID}:$(openssl rand -base64 12)" | chroot ${LIVE_ROOTDIR} /usr/sbin/chpasswd
fi

# Determine the full name of the live account in the image:
if [ -z "${LIVEUIDFN}" ]; then
  eval LIVEUIDFN=\$LIVEUIDFN_${LIVEDE}
  if [ -z "${LIVEUIDFN}" ]; then
    LIVEUIDFN="${DISTRO^} Live User"
  fi
fi

# Create a nonprivileged user account (called "live" by default):
chroot ${LIVE_ROOTDIR} /usr/sbin/useradd -c "${LIVEUIDFN}" -g users -G wheel,audio,cdrom,floppy,plugdev,video,power,netdev,lp,scanner,kmem,dialout,games,disk,input -u ${LIVEUIDNR} -d /home/${LIVEUID} -m -s /bin/bash ${LIVEUID}
echo "${LIVEUID}:${LIVEPW}" | chroot ${LIVE_ROOTDIR} /usr/sbin/chpasswd

# Configure suauth if we are not on a PAM system (where this does not work):
if [ ! -L ${LIVE_ROOTDIR}/lib${DIRSUFFIX}/libpam.so.? ]; then
  cat <<EOT >${LIVE_ROOTDIR}/etc/suauth
root:${LIVEUID}:OWNPASS
root:ALL EXCEPT GROUP wheel:DENY
EOT
  chmod 600 ${LIVE_ROOTDIR}/etc/suauth
fi

# Configure sudoers:
chmod 640 ${LIVE_ROOTDIR}/etc/sudoers
sed -i ${LIVE_ROOTDIR}/etc/sudoers -e 's/# *\(%wheel\sALL=(ALL)\sALL\)/\1/'
chmod 440 ${LIVE_ROOTDIR}/etc/sudoers

# Add some convenience to the bash shell:
mkdir -p  ${LIVE_ROOTDIR}/etc/skel/
cat << "EOT" > ${LIVE_ROOTDIR}/etc/skel/.bashrc
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# Check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
EOT
cat << "EOT" > ${LIVE_ROOTDIR}/etc/skel/.profile
# Source a .bashrc if it exists:
[[ -r ~/.bashrc ]] && . ~/.bashrc

# Define some useful aliases:
alias ll="ls -la $LS_OPTIONS"
lsp() { basename $(ls -1 "/var/log/packages/$@"*) ; }
alias md="mkdir"
alias tarview="tar -tvf"
# GREP_OPTIONS="--color=auto" is deprecated, use alias to enable colored output:
alias grep="grep --color=auto"
alias fgrep="fgrep --color=auto"
alias egrep="egrep --color=auto"

# Ctrl-D should not log us off immediately; now it needs 10 times:
set -o ignoreeof
EOT

# Do the root account the same favor:
cat ${LIVE_ROOTDIR}/etc/skel/.profile > ${LIVE_ROOTDIR}/root/.profile
chown root:root ${LIVE_ROOTDIR}/root/.profile

# If the 'vi' symlink doees not exist, add it:
if [ ! -e ${LIVE_ROOTDIR}/usr/bin/vi ]; then
  if [ -x ${LIVE_ROOTDIR}/usr/bin/elvis ]; then
    ln -s elvis ${LIVE_ROOTDIR}/usr/bin/vi
  else
    ln -s vim ${LIVE_ROOTDIR}/usr/bin/vi
  fi
fi

if [ -f ${LIVE_ROOTDIR}/etc/rc.d/rc.networkmanager ]; then
  # Enable NetworkManager if present:
  chmod +x ${LIVE_ROOTDIR}/etc/rc.d/rc.networkmanager
  # And disable Slackware's own way of configuring eth0:
  cat <<EOT > ${LIVE_ROOTDIR}/etc/rc.d/rc.inet1.conf
IFNAME[0]="eth0"
IPADDR[0]=""
NETMASK[0]=""
USE_DHCP[0]=""
DHCP_HOSTNAME[0]=""

GATEWAY=""
DEBUG_ETH_UP="no"
EOT

  # Ensure that NetworkManager uses its internal DHCP client - seems to give
  # better compliancy:
  sed -e "s/^dhcp=dhcpcd/#&/" -e "s/^#\(dhcp=internal\)/\1/" \
      -i ${LIVE_ROOTDIR}/etc/NetworkManager/NetworkManager.conf

else
  # Use Slackware's own network configurion routing for eth0 in the base image:
  cat <<EOT > ${LIVE_ROOTDIR}/etc/rc.d/rc.inet1.conf
IFNAME[0]="eth0"
IPADDR[0]=""
NETMASK[0]=""
USE_DHCP[0]="yes"
DHCP_HOSTNAME[0]="${LIVE_HOSTNAME}"

GATEWAY=""
DEBUG_ETH_UP="no"
EOT
fi

# Enable a Slackware mirror for slackpkg:
cat <<EOT >> ${LIVE_ROOTDIR}/etc/slackpkg/mirrors
#http://mirrors.slackware.com/slackware/slackware${DIRSUFFIX}-${SL_VERSION}/
http://ftp.osuosl.org/.2/slackware/slackware${DIRSUFFIX}-${SL_VERSION}/
EOT

## Blacklist the l10n packages;
#cat << EOT >> ${LIVE_ROOTDIR}/etc/slackpkg/blacklist
#
## Blacklist the l10n packages;
#calligra-l10n-
#kde-l10n-
#
#EOT

# If we added slackpkg+ for easier system management, let's configure it too.
# Update the cache for slackpkg:
echo "-- Creating slackpkg cache, takes a few seconds..."
chroot "${LIVE_ROOTDIR}" /bin/bash <<EOSL 2>${DBGOUT}

if [ -f var/log/packages/slackpkg+-* ] ; then
  cat <<EOPL > etc/slackpkg/slackpkgplus.conf
SLACKPKGPLUS=on
VERBOSE=1
ALLOW32BIT=off
USEBL=1
WGETOPTS="--timeout=20 --tries=2"
GREYLIST=on
PKGS_PRIORITY=( restricted alienbob ktown mate )
REPOPLUS=( slackpkgplus restricted alienbob ktown mate )
MIRRORPLUS['slackpkgplus']=http://slakfinder.org/slackpkg+/
MIRRORPLUS['restricted']=http://slackware.nl/people/alien/restricted_sbrepos/${SL_VERSION}/${SL_ARCH}/
MIRRORPLUS['alienbob']=http://slackware.nl/people/alien/sbrepos/${SL_VERSION}/${SL_ARCH}/
MIRRORPLUS['mate']=http://slackware.uk/msb/${SL_VERSION}/latest/${SL_ARCH}/ 
#MIRRORPLUS['studioware']=http://slackware.uk/studioware/${SL_VERSION}/ 
EOPL
  # Use the appropriate ktown variant:
  eval $( grep "^ *VARIANT=" ${LIVE_TOOLDIR}/pkglists/ktown.conf)
  if [ "$VARIANT" = "testing" ]; then
    cat <<EOPL >> etc/slackpkg/slackpkgplus.conf
#MIRRORPLUS['ktown']=http://slackware.nl/alien-kde/${SL_VERSION}/latest/${SL_ARCH}/
MIRRORPLUS['ktown']=http://slackware.nl/alien-kde/${SL_VERSION}/testing/${SL_ARCH}/
EOPL
  else
    cat <<EOPL >> etc/slackpkg/slackpkgplus.conf
#MIRRORPLUS['ktown']=http://slackware.nl/alien-kde/${SL_VERSION}/testing/${SL_ARCH}/
MIRRORPLUS['ktown']=http://slackware.nl/alien-kde/${SL_VERSION}/latest/${SL_ARCH}/
EOPL
  fi
fi

# Slackpkg wants you to opt-in on slackware-current:
if [ "${SL_VERSION}" = "current" ]; then
  mkdir -p /var/lib/slackpkg
  touch /var/lib/slackpkg/current
fi

ARCH=${SL_ARCH} /usr/sbin/slackpkg -batch=on update gpg
ARCH=${SL_ARCH} /usr/sbin/slackpkg -batch=on update
# Let any lingering .new files replace their originals:
yes o | ARCH=${SL_ARCH} /usr/sbin/slackpkg new-config

EOSL

# Add our scripts to the Live OS:
mkdir -p  ${LIVE_ROOTDIR}/usr/local/sbin
install -m0755 ${LIVE_TOOLDIR}/makemod ${LIVE_TOOLDIR}/iso2usb.sh ${LIVE_TOOLDIR}/upslak.sh ${LIVE_ROOTDIR}/usr/local/sbin/

# Add PXE Server infrastructure:
mkdir -p ${LIVE_ROOTDIR}/var/lib/tftpboot/pxelinux.cfg
cp -ia /usr/share/syslinux/pxelinux.0 ${LIVE_ROOTDIR}/var/lib/tftpboot/
ln -s /mnt/livemedia/boot/generic ${LIVE_ROOTDIR}/var/lib/tftpboot/
ln -s /mnt/livemedia/boot/initrd.img ${LIVE_ROOTDIR}/var/lib/tftpboot/
mkdir -p ${LIVE_ROOTDIR}/var/lib/tftpboot/EFI/BOOT
ln -s /mnt/livemedia/EFI/BOOT ${LIVE_ROOTDIR}/var/lib/tftpboot/uefi
ln -s /mnt/livemedia/EFI/BOOT/bootx64.efi ${LIVE_ROOTDIR}/var/lib/tftpboot/EFI/BOOT/
cat ${LIVE_TOOLDIR}/pxeserver.tpl | sed \
  -e "s/@DIRSUFFIX@/$DIRSUFFIX/g" \
  -e "s/@DISTRO@/$DISTRO/g" \
  -e "s/@CDISTRO@/${DISTRO^}/g" \
  -e "s/@UDISTRO@/${DISTRO^^}/g" \
  -e "s/@KVER@/$KVER/g" \
  -e "s/@LIVEDE@/$LIVEDE/g" \
  -e "s/@LIVEMAIN@/$LIVEMAIN/g" \
  -e "s/@MARKER@/$MARKER/g" \
  -e "s/@SL_VERSION@/$SL_VERSION/g" \
  -e "s/@VERSION@/$VERSION/g" \
  > ${LIVE_ROOTDIR}/usr/local/sbin/pxeserver
chmod 755 ${LIVE_ROOTDIR}/usr/local/sbin/pxeserver

# Add a harddisk installer to the ISO.
# The huge kernel does not require an initrd and installation to the
# hard drive will not be complicated, so a liveslak install is recommended
# for newbies only if the ISO contains huge kernel...
if [ -f ${DEF_SL_PKGROOT}/../isolinux/initrd.img ]; then
  echo "-- Adding 'setup2hd' hard disk installer to /usr/share/${LIVEMAIN}/."
  # Extract the 'setup' files we need from the Slackware installer
  # and move them to a single directory in the ISO:
  mkdir -p ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}
  cd  ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}
    uncompressfs ${DEF_SL_PKGROOT}/../isolinux/initrd.img | cpio -i -d -m -H newc usr/lib/setup/* sbin/probe sbin/fixdate
    mv -i usr/lib/setup/* sbin/probe sbin/fixdate .
    rm -r usr sbin
    rm -f setup
  cd - 1>/dev/null
  # Fix some occurrences of '/mnt' that should not be used in the Live ISO
  # (this was applied in Slackware > 14.2 but does not harm to do this anyway):
  sed -i ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/* \
    -e 's,T_PX=/mnt,T_PX="`cat $TMP/SeTT_PX`",g' \
    -e 's, /mnt, ${T_PX},g' \
    -e 's,=/mnt$,=${T_PX},g' \
    -e 's,=/mnt/,=${T_PX}/,g'
  # Allow a choice of dialog:
  sed -i ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/* \
    -e '1a \\nDIALOG=${DIALOG:-dialog}\n' \
    -e 's/dialog -/${DIALOG} -/'
  # If T_PX is used in a script, it should be defined first:
  for FILE in ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/* ; do
    if grep -q T_PX $FILE ; then
      if ! grep -q "^T_PX=" $FILE ; then
        if ! grep -q "^TMP=" $FILE ; then
          sed -e '/#!/a T_PX="`cat $TMP/SeTT_PX`"' -i $FILE
          sed -e '/#!/a TMP=/var/log/setup/tmp' -i $FILE
        else
          sed -e '/^TMP=/a T_PX="`cat $TMP/SeTT_PX`"' -i $FILE
        fi
      fi
    fi
  done
  if [ -f ${LIVE_ROOTDIR}/sbin/liloconfig ]; then
    if [ -f ${LIVE_TOOLDIR}/patches/liloconfig_${SL_VERSION}.patch ]; then
      LILOPATCH=liloconfig_${SL_VERSION}.patch
    elif [ -f ${LIVE_TOOLDIR}/patches/liloconfig.patch ]; then
      LILOPATCH=liloconfig.patch
    else
      LILOPATCH=""
    fi
    if [ -n "${LILOPATCH}" ]; then
      patch ${LIVE_ROOTDIR}/sbin/liloconfig ${LIVE_TOOLDIR}/patches/${LILOPATCH}
    fi
  fi
  if [ -f ${LIVE_ROOTDIR}/usr/sbin/eliloconfig ]; then
    if [ -f ${LIVE_TOOLDIR}/patches/eliloconfig_${SL_VERSION}.patch ]; then
      ELILOPATCH=eliloconfig_${SL_VERSION}.patch
    elif  [ -f ${LIVE_TOOLDIR}/patches/eliloconfig.patch ]; then
      ELILOPATCH=eliloconfig.patch
    else
      ELILOPATCH=""
    fi
    if [ -n "${ELILOPATCH}" ]; then
      patch ${LIVE_ROOTDIR}/usr/sbin/eliloconfig ${LIVE_TOOLDIR}/patches/${ELILOPATCH}
    fi
  fi
  # Fix some occurrences of '/usr/lib/setup/' that are covered by $PATH:
  sed -i -e 's,/usr/lib/setup/,,g' -e 's,:/usr/lib/setup,:/usr/share/${LIVEMAIN},g' ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/*
  # Prevent SeTconfig from asking redundant questions after a Live OS install:
  sed -i ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/SeTconfig \
    -e '/.\/var\/log\/setup\/$SCRIPT $T_PX $ROOT_DEVICE/i # Skip stuff that was taken care of by liveslak\nif [ -f $TMP/SeTlive ] && echo $SCRIPT |grep -qE "(make-bootdisk|mouse|setconsolefont|xwmconfig)"; then true; else' \
    -e '/.\/var\/log\/setup\/$SCRIPT $T_PX $ROOT_DEVICE/a fi'
  # Add the Slackware Live HD installer scripts:
  for USCRIPT in SeTuacct SeTudiskpart SeTumedia SeTupass SeTpasswd setup.liveslak setup.slackware ; do
    cat ${LIVE_TOOLDIR}/setup2hd/${USCRIPT}.tpl | sed \
      -e "s/@DIRSUFFIX@/$DIRSUFFIX/g" \
      -e "s/@DISTRO@/$DISTRO/g" \
      -e "s/@CDISTRO@/${DISTRO^}/g" \
      -e "s/@UDISTRO@/${DISTRO^^}/g" \
      -e "s/@KVER@/$KVER/g" \
      -e "s/@LIVEDE@/$LIVEDE/g" \
      -e "s/@LIVEMAIN@/$LIVEMAIN/g" \
      -e "s/@LIVEUID@/$LIVEUID/g" \
      -e "s/@LIVEUIDNR@/$LIVEUIDNR/g" \
      -e "s/@MARKER@/$MARKER/g" \
      -e "s/@SL_VERSION@/$SL_VERSION/g" \
      -e "s/@VERSION@/$VERSION/g" \
      > ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${USCRIPT}
    chmod 755 ${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/${USCRIPT}
  done
  mkdir -p ${LIVE_ROOTDIR}/usr/local/sbin
  cat ${LIVE_TOOLDIR}/setup2hd.tpl | sed \
    -e "s/@DIRSUFFIX@/$DIRSUFFIX/g" \
    -e "s/@DISTRO@/$DISTRO/g" \
    -e "s/@CDISTRO@/${DISTRO^}/g" \
    -e "s/@UDISTRO@/${DISTRO^^}/g" \
    -e "s/@KVER@/$KVER/g" \
    -e "s/@LIVEDE@/$LIVEDE/g" \
    -e "s/@LIVEMAIN@/$LIVEMAIN/g" \
    -e "s/@LIVEUID@/$LIVEUID/g" \
    -e "s/@LIVEUIDNR@/$LIVEUIDNR/g" \
    -e "s/@MARKER@/$MARKER/g" \
    -e "s/@SL_VERSION@/$SL_VERSION/g" \
    -e "s/@VERSION@/$VERSION/g" \
    > ${LIVE_ROOTDIR}/usr/local/sbin/setup2hd
  chmod 755 ${LIVE_ROOTDIR}/usr/local/sbin/setup2hd
  # Slackware Live HD post-install customization hook:
  if [ -f ${LIVE_TOOLDIR}/setup2hd.local.tpl ]; then
    # The '.local' suffix means: install it as a sample file only:
    HOOK_SRC="${LIVE_TOOLDIR}/setup2hd.local.tpl"
    HOOK_DST="${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/setup2hd.$DISTRO.sample"
  elif [ -f ${LIVE_TOOLDIR}/setup2hd.$DISTRO ]; then
    # Install the hook; the file will be sourced by "setup2hd".
    HOOK_SRC="${LIVE_TOOLDIR}/setup2hd.$DISTRO"
    HOOK_DST="${LIVE_ROOTDIR}/usr/share/${LIVEMAIN}/setup2hd.$DISTRO"
  fi
  cat ${HOOK_SRC} | sed \
    -e "s/@DIRSUFFIX@/$DIRSUFFIX/g" \
    -e "s/@DISTRO@/$DISTRO/g" \
    -e "s/@CDISTRO@/${DISTRO^}/g" \
    -e "s/@UDISTRO@/${DISTRO^^}/g" \
    -e "s/@KVER@/$KVER/g" \
    -e "s/@LIVEDE@/$LIVEDE/g" \
    -e "s/@LIVEMAIN@/$LIVEMAIN/g" \
    -e "s/@MARKER@/$MARKER/g" \
    -e "s/@SL_VERSION@/$SL_VERSION/g" \
    -e "s/@VERSION@/$VERSION/g" \
    > ${HOOK_DST}
  chmod 644 ${HOOK_DST}
 else
  echo "-- Could not find ${DEF_SL_PKGROOT}/../isolinux/initrd.img - not adding 'setup2hd'!"
fi

# Add the documentation:
mkdir -p  ${LIVE_ROOTDIR}/usr/doc/liveslak-${VERSION}
install -m0644 ${LIVE_TOOLDIR}/README* ${LIVE_ROOTDIR}/usr/doc/liveslak-${VERSION}/
mkdir -p  ${LIVE_ROOTDIR}/usr/doc/${DISTRO}${DIRSUFFIX}-${SL_VERSION}
install -m0644 \
  ${DEF_SL_PKGROOT}/../{CHANGES_AND_HINTS,COPY,README,RELEASE_NOTES,*HOWTO}* \
  ${DEF_SL_PKGROOT}/../usb-and-pxe-installers/README* \
  ${LIVE_ROOTDIR}/usr/doc/${DISTRO}${DIRSUFFIX}-${SL_VERSION}/

# -------------------------------------------------------------------------- #
echo "-- Configuring the X base system."
# -------------------------------------------------------------------------- #

# Reduce the number of local consoles, two should be enough:
sed -i -e '/^c3\|^c4\|^c5\|^c6/s/^/# /' ${LIVE_ROOTDIR}/etc/inittab

# Also treat members of the 'wheel' group as admins next to root:
mkdir -p ${LIVE_ROOTDIR}/etc/polkit-1/rules.d
cat <<EOT > ${LIVE_ROOTDIR}/etc/polkit-1/rules.d/10-wheel-admin.rules
polkit.addAdminRule(function(action, subject) {
    return ["unix-group:wheel"];
});
EOT

# Give the 'live' user a face:
if [ -f "${LIVE_TOOLDIR}/media/${LIVEDE,,}/icons/default.png" ]; then
  # Use custom face icon if available for the Live variant:
  FACE_ICON="${LIVE_TOOLDIR}/media/${LIVEDE,,}/icons/default.png"
else
  # Use the default Slackware blue 'S':
  FACE_ICON="${LIVE_TOOLDIR}/blueSW-64px.png"
fi
convert ${FACE_ICON} -resize 64x64 - >${LIVE_ROOTDIR}/home/${LIVEUID}/.face.icon
chown --reference=${LIVE_ROOTDIR}/home/${LIVEUID} ${LIVE_ROOTDIR}/home/${LIVEUID}/.face.icon
( cd ${LIVE_ROOTDIR}/home/${LIVEUID}/ ; ln .face.icon .face )
mkdir -p ${LIVE_ROOTDIR}/usr/share/apps/kdm/pics/users
convert ${FACE_ICON} -resize 64x64 - >${LIVE_ROOTDIR}/usr/share/apps/kdm/pics/users/blues.icon

# Give XDM a nicer look:
mkdir -p ${LIVE_ROOTDIR}/etc/X11/xdm/liveslak-xdm
cp -a ${LIVE_TOOLDIR}/xdm/* ${LIVE_ROOTDIR}/etc/X11/xdm/liveslak-xdm/
# Point xdm to the custom /etc/X11/xdm/liveslak-xdm/xdm-config:
sed -i ${LIVE_ROOTDIR}/etc/rc.d/rc.4 -e 's,bin/xdm -nodaemon,& -config /etc/X11/xdm/liveslak-xdm/xdm-config,'
# Adapt xdm configuration to target architecture:
sed -i "s/@LIBDIR@/lib${DIRSUFFIX}/g" ${LIVE_ROOTDIR}/etc/X11/xdm/liveslak-xdm/xdm-config

# XDM needs a C preprocessor to calculate the login box position, and if
# the ISO contains mcpp instead of the cpp contained in full gcc, we will
# create a symlink (don't forget to install mcpp of course!):
if [ ! -e ${LIVE_ROOTDIR}/usr/bin/cpp ] && [ -x ${LIVE_ROOTDIR}/usr/bin/mcpp ];
then
  ln -s mcpp ${LIVE_ROOTDIR}/usr/bin/cpp
fi
 
# The Xscreensaver should show a blank screen only, to prevent errors about
# missing modules:
echo "mode:           blank" > ${LIVE_ROOTDIR}/home/${LIVEUID}/.xscreensaver

# Make the EmojiOne TTF font universally available:
mkdir -p ${LIVE_ROOTDIR}/etc/fonts
cat << EOT > ${LIVE_ROOTDIR}/etc/fonts/local.conf
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- /etc/fonts/local.conf file to customize system font access -->
<fontconfig>
<!-- Contains the EmojiOne TTF font: -->
<dir>/usr/lib${DIRSUFFIX}/firefox/fonts</dir>
</fontconfig>
EOT
chroot ${LIVE_ROOTDIR} fc-cache -f

# Allow direct scanning via xsane (no temporary intermediate files) in Gimp:
if [ ! -L ${LIVE_ROOTDIR}/usr/lib${DIRSUFFIX}/gimp/2.0/plug-ins/xsane  ]; then
  mkdir -p ${LIVE_ROOTDIR}/usr/lib${DIRSUFFIX}/gimp/2.0/plug-ins
  ln -s /usr/bin/xsane \
    ${LIVE_ROOTDIR}/usr/lib${DIRSUFFIX}/gimp/2.0/plug-ins/xsane
fi

## Enable this only after we checked all dialog calls for compatibility ##
## If Xdialog is installed, set DIALOG environment variable:            ##
mkdir -p ${LIVE_ROOTDIR}/etc/profile.d
cat <<EOT > ${LIVE_ROOTDIR}/etc/profile.d/dialog.sh
#!/bin/sh
if [ -x /usr/bin/Xdialog ]; then
  DIALOG=Xdialog
  XDIALOG_HIGH_DIALOG_COMPAT=1
  XDIALOG_FORCE_AUTOSIZE=1
  export DIALOG XDIALOG_HIGH_DIALOG_COMPAT XDIALOG_FORCE_AUTOSIZE
fi
EOT
cat <<EOT > ${LIVE_ROOTDIR}/etc/profile.d/dialog.csh
#!/bin/csh
if (-x /usr/bin/Xdialog) then
  setenv DIALOG Xdialog
  setenv XDIALOG_HIGH_DIALOG_COMPAT 1
  setenv XDIALOG_FORCE_AUTOSIZE 1
endif
EOT
# Once we are certain this works, make the scripts executable:
chmod 0644 ${LIVE_ROOTDIR}/etc/profile.d/dialog.{c,}sh

# Add a shortcut to 'setup2hd' on the user's desktop:
mkdir -p ${LIVE_ROOTDIR}/usr/share/pixmaps
install -m 0644 ${LIVE_TOOLDIR}/media/slackware/icons/graySW_512px.png \
  ${LIVE_ROOTDIR}/usr/share/pixmaps/liveslak.png
mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/Desktop
cat <<EOT > ${LIVE_ROOTDIR}/home/${LIVEUID}/Desktop/.directory
[Desktop Entry]
Encoding=UTF-8
Icon=user-desktop
Type=Directory
EOT
cat <<EOT > ${LIVE_ROOTDIR}/home/${LIVEUID}/Desktop/setup2hd.desktop
#!/usr/bin/env xdg-open
[Desktop Entry]
Type=Application
Terminal=true
Name=Install ${DISTRO^}
Comment=Install ${DISTRO^} (live or regular) to Harddisk
Icon=/usr/share/pixmaps/liveslak.png
Exec=sudo -i /usr/local/sbin/setup2hd
EOT
# Let Plasma5 trust the desktop shortcut:
chmod 0544 ${LIVE_ROOTDIR}/home/${LIVEUID}/Desktop/setup2hd.desktop


# -------------------------------------------------------------------------- #
echo "-- Configuring XFCE."
# -------------------------------------------------------------------------- #

# Prepare some XFCE defaults for the 'live' user and any new users.
# (don't show icons on the desktop for irrelevant stuff).
# Also, allow other people to add their own custom skel*.txz archives:
mkdir -p ${LIVE_ROOTDIR}/etc/skel/
for SKEL in ${LIVE_TOOLDIR}/skel/skel*.txz ; do
  tar -xf ${SKEL} -C ${LIVE_ROOTDIR}/etc/skel/
done

if [ "$LIVEDE" = "XFCE" ]; then
  # Since the XFCE ISO no longer has xpdf, use Firefox as the PDF viewer:
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.config
  cat << EOF > ${LIVE_ROOTDIR}/etc/skel/.config/mimeapps.list
[Default Applications]
application/pdf=mozilla-firefox.desktop
EOF
fi


# Only configure for KDE4 if it is actually installed:
if [ -d ${LIVE_ROOTDIR}/usr/lib${DIRSUFFIX}/kde4/libexec ]; then

  # -------------------------------------------------------------------------- #
  echo "-- Configuring KDE4."
  # -------------------------------------------------------------------------- #

  # Adjust some usability issues with the default desktop layout:
  if [ -f ${LIVE_ROOTDIR}/usr/share/apps/plasma/layout-templates/org.kde.plasma-desktop.defaultPanel/contents/layout.js ]; then
    # Only apply to an unmodified file (Slackware 14.2 already implements it):
    if grep -q 'tasks.writeConfig' ${LIVE_ROOTDIR}/usr/share/apps/plasma/layout-templates/org.kde.plasma-desktop.defaultPanel/contents/layout.js ; then
      sed -i \
        -e '/showActivityManager/a konsole = panel.addWidget("quicklaunch")' \
        -e '/showActivityManager/a dolphin = panel.addWidget("quicklaunch")' \
        -e '/showActivityManager/a firefox = panel.addWidget("quicklaunch")' \
        -e '$a firefox.writeConfig("iconUrls","file:///usr/share/applications/mozilla-firefox.desktop")' \
        -e '$a dolphin.writeConfig("iconUrls","file:////usr/share/applications/kde4/dolphin.desktop")' \
        -e '$a konsole.writeConfig("iconUrls","file:///usr/share/applications/kde4/konsole.desktop")' \
        -e '/tasks.writeConfig/d' \
        ${LIVE_ROOTDIR}/usr/share/apps/plasma/layout-templates/org.kde.plasma-desktop.defaultPanel/contents/layout.js
    fi
  fi

  # Prepare some KDE4 defaults for the 'live' user and any new users.

  # Preselect the user 'live' in KDM:
  mkdir -p ${LIVE_ROOTDIR}/var/lib/kdm
  cat <<EOT > ${LIVE_ROOTDIR}/var/lib/kdm/kdmsts
[PrevUser]
:0=${LIVEUID}
EOT
  chmod 600 ${LIVE_ROOTDIR}/var/lib/kdm/kdmsts

  # Set default GTK+ theme for Qt applications:
  mkdir -p  ${LIVE_ROOTDIR}/etc/skel/
  cat << EOF > ${LIVE_ROOTDIR}/etc/skel/.gtkrc-2.0
include "/usr/share/themes/Adwaita/gtk-2.0/gtkrc"
include "/usr/share/gtk-2.0/gtkrc"
include "/etc/gtk-2.0/gtkrc"
gtk-theme-name="Adwaita"
EOF
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.config/gtk-3.0
  cat << EOF > ${LIVE_ROOTDIR}/etc/skel/.config/gtk-3.0/settings.ini
[Settings]
gtk-theme-name = Adwaita
EOF

  # Be gentle to low-performance USB media and limit disk I/O:
  mkdir -p  ${LIVE_ROOTDIR}/etc/skel/.kde/share/config
  cat <<EOT > ${LIVE_ROOTDIR}/etc/skel/.kde/share/config/nepomukserverrc
[Basic Settings]
Configured repositories=main
Start Nepomuk=false

[Service-nepomukstrigiservice]
autostart=false

[main Settings]
Storage Dir[\$e]=\$HOME/.kde/share/apps/nepomuk/repository/main/
Used Soprano Backend=redlandbackend
rebuilt index for type indexing=true
EOT

  # Disable baloo:
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.kde4/share/apps/config
  cat <<EOT >${LIVE_ROOTDIR}/etc/skel/.kde4/share/apps/config/baloofilerc
[Basic Settings]
Indexing-Enabled=false
EOT

  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.config
  cat <<EOT > ${LIVE_ROOTDIR}/etc/skel/.config/kwalletrc
[Auto Allow]
kdewallet=Network Management,KDE Daemon,KDE Control Module

[Wallet]
Close When Idle=false
Enabled=true
First Use=true
Use One Wallet=true
EOT

  # Start Konsole with a login shell:
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.kde/share/apps/konsole
  cat <<EOT > ${LIVE_ROOTDIR}/etc/skel/.kde/share/apps/konsole/Shell.profile
[General]
Command=/bin/bash -l
Name=Shell
Parent=FALLBACK/
EOT
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.config
  cat <<EOT >> ${LIVE_ROOTDIR}/etc/skel/.config/konsolerc
[Desktop Entry]
DefaultProfile=Shell.profile

EOT

  # Configure (default) UTC timezone so we can change it during boot:
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.kde/share/config
  cat <<EOT > ${LIVE_ROOTDIR}/etc/skel/.kde/share/config/ktimezonedrc
[TimeZones]
LocalZone=UTC
ZoneinfoDir=/usr/share/zoneinfo
Zonetab=/usr/share/zoneinfo/zone.tab
ZonetabCache=
EOT

fi # End KDE4


# Only configure for Plasma5 if it is actually installed:
if [ -d ${LIVE_ROOTDIR}/usr/lib${DIRSUFFIX}/kf5 ]; then

  # -------------------------------------------------------------------------- #
  echo "-- Configuring Plasma5."
  # -------------------------------------------------------------------------- #

  # This section is for any Plasma5 based variant.

  # Install a custom login/desktop/lock background if an image is present:
  plasma5_custom_bg

  # Remove the confusing openbox session if present:
  rm -f ${LIVE_ROOTDIR}/usr/share/xsessions/openbox-session.desktop || true
  # Remove the buggy mediacenter session:
  rm -f ${LIVE_ROOTDIR}/usr/share/xsessions/plasma-mediacenter.desktop || true
  # Remove non-functional wayland session:
  if [ ! -f ${LIVE_ROOTDIR}/usr/lib${DIRSUFFIX}/qt5/bin/qtwaylandscanner ];
  then
    rm -f ${LIVE_ROOTDIR}/usr/share/wayland-sessions/plasmawayland.desktop || true
  fi

  # Remove broken/unwanted shortcuts (discover and konqueror) from taskbar:
  sed -i ${LIVE_ROOTDIR}/usr/share/plasma/plasmoids/org.kde.plasma.taskmanager/contents/config/main.xml \
    -e 's#,applications:org.kde.discover.desktop##' \
    -e s'#,preferred://browser##'

  # Set the OS name to "Slackware Live" in "System Information":
  echo "Name=${DISTRO^} Live" >> ${LIVE_ROOTDIR}/etc/kde/xdg/kcm-about-distrorc

  # Set sane SDDM defaults on first boot (root-owned file):
  mkdir -p ${LIVE_ROOTDIR}/var/lib/sddm
  cat <<EOT > ${LIVE_ROOTDIR}/var/lib/sddm/state.conf 
[Last]
# Name of the last logged-in user.
# This user will be preselected when the login screen appears
User=${LIVEUID}

# Name of the session for the last logged-in user.
# This session will be preselected when the login screen appears.
Session=/usr/share/xsessions/plasma.desktop
EOT
  chroot ${LIVE_ROOTDIR} chown -R sddm:sddm var/lib/sddm

  # Thanks to Fedora Live: https://git.fedorahosted.org/cgit/spin-kickstarts.git
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.config/akonadi
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.local/share/akonadi
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.kde/share/config

  # Configure kdesu to use 'sudo' instead of 'su', so that it will ask
  # for the 'live'password instead of the 'root'password:
  cat <<KDESU_EOF >${LIVE_ROOTDIR}/etc/skel/.config/kdesurc
[super-user-command]
super-user-command=sudo
KDESU_EOF

  # Set akonadi backend:
  cat <<AKONADI_EOF >${LIVE_ROOTDIR}/etc/skel/.config/akonadi/akonadiserverrc
[%General]
Driver=QSQLITE

[QSQLITE]
Name=/home/${LIVEUID}/.local/share/akonadi/akonadi.db
AKONADI_EOF

  # Disable baloo:
  cat <<BALOO_EOF >${LIVE_ROOTDIR}/etc/skel/.config/baloofilerc
[Basic Settings]
Indexing-Enabled=false
BALOO_EOF

  # Disable kres-migrator:
  cat <<KRES_EOF >${LIVE_ROOTDIR}/etc/skel/.kde/share/config/kres-migratorrc
[Migration]
Enabled=false
KRES_EOF

  # Disable kwallet migrator:
  cat <<KWALLET_EOL >${LIVE_ROOTDIR}/etc/skel/.config/kwalletrc
[Migration]
alreadyMigrated=true

KWALLET_EOL

  # Start Konsole with a login shell:
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.local/share/konsole
  cat <<EOT > ${LIVE_ROOTDIR}/etc/skel/.local/share/konsole/Shell.profile
[Appearance]
ColorScheme=BlackOnWhite

[General]
Command=/bin/bash -l
Name=Shell
Parent=FALLBACK/
TerminalColumns=80
TerminalRows=25

[Interaction Options]
AutoCopySelectedText=true
TrimTrailingSpacesInSelectedText=true
EOT
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.config
  cat <<EOT >> ${LIVE_ROOTDIR}/etc/skel/.config/konsolerc
[Desktop Entry]
DefaultProfile=Shell.profile

EOT

  # Configure (default) UTC timezone so we can change it during boot:
  mkdir -p ${LIVE_ROOTDIR}/etc/skel/.config
  cat <<EOTZ > ${LIVE_ROOTDIR}/etc/skel/.config/ktimezonedrc
[TimeZones]
LocalZone=UTC
ZoneinfoDir=/usr/share/zoneinfo
Zonetab=/usr/share/zoneinfo/zone.tab
EOTZ

  # Make sure that Plasma and SDDM work on older GPUs,
  # by forcing Qt5 to use software GL rendering:
  cat <<"EOGL" >> ${LIVE_ROOTDIR}/usr/share/sddm/scripts/Xsetup

OPENGL_VERSION=$(LANG=C glxinfo |grep '^OpenGL version string: ' |head -n 1 |sed -e 's/^OpenGL version string: \([0-9]\).*$/\1/g')
if [ "$OPENGL_VERSION" -lt 2 ]; then
  QT_XCB_FORCE_SOFTWARE_OPENGL=1
  export QT_XCB_FORCE_SOFTWARE_OPENGL
fi

EOGL

  # Workaround a bug where SDDM does not always use the configured keymap:
  echo "setxkbmap" >> ${LIVE_ROOTDIR}/usr/share/sddm/scripts/Xsetup

  # Do not show the blueman applet, Plasma5 has its own BlueTooth widget:
  echo "NotShowIn=KDE;" >> ${LIVE_ROOTDIR}/etc/xdg/autostart/blueman.desktop

  # Set QtWebkit as the Konqueror rendering engine if available:
  if [ -f ${LIVE_ROOTDIR}/usr/share/kservices5/kwebkitpart.desktop  ]; then
    mkdir ${LIVE_ROOTDIR}/home/${LIVEUID}/.config
    cat <<EOT >> ${LIVE_ROOTDIR}/home/${LIVEUID}/.config/mimeapps.list
[Added KDE Service Associations]
application/xhtml+xml=kwebkitpart.desktop;
application/xml=kwebkitpart.desktop;
text/html=kwebkitpart.desktop;
EOT
  fi

fi # End Plasma5

if [ "$LIVEDE" = "DLACK" ]; then

  # -------------------------------------------------------------------------- #
  echo "-- Configuring DLACK."
  # -------------------------------------------------------------------------- #

  # Make sure we start in graphical mode with gdm enabled.
  ln -sf /lib/systemd/system/graphical.target ${LIVE_ROOTDIR}/etc/systemd/system/default.target
  ln -sf /lib/systemd/system/gdm.service ${LIVE_ROOTDIR}/etc/systemd/system/display-manager.service

  # Do not show the blueman applet, Gnome3 has its own BlueTooth widget:
  echo "NotShowIn=GNOME;" >> ${LIVE_ROOTDIR}/etc/xdg/autostart/blueman.desktop

  # Do not start gnome-initial-setup:
  mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/.config
  touch ${LIVE_ROOTDIR}/home/${LIVEUID}/.config/gnome-initial-setup-done

  # Do not let systemd re-generate dynamic linker cache on boot:
  echo "File created by ${MARKER}.  See systemd-update-done.service(8)." \
    |tee ${LIVE_ROOTDIR}/etc/.updated >${LIVE_ROOTDIR}/var/.updated

fi # End LIVEDE = DLACK  

if [ "$LIVEDE" = "DAW" ]; then

  # -------------------------------------------------------------------------- #
  echo "-- Configuring DAW."
  # -------------------------------------------------------------------------- #

  # Stream ALSA through Pulse and all through Jack. This is achieved by
  # having pulseaudio-jack module installed and starting jack-dbus:

  # We default to using a 48000 Hz sample rate throughout, assuming that
  # modern sound hardware will support this, and it lowers the latency:
  if [ -f ${LIVE_ROOTDIR}/etc/pulse/daemon.conf ]; then
    cat <<EOT >> ${LIVE_ROOTDIR}/etc/pulse/daemon.conf
; Run 'pulseaudio --dump-resample-methods' for all possible options.
; We want higher-quality resampling than the default:
resample-method = speex-float-9
; Jack is configured for 48KHz so let's make pulseaudio use it too:
default-sample-rate = 48000
alternate-sample-rate = 44100
EOT
  fi

  mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/.config/rncbc.org
  cat <<EOT > ${LIVE_ROOTDIR}/home/${LIVEUID}/.config/rncbc.org/QjackCtl.conf
[Options]
DBusEnabled=true
GraphButton=true
JackDBusEnabled=true
KeepOnTop=false
PostShutdownScript=true
PostShutdownScriptShell=killall a2jmidid &
PostStartupScript=true
PostStartupScriptShell=/usr/bin/a2jmidid -e &
ServerConfig=true
ServerConfigName=.jackdrc
ShutdownScript=false
ShutdownScriptShell=
Singleton=true
StartJack=true
StartMinimized=true
StartupScript=false
StartupScriptShell=
StopJack=true
SystemTray=true
SystemTrayQueryClose=false
XrunRegex=xrun of at least ([0-9|\\.]+) msecs

[Presets]
DefPreset=(default)

[Settings]
Driver=alsa
Frames=256
MidiDriver=seq
Periods=2
PortMax=256
Priority=5
Realtime=true
SampleRate=48000
Server=jackd
StartDelay=2
Sync=true
EOT

  # Add a default jackd configuration:
  cat <<EOT > ${LIVE_ROOTDIR}/home/${LIVEUID}/.jackdrc
/usr/bin/jackd -dalsa -dhw:0 -r48000 -p1024 -n2
EOT

  # Autostart qjackctl:
  mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/.config/autostart
  cp -a ${LIVE_ROOTDIR}/usr/share/applications/qjackctl.desktop \
    ${LIVE_ROOTDIR}/home/${LIVEUID}/.config/autostart

  # Add all our programs into their own submenu Applications>Multimedia>DAW
  # to avoid clutter in the Multimedia menu. We will use a custom category
  # "X-DAW" to decide what goes into the new submenu.
  # Also move the X42 and LSP Plugin submenus below the new DAW submenu.
  # see https://specifications.freedesktop.org/menu-spec/menu-spec-1.0.html
  # We overwrite the menu entries from the 'daw_base' package,
  # since we want the slightly different liveslak contents instead:
  install -Dm 644 ${LIVE_TOOLDIR}/media/${LIVEDE,,}/menu/liveslak-daw.menu \
    $LIVE_ROOTDIR/etc/xdg/menus/applications-merged/${DISTRO}-daw.menu
  install -Dm 644 \
    ${LIVE_TOOLDIR}/media/${LIVEDE,,}/menu/liveslak-daw.directory \
    $LIVE_ROOTDIR/usr/share/desktop-directories/liveslak-daw.directory
  install -Dm 644 ${LIVE_TOOLDIR}/media/${LIVEDE,,}/menu/liveslak-daw.png \
    -t $LIVE_ROOTDIR/usr/share/icons/hicolor/512x512/apps/

  # Any menu entry that does not yet have a Category "X-DAW" will now have to
  # get that added so that our mew submenu will be populated:
  for DAWPKG in $(cat ${LIVE_TOOLDIR}/pkglists/z03_daw.lst |grep -v x42 |grep -Ev '(^ *#)' ) ; do
    # Find the installed full package name belonging to the DAW package:
    PKGINST=$( ls -1 ${LIVE_ROOTDIR}/var/log/packages/${DAWPKG}* 2>/dev/null |grep -E "/var/log/packages/${DAWPKG}-[^-]+-[^-]+-[^-]+$" || true)
    if [ -n "${PKGINST}" ]; then
      for DESKTOPF in $(grep 'usr/share/applications/.*.desktop' ${PKGINST})
      do
        if ! grep -q X-DAW ${LIVE_ROOTDIR}/${DESKTOPF} ; then
          sed -i ${LIVE_ROOTDIR}/${DESKTOPF} \
            -e "s/^Categories=\(.*\)/Categories=X-DAW;\1/"
        fi
        ## Hide the application in Multimedia (which is based on the AudioVideo
        ## category) to prevent them from getting listed twice:
        #sed -i ${LIVE_ROOTDIR}/${DESKTOPF} -e "/^Categories=/s/AudioVideo;//"
      done
    fi
  done

  # VCV Rack plugins need to be linked into the user-directory to be seen:
  mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/.Rack/plugins-v1
  for PLUGIN in $(find ${LIVE_ROOTDIR}/usr/share/vcvrack/ -type f -name "*.zip" -mindepth 1 -maxdepth 1); do
    ln -s /usr/share/vcvrack/$(basename ${PLUGIN}) ${LIVE_ROOTDIR}/home/${LIVEUID}/.Rack/plugins-v1/
  done

fi # End LIVEDE = DAW

if [ "$LIVEDE" = "STUDIOWARE" ]; then

  # -------------------------------------------------------------------------- #
  echo "-- Configuring STUDIOWARE."
  # -------------------------------------------------------------------------- #

  # Create group and user for the Avahi service:
  chroot ${LIVE_ROOTDIR} /usr/sbin/groupadd -g 214 avahi
  chroot ${LIVE_ROOTDIR} /usr/sbin/useradd -c "Avahi Service Account" -u 214 -g 214 -d /dev/null -s /bin/false avahi
  echo "avahi:$(openssl rand -base64 12)" | chroot ${LIVE_ROOTDIR} /usr/sbin/chpasswd

fi # End LIVEDE = STUDIOWARE

if [ "$LIVEDE" = "DAW" -o "$LIVEDE" = "STUDIOWARE" ];
then

  # -------------------------------------------------------------------------- #
  echo "-- Configuring $LIVEDE (RT behaviour)."
  # -------------------------------------------------------------------------- #

  # Install real-time configuration in case the OS-installed packages
  # have not yet done this for us ('daw_base' for instance).
  # The script looks for specific filenames as used in 'daw_base':
  #   /etc/security/limits.d/rt_audio.conf
  #   /etc/sysctl.d/daw.conf
  #   /etc/udev/rules.d/40-timer-permissions.rules

  # RT Scheduling and Locked Memory:
  # Implementation depends on whether PAM is installed:
  if [ -L ${LIVE_ROOTDIR}/lib${DIRSUFFIX}/libpam.so.? ]; then
    if [ ! -f ${LIVE_ROOTDIR}/etc/security/limits.d/rt_audio.conf ]; then
      # On PAM based OS, allow user in 'audio' group to invoke rt capability:
      mkdir -p ${LIVE_ROOTDIR}/etc/security/limits.d
      cat <<EOT > ${LIVE_ROOTDIR}/etc/security/limits.d/rt_audio.conf
# Realtime capability allowed for user in the 'audio' group:
# Use 'unlimited' with care, you can lock up your system when app misbehaves:
#@audio   -  memlock    2097152
@audio   -  memlock    unlimited
@audio   -  rtprio     95
EOT
    fi
  else
    cat << "EOT" > ${LIVE_ROOTDIR}/etc/initscript
# Set umask to safe level:
umask 022
# Disable core dumps:
ulimit -c 0
# Allow unlimited size to be locked into memory:
ulimit -l unlimited
# Address issue of jackd failing to start with realtime scheduling:
ulimit -r 95

# Execute the program.
eval exec "$4"
EOT
    chmod +x ${LIVE_ROOTDIR}/etc/initscript
  fi

  if [ ! -f ${LIVE_ROOTDIR}/etc/udev/rules.d/40-timer-permissions.rules ]; then
    # Allow access for 'audio' group to the high precision event timer,
    # which may benefit a DAW which relies on ALSA MIDI;
    # Also grant write access to /dev/cpu_dma_latency to prevent CPU's
    # from going into idle state:
    mkdir -p ${LIVE_ROOTDIR}/etc/udev/rules.d
    cat <<EOT > ${LIVE_ROOTDIR}/etc/udev/rules.d/40-timer-permissions.rules
KERNEL=="rtc0", GROUP="audio"
KERNEL=="hpet", GROUP="audio"
KERNEL=="cpu_dma_latency", GROUP="audio"
EOT
  fi

  if [ ! -f ${LIVE_ROOTDIR}/etc/sysctl.d/daw.conf ]; then
    # Audio related sysctl settings for better realtime performance:
    mkdir -p ${LIVE_ROOTDIR}/etc/sysctl.d
    cat <<EOT > ${LIVE_ROOTDIR}/etc/sysctl.d/daw.conf
# https://wiki.linuxaudio.org/wiki/system_configuration
dev.hpet.max-user-freq = 3072
fs.inotify.max_user_watches = 524288
vm.swappiness = 10
EOT
  fi

  #  # This would benefit a DAW, but if the user runs the Live OS on a laptop,
  #  # she might want to decide about this herself:
  #  mkdir -p ${LIVE_ROOTDIR}/etc/default
  #cat <<EOT > ${LIVE_ROOTDIR}/etc/default/cpufreq
  #SCALING_GOVERNOR=performance
  #EOT

fi # End LIVEDE = DAW/STUDIOWARE

# You can define the function 'custom_config()' by uncommenting it in
# the configuration file 'make_slackware_live.conf'.
if type custom_config 1>/dev/null 2>/dev/null ; then

  # -------------------------------------------------------------------------- #
  echo "-- Configuring ${LIVEDE} by calling 'custom_config()'."
  # -------------------------------------------------------------------------- #

  # This is particularly useful if you defined a non-standard "LIVEDE"
  # in 'make_slackware_live.conf', in which case you must specify your custom
  # package sequence in the variable "SEQ_CUSTOM" in that same .conf file.
  custom_config

fi

# Workaround a bug where our Xkbconfig is not loaded sometimes:
echo "setxkbmap" > ${LIVE_ROOTDIR}/home/${LIVEUID}/.xprofile

# Give the live user a copy of our XFCE (and more) skeleton configuration:
cd ${LIVE_ROOTDIR}/etc/skel/
  find . -exec cp -a --parents "{}" ${LIVE_ROOTDIR}/home/${LIVEUID}/ \;
  find ${LIVE_ROOTDIR}/home/${LIVEUID}/ -type f -exec sed -i -e "s,/home/live,/home/${LIVEUID}," "{}" \;
cd - 1>/dev/null

if [ "${ADD_CACERT}" = "YES" -o "${ADD_CACERT}" = "yes" ]; then
  echo "-- Importing CACert root certificates into OS and browsers."
  # Import CACert root certificates into the OS:
  ( mkdir -p ${LIVE_ROOTDIR}/etc/ssl/certs
    cd ${LIVE_ROOTDIR}/etc/ssl/certs
    wget -q -O cacert-root.crt http://www.cacert.org/certs/root.crt
    wget -q -O cacert-class3.crt http://www.cacert.org/certs/class3.crt
    ln -s cacert-root.crt \
      $(openssl x509 -noout -hash -in cacert-root.crt).0
    ln -s cacert-class3.crt \
      $(openssl x509 -noout -hash -in cacert-class3.crt).0
  )

  # Create Mozilla Firefox profile:
  mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/firefox/${LIVEUID}_profile.default
  cat << EOT > ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/firefox/profiles.ini
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=${LIVEUID}_profile.default
Default=1
EOT

  # Create Mozilla Seamonkey profile:
  mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/seamonkey/${LIVEUID}_profile.default
  cat << EOT > ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/seamonkey/profiles.ini
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=${LIVEUID}_profile.default
Default=1
EOT

  # Create Pale Moon profile:
  mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/.moonchild\ productions/pale\ moon/${LIVEUID}_profile.default
    cat << EOT > ${LIVE_ROOTDIR}/home/${LIVEUID}/.moonchild\ productions/pale\ moon/profiles.ini
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=${LIVEUID}_profile.default
Default=1
EOT

  # Import CACert root certificates into the browsers:
  (
    # Mozilla Firefox:
    certutil -N --empty-password -d ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/firefox/${LIVEUID}_profile.default
    certutil -d ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/firefox/${LIVEUID}_profile.default \
      -A -t TC -n "CAcert.org" -i ${LIVE_ROOTDIR}/etc/ssl/certs/cacert-root.crt
    certutil -d ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/firefox/${LIVEUID}_profile.default \
      -A -t TC -n "CAcert.org Class 3" -i ${LIVE_ROOTDIR}/etc/ssl/certs/cacert-class3.crt
    # Seamonkey and Pale Moon (can just be a copy of the Firefox files):
    cp -a \
      ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/firefox/${LIVEUID}_profile.default/* \
      ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/seamonkey/${LIVEUID}_profile.default/
    cp -a \
      ${LIVE_ROOTDIR}/home/${LIVEUID}/.mozilla/firefox/${LIVEUID}_profile.default/* \
      ${LIVE_ROOTDIR}/home/${LIVEUID}/.moonchild\ productions/pale\ moon/${LIVEUID}_profile.default/
    # NSS databases for Chrome based browsers have a different format (sql)
    # than Mozilla based browsers:
    mkdir -p ${LIVE_ROOTDIR}/home/${LIVEUID}/.pki/nssdb
    certutil -N --empty-password -d ${LIVE_ROOTDIR}/home/${LIVEUID}/.pki/nssdb
    certutil -d sql:${LIVE_ROOTDIR}/home/${LIVEUID}/.pki/nssdb \
      -A -t TC -n "CAcert.org" -i ${LIVE_ROOTDIR}/etc/ssl/certs/cacert-root.crt
    certutil -d sql:${LIVE_ROOTDIR}/home/${LIVEUID}/.pki/nssdb \
      -A -t TC -n "CAcert.org Class 3" -i ${LIVE_ROOTDIR}/etc/ssl/certs/cacert-class3.crt
  )
  # TODO: find out how to configure KDE with additional Root CA's.
fi # End ADD_CACERT

# Make sure that user 'live' owns her own files:
chroot ${LIVE_ROOTDIR} chown -R ${LIVEUID}:users home/${LIVEUID}

# -------------------------------------------------------------------------- #
echo "-- Tweaking system startup."
# -------------------------------------------------------------------------- #

# Configure the default DE when running startx:
if [ "$LIVEDE" = "MATE" ]; then
  ln -sf xinitrc.mate-session ${LIVE_ROOTDIR}/etc/X11/xinit/xinitrc
elif [ "$LIVEDE" = "CINNAMON" ]; then
  ln -sf xinitrc.cinnamon-session ${LIVE_ROOTDIR}/etc/X11/xinit/xinitrc
elif [ "$LIVEDE" = "DLACK" ]; then
  ln -sf xinitrc.gnome ${LIVE_ROOTDIR}/etc/X11/xinit/xinitrc
elif [ -f ${LIVE_ROOTDIR}/etc/X11/xinit/xinitrc.kde ]; then
  ln -sf xinitrc.kde ${LIVE_ROOTDIR}/etc/X11/xinit/xinitrc
elif [ -f ${LIVE_ROOTDIR}/etc/X11/xinit/xinitrc.xfce ]; then
  ln -sf xinitrc.xfce ${LIVE_ROOTDIR}/etc/X11/xinit/xinitrc
fi

# Configure the default runlevel:
sed -i ${LIVE_ROOTDIR}/etc/inittab -e "s/\(id:\).\(:initdefault:\)/\1${RUNLEVEL}\2/"

# Disable unneeded/unwanted services:
[ -f ${LIVE_ROOTDIR}/etc/rc.d/rc.acpid ] && chmod -x ${LIVE_ROOTDIR}/etc/rc.d/rc.acpid
[ -f ${LIVE_ROOTDIR}/etc/rc.d/rc.pcmcia ] && chmod -x ${LIVE_ROOTDIR}/etc/rc.d/rc.pcmcia
[ -f ${LIVE_ROOTDIR}/etc/rc.d/rc.pulseaudio ] && chmod -x ${LIVE_ROOTDIR}/etc/rc.d/rc.pulseaudio
[ -f ${LIVE_ROOTDIR}/etc/rc.d/rc.yp ] && chmod -x ${LIVE_ROOTDIR}/etc/rc.d/rc.yp
[ -f ${LIVE_ROOTDIR}/etc/rc.d/rc.sshd ] && chmod -x ${LIVE_ROOTDIR}/etc/rc.d/rc.sshd

# But enable NFS client support and CUPS:
[ -f ${LIVE_ROOTDIR}/etc/rc.d/rc.rpc ] && chmod +x ${LIVE_ROOTDIR}/etc/rc.d/rc.rpc
[ -f ${LIVE_ROOTDIR}/etc/rc.d/rc.cups ] && chmod +x ${LIVE_ROOTDIR}/etc/rc.d/rc.cups
[ -f ${LIVE_ROOTDIR}/etc/rc.d/rc.cups-browsed ] && chmod +x ${LIVE_ROOTDIR}/etc/rc.d/rc.cups-browsed

# Add a softvol pre-amp to ALSA - some computers have too low volumes.
# If etc/asound.conf exists it's configuring ALSA to use Pulse,
# so in that case the pre-amp is not needed:
if [ ! -f ${LIVE_ROOTDIR}/etc/asound.conf ]; then
  cat <<EOAL > ${LIVE_ROOTDIR}/etc/asound.conf
pcm.!default {
  type asym
  playback.pcm "plug:softvol"
  capture.pcm "plug:dsnoop"
}

pcm.softvol {
  type softvol
  slave.pcm "dmix"
  control { name "PCM"; card 0; }
  max_dB 32.0
}
EOAL
else
  if ! grep -q sysdefault ${LIVE_ROOTDIR}/etc/asound.conf ; then
    # If pulse is used, configure a fallback to use the system default
    # or else there will not be sound on first boot:
    sed -i ${LIVE_ROOTDIR}/etc/asound.conf \
        -e '/type pulse/ a \ \ fallback "sysdefault"'
  fi
fi

# Skip all filesystem checks at boot:
touch ${LIVE_ROOTDIR}/etc/fastboot

# We will not write to the hardware clock:
sed -i -e '/systohc/s/^/# /' ${LIVE_ROOTDIR}/etc/rc.d/rc.6

# Run some package setup scripts (usually run by the slackware installer),
# as well as some of the delaying commands in rc.M and rc.modules:
chroot ${LIVE_ROOTDIR} /bin/bash <<EOCR
# Rebuild SSL certificate database:
/usr/sbin/update-ca-certificates --fresh 1>/dev/null 2>${DBGOUT}

# Run bits from rc.M so we won't need to run them again in the live system:
/sbin/depmod $KVER
/sbin/ldconfig
EOCR

chroot ${LIVE_ROOTDIR} /bin/bash <<EOCR
# Update the desktop database:
if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database /usr/share/applications > /dev/null 2>${DBGOUT}
fi

# Update hicolor theme cache:
if [ -d /usr/share/icons/hicolor ]; then
  if [ -x /usr/bin/gtk-update-icon-cache ]; then
    /usr/bin/gtk-update-icon-cache -f -t /usr/share/icons/hicolor 1>/dev/null 2>${DBGOUT}
  fi
fi

# Update the mime database:
if [ -x /usr/bin/update-mime-database ]; then
  /usr/bin/update-mime-database /usr/share/mime >/dev/null 2>${DBGOUT}
fi

# Font configuration:
if [ -x /usr/bin/fc-cache ]; then
  for fontdir in 100dpi 75dpi OTF Speedo TTF Type1 cyrillic ; do
    if [ -d /usr/share/fonts/\$fontdir ]; then
      mkfontscale /usr/share/fonts/\$fontdir 1>/dev/null 2>${DBGOUT}
      mkfontdir /usr/share/fonts/\$fontdir 1>/dev/null 2>${DBGOUT}
    fi
  done
  if [ -d /usr/share/fonts/misc ]; then
    mkfontscale /usr/share/fonts/misc  1>/dev/null 2>${DBGOUT}
    mkfontdir -e /usr/share/fonts/encodings -e /usr/share/fonts/encodings/large /usr/share/fonts/misc 1>/dev/null 2>${DBGOUT}
  fi
  /usr/bin/fc-cache -f 1>/dev/null 2>${DBGOUT}
fi

if [ -x /usr/bin/update-gtk-immodules ]; then
  /usr/bin/update-gtk-immodules
fi
if [ -x /usr/bin/update-gdk-pixbuf-loaders ]; then
  /usr/bin/update-gdk-pixbuf-loaders
fi
if [ -x /usr/bin/update-pango-querymodules ]; then
  /usr/bin/update-pango-querymodules
fi

if [ -x /usr/bin/glib-compile-schemas ]; then
  /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas >/dev/null 2>${DBGOUT}
fi

# Delete unwanted cache files:
if [ -d usr/share/icons ]; then
  find usr/share/icons -name icon-theme.cache -exec rm "{}" \;
fi
EOCR

# Disable above commands in rc.M and rc.modules:
sed -e "s% /usr/bin/update.*verbose%#&%" -i ${LIVE_ROOTDIR}/etc/rc.d/rc.M 
sed -e '/^ *\/usr\/bin\/glib-c/ s, /usr/bin/glib-c,#&,' -i ${LIVE_ROOTDIR}/etc/rc.d/rc.M
sed -e "s% /sbin/depmod -%#&%" -i ${LIVE_ROOTDIR}/etc/rc.d/rc.modules 

# Start/stop the NVIDIA persistence daemon if a NVIDIA driver is loaded;
# Note that this assumes the nvidia-driver and nvidia-kernel packages
# from slackbuilds.org are being used:
cat <<EOT >> ${LIVE_ROOTDIR}/etc/rc.d/rc.local

# For CUDA/OpenCL to work after reboot, create missing nvidia device nodes:
if [ -x /usr/bin/nvidia-modprobe ]; then
  echo "Creating missing nvidia device nodes..."
  /usr/bin/nvidia-modprobe -c 0 -u
fi

# Start the nvidia-persistenced daemon:
if  [ -x /etc/rc.d/rc.nvidia-persistenced ] && [ -d /var/run/nvidia-persistenced ]; then
  echo "Starting nvidia persistence daemon..."
  sed -e "s/NVPD_USER=.*/NVPD_USER=${NVUID}/" -i /etc/rc.d/rc.nvidia-persistenced
  chown ${NVUID}:${NVGRP} /var/run/nvidia-persistenced 2>/dev/null
  /etc/rc.d/rc.nvidia-persistenced start
fi
EOT

cat <<EOT >> ${LIVE_ROOTDIR}/etc/rc.d/rc.local_shutdown

# Stop the nvidia-persistenced daemon:
if  [ -x /etc/rc.d/rc.nvidia-persistenced ]; then
  echo "Stopping nvidia persistence daemon..."
  /etc/rc.d/rc.nvidia-persistenced stop
fi
EOT

# Clean out the unneeded stuff:
# Note: this will fail when a directory is encountered. This failure points
# to a packaging issue; find and fix the responsible package.
rm -f ${LIVE_ROOTDIR}/tmp/[A-Za-z]*
rm -f ${LIVE_ROOTDIR}/var/mail/*
rm -f ${LIVE_ROOTDIR}/root/.bash*

# Create a locate cache:
echo "-- Creating locate cache, takes a few seconds..."
if [ -x ${LIVE_ROOTDIR}/etc/cron.daily/mlocate ]; then
  LOCATE_BIN=mlocate
else
  LOCATE_BIN=slocate
fi
chroot ${LIVE_ROOTDIR} /etc/cron.daily/${LOCATE_BIN} 2>${DBGOUT}

# -----------------------------------------------------------------------------
# Done with configuring the live system!
# -----------------------------------------------------------------------------

# Squash the configuration into its own module:
umount ${LIVE_ROOTDIR} 2>${DBGOUT} || true
mksquashfs ${INSTDIR} ${LIVE_MOD_SYS}/0099-${DISTRO}_zzzconf-${SL_VERSION}-${SL_ARCH}.sxz -noappend -comp ${SQ_COMP} ${SQ_COMP_PARAMS}
rm -rf ${INSTDIR}/*

# End result: we have our .sxz file and the INSTDIR is empty again,
# Next step is to loop-mount the squashfs file onto INSTDIR.

# Add the system configuration tree to the readonly lowerdirs for the overlay:
RODIRS="${INSTDIR}:${RODIRS}"

# Mount the module for use in the final assembly of the ISO:
mount -t squashfs -o loop ${LIVE_MOD_SYS}/0099-${DISTRO}_zzzconf-${SL_VERSION}-${SL_ARCH}.sxz ${INSTDIR}

unset INSTDIR

# -----------------------------------------------------------------------------
# Prepare the system for live booting.
# -----------------------------------------------------------------------------

echo "-- Preparing the system for live booting."
umount ${LIVE_ROOTDIR} 2>${DBGOUT} || true
mount -t overlay -o lowerdir=${RODIRS%:*},upperdir=${LIVE_BOOT},workdir=${LIVE_OVLDIR} overlay ${LIVE_ROOTDIR}

mount --bind /proc ${LIVE_ROOTDIR}/proc
mount --bind /sys ${LIVE_ROOTDIR}/sys
mount --bind /dev ${LIVE_ROOTDIR}/dev

# Determine the installed kernel version:
if [ "$SL_ARCH" = "x86_64" -o "$SMP32" = "NO" ]; then
  KGEN=$(ls --indicator-style=none ${LIVE_ROOTDIR}/var/log/packages/kernel*modules* |grep -v smp |head -1 |rev | cut -d- -f3 |tr _ - |rev)
  KVER=$(ls --indicator-style=none ${LIVE_ROOTDIR}/lib/modules/ |grep -v smp |head -1)
else
  KGEN=$(ls --indicator-style=none ${LIVE_ROOTDIR}/var/log/packages/kernel*modules* |grep smp |head -1 |rev | cut -d- -f3 |tr _ - |rev)
  KVER=$(ls --indicator-style=none ${LIVE_ROOTDIR}/lib/modules/ |grep smp |head -1)
fi

# Create an initrd for the generic kernel, using a modified init script:
echo "-- Creating initrd for kernel-generic $KVER ..."
chroot ${LIVE_ROOTDIR} /sbin/mkinitrd -c -w ${WAIT} -l us -o /boot/initrd_${KVER}.img -k ${KVER} -m ${KMODS} -L -C dummy 1>${DBGOUT} 2>${DBGOUT}
# Modify the initrd content for the Live OS.
# Note: 'upslak.sh' needs to be updated when this 'cat' command changes:
cat $LIVE_TOOLDIR/liveinit.tpl | sed \
  -e "s/@LIVEMAIN@/$LIVEMAIN/g" \
  -e "s/@MARKER@/$MARKER/g" \
  -e "s/@MEDIALABEL@/$MEDIALABEL/g" \
  -e "s/@PERSISTENCE@/$PERSISTENCE/g" \
  -e "s/@DARKSTAR@/$LIVE_HOSTNAME/g" \
  -e "s/@LIVEUID@/$LIVEUID/g" \
  -e "s/@DISTRO@/$DISTRO/g" \
  -e "s/@CDISTRO@/${DISTRO^}/g" \
  -e "s/@UDISTRO@/${DISTRO^^}/g" \
  -e "s/@VERSION@/$VERSION/g" \
  -e "s/@SQ_EXT_AVAIL@/${SQ_EXT_AVAIL}/g" \
  -e "s,@DEF_KBD@,${DEF_KBD},g" \
  -e "s,@DEF_LOCALE@,${DEF_LOCALE},g" \
  -e "s,@DEF_TZ@,${DEF_TZ},g" \
  > ${LIVE_ROOTDIR}/boot/initrd-tree/init
cat /dev/null > ${LIVE_ROOTDIR}/boot/initrd-tree/luksdev
# We do not add openobex to the initrd and don't want to see irrelevant errors:
rm ${LIVE_ROOTDIR}/boot/initrd-tree/lib/udev/rules.d/*openobex*rules 2>${DBGOUT} || true
# Add dhcpcd for NFS root support (just to have it - even if we won't need it):
DHCPD_PKG=$(find ${DEF_SL_PKGROOT}/../ -name "dhcpcd-*.t?z" |head -1)
tar -C ${LIVE_ROOTDIR}/boot/initrd-tree/ -xf ${DHCPD_PKG} \
  var/lib/dhcpcd lib/dhcpcd sbin/dhcpcd usr/lib${DIRSUFFIX}/dhcpcd \
  etc/dhcpcd.conf.new
mv ${LIVE_ROOTDIR}/boot/initrd-tree/etc/dhcpcd.conf{.new,}
# Add getfattr to read extended attributes (even if we won't need it):
ATTR_PKG=$(find ${DEF_SL_PKGROOT}/../ -name "attr-*.t?z" |head -1)
tar --wildcards -C ${LIVE_ROOTDIR}/boot/initrd-tree/ -xf ${ATTR_PKG} \
  lib${DIRSUFFIX}/libattr.so.* usr/bin/getfattr
# Generate library symlinks for libattr (getfattr depends on them):
( cd ${LIVE_ROOTDIR}/boot/initrd-tree/lib${DIRSUFFIX} ; ldconfig -n . )
# Stamp the Slackware version into the initrd (at least dhcpcd needs this):
mkdir -p ${LIVE_ROOTDIR}/boot/initrd-tree/etc/rc.d
cp -a ${LIVE_ROOTDIR}/etc/slackware-version ${LIVE_ROOTDIR}/etc/os-release \
  ${LIVE_ROOTDIR}/boot/initrd-tree/etc/
if [ "$NFSROOTSUP" = "YES" ]; then
  # Add just the right kernel network modules by pruning unneeded stuff:
  if [ "$SL_ARCH" = "x86_64" -o "$SMP32" = "NO" ]; then
    KMODS_PKG=$(find ${DEF_SL_PKGROOT}/../ -name "kernel-modules-*$(echo $KGEN |tr - _)*.t?z" |grep -v smp |head -1)
  else
    KMODS_PKG=$(find ${DEF_SL_PKGROOT}/../ -name "kernel-modules-*$(echo $KGEN |tr - _)*.t?z" |grep smp |head -1)
  fi
  KMODS_TEMP=$(mktemp -d -p /mnt -t liveslak.XXXXXX)
  if [ ! -d $KMODS_TEMP ]; then
    echo "*** Failed to create a temporary extraction directory for the initrd!"
    cleanup
    exit 1
  fi
  # We need to extract the full kernel-modules package for deps resolving:
  tar -C ${KMODS_TEMP} -xf ${KMODS_PKG}
  # Get the kernel modules:
  for NETMODPATH in ${NETMODS} ; do 
    cd ${KMODS_TEMP}
      cp -a --parents lib/modules/${KVER}/${NETMODPATH} \
        ${LIVE_ROOTDIR}/boot/initrd-tree/
    cd - 1>/dev/null
    # Prune the ones we do not need:
    for KNETRM in ${NETEXCL} ; do
      find ${LIVE_ROOTDIR}/boot/initrd-tree/lib/modules/${KVER}/${NETMODPATH} \
        -name $KNETRM -depth -exec rm -rf {} \;
    done
    # Add any dependency modules:
    for MODULE in $(find ${LIVE_ROOTDIR}/boot/initrd-tree/lib/modules/${KVER}/${NETMODPATH} -type f -exec basename {} .ko \;) ; do
      /sbin/modprobe --dirname ${KMODS_TEMP} --set-version $KVER --show-depends --ignore-install $MODULE 2>/dev/null |grep "^insmod " |cut -f 2 -d ' ' |while read SRCMOD; do
        if [ "$(basename $SRCMOD .ko)" != "$MODULE" ]; then
          cd ${KMODS_TEMP}
            # Need to strip ${KMODS_TEMP} from the start of ${SRCMOD}:
            cp -a --parents $(echo $SRCMOD |sed 's|'${KMODS_TEMP}'/|./|' ) \
              ${LIVE_ROOTDIR}/boot/initrd-tree/
          cd - 1>/dev/null
        fi
      done
    done
  done
  # Remove the temporary tree:
  rm -rf ${KMODS_TEMP}
  # We added extra modules to the initrd, so we run depmod again:
  chroot ${LIVE_ROOTDIR}/boot/initrd-tree /sbin/depmod $KVER
  # Add the firmware for network cards that need them:
  KFW_PKG=$(find ${DEF_SL_PKGROOT}/../ -name "kernel-firmware-*.t?z" |head -1)
  tar tf ${KFW_PKG} |grep -E "($(echo $NETFIRMWARE |tr ' ' '|'))" \
    |xargs tar -C ${LIVE_ROOTDIR}/boot/initrd-tree/ -xf ${KFW_PKG} \
    2>/dev/null || true
fi
# Wrap up the initrd.img again:
( cd ${LIVE_ROOTDIR}/boot/initrd-tree
  find . | cpio -o -H newc | $COMPR >${LIVE_ROOTDIR}/boot/initrd_${KVER}.img 2>${DBGOUT}
)
rm -rf ${LIVE_ROOTDIR}/boot/initrd-tree

# ... and cleanup these mounts again:
umount ${LIVE_ROOTDIR}/{proc,sys,dev} || true
umount ${LIVE_ROOTDIR} || true
# Paranoia:
[ ! -z "${LIVE_BOOT}" ] && rm -rf ${LIVE_BOOT}/{etc,tmp,usr,var} 1>${DBGOUT} 2>${DBGOUT}

# Copy kernel and move the modified initrd (we do not need it in the Live OS).
# Note to self: syslinux does not 'see' files unless they are DOS 8.3 names?
rm -rf ${LIVE_STAGING}/boot
mkdir -p ${LIVE_STAGING}/boot
cp -a ${LIVE_BOOT}/boot/vmlinuz-generic*-$KGEN ${LIVE_STAGING}/boot/generic
mv ${LIVE_BOOT}/boot/initrd_${KVER}.img ${LIVE_STAGING}/boot/initrd.img

# Squash the boot directory into its own module:
mksquashfs ${LIVE_BOOT} ${LIVE_MOD_SYS}/0000-${DISTRO}_boot-${SL_VERSION}-${SL_ARCH}.sxz -noappend -comp ${SQ_COMP} ${SQ_COMP_PARAMS}

# Determine additional boot parameters to be added:
if [ -z ${KAPPEND} ]; then
  eval KAPPEND=\$KAPPEND_${LIVEDE}
fi

# Copy the syslinux configuration.
# The next block checks here for a possible UEFI grub boot image:
cp -a ${LIVE_TOOLDIR}/syslinux ${LIVE_STAGING}/boot/

# EFI support always for 64bit architecture, but conditional for 32bit.
if [ "$SL_ARCH" = "x86_64" -o "$EFI32" = "YES" ]; then
  # Copy the UEFI boot directory structure:
  mkdir -p ${LIVE_STAGING}/EFI/BOOT
  cp -a ${LIVE_TOOLDIR}/EFI/BOOT/{grub-embedded.cfg,make-grub.sh,*.txt,theme} ${LIVE_STAGING}/EFI/BOOT/
  if [ "$LIVEDE" = "XFCE" ]; then
    # We do not use the unicode font, so it can be removed to save space:
    rm -f ${LIVE_STAGING}/EFI/BOOT/theme/unicode.pf2
  fi

  # Create the grub fonts used in the theme.
  # Command outputs string like this: "Font name: DejaVu Sans Mono Regular 5".
  for FSIZE in 5 10 12 20 ; do
    grub-mkfont -s ${FSIZE} -av \
      -o ${LIVE_STAGING}/EFI/BOOT/theme/dejavusansmono${FSIZE}.pf2 \
      /usr/share/fonts/TTF/DejaVuSansMono.ttf \
      | grep "^Font name: "
  done

  # The grub-embedded.cfg in the bootx64.efi/bootia32.efi looks for this file:
  touch ${LIVE_STAGING}/EFI/BOOT/${MARKER}

  # Generate the UEFI grub boot image if needed:
  if [ ! -f ${LIVE_STAGING}/EFI/BOOT/boot${EFISUFF}.efi -o ! -f ${LIVE_STAGING}/boot/syslinux/efiboot.img ]; then
    ( cd ${LIVE_STAGING}/EFI/BOOT
      sed -i -e "s/SLACKWARELIVE/${MARKER}/g" grub-embedded.cfg
      sh make-grub.sh EFIFORM=${EFIFORM} EFISUFF=${EFISUFF}
    )
  fi

  # Generate the grub configuration for UEFI boot:
  gen_uefimenu ${LIVE_STAGING}/EFI/BOOT
fi # End EFI support menu.

if [ "$SYSMENU" = "NO" ]; then
  # Simple isolinux choices, no UEFI support.
  echo "include syslinux.cfg" > ${LIVE_STAGING}/boot/syslinux/isolinux.cfg
else
  # NOTE: Convert a PNG image to VESA bitmap before using it with vesamenu:
  # $ convert -depth 16 -colors 65536 in.png out.png
  cp -a /usr/share/syslinux/vesamenu.c32 ${LIVE_STAGING}/boot/syslinux/
  echo "include menu/vesamenu.cfg" > ${LIVE_STAGING}/boot/syslinux/isolinux.cfg
  # Generate the multi-language menu:
  gen_bootmenu ${LIVE_STAGING}/boot/syslinux
fi
for SLFILE in message.txt f2.txt syslinux.cfg lang.cfg ; do
  if [ -f ${LIVE_STAGING}/boot/syslinux/${SLFILE} ]; then
    sed -i ${LIVE_STAGING}/boot/syslinux/${SLFILE} \
      -e "s/@DIRSUFFIX@/$DIRSUFFIX/g" \
      -e "s/@DISTRO@/$DISTRO/g" \
      -e "s/@CDISTRO@/${DISTRO^}/g" \
      -e "s/@UDISTRO@/${DISTRO^^}/g" \
      -e "s/@KVER@/$KVER/g" \
      -e "s/@LIVEMAIN@/$LIVEMAIN/g" \
      -e "s/@MEDIALABEL@/$MEDIALABEL/g" \
      -e "s/@LIVEDE@/$(echo $LIVEDE |sed 's/BASE//')/g" \
      -e "s/@SL_VERSION@/$SL_VERSION/g"
  fi
done
# The iso2usb.sh script can use this copy of a MBR file as fallback:
cp -a /usr/share/syslinux/gptmbr.bin ${LIVE_STAGING}/boot/syslinux/
# We have memtest in the syslinux bootmenu:
mv ${LIVE_STAGING}/boot/syslinux/memtest ${LIVE_STAGING}/boot/

# Make use of proper console font if we have it available:
if [ -f /usr/share/kbd/consolefonts/${CONSFONT}.gz ]; then
  gunzip -cd /usr/share/kbd/consolefonts/${CONSFONT}.gz > ${LIVE_STAGING}/boot/syslinux/${CONSFONT}
elif [ ! -f ${LIVE_STAGING}/boot/syslinux/${CONSFONT} ]; then
  sed -i -e "s/^font .*/#&/" ${LIVE_STAGING}/boot/syslinux/menu/*menu*.cfg
fi

# -----------------------------------------------------------------------------
# Assemble the ISO
# -----------------------------------------------------------------------------

echo "-- Assemble the ISO image."

# We keep strict size requirements for the XFCE ISO:
# addons/optional modules will not be added.
if [ "$LIVEDE" != "XFCE"  ]; then
  # Copy our stockpile of add-on modules into place:
  if ls ${LIVE_TOOLDIR}/addons/*.sxz 1>/dev/null 2>&1 ; then
    cp ${LIVE_TOOLDIR}/addons/*.sxz ${LIVE_MOD_ADD}/
  fi
  # If we have optionals, copy those too:
  if ls ${LIVE_TOOLDIR}/optional/*.sxz 1>/dev/null 2>&1 ; then
    cp ${LIVE_TOOLDIR}/optional/*.sxz ${LIVE_MOD_OPT}/
  fi
fi

if [ "$LIVEDE" != "XFCE"  -a "$LIVEDE" != "LEAN" -a "$LIVEDE" != "SLACKWARE" ]
then
  # KDE/PLASMA etc will profit from accelerated graphics support;
  # however the SLACKWARE ISO should not have any non-Slackware content.
  # You can 'cheat' when building the SLACKWARE ISO by copying the graphics
  # drivers into the 'optional' directory yourself.
  if ls ${LIVE_TOOLDIR}/graphics/*${KVER}-${SL_VERSION}-${SL_ARCH}.sxz 1>/dev/null 2>&1 ; then
    # Add custom (proprietary) graphics drivers:
    echo "-- Adding binary GPU drivers supporting kernel ${KVER}."
    cp ${LIVE_TOOLDIR}/graphics/*${KVER}-${SL_VERSION}-${SL_ARCH}.sxz ${LIVE_MOD_OPT}/
    if ls ${LIVE_TOOLDIR}/graphics/nvidia-*xx.ids 1>/dev/null 2>&1 ; then
      cp ${LIVE_TOOLDIR}/graphics/nvidia-*xx.ids ${LIVE_MOD_OPT}/
    fi
  fi
fi

# Directory for rootcopy files (everything placed here will be copied
# verbatim into the overlay root):
mkdir -p ${LIVE_STAGING}/${LIVEMAIN}/rootcopy

# Create an ISO file from the directories found below ${LIVE_STAGING}:
create_iso ${LIVE_STAGING}

# Clean out the mounts etc:
cleanup