This file is indexed.

/usr/sbin/drblsrv is in drbl 2.20.11-4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   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
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
#!/bin/bash
#
# Copyright (C) 2002-2012 by NCHC, Steven Shiau, K. L. Huang, and Ceasar Sun
# (steven _at nchc org tw, klhaung _at_ gmail com, ceasar _at_ nchc org tw)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Those programs doing real jobs:
# drblsrv -> drblsrv-offline -> prepare-files-for-PXE-client -> generate-pxe-menu

# Load DRBL setting and functions
# Setting
# Source function library.
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions

# Load DRBL setting and functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions

# functions
check_if_repository_in_blacklist() {
  # yum_repo_blacklists is read from drbl.conf
  local url_tmp="$1"
  for iu in $url_tmp; do
    # If the url is in blacklist (like: http://ftp.isu.edu.tw), some problems! Terminated!
    if [ -n "$(echo "$yum_repo_blacklists" | grep -E "\<$iu")" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      if [ -n "$(echo "$iu" | grep -E "http://ftp.isu.edu.tw")" ]; then 
       # For some known site, output the reason
       echo "$msg_http_isu_edu_tw_not_compatible"
      fi
      echo "$iu: $msg_http_isu_edu_tw_not_compatible"
      echo "$msg_prompt_chang_yum_config"
      echo "$msg_program_stop"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      exit 1
    fi
  done
}
#
find_yum_repos_dir_path_for_opensuse() {
  # yum_conf_dir_path is global variable
  # Before OpenSuSE 10.3, the yum repository conf dir is /etc/yum.repos.d, while from OpenSuSE 11.0, it's /etc/yum/repos.d/
  if [ -d /etc/yum.repos.d/ ]; then
    yum_conf_dir_path=/etc/yum.repos.d
  elif [ -d /etc/yum/repos.d/ ]; then
    yum_conf_dir_path=/etc/yum/repos.d
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Yum repository conf dir NOT found!!!" 
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    echo "$msg_program_stop"
    exit 1
  fi
} # end of find_yum_repos_dir_path_for_opensuse
#
do_some_cleanup_before_uninstall() {
  echo "$msg_delimiter_star_line"
  # stop clonezilla so that no daemon (ocsmgrd, udpcast) is running
  echo "Stopping clonezilla if it is running..."
  drbl-ocs -l 0 stop

  if [ -n "$(unalias ls &>/dev/null; ls $sanboot_img_dump_dir/*aoe-*-for* 2>/dev/null)" ]; then
    echo "$msg_delimiter_star_line"
    echo "Stop vblade (for AoE) service ?"
    echo -n "[Y/n] "
    read stop_vblade_answer
    case "$SL_remove_answer" in 
       n|N|[nN][oO]) continue ;;
       *) drbl-aoe-serv stop ;;
    esac
  fi
  
  echo "$msg_delimiter_star_line"
  # rm the YP securenets
  drbl-yp-securenets clean

  if is_systemd_init; then
    echo "$msg_delimiter_star_line"
    # rm the drbl-clients-nat systemd service for server
    echo "Disabling drbl-clients-nat service..."
    systemctl disable /lib/systemd/system/drbl-clients-nat.service
    rm -f /lib/systemd/system/drbl-clients-nat.service
  fi

  echo "$msg_delimiter_star_line"
  echo "$msg_remove_SmallLinuxs_question"
  echo -n "[Y/n] "
  read SL_remove_answer
  case "$SL_remove_answer" in 
     n|N|[nN][oO]) continue ;;
     *) drbl-sl -l $lang_answer -u all ;;
  esac
  echo "$msg_delimiter_star_line"
} # end of do_some_cleanup_before_uninstall
#
run_drblsrv_offline() {
  # To run drblsrv_offline, these depends on distributions
  if [ -n "$CONSOLE_OUTPUT" ]; then
   # since there is space in $CONSOLE_OUTPUT, we have to use two params so that we can pass the varialbe with ""
   console_opt1="--console-output"
   console_opt2="$CONSOLE_OUTPUT"
  fi
  if [ -n "$PXE_SERIAL_OUTPUT" ]; then
   # since there is space in $PXE_SERIAL_OUTPUT, we have to use two params so that we can pass the varialbe with ""
   pxe_serial_opt1="--pxe-serial-output"
   pxe_serial_opt2="$PXE_SERIAL_OUTPUT"
  fi
  case "$client_kernel_install_mode" in
     "from_server")
        echo Now run: drblsrv-offline $verbose_opt -c -d -a -l $lang -s $client_kernel_ver $console_opt1 \"$console_opt2\" $pxe_serial_opt1 \"$pxe_serial_opt2\"
        drblsrv-offline $verbose_opt -c -d -a -l $lang -s $client_kernel_ver $console_opt1 "$console_opt2" $pxe_serial_opt1 "$pxe_serial_opt2"
        ;;
     "from_downloaded_pkg")
        case "$OS_type" in
          RH|MDK)
            #client_kernel_pkg="$(unalias ls 2>/dev/null; ls $client_kernel_tmp_dir/*.rpm)"
            client_kernel_pkg=$client_kernel_tmp_dir/$chosen_kernel_for_client

            ;;
  	  DBN)
	    # ${latest_drbl_kernel_in_rep} is global variable when downloading kernel from apt repository
	    # How about there are 2 kernel debs in /var/cache/apt/archives like:
	    # linux-image-2.6.18-5-486_2.6.18.dfsg.1-13etch4_i386.deb 
	    # linux-image-2.6.18-5-486_2.6.18.dfsg.1-13etch5_i386.deb
	    # and $latest_drbl_kernel_in_rep=linux-image-2.6.18-5-486
	    # Sol: use pkg-ver-latest to parse the latest one, since it's the same version no, just minor changes. It shouldn't cause any problem.
            client_kernel_pkg="$(unalias ls 2>/dev/null; ls $client_kernel_tmp_dir/${latest_drbl_kernel_in_rep}_*.deb 2>/dev/null | pkg-ver-latest)"
            ;;
          SUSE)
	    # ${latest_drbl_kernel_in_rep} is global variable when downloading kernel from apt/yum repository
            client_kernel_pkg="$(unalias ls 2>/dev/null; ls $client_kernel_tmp_dir/${latest_drbl_kernel_in_rep}* 2>/dev/null)"
            ;;
        esac
        # $drblsrv_offline_extra_opt is especially for Ubuntu restricted kernel (linux-restricted-modules) and linux-ubuntu-modules
        echo Now run: drblsrv-offline $verbose_opt -c -d -a -l $lang -k $client_kernel_pkg $drblsrv_offline_extra_opt $console_opt1 \"$console_opt2\" $pxe_serial_opt1 \"$pxe_serial_opt2\"
        drblsrv-offline $verbose_opt -c -d -a -l $lang -k $client_kernel_pkg $drblsrv_offline_extra_opt $console_opt1 "$console_opt2" $pxe_serial_opt1 "$pxe_serial_opt2"

        # Clean the kernel temp dir to release used disk space
        # RH/MDK: we do not use apt to download the kernel, so client_kernel_tmp_dir won't be /var/cache/apt/archives. We can remove it.
        # DBN: we USE apt to download the kernel in debian, so client_kernel_tmp_dir is /var/cache/apt/archives. We can NOT remove it.
        # SuSE: we use apt or yum to download the kernel, if the $client_kernel_tmp_dir is /var/cache/apt/archives, do not remove it
        if [ -n "$client_kernel_tmp_dir" -a \
             -d "$client_kernel_tmp_dir/" -a \
             -z "$(echo "$client_kernel_tmp_dir" | grep -Ew "^/var/cache/apt/archives")" ]; then
          rm -rf $client_kernel_tmp_dir/
        fi
        ;;
     "already_exist")
        echo "$msg_latest_kernel_is_already_installed"
        echo Now run: drblsrv-offline $verbose_opt -c -d -a -l $lang -p $console_opt1 \"$console_opt2\" $pxe_serial_opt1 \"$pxe_serial_opt2\"
        drblsrv-offline $verbose_opt -c -d -a -l $lang -p $console_opt1 "$console_opt2" $pxe_serial_opt1 "$pxe_serial_opt2"
        ;;
  esac
} # end of run_drblsrv_offline
#
setup_RH_yum() {
# 1.
# OS AYO repository
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_os_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_os_ayo_repository_0"
echo "$msg_os_ayo_repository_download_fedora_redhat_com"
echo "$msg_os_ayo_repository_use_existing_yum_config"
echo "$msg_os_ayo_repository_enter_by_yourself"
echo -n "[0] "
read os_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_RH_os_yum_repo_answer $OS_Version $os_ayo_repo_answer 

# drbl AYO repository
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_drbl_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_drbl_ayo_repository_0"
echo "$msg_drbl_ayo_repository_1"
echo "$msg_drbl_ayo_repository_2"
echo "$msg_drbl_ayo_repository_enter_by_yourself"
echo -n "[0] "
read drbl_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_RH_drbl_yum_repo_answer $drbl_ayo_repo_answer

# ask some RPM and  apt repository options, such as drbl-testing, drbl-unstable
ask_and_parse_rpm_ayo_repo_options

generate_fedora_yum_conf

generate_drbl_yum_conf

} # end of setup_RH_yum

setup_RH_apt() {
# 1.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_os_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_os_ayo_repository_0"
echo "$msg_os_ayo_repository_1"
#echo "$msg_ayo_repository_2"
echo "$msg_os_ayo_repository_3"
#echo "$msg_ayo_repository_4"
echo "$msg_os_ayo_repository_5"
echo -n "[0] "
read os_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_RH_os_apt_repo_answer $OS_Version $os_ayo_repo_answer 

#
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_drbl_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_drbl_ayo_repository_0"
echo "$msg_drbl_ayo_repository_1"
echo "$msg_drbl_ayo_repository_2"
echo "$msg_drbl_ayo_repository_3"
#echo "$msg_ayo_repository_4"
echo "$msg_drbl_ayo_repository_5"
echo -n "[0] "
read drbl_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_RH_drbl_apt_repo_answer $drbl_ayo_repo_answer

# ask some RPM and  apt repository options, such as drbl-testing, drbl-unstable
ask_and_parse_rpm_ayo_repo_options

# install wget if not installed, which is always necessary
rpm -q wget &>/dev/null
if [ "$?" -gt 0 ]; then 
  echo "$msg_no_wget_get_it"
  WGET_idx=$(echo $OS_Version | tr "\." "_")
  # we will get the variables like wget_rpm_RH8_0, wget_rpm_FC1, wget_rpm_MDK9_2
  eval WGET_RPM=\$wget_rpm_$WGET_idx
  for irpm in $RH_RPMS_os_update_dir; do
    echo "Try to install wget from $url_os_coreroot/$irpm/$WGET_RPM..."
    # TODO: make it work with file://...
    rpm -ivh --quiet $url_os_coreroot/$irpm/$WGET_RPM &>/dev/null
    RETVAL=$?
    [ $RETVAL -eq 0 ] && break
  done
  # check if wget installed
  rpm -q wget &>/dev/null
  if [ "$?" -gt 0 ]; then
   echo "$msg_wget_installed_failure"
  else
   echo "$msg_wget_installed_successful"
  fi
fi
# check if apt is installed or not
rpm -q apt &>/dev/null
if [ "$?" -gt 0 ]; then 
   echo "$msg_install_apt"
   # install apt
   echo "$msg_no_apt_get_it"
   case "$ayo_repo_answer" in
   # if local apt repository, do not use url in the network 
   3)
     # local apt repository
     rpm -Uvh $url_drbl_collect_root/RPMS.drbl-collect/apt-[0-9]*.i386.rpm
     RETVAL=$?
     if [ "$RETVAL" -gt 0 ]; then 
        echo "$msg_Failed_to_install $pkg_file..."
        return $RETVAL
     fi
     ;;
   *)
     get_inst_rpm -u $url_drbl_collect_root/RPMS.drbl-collect/ -p apt -a i386 -r "rpm -Uvh"
     rc=$?
     check_rpm_install $rc apt
   esac
else
   echo "$msg_delimiter_star_line"
   echo "$msg_apt_already_installed"
   echo "$msg_delimiter_star_line"
fi
# Clean the "Architecture "i[356]86"; "in apt.conf first,
# later we will insert Architecture "i386" to /etc/apt/apt.conf here,
perl -p -i -e "s/Architecture *\"i[356]86\".*;//g" /etc/apt/apt.conf

# backup apt sources.list if it's found.
echo "$msg_delimiter_star_line"
backup_apt_sources_list

# set the apt sources.list
# OS
case "$os_ayo_repo_answer" in
    3) 
      #local harddisk dir
cat <<-APT_END > /etc/apt/sources.list
rpm copy:$url_os/$os_ayo_path . $core_dir updates $FRESHRPMS
#rpm-src copy:$url_os/$os_ayo_path . $core_dir updates $FRESHRPMS
APT_END
    ;;
    *)
cat <<-APT_END > /etc/apt/sources.list
rpm $url_os/$os_ayo_path . $core_dir updates $FRESHRPMS
#rpm-src $url_os/$os_ayo_path . $core_dir updates $FRESHRPMS
APT_END
esac

# DRBL
case "$drbl_ayo_repo_answer" in
    3) 
      #local harddisk dir
cat <<-APT_END >> /etc/apt/sources.list
rpm copy:$url_drbl/$drbl_ayo_path . drbl-stable $DRBL_TEST $DRBL_UNSTABLE $CJK_REPOSITORY $DRBL_EXTRA 
#rpm-src copy:$url_drbl/$drbl_ayo_path . drbl-stable $DRBL_TEST $DRBL_UNSTABLE $CJK_REPOSITORY $DRBL_EXTRA 
APT_END
    ;;
    *)
cat <<-APT_END >> /etc/apt/sources.list
rpm $url_drbl/$drbl_ayo_path . drbl-stable $DRBL_TEST $DRBL_UNSTABLE $CJK_REPOSITORY $DRBL_EXTRA 
rpm $url_drbl_collect_root . drbl-collect
#rpm-src $url_drbl/$drbl_ayo_path . drbl-stable $DRBL_TEST $DRBL_UNSTABLE $CJK_REPOSITORY $DRBL_EXTRA 
#rpm-src $url_drbl_collect_root . drbl-collect
APT_END
esac

# append some examples to sources.list 
append_RH_apt_sources_list_example

echo "--------------------------------------------------------------------"
echo "$msg_enjoy_apt"

} # setup_RH_apt

install_drbl_RH() {
# function to install Redhat/Fedora DRBL

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

# clean the stale files and make necessary directories
[ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
# create the directory for diskless image
[ ! -d "/tftpboot/nbi_img" ] && mkdir -p /tftpboot/nbi_img

# 1.
# setup apt/yum first
# parse os to set the repository
parse_os_to_set_repository

echo "$msg_delimiter_star_line"

# Check package in blacklist. Especially for grub2.
case "$OS_Version" in
   CO*|RH*)
     grub2_ver_rel="$(LC_ALL=C rpm -q --qf "%{VERSION}-%{RELEASE}\n" grub2)"
     # For newer CentOS 7 (e.g. 7.1.1503) and Fedora 22, 23. It seems either (tftp) can not be accessed or some other issues.
     grub2_blacklist_v="2.02-0.16 2.02-0.17 2.02-0.23"
     for iblack in $grub2_blacklist_v; do
       if [ -n "$(echo $grub2_ver_rel | grep -iw "$iblack")" ]; then
         [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
         echo "This version of grub2 ($iblack) is buggy. You have to downgrade or upgrade it to bug free version, otherwise your uEFI client won't be able to make uEFI network boot, and its error in clients is like:"
         echo "error: timeout: could not resolve hardware address."
	 echo "Or"
	 echo "Blank screen for a while, then show an error about \"Could not find config file\" on tftp server."
         [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
         echo "E.g. you can run these commands as root to downgrade the bug free version of grub2 on CentOS:"
         if [ -n "$(rpm -q grub2-efi-modules)" ]; then
           echo "yum remove grub2-efi-modules"
         fi 
         echo "yum downgrade http://vault.centos.org/7.0.1406/os/x86_64/Packages/grub2-2.02-0.2.10.el7.centos.1.x86_64.rpm http://vault.centos.org/7.0.1406/os/x86_64/Packages/grub2-efi-2.02-0.2.10.el7.centos.1.x86_64.rpm http://vault.centos.org/7.0.1406/os/x86_64/Packages/grub2-tools-2.02-0.2.10.el7.centos.1.x86_64.rpm"
         echo "$msg_delimiter_star_line"
         confirm_continue_or_not_default_quit
	 break
       fi
     done
     ;;
esac
if [ -z "$select_repository" ]; then
  # default setting, maybe overwritten by command option
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_do_you_want_set_yum_repo"
  echo "$msg_set_yum_conf_yes_prompt"
  echo "$msg_set_yum_conf_no_prompt"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read set_repo_ans 
  case "$set_repo_ans" in
     y|Y|[yY][eE][sS])
       select_repository="yes"
       ;;
     *)
       select_repository="no"
       ;;
  esac
fi

# Set the fedora repository.
if [ "$installer" = "yum" ]; then
  # FC1-4: fedora.repo
  # FC5: fedore-core.repo
  # FC7: fedora.repo
  if [ -e "/etc/yum.repos.d/fedora-core.repo" ]; then
    FC_CORE_REPO="fedora-core.repo"
  elif [ -e "/etc/yum.repos.d/fedora.repo" ]; then
    FC_CORE_REPO="fedora.repo"
  else
    # default one:
    FC_CORE_REPO="fedora-core.repo"
  fi
fi

if [ "$select_repository" = "yes" ]; then 
  # Since we might for to install rpm package, it's better to import RPM key for yum, the default setting of yum enables key check.
  [ "$installer" = "yum" ] && import_rpm_key
  # For RH8/9.0/FC1-2, already set $select_repository=yes, so this will be run
  echo "Now select the $OS_Version repository settings..."
  setup_${OS_type}_${installer}
  install_curl_etc_via_${installer}_if_necessary
else
  # This only for yum
  # put drbl-core yum repos conf
  [ ! -d "/etc/yum.repos.d" ] && mkdir -p /etc/yum.repos.d

  # Check if the repository setting exists
  # TODO: need beter method to check.
  if [ ! -f "/etc/yum.repos.d/$FC_CORE_REPO" -a ! -f "/etc/yum.repos.d/CentOS-Base.repo" ]; then
     echo "$msg_delimiter_star_line"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
     echo "No yum repository setting found!" 
     echo "You must set the yum repository in /etc/yum.repos.d/!"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     echo "$msg_delimiter_star_line"
     echo "$msg_program_stop"
     exit 1
  fi
  echo "Use the $OS_Version repository settings in /etc/yum.repos.d/."

  # backup the old setting
  echo -n "Seting the DRBL yum repository in /etc/yum.repos.d/... "
  for ifile in $drbl_yum_repo_list; do
    [ -f "/etc/yum.repos.d/$ifile" ] && mv -f /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
  done
  cp -f $DRBL_SCRIPT_PATH/setup/yum-repos/drbl*.repo /etc/yum.repos.d/
  echo "done!"

  # ask some RPM and apt repository options, such as drbl-testing, drbl-unstable
  ask_and_parse_rpm_ayo_repo_options
  # turn on some branches if set
  if [ -n "$DRBL_STABLE" ]; then
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" /etc/yum.repos.d/drbl.repo
  else
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=0/gi" /etc/yum.repos.d/drbl.repo
  fi
  if [ -n "$DRBL_TEST" ]; then
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" /etc/yum.repos.d/drbl-testing.repo
  else
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=0/gi" /etc/yum.repos.d/drbl-testing.repo
  fi
  if [ -n "$DRBL_UNSTABLE" ]; then
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" /etc/yum.repos.d/drbl-unstable.repo
  else
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=0/gi" /etc/yum.repos.d/drbl-unstable.repo
  fi
  # [ -n "$DRBL_EXTRA" ] && perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" /etc/yum.repos.d/drbl-extra.repo

  get_url_ocs_then_install_curl_etc_via_yum_if_necessary

  if [ -z "$url_os" ]; then
    # Not found, use the default one...
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "The repository in the system is not found, use DRBL default one."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    parse_${OS_type}_os_${installer}_repo_answer $OS_Version default
    parse_${OS_type}_drbl_${installer}_repo_answer default
    echo "Use yum repository in $url_os_coreroot"
    check_if_repository_in_blacklist $url_os
  fi
fi

# ask some setting for client, such as SMP, serial console...
ask_and_parse_client_extra_setting

# parse the setting of client archi.
parse_client_archi_setting

# It's important to remove the cache of apt, otherwise 
# the RPM::Architecture will not take effect. check this:
# http://distro2.conectiva.com.br/pipermail/apt-rpm/2003-November/002015.html
# ... RPM::Architecture, is what he's looking for. His problem was that the cache was built before he set the architecture. Doing "rm -f /var/cache/apt/*.bin; apt-get update" would make it work.

if [ "$installer" = "apt" ]; then
  echo "$msg_clean_apt_cache"
  for ifile in /var/cache/apt/*.bin; do
     rm -f /var/cache/apt/$ifile
  done
fi

# Update the apt data with repository.
# if it's yum, skip this, later yum will do it. Because yum update will also
# do upgrade...
[ "$installer" = "apt" ] && $inst_prog update

# 2.
# check if apt is integrity... Ex: original MDK 9.2 is buggy...
if [ "$installer" = "apt" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_check_apt_integrity"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  check_apt_integrity
fi

# 3.
echo "$msg_delimiter_star_line"
# Before update or install packages, if there is yum-updatesd service, stop it first to avoid conflict
flag_start_yumupdatesd_later="no"
if [ -e /var/lock/subsys/yum-updatesd ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Stopping service yum-updatesd to avoid locking problem... "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  /etc/init.d/yum-updatesd stop
  flag_start_yumupdatesd_later="yes"
  # In case the following is running, kill it:
  # /usr/bin/python -tt /usr/libexec/yum-updatesd-helper --check --dbus
  # Otherwise we will see this annoying message:
  # "Another app is currently holding the yum lock; waiting for it to exit..."
  # Here we use subshell to avoid function names in /etc/init.d/functions might be the same with that in DRBL.
  ( # We need the function killproc in /etc/init.d/functions
    . /etc/init.d/functions
    killproc yum-updatesd-helper &>/dev/null
  )
fi
# 3.a
# We will ask if upgrade system first or not.
# Try to find if the i386 glibc and openssl are installed or not
# If they are installed, hold them not to be upgraded to i686
echo "$msg_delimiter_star_line"
if [ -z "$upgrade_system_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_upgrade_system_question"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read upgrade_system_answer
fi

case "$upgrade_system_answer" in 
   y|Y|[yY][eE][sS])
      do_upgrade_system_for_RH
      ;;
esac

# 3.b
# ugly...might be not my fault...
# for some distribution some version, we have to force upgrading some packages.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_try_to_update_some_pkgs"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
case "$OS_Version" in
  "RH8.0"|"RH9")
        # This one only works for RH8
        $inst_prog -y install redhat-config-xfree86
        ;;
  "FC1")
        # gpm is buggy, can not work with clonezilla, so upgrage to mine
	# in drbl repository
        echo "$msg_OS_version: $OS_Version."
	echo "$msg_force_to_upgrade_some_pkgs"
	$inst_prog -y install gpm redhat-config-xfree86
        ;;
  "FC2")
        # FC2 use different name "system-config-display" instead of "redhat-config-xfree86"
        echo "$msg_OS_version: $OS_Version."
	echo "$msg_force_to_upgrade_some_pkgs"
	$inst_prog -y install system-config-display
        ;;
  "FC4")
        # in FC4, sfdisk, rpm and nc are buggy, 
	# sfdisk can not work with clonezilla,
	# https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=159418
	# Original nc is buggy since it will skip some input tail...
	# So upgrage
        echo "$msg_OS_version: $OS_Version."
	echo "$msg_force_to_upgrade_some_pkgs"
	$inst_prog -y install rpm util-linux nc
        ;;
  "FC5")
        # released lftp (3.4.2-5) is buggy, can not work 
	# with http://free.nchc.org.tw/fedora
        echo "$msg_OS_version: $OS_Version."
	echo "$msg_force_to_upgrade_some_pkgs"
	$inst_prog -y install lftp
        ;;
  "FC6")
        # released kudzu (1.2.57.1-2) is buggy: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=213602
        echo "$msg_OS_version: $OS_Version."
	echo "$msg_force_to_upgrade_some_pkgs"
	$inst_prog -y install kudzu
        ;;
esac

# 4.
# prepare the kernel for clients
echo "$msg_delimiter_star_line"
echo "$msg_prepare_kernel_for_clients"
prepare_install_RH_MDK_client_kernel

# 5.
# Install the necessary packages for DRBL 
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_install_DRBL_necesary_files"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"
echo "$msg_install_dhcp_tftp_etc"

# wget maybe not installed, we will check it is installed only when apt is not installed, so if apt/yum is installed while wget is not installed, get_inst_rpm() will failed! Add wget here.

# $PKG_FROM_RH, $PKG_TO_QUERY, $PKG_TO_QUERY_RH and $PKG_FROM_DRBL are loaded from conf/drbl.conf
PKG_TO_QUERY="$PKG_TO_QUERY $PKG_TO_QUERY_RH"
echo "Searching if $PKG_TO_QUERY available... "
case "$installer" in
  yum)
     for ipkg in $PKG_TO_QUERY; do
       if [ -n "$(LC_ALL=C yum list $ipkg 2>/dev/null | grep -E "^$ipkg")" ]; then
         echo "Package $ipkg exists in repository."
         PKG_FROM_RH="$PKG_FROM_RH $ipkg"
       fi
     done
     ;;
  apt)
     for ipkg in $PKG_TO_QUERY; do
       if [ -n "$(apt-cache pkgnames $ipkg 2>/dev/null)" ]; then
         echo "Package $ipkg exists in repository."
         PKG_FROM_RH="$PKG_FROM_RH $ipkg"
       fi
     done
     ;;
esac

$inst_prog -y install $PKG_FROM_RH $PKG_FROM_DRBL
# TODO
RETVAL=$?
check_apt_install $RETVAL

# 6.
# After the dist upgrade, check the ARCH for glibc, openssl... if it does not fit the requirement, replace it.
install_client_archi_glibc_openssl_RH

echo "$msg_delimiter_star_line"
# 7.
# 7.a
# netinstall
if [ "$DRBL_NETINSTALL" = "yes" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_install_net_install_images"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_delimiter_star_line"
  drbl-netinstall -i all -s -v
fi

# 7.b
# Creat dev tarball in $drbl_pkgdir,
# which will be copied to /tftpboot/node_root/etc/diskless-image 
# in deploy script
tmp_dev_dir="$(mktemp -d -q /tmp/devtmp.XXXXXX)"
dev_wgettmp="$(mktemp -d -q /tmp/dev_wgettmp.XXXXXX)"
[ ! -d $drbl_pkgdir ] && mkdir -p $drbl_pkgdir

echo "$msg_delimiter_star_line"

# get dev
if [ -n "$(LC_ALL=C ls /sbin/udev* 2>/dev/null)" ]; then
  # udev
  echo "udev is found in server... The DRBL client will use udev... "
  # checking, just in case.
  # we must install udev...
  if ! rpm -q --quiet udev; then
    echo -n "Try to install udev..."
    $inst_prog -y install udev
    _install_udev_result=$?
    echo "done!"
    if [ "$_install_udev_result" -ne 0 ]; then
      # actually this should not happen, but who knows ?
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "Program udev was found in the server, but unable to install a complete udev rpm!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop"
      exit 1
    fi
  fi
else
  echo "$msg_get_dev"
  echo "$msg_creating_dev"
  # pre-packaged dev.tgz
  dev_got=0
  for ipkg in dev MAKEDEV; do
   Is_ipkg_avail=""
   for idir in $RH_RPMS_os_update_dir; do
     Is_ipkg_avail="$(list_available_rpm $url_os_coreroot/$idir/ | grep -E "^$ipkg-[0-9]+.*.i386.rpm" 2>/dev/null)"
     if [ -n "$Is_ipkg_avail" ]; then
       echo "Downloading $ipkg rpm from $url_os_coreroot/$idir/..." 
       wget -r -l1 --no-parent --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$dev_wgettmp" -A "$ipkg-[0-9]*.i386.rpm" $url_os_coreroot/${idir}/
     fi
     # once it's done, skip the next search
     if [ "$(ls $dev_wgettmp/$ipkg-[0-9]*.i386.rpm 2> /dev/null)" ]; then
       echo "Got the $ipkg rpm!"
       dev_got=1
       break
     else
       [ -n "$verbose" ] && echo "\"$ipkg\" is not found in $url_os_coreroot/$idir/."
     fi
   done
   # if we can not download dev rpm, then skip the MAKEDEV download.
   [ "$dev_got" = 0 ] && break
  done
  (
    if [ -n "$(ls $dev_wgettmp/dev[-_]*.i386.rpm 2>/dev/null)" -a -n "$(ls $dev_wgettmp/MAKEDEV[-_]*.i386.rpm 2>/dev/null)" ]; then
      cd $tmp_dev_dir
      echo -n "Creating the device files for DRBL client..."
      # To avoid the duplicated rpms... just find the latest one.
      latest_dev="$(list_latest_rpm $dev_wgettmp/ | grep -E "^dev[-_].*.i386.rpm")"
      latest_MAKEDEV="$(list_latest_rpm $dev_wgettmp/ | grep -E "^MAKEDEV[-_].*.i386.rpm")"
      rpm2cpio $dev_wgettmp/$latest_dev | cpio -idm
      rpm2cpio $dev_wgettmp/$latest_MAKEDEV | cpio -idm
      cd $tmp_dev_dir/dev && tar czf $drbl_pkgdir/dev.tgz *
      echo "done!"
    else
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "No udev, and no pre-packaged devices file!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop"
      exit 1
    fi
  )
  echo "$msg_clean_tmp_dev"
fi
[ -d "$tmp_dev_dir" -a -n "$(echo $tmp_dev_dir | grep "devtmp" )" ] && rm -rf $tmp_dev_dir
[ -d "$dev_wgettmp" -a -n "$(echo $dev_wgettmp | grep "dev_wgettmp" )" ] && rm -rf $dev_wgettmp
#
echo "$msg_delimiter_star_line"
if [ "$flag_start_yumupdatesd_later" = "yes" ]; then
  echo "Finished the package installation, now resume yum-updatesd."
  /etc/init.d/yum-updatesd start
fi

# 8.
echo "$msg_delimiter_star_line"
# Force to install all the kernel selected.
echo "$msg_install_kernel_for_clients ..."
echo "$msg_search_kernel_in_ayo kernel${SMP_OPTION} ..."
echo "$msg_delimiter_star_line"
run_drblsrv_offline

} # end of install_drbl_RH

uninstall_drbl_RH() {
# check if apt is installed or not
if ! rpm -q --quiet apt && ! rpm -q --quiet yum; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$msg_no_apt_already_uninstall"
   echo "$msg_program_stop"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   exit 1
fi

# if apt is not installed, the installer must be yum
if ! rpm -q --quiet apt; then
   installer="yum"
   inst_prog="yum"
fi

# Set the fedora repository.
if [ "$installer" = "yum" ]; then
  # FC1-4: fedora.repo
  # FC5: fedore-core.repo
  if [ -e "/etc/yum.repos.d/fedora-core.repo" ]; then
    FC_CORE_REPO="fedora-core.repo"
  elif [ -e "/etc/yum.repos.d/fedora.repo" ]; then
    FC_CORE_REPO="fedora.repo"
  else
    # default one:
    FC_CORE_REPO="fedora-core.repo"
  fi
fi

#
do_some_cleanup_before_uninstall
drbl-netinstall -u all -s

#
# skip removing dialog, rsync, curl... which are usually necessary.
# TODO, separate ntp, it seems that to remove ntp together will fail in FC3 
# with yum.
$inst_prog remove $PKG_TO_UNINSTALL_PART1_RH $PKG_FROM_DRBL
[ -n "$PKG_TO_UNINSTALL_PART2_RH" ] && $inst_prog remove $PKG_TO_UNINSTALL_PART2_RH

# rename some config files if they still exist
for icfg in dhcpd.conf dhcpd.conf.etherboot.include exports sysconfig/iptables; do
  [ -f "/etc/${icfg}" ] && mv -f /etc/${icfg} /etc/${icfg}.drblsave 
done

# remove the stale files
rm -f /etc/init.d/ocs
rm -f $SYSCONF_PATH/mkswapfile

echo "$msg_remove_drbl_setup_question"
echo -n "[y/N] "
read drbl_remove_answer
case "$drbl_remove_answer" in 
   y|Y|[yY][eE][sS])
      $inst_prog remove drbl
      drbl_remove_answer=y
      ;;
esac
if [ "$installer" = "apt" ]; then
  echo "$msg_remove_apt_question"
  echo -n "[y/N] "
  read apt_answer
  case "$apt_answer" in 
     y|Y|[yY][eE][sS])
        rpm -e apt
        [ -d /var/cache/apt/ ] && echo "$msg_cleaning_apt_cache" && rm -rf /var/cache/apt/
     ;;
  esac
elif [ "$installer" = "yum" ]; then
  echo "$msg_remove_yum_repo_question"
  echo -n "[y/N] "
  read yum_repo_answer
  case "$yum_repo_answer" in 
   y|Y|[yY][eE][sS])
     repo_lists="$drbl_yum_repo_list"
     for ifile in $repo_lists; do
       [ -f "/etc/yum.repos.d/$ifile" ] && mv -v /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
     done
  esac
fi

# clean tftpboot and misc files.
clean_tftpboot_and_misc

echo "$msg_Done!!!"

} # end of uninstall_drbl_RH

#
setup_MDK_urpmi() {
# Setting
echo "$msg_delimiter_star_line"

# 1.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_os_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
mdk_os_ayo_repo_answer_def="2"
echo "$msg_os_ayo_repository_0 (http),"
echo "1 -> ftp.twaren.net (http),"
echo "2 -> mirrors.kernel.org (http),"
echo "3 -> ftp.isu.edu.tw (ftp),"
echo "$msg_os_ayo_repository_5"
echo -n "[$mdk_os_ayo_repo_answer_def] "
read mdk_os_ayo_repo_answer
[ -z "$mdk_os_ayo_repo_answer" ] && mdk_os_ayo_repo_answer="$mdk_os_ayo_repo_answer_def"
echo "$msg_delimiter_star_line"
parse_MDK_os_ayo_repo_answer $mdk_os_ayo_repo_answer

echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_drbl_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
mdk_drbl_ayo_repo_answer_def="0"
echo "$msg_drbl_ayo_repository_0 (http),"
echo "$msg_drbl_ayo_repository_1_mdk (http),"
echo "$msg_drbl_ayo_repository_2_mdk (http),"
echo "$msg_drbl_ayo_repository_5,"
echo -n "[$mdk_drbl_ayo_repo_answer_def] "
read mdk_drbl_ayo_repo_answer
echo "$msg_delimiter_star_line"
[ -z "$mdk_drbl_ayo_repo_answer" ] && mdk_drbl_ayo_repo_answer="$mdk_drbl_ayo_repo_answer_def"
parse_MDK_drbl_ayo_repo_answer $mdk_drbl_ayo_repo_answer

# ask some RPM and  apt repository options, such as drbl-testing, drbl-unstable
ask_and_parse_rpm_ayo_repo_options

# Import the pub GPG keys
for ik in /etc/RPM-GPG-KEYS/*.asc; do
  key_version=$(basename $ik | sed -e "s/.asc//g")
  if ! rpm -q gpg-pubkey-$key_version &>/dev/null; then
    echo -n "Importing the public GPG keys from $ik... "
    rpm --import $ik
    echo "done!"
  fi
done

# TODO: find a method to keep the existing OS uprmi, but overwrite the DRBL urpmi
[  -f "/etc/urpmi/urpmi.cfg" ] && cp -v /etc/urpmi/urpmi.cfg /etc/urpmi/urpmi.cfg.drblsave
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_reset_all_existing_urpmi_setting"
echo "$msg_are_u_sure_u_want_to_continue"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[Y/n] "
read remove_urpmi_setting
case "$remove_urpmi_setting" in
  n|N|[nN][oO])
     echo "$msg_program_stop"
     exit 1
     ;;
  y|Y|[yY][eE][sS])
     echo "$msg_ok_let_do_it"
     ;;
esac
echo "Removing the old urpmi setting..."
echo "$msg_delimiter_star_line"
urpmi.removemedia -a

echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "Adding urpmi source..."
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"
# OS
urpmi.addmedia --distrib --probe-synthesis MDK${MDK_VER_PATH} $url_os/$os_ayo_path/$MDK_VER_PATH/$ARCH/
urpmi.addmedia MDK${MDK_VER_PATH}-updates $url_os/$os_ayo_path/updates/${update_extra_code_name}$MDK_VER_PATH/$update_rpmdir_info

# DRBL
case "$OS_Version" in
    # For older than 2009.0 version, we need to use "with ../...cz"
    # For 2009.0 or later, not necessary, since media_info is already in the RPMS.drbl-*/
  MDK10.[1-2]|MDV200[678].*)
    drbl_stable_mdv_with_option="with ../base/synthesis.hdlist.drbl.cz"
    drbl_testing_mdv_with_option="with ../base/synthesis.hdlist.drbl-testing.cz"
    drbl_unstable_mdv_with_option="with ../base/synthesis.hdlist.drbl-unstable.cz"
    ;;
esac
urpmi.addmedia drbl-stable $url_drbl/$drbl_ayo_path/RPMS.drbl-stable/ $drbl_stable_mdv_with_option
if [ -n "$DRBL_TEST" ]; then
  urpmi.addmedia drbl-testing $url_drbl/$drbl_ayo_path/RPMS.drbl-testing/ $drbl_testing_mdv_with_option
fi
if [ -n "$DRBL_UNSTABLE" ]; then
  urpmi.addmedia drbl-unstable $url_drbl/$drbl_ayo_path/RPMS.drbl-unstable/ $drbl_unstable_mdv_with_option
fi

echo "--------------------------------------------------------------------"
echo "The urpmi setting is finished."

} # setup_MDK_urpmi

install_drbl_MDK() {
# function to install MDK DRBL

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# clean the stale files and make necessary directories
[ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
# create the directory for diskless image
[ ! -d "/tftpboot/nbi_img" ] && mkdir -p /tftpboot/nbi_img

# 1.
# setup urpmi first
# parse os to set the repository
parse_os_to_set_repository

if [ "$select_repository" = "yes" ]; then 
  setup_${OS_type}_${installer}
else
  # we need $url_os, "$os_ayo_path"and "$url_os_coreroot for kernel and dev
  parse_${OS_type}_os_ayo_repo_answer default $OS_Version
  parse_${OS_type}_drbl_ayo_repo_answer default
  echo "Use urpmi repository in $url_os_coreroot"
fi

# Now only for urpmi only, no more apt in MDK
inst_prog="urpmi"
inst_opt="--auto --force"
inst_action=""

# we need curl to parse url to get kernel.
# we need lftp to parse the yum repo config if the repo is in ftp://
$inst_prog $inst_opt $inst_action lftp curl

# ask some setting for client, such as SMP, serial console...
ask_and_parse_client_extra_setting

# for MDK, although it's only i586, but for mdk10.1/10.2, i686 is necessary for
# SMP. MDK does NOT provide i586-smp kernel
parse_client_archi_setting

# 2.
# We will upgrade system first.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_upgrade_system_question"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[y/N] "
read upgrade_system_answer
case "$upgrade_system_answer" in 
  y|Y|[yY][eE][sS])
      do_upgrade_system_for_MDK
      ;;
esac

# 3.
# prepare the kernel for clients
echo "$msg_delimiter_star_line"
echo "$msg_prepare_kernel_for_clients"
prepare_install_RH_MDK_client_kernel

# 4.
# Install the necessary packages for DRBL 
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_install_DRBL_necesary_files"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"

# $PKG_FROM_MDK, $PKG_TO_QUERY and $PKG_FROM_DRBL are loaded from conf/drbl.conf
# Check if lvm2 is available, if so, add it, we need this for 
# clonezilla to save or restore LVM2 PV/VG/LV.
# Check lzop also.
echo "Searching if $PKG_TO_QUERY available... "
for ipkg in $PKG_TO_QUERY; do
  if urpmq $ipkg &>/dev/null; then
    echo "Package $ipkg exists in repository."
    PKG_FROM_MDK="$PKG_FROM_MDK $ipkg"
  fi
done

$inst_prog $inst_opt $inst_action $PKG_FROM_MDK $PKG_FROM_DRBL

# 4.b
# ugly...might be not my fault...
# for some distribution some version, we have to force upgrading some packages.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_try_to_update_some_pkgs"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
case "$OS_Version" in
  MDV200[6789].*|MDV201[0].*)
        # released cdialog is buggy, can not work correctly without gpm running
        echo "$msg_OS_version: $OS_Version."
	echo "$msg_force_to_upgrade_some_pkgs"
	$inst_prog $inst_opt $inst_action gpm
        ;;
esac

# netinstall
if [ "$DRBL_NETINSTALL" = "yes" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_install_net_install_images"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_delimiter_star_line"
  drbl-netinstall -i all -s -v
fi

# 5. dev for client machines
# For MDK 9.2... no dev rpm...It's necessary to make one before run
# this... (Just create the tarball from some Mandrake machine)
# It's ugly here...
[ ! -d $drbl_pkgdir ] && mkdir -p $drbl_pkgdir
if [ -f $drbl_setup_path/files/${OS_type}/${OS_Version}/dev.${OS_Version}.tgz ]; then
  cp -f $drbl_setup_path/files/${OS_type}/${OS_Version}/dev.${OS_Version}.tgz $drbl_pkgdir/dev.tgz
else
  # we must install udev...
  if ! rpm -q --quiet udev; then
    echo -n "Try to install udev..."
    $inst_prog $inst_opt $inst_action udev
    echo "done!"
  fi
  if ! rpm -q --quiet udev; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "No udev, and no pre-packaged devices file!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    echo "$msg_program_stop"
    exit 1
  fi
fi

# 6.
echo "$msg_delimiter_star_line"
# Force to install all the kernel selected.
echo "$msg_install_kernel_for_clients ..."
echo "$msg_search_kernel_in_ayo kernel${SMP_OPTION} ..."
echo "$msg_delimiter_star_line"
run_drblsrv_offline

} # end of install_drbl_MDK

uninstall_drbl_MDK() {
# check if apt is installed or not
if ! rpm -q --quiet apt && ! rpm -q --quiet urpmi; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$msg_no_apt_already_uninstall"
   echo "$msg_program_stop"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   exit 1
fi

# if apt is not installed, the installer must be urpmi
if ! rpm -q --quiet apt; then
   installer="urpmi"
   uninst_prog="urpme"
   uninst_action=""
else
   installer="apt"
   uninst_prog="apt-get"
   uninst_action="remove"
fi

#
do_some_cleanup_before_uninstall
drbl-netinstall -u all -s

# Do not uninstall curl, usually necessary
$uninst_prog $uninst_action $PKG_TO_UNINSTALL_PART1_MDK $PKG_FROM_DRBL
[ -n "$PKG_TO_UNINSTALL_PART2_MDK" ] && $uninst_prog $uninst_action $PKG_TO_UNINSTALL_PART2_MDK

# rename some config files if they exist
for icfg in dhcpd.conf dhcpd.conf.etherboot.include exports sysconfig/iptables; do
  [ -f "/etc/${icfg}" ] && mv -f /etc/${icfg} /etc/${icfg}.drblsave 
done

# remove the stale files
rm -f /etc/init.d/ocs
rm -f $SYSCONF_PATH/mkswapfile

echo "$msg_remove_drbl_setup_question"
echo -n "[y/N] "
read drbl_remove_answer
case "$drbl_remove_answer" in 
  y|Y|[yY][eE][sS])
    $uninst_prog $uninst_action drbl
    ;;
esac

echo "$msg_remove_urpmi_media_question"
echo -n "[y/N] "
read urpmi_media_answer
case "$urpmi_media_answer" in 
   y|Y|[yY][eE][sS])
     urpmi.removemedia -a
   ;;
esac

# clean tftpboot and misc files.
clean_tftpboot_and_misc

echo "$msg_Done!!!"

} # end of uninstall_drbl_MDK

uninstall_drbl_DBN() {
# function to uninstall Debian DRBL
[ -e /etc/lsb-release ] && . /etc/lsb-release
#if [ "$DISTRIB_ID" = "Ubuntu" ]; then
#  # Ubuntu, not Debian.
#  # Force to set LC_ALL=C, otherwise in Ubuntu, it will show a lot warning messages in debconf like
#  # perl: warning: Setting locale failed.
#  # perl: warning: Please check that your locale settings:
#  # LANGUAGE = "zh_TW:zh:en_US:en",
#  # LC_ALL = "zh_TW.Big5",
#  # LANG = "zh_TW.UTF-8"
#  # are supported and installed on your system.
#  # perl: warning: Falling back to the standard locale ("C").
#  export LC_ALL=C
#fi

#
do_some_cleanup_before_uninstall
drbl-netinstall -u all -s

#
# do not remove rsync, bc, iptables, curl since this will remove a lots in B2D.
# $PKG_TO_UNSINSTALL_PART*_DBN and $PKG_FROM_DRBL loaded from conf/drbl.conf
apt-get --purge remove $PKG_TO_UNINSTALL_PART1_DBN $PKG_FROM_DRBL
# Separate the dhcp3-server/dhcp3-common as an another package to be removed, othewise something went wrong in Woody. Do not put --purge 
[ -n "$PKG_TO_UNINSTALL_PART2_DBN" ] && apt-get remove $PKG_TO_UNINSTALL_PART2_DBN

# rename some config files if they exist
for icfg in exports drbl-nat.up.rules; do
  [ -f "/etc/${icfg}" ] && mv -f /etc/${icfg} /etc/${icfg}.drblsave 
done
for icfg in dhcpd.conf dhcpd.conf.etherboot.include; do
  # For distribution <= lenny
  [ -f /etc/dhcp3/$icfg ] && mv -f /etc/dhcp3/${icfg} /etc/dhcp3/${icfg}.drblsave
  # For distribution >= squeeze
  [ -f /etc/dhcp/$icfg ] && mv -f /etc/dhcp/${icfg} /etc/dhcp/${icfg}.drblsave
done
# remove drbl-clients-nat
if [ -e /etc/init.d/drbl-clients-nat ]; then
  get_debian_ubuntu_init_serv_control_prog
  if [ "$dbn_ubn_serv_control_prog" = "use-insserv" ]; then
    # Use insserv
    insserv -r drbl-clients-nat &>/dev/null
  else
    # Use update-rc.d
    update-rc.d -f drbl-clients-nat remove &>/dev/null
  fi
  rm -f /etc/init.d/drbl-clients-nat
fi

# remove the stale file
rm -f /etc/init.d/ocs
rm -f $SYSCONF_PATH/mkswapfile
[ -f $SYSCONF_PATH/drbl-nat.up.rules ] && mv -f $SYSCONF_PATH/drbl-nat.up.rules $SYSCONF_PATH/drbl-nat.up.rules.drblsave

echo "$msg_remove_drbl_setup_question"
echo -n "[y/N] "
read drbl_remove_answer
case "$drbl_remove_answer" in 
  y|Y|[yY][eE][sS])
    apt-get --purge remove drbl
    ;;
esac

# clean tftpboot and misc files.
clean_tftpboot_and_misc

echo "$msg_Done!!!"

} # end of uninstall_drbl_DBN

uninstall_drbl_SUSE() {
# Function to uninstall SuSE DRBL
#
do_some_cleanup_before_uninstall
drbl-netinstall -u all -s

# do not remove curl, usually necessary
if [ "$installer" = "apt" ]; then
  apt-get --purge remove $PKG_TO_UNINSTALL_SUSE $PKG_FROM_DRBL
elif [ "$installer" = "yum" ]; then
  find_yum_repos_dir_path_for_opensuse
  yum remove $PKG_TO_UNINSTALL_SUSE $PKG_FROM_DRBL
elif [ "$installer" = "zypper" ]; then
  zypper remove $PKG_TO_UNINSTALL_SUSE $PKG_FROM_DRBL
fi

# rename some config files if they exist
for icfg in dhcpd.conf dhcpd.conf.etherboot.include exports; do
  [ -f "/etc/${icfg}" ] && mv -f /etc/${icfg} /etc/${icfg}.drblsave 
done

# remove the stale files
rm -f /etc/init.d/ocs
rm -f /etc/apt/apt.conf.d/drbl-arch.conf
rm -f $SYSCONF_PATH/mkswapfile

if [ "$installer" = "apt" ]; then
  echo "$msg_remove_apt_question"
  echo -n "[y/N] "
  read apt_answer
  case "$apt_answer" in 
     y|Y|[yY][eE][sS])
        rpm -e apt-libs apt
        [ -d /var/cache/apt/ ] && echo "$msg_cleaning_apt_cache" && rm -rf /var/cache/apt/
     ;;
  esac
elif [ "$installer" = "yum" ]; then
  echo "$msg_remove_yum_repo_question"
  echo -n "[y/N] "
  read yum_repo_answer
  case "$yum_repo_answer" in 
   y|Y|[yY][eE][sS])
     # we won't touch the existing one, just copied by drbl.
     repo_lists="$drbl_yum_repo_list opensuse-${OS_VER_NO}.repo opensuse-updates-${OS_VER_NO}.repo"
     for ifile in $repo_lists; do
       [ -f "$yum_conf_dir_path/$ifile" ] && mv -v $yum_conf_dir_path/$ifile $yum_conf_dir_path/$ifile.drblsave
     done
  esac
fi

echo "$msg_remove_drbl_setup_question"
echo -n "[y/N] "
read drbl_remove_answer
case "$drbl_remove_answer" in 
  y|Y|[yY][eE][sS])
   [ "$installer" = "apt" ] && apt-get --purge remove drbl
   [ "$installer" = "yum" ] && yum remove drbl
   [ "$installer" = "zypper" ] && zypper remove drbl
   ;;
esac

# clean tftpboot and misc files.
clean_tftpboot_and_misc

echo "$msg_Done!!!"

} # end of uninstall_drbl_SUSE

# common functions for DRBL in RedHat/Fedora/Mandrake Linux.

#
apt_install_result_msg() {
  RETVAL="$1"
  if [ "$RETVAL" -gt 0 ]; then 
    echo "$msg_failed_to_install_pkgs"
    echo "$msg_please $msg_browse $drbl_url_zh_TW $msg_or $drbl_url_en_US $msg_for_more_details. "
    echo "$msg_press_ctrl_c_stop"
    read
    exit 1 
  fi
}
#
check_apt_install() {
  local RETVAL=$1
  if [ "$RETVAL" -gt 0 ]; then 
    echo "$msg_failed_to_install_pkgs"
    echo "$msg_please $msg_browse $drbl_url_zh_TW $msg_or $drbl_url_en_US $msg_for_more_details. "
    echo "$msg_press_ctrl_c_stop"
    read
    exit 1 
  fi
}

#
check_rpm_install() {
  local RETVAL=$1
  local pkg=$2
  if [ "$RETVAL" -gt 0 ]; then
  echo "XXXXXXX        XXXXXXX       XXXXXXX"
  echo "$msg_Failed_to_install \"$pkg\"!!!"
  echo "$msg_check_network_and_url $url"
  exit 1
  fi
}

# define a function get_inst_rpm to get and install rpm package which 
# apt can not process now. ie. only i386 rpms
get_inst_rpm() {
  # Note! the last / in $url must be kept!!!
  # example: get_inst_rpm -u http://os.nchc.org.tw/... -p glibc -a i386
  # If -f|--url-pkg is set, the rpm name is specified, so we will NOT use -A '*...rpm" for wget.
  # Note: -f|--url-pkg (no wildcard) and "-u|--url, -p|--pkg, -a|--arch" (wildcard) are two different ways to assign the package. They conflict each other.
  local specified_name="no"
  local url_tmp pkg arch rpm_run_and_opt dir="" assign_pkg_tmp_dir
  while [ $# -gt 0 ]; do
    case "$1" in
     -f|--url-pkg)
        # full name (path and name, no wildcard)
        shift; url_pkg="$1"
        specified_name="yes"
	url_tmp="$(dirname $url_pkg)"
	# Note! This pkg is like: glibc-2.4-25.i586.rpm
	pkg="$(basename $url_pkg)"
	shift;;
     -u|--url)
        shift; url_tmp="$1"
	shift;;
     -p|--pkg)
        shift; pkg="$1"
	shift;;
     -a|--arch)
        shift; arch="$1"
	shift;;
     -r|--rpm-run-and-opt)
        shift; rpm_run_and_opt="$1"
	shift;;
     -t|--tmp-dir)
        shift; assign_pkg_tmp_dir="$1"
	shift;;
     *)
        break ;;
    esac
  done
  if [ -z "$assign_pkg_tmp_dir" ]; then
     pkg_tmp_dir="$(mktemp -d -q /tmp/${pkg}-rpm-tmp.XXXXXX)"
  else
     pkg_tmp_dir="$assign_pkg_tmp_dir"
  fi
  # try to append the version for regular expression if pkg without version number filter.
  if [ "$specified_name" = "no" ]; then
    if [ -n "$pkg" -a -z "$(echo $pkg | grep -E "\-[0-9]")" ]; then
      pkg="$pkg-[0-9]"
    fi
  fi
  case "$url_tmp" in
  http://*|ftp://*)
    if [ "$specified_name" = "yes" ]; then
      wget -r -l1 --no-parent --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$pkg_tmp_dir" $url_pkg
    else
      # use wildcard
      wget -r -l1 --no-parent --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$pkg_tmp_dir" -A "$pkg*.$arch.rpm" $url_tmp/
    fi
    ;;
  file://*)
    # TODO: maybe use link (ln -fs ) ?
    dir="$(echo $url_tmp | sed -e "s|file://||g")"
    if [ "$specified_name" = "yes" ]; then
      cp -f "$dir/$pkg" $pkg_tmp_dir
    else
      # use wildcard
      cp -f "$dir/$pkg*.$arch.rpm" $pkg_tmp_dir
    fi
    ;;
  esac
  pkg_file="$(ls $pkg_tmp_dir/$pkg* 2>/dev/null)"
  # If the rpm_run_and_opt exists, we install the rpm package
  if [ -n "$rpm_run_and_opt" ]; then
    if [ -n "$pkg_file" ]; then
      $rpm_run_and_opt $opt $pkg_file
      RETVAL=$?
      if [ "$RETVAL" -gt 0 ]; then 
         if [ -z "$(echo $pkg | grep -E "\<glibc\>")" ]; then
           # UGLY!!!
           # For glibc, with --force, although the reinstallation is successful,
           # but the return code is always 1. ignore that.
           echo "$msg_Failed_to_install $pkg_file..."
         fi
      fi
    else
      RETVAL=1
    fi
  else
      # only download
      RETVAL=0
  fi
  # If the pkg_tmp_dir is not assigned, we can remove it. Otherwise it's for
  # package to be used.
  if [ -z "$assign_pkg_tmp_dir" ]; then
    [ -d "$pkg_tmp_dir" -a -n "$(echo $pkg_tmp_dir | grep "-rpm-tmp" )" ] && rm -rf $pkg_tmp_dir
  fi
  return $RETVAL
} # end of get_inst_rpm
# force to run apt-get install, this will force to install the necessary packages
check_apt_integrity() {
   apt-get -f --yes install
}

#
prepare_install_RH_MDK_client_kernel() {
local kgot
local KEXTRA=""
local avail_krpm
LATEST_KTMP="$(mktemp /tmp/latest_ktmp.XXXXXX)"

# For Fedora Core 5 with x86_64 arch, all kernels have SMP support, even though the name will not contain smp.
case "$OS_Version" in
   FC[12][0123456789])
     # From Fedora Core 6, one kernel fits single processor and SMP (including 
     # i386 and x86_64 distributions).
     SMP_OPTION=""
     ;;
esac

# set the OS and update dir
eval RPMS_os_update_dir=\$${OS_type}_RPMS_os_update_dir

# Set KARCH and KARCH_RPM, the reason we need to use these two different variable is that some kernel has "$KARCH_RPM" tag, but actually it's for $KARCH, especially in MDK10.[1-2]|MDV2005|MDV2006.0.
case "$client_archi_set" in
 "best_optimization")
   # optimization, it could be i386/i586/i686/x86_64
   KARCH="$(drbl-check-kernel-cpu-arch `uname -r`)"
   # if not found (maybe user make his own kernel,
   # but did NOT put config in /boot), set it as that in the server
   [ -z "$KARCH" ] && KARCH=`uname -m`
   KARCH_RPM=$KARCH
   ;;
 "i386")
   # for i386... this is not available for FC2/3, but we will force it
   # to be i586 before this...
   KARCH=i386
   KARCH_RPM=$KARCH
   ;;
 *)
   # for i586
   KARCH=i586
   KARCH_RPM=$KARCH
   ;;
esac

# For Mandriva, before overwrite KARCH, we have to decide "KEXTRA" first.
# Makdrake 10.1/10.2, MDV2005/2006.0 uses kernel-i586-up-1GB for 586 CPU,
# The kernel rpm kernel-2.6.11.6mdk-1-1mdk.i586.rpm is only working with i686 CPU.
# BUG REPORTED, but they do NOT care!
# So I have to put a special case here. 
# http://qa.mandrakesoft.com/show_bug.cgi?id=14581
# http://qa.mandrakesoft.com/show_bug.cgi?id=15383
# SHIT! Why MDK 10.1/10.2/2006.0 kernel arch does NOT consistent with rpm name ?
#
# Mandriva 2008.0, new kernel name "kernel-desktop" (for i686) or "kernel-desktoip586" (for i586)...
case "$OS_Version" in
  MDK10.[1-2]|MDV2005|MDV2006.0)
    case "$KARCH" in
     i[35]86)
       if [ -z "$SMP_OPTION" ]; then
         KEXTRA="-i586-up-1GB"
         echo "Since there is a bug in Mandarke 10.1, Mandriva 2005 and Mandriva 2006.0, we need to use the kernel rpm package "kernel${KEXTRA}" for i586 CPU client!"
       fi
       ;;
    esac
    ;;
  MDV200[89].*|MDV201[0].*)
    case "$KARCH" in
     i[35]86)
       if [ -z "$SMP_OPTION" ]; then
	 # The kernel package name is like: kernel-desktop586-2.6.22.9-1mdv-1-1mdv2008.0.i586.rpm
         KEXTRA="-desktop586"
         echo "Since the kernel-.*-i586.rpm is actually for i686 CPU in Mandriva 2008 or later, we need to use the kernel rpm package "kernel${KEXTRA}" for i586 CPU client!"
       fi
       ;;
     i686)
       if [ -z "$SMP_OPTION" ]; then
         # The kernel package name is like: kernel-desktop-2.6.22.9-1mdv-1-1mdv2008.0.i586.rpm
         KEXTRA="-desktop"
       fi
       ;;
    esac
    ;;
   FC*)
     # For Fedora 17, kernel package availables:
     # kernel-3.3.4-5.fc17.i686.rpm
     # kernel-PAE-3.3.4-5.fc17.i686.rpm
     # This is regular expression, we will put it in as parameters of "grep -E"
     KEXTRA="(|-PAE)"
    ;;
esac

# Mandriva i586 version is not consistent. We have to overwrite the above rules.
# Ex: Makdrake 10.1/10.2 uses kernel-i586-up-1GB for 586 CPU,
# The kernel rpm kernel-2.6.11.6mdk-1-1mdk.i586.rpm is only working 
# with i686 CPU.
# STUPID!!! We have to force the rpm package tag (KARCH_RPM) as i586, 
# but actually it for i686 (KARCH). SHIT!!!
case "$OS_Version" in
  MDK10.[1-2]|MDV*)
     if [ "$ARCH" = "i586" ]; then
       KARCH=i686
       KARCH_RPM=i586
     fi
     ;;
esac

# get the client_archi, which is used to filter the rpm package name.
case "$KARCH" in
  i[0-9]86)
     # skip the leading "i"
     client_archi=${KARCH_RPM:1}
   ;;
   *)
     # for x86_64
     client_archi=${KARCH_RPM}
     ;;
esac

case "$OS_type" in
  MDK)
    # For MDK, since only 1 CPU kernel type, then it will be universal. We
    # use "cat -" to pipe, i.e. all work.
    cpu_filter="cat -"
    ;;
  *)
    cpu_filter="grep -i $client_archi"
    ;;
esac

#
klist_no=
i=1
for idir in $RPMS_os_update_dir; do
  kgot="$(list_available_rpm $url_os_coreroot/${idir}/  | grep -E "^kernel${KEXTRA}${SMP_OPTION}-[2-9]\.[0-9]+\.[0-9]+" | grep -v "test" | $cpu_filter | sort -V -r | uniq)" 
  # store the url to show error msg later if no kernel available.
  klist_ar[$i]="$url_os_coreroot/${idir}/"
  i=$((i+1))
  # RPMS.os and RPMS.core might be symbolic link, so maybe the result will
  # duplicate. To avoid this, we filter it first.
  if [ -n "$kgot" ]; then
     for ikgot in $kgot; do
      if ! grep -q $ikgot $LATEST_KTMP; then
	# make a tag so that we know the kernel is from ?
        if [ -n "$(echo $idir | grep -i updates)" ]; then
          ktag="(in updates repository)"
	else
          ktag="(in release repository)"
        fi
	klist_no=$(($klist_no + 1))
        echo "${klist_no}: $ikgot $ktag" >> $LATEST_KTMP
      fi
     done
  fi
done

if [ -n "$SMP_OPTION" ]; then
  # enable SMP filter
  smp_filter="grep -i smp"
else
  # "NOT SMP" filter
  smp_filter="grep -iv smp"
fi

# Another available kernel maybe is from server, check it:
running_karch="$(drbl-check-kernel-cpu-arch `uname -r`)"
running_kernel_in_server="$(echo $(uname -r) $running_karch | $cpu_filter | $smp_filter)"
if [ -n "$running_kernel_in_server" ]; then
  klist_no=$(($klist_no + 1))
  echo "${klist_no}: kernel_$running_kernel_in_server (from this DRBL server)" >> $LATEST_KTMP
fi

# If no kernel is found, show some method
if [ -z "$(cat $LATEST_KTMP 2>/dev/null)" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "$msg_no_kernel_rpm_is_available ${klist_ar[@]}"
  echo "$msg_txt_check:"
  echo "$msg_internet_connection"
  [ "$installer" = "yum" ] && echo "$msg_use_baseurl_in_yum_centos"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

#
chosen_kernel_for_client=""
while [ -z "$chosen_kernel_for_client" ]; do
  # We use the release kernel as the default one, since FC3/4 the kernel changes too aggressive in updates.
  # Maybe there are more than 1 kernel image in the directory, like in MDK 10.1
  # 1: kernel-2.4.28.0.rc1.6mdk-1-1mdk (in updates repository)
  # 2: kernel-2.6.8.1.25mdk-1-1mdk (in updates repository)
  # 3: kernel-2.4.27.0.pre2.1mdk-1-1mdk (in release repository)
  # 4: kernel-2.6.8.1.12mdk-1-1mdk (in release repository)
  latest_krn_in_release="$(awk -F" " '/in release repository/ {print $2}' $LATEST_KTMP | pkg-ver-latest| head -n 1)"
  # Maybe we can not find the desired kernel in release. For example, in RH8.0/RH9/FC1, if we want to use i586 smp kernel, in FC1, there is no kernel-smp-2.4.22-1.2115.nptl.i586.rpm, only kernel-smp-2.4.22-1.2115.nptl.athlon.rpm, kernel-smp-2.4.22-1.2115.nptl.i686.rpm in http://opensource.nchc.org.tw/fedora/linux/core/1/i386/os/Fedora/RPMS/. However, i586 smp kernel exists in Rh8.0/RH9/FC1 updates dir. When this happens, we default to use the one in updates.
  if [ -n "$latest_krn_in_release" ]; then
    default_release="$(awk -F":" "/$latest_krn_in_release/ {print \$1}" $LATEST_KTMP)"
    default_release_ker_name="$(awk -F":" "/$latest_krn_in_release/ {print \$2}" $LATEST_KTMP)"
  else
    # default to use the one in updates.
    latest_krn_in_updates="$(awk -F" " '/in updates repository/ {print $2}' $LATEST_KTMP | pkg-ver-latest| head -n 1)"
    default_release="$(awk -F":" "/$latest_krn_in_updates/ {print \$1}" $LATEST_KTMP)"
    default_release_ker_name="$(awk -F":" "/$latest_krn_in_updates/ {print \$2}" $LATEST_KTMP)"
  fi
  # If only 1 kernel available, just use that.
  if [ "$(wc -l $LATEST_KTMP | awk -F" " '{print $1}')" -eq 1 ]; then
    echo "$msg_only_one_kernel_meets_requirement: $default_release_ker_name, use it."
    chosen_ker="$default_release"
  else
    echo "$msg_available_kernel:"
    cat $LATEST_KTMP
    echo "$msg_which_kernel_do_you_perfer"
    echo -n "[$default_release] "
    read chosen_ker
    [ -z "$chosen_ker" ] && chosen_ker="$default_release"
    echo "$msg_you_choose $chosen_ker."
  fi
  chosen_kernel_for_client="$(awk -F" " "/^$chosen_ker:/ {print \$2}" $LATEST_KTMP)"
  chosen_kernel_for_client_from_server="$(grep "^$chosen_ker:.*from.*server" $LATEST_KTMP)"
  if [ -z "$chosen_kernel_for_client" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "$msg_not_available_enter_again"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  fi
done
[ -f "$LATEST_KTMP" ] && rm -f $LATEST_KTMP
#
if [ -n "$chosen_kernel_for_client_from_server" ]; then
     client_kernel_install_mode="from_server"
else
     client_kernel_install_mode="from_downloaded_pkg"
fi

echo "${msg_chosen_kernel}: $chosen_kernel_for_client"

# Get kernel for client
case "$client_kernel_install_mode" in
  from_server)
    client_kernel_ver=`uname -r`
    ;;
  from_downloaded_pkg)
    # Use RPM to install kernel. 
    # Try to find that in RPMS.core or RPMS.updates...

    avail_krpm=""
    echo "Searching $KARCH kernel in repository... "
    for idir in $RPMS_os_update_dir; do
      avail_krpm="$(list_available_rpm $url_os_coreroot/${idir} | grep "^$chosen_kernel_for_client" 2>/dev/null)"
      rc=1
      if [ -n "$avail_krpm" ]; then
        kgot_url_rpm="$url_os_coreroot/${idir}/${avail_krpm}"
        client_kernel_tmp_dir="$(mktemp -d /tmp/client_kernel_PKG.XXXXXX)"
        echo -n "Downloading $KARCH $chosen_kernel_for_client from $kgot_url_rpm to $client_kernel_tmp_dir/... "
        # just download it
        get_inst_rpm -f $kgot_url_rpm -t $client_kernel_tmp_dir
	 rc=$?
         # FC 22 or later use kernel-core-`uname -r`*.rpm as real  package , but kernel-`uname-r`*.rpm only be meta package 
         if [ -z "$(LC_ALL=C rpm -qpl $client_kernel_tmp_dir/${avail_krpm} 2>/dev/null | grep vmlinuz )" ] ; then
            kcore_rpm="$(LC_ALL=C rpm -qpR $client_kernel_tmp_dir/${avail_krpm} | grep -E '^kernel[-PAE]*-core' | sed -e 's/uname-r\ *=\ *//g' -e 's/+PAE$//g').rpm"
            kmodules_rpm="$(LC_ALL=C rpm -qpR $client_kernel_tmp_dir/${avail_krpm} | grep -E '^kernel[-PAE]*-modules' | sed -e 's/uname-r\ *=\ *//g' -e 's/+PAE$//g').rpm"
            kcoregot_url_rpm="$url_os_coreroot/${idir}/${kcore_rpm}"
            kmodulesgot_url_rpm="$url_os_coreroot/${idir}/${kmodules_rpm}"
            get_inst_rpm -f $kcoregot_url_rpm -t $client_kernel_tmp_dir
            get_inst_rpm -f $kmodulesgot_url_rpm -t $client_kernel_tmp_dir
             if [ -n "$(LC_ALL=C rpm -qpl $client_kernel_tmp_dir/${kcore_rpm} 2>/dev/null | grep vmlinuz )" ] ; then 
                chosen_kernel_for_client=${kcore_rpm}
                drblsrv_offline_extra_opt="$drblsrv_offline_extra_opt -e $client_kernel_tmp_dir/$kmodules_rpm"
              else
                 rc=1
              fi
         fi
      fi
      # once it's done, skip the next search
      if [ "$rc" -eq 0 ]; then
        echo "done!"
        break
      else
        [ -n "$verbose" ] && echo "$chosen_kernel_for_client is not found in $url_os_coreroot/${idir}."
      fi
    done

    if [ "$rc" -ne 0 ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "$chosen_kernel_for_client is NOT found in the repository!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop"
      exit 1
    fi
esac
} # end of prepare_install_RH_MDK_client_kernel

#
ask_if_setup_proxy() {
echo "$msg_delimiter_star_line"
DOMAINNAME=`/bin/dnsdomainname`
if [ -n "$DOMAINNAME" ]; then
    DEFAULT_PROXY="proxy.$DOMAINNAME"
fi

if [ -z "$set_proxy_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_set_proxy"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read set_proxy_answer
fi

case "$set_proxy_answer" in
    [yY]|[yY])
      echo "$msg_http_proxy_server"
      echo -n "[$DEFAULT_PROXY] "
      read http_proxy_server_name
      echo "$msg_http_proxy_port"
      echo -n "[3128] "
      read http_proxy_server_port
      if [ -z  "$http_proxy_server_name" ]; then
          http_proxy_server_name="proxy.$DOMAINNAME"
      fi
      if [ -z  "$http_proxy_server_port" ]; then
          http_proxy_server_port="3128"
      fi

      # use the one use input for DEFAULT value. 
      echo "$msg_ftp_proxy_server"
      echo -n "[$http_proxy_server_name] "
      read ftp_proxy_server_name
      echo "$msg_ftp_proxy_port"
      echo -n "[$http_proxy_server_port] "
      read ftp_proxy_server_port
      if [ -z  "$ftp_proxy_server_name" ]; then
          ftp_proxy_server_name="$http_proxy_server_name"
      fi
      if [ -z  "$ftp_proxy_server_port" ]; then
          ftp_proxy_server_port="$http_proxy_server_port"
      fi

      echo "$msg_delimiter_star_line"
      echo "$msg_http_proxy_you_set: http://$http_proxy_server_name:$http_proxy_server_port"
      echo "$msg_ftp_proxy_you_set:  http://$ftp_proxy_server_name:$ftp_proxy_server_port"
      echo "$msg_delimiter_star_line"

      export http_proxy=http://$http_proxy_server_name:$http_proxy_server_port
      export ftp_proxy=ftp://$ftp_proxy_server_name:$ftp_proxy_server_port
      ;;
    *)
esac
} # end of ask_if_setup_proxy

#
ask_and_parse_rpm_ayo_repo_options() {
echo "$msg_delimiter_star_line"
if [ -z "$drbl_unstable_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_drbl_unstable_option"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read drbl_unstable_answer
fi
  
#
case "$drbl_unstable_answer" in
  y|Y|[yY][eE][sS]) true ;;
  *)
    if [ -z "$drbl_test_answer" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "$msg_drbl_test_option"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo -n "[y/N] "
      read drbl_test_answer
    fi
    ;;
esac

# [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
# echo "$msg_drbl_extra_option"
# [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
# echo -n "[y/N] "
# read drbl_extra_answer

# parse it
case "$drbl_test_answer" in
    [yY]|[yY][eE][Ss])
      DRBL_TEST="drbl-testing"
      ;;
    *)
      DRBL_TEST=""
esac
case "$drbl_unstable_answer" in
    [yY]|[yY][eE][Ss])
      DRBL_UNSTABLE="drbl-unstable"
      ;;
    *)
      DRBL_UNSTABLE=""
esac

if [ -z "$DRBL_TEST" -a -z "$DRBL_UNSTABLE" ]; then
  DRBL_STABLE="drbl-stable"
fi
# case "$drbl_extra_answer" in
#     [yY]|[yY][eE][Ss])
#       DRBL_EXTRA="drbl-extra"
#       ;;
#     *)
#       DRBL_EXTRA=""
# esac

} # end of ask_and_parse_rpm_ayo_repo_options

ask_and_parse_client_extra_setting() {
# netinstall options
if [ -z "$drbl_netinstall_answer" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_drbl_netinstall_option"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read drbl_netinstall_answer
fi

#
case "$OS_Version" in
  # Since 2006/09, most new distribution with kernel >=2.6.17 only supports single kernel package, which auto-detect CPU. Hence we won't ask this question in those newer distribution. Only old distribution:
  RH*|FC[1-5]|CO4*|MDK*|MDV2005|MDV2006.0|SUSE9.3|SUSE10.[0-1]|DBN3.[0-1])
   # SMP clients ?
   if [ -z "$drbl_SMP_answer" ]; then
     echo "$msg_delimiter_star_line"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_smp_clients_RH"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     echo -n "[y/N] "
     read drbl_SMP_answer
   fi
   ;;
  *)
   echo "$msg_delimiter_star_line"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "This GNU/Linux distribution uses one kernel to support SMP and non-SMP arch."
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   ;;
esac

# forget about this... we did NOT compile some module for a long time.
#[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
#echo "$msg_drbl_driver"
#[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
#echo -n "[Y/n] "
#read driver_answer

# console output
if [ -z "$console_output_answer" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_serial_console_option"
  echo "$msg_know_nothing_serial_console"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read console_output_answer
fi

case "$console_output_answer" in
    [yY]|[yY])
      echo "$msg_serial_console_only"
      echo -n "[y/N] "
      read serial_console_only
      echo "$msg_redirect_to_which_port"
      echo "$msg_text_Enter \"1\" for COM1, \"2\" for COM2, \"3\" for COM3, \"4\" for COM4,"
      echo -n "[1] "
      read console_port
      echo "$msg_serial_console_speed"
      echo "$msg_text_Enter \"1\" for 9600, \"2\" for 19200, \"3\" for 38400."
      echo -n "[3] "
      read console_port_speed
      ;;
    *)
esac

# parse it
case "$console_output_answer" in
    [yY]|[yY][eE][Ss])
      case "$console_port" in
          2)
            PXE_CONSOLE_PORT="1"
            CONSOLE_PORT="ttyS1"
            ;;
          3)
            PXE_CONSOLE_PORT="2"
            CONSOLE_PORT="ttyS2"
            ;;
          4)
            PXE_CONSOLE_PORT="3"
            CONSOLE_PORT="ttyS3"
            ;;
          *)
            PXE_CONSOLE_PORT="0"
            CONSOLE_PORT="ttyS0"
      esac
      case "$console_port_speed" in
          1)
            CONSOLE_PORT_SPEED="9600"
            ;;
          2)
            CONSOLE_PORT_SPEED="19200"
            ;;
          *)
            CONSOLE_PORT_SPEED="38400"
      esac
      case "$serial_console_only" in
      [yY]|[yY][eE][Ss])
        # note! The latter console will dominate the whole boot message
        CONSOLE_OUTPUT="console=tty0 console=$CONSOLE_PORT,"$CONSOLE_PORT_SPEED"n81"
	;;
      *)
        CONSOLE_OUTPUT="console=$CONSOLE_PORT,"$CONSOLE_PORT_SPEED"n81 console=tty0 "
      esac
      PXE_SERIAL_OUTPUT="serial $PXE_CONSOLE_PORT $CONSOLE_PORT_SPEED"
      echo "------------------------------------------------------"
      echo "$msg_serial_console_parameter: $CONSOLE_OUTPUT"
      echo "------------------------------------------------------"
      ;;
    *)
      CONSOLE_OUTPUT=""
      PXE_SERIAL_OUTPUT=""
esac
case "$drbl_SMP_answer" in
    [yY]|[yY][eE][Ss])
      SMP_OPTION="-smp"
      ;;
    *)
      SMP_OPTION=""
esac

# netinstall
case "$drbl_netinstall_answer" in
    [yY]|[yY][eE][Ss])
      DRBL_NETINSTALL="yes"
      ;;
    *)
      DRBL_NETINSTALL="no"
esac

} # end of ask_and_parse_client_extra_setting

#
backup_apt_sources_list() {
  if [ -f /etc/apt/sources.list ]; then
    echo "$msg_backup_apt_sources"
    mv -v /etc/apt/sources.list /etc/apt/sources.list.orig
  fi
}

#
clean_tftpboot_and_misc() {
  local bind_dirs
  # Unmount those "--bind" dir under /tftpboot/
  # The results of "mount" command is like:
  # /usr on /tftpboot/node_root/usr type none (rw,bind)
  bind_dirs="$(LC_ALL=C mount | awk -F" " '{print $3}' | grep -Ew "/tftpboot")"
  bind_dirs="$(echo $bind_dirs)"  # make it in a line
  for i in $bind_dirs; do
    echo "Unmounting binding dir $i..."
    umount $i
  done
  echo "$msg_delimiter_star_line"
  echo "$msg_cleaning_tftpboot_and_misc"
  echo "$msg_delimiter_star_line"
  # clean the nodes link, like 192.168.1.1...
  find /tftpboot/ -maxdepth 1 -type l -exec rm -f {} \;
  # we won't to affect those netinstall, maybe they are not removed previuosly. .
  find /tftpboot/nbi_img/ ! -name "*netinstall*" ! -type d -exec rm -f {} \;
  [ -d "/tftpboot/nodes/" ] && rm -rf /tftpboot/nodes
  [ -d "/tftpboot/node_root/" ] && rm -rf /tftpboot/node_root
  [ -d "$drbl_syscfg" -a "$drbl_remove_answer" = "y" ] && rm -rf $drbl_syscfg 
  [ -f "$ocs_lock_dir/clonezilla.lock" ] && rm -f $ocs_lock_dir/clonezilla.lock
  # For old version, we have to remove the old tag, now we use $ocs_lock_dir/clonezilla.lock instead of $ocsroot/clonezilla.lock
  [ -f "$ocsroot/clonezilla.lock" ] && rm -f $ocsroot/clonezilla.lock
  [ -f /etc/yp.conf ] && mv -v /etc/yp.conf /etc/yp.conf.drblsave
  [ -f /etc/ypserv.conf ] && mv -v /etc/ypserv.conf /etc/ypserv.conf.drblsave
  [ -f /var/yp/securenets ] && mv -v /var/yp/securenets /var/yp/securenets.drblsave
  [ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
} # end of clean_tftpboot_and_misc()

append_RH_apt_sources_list_example() {
cat <<-APT_END >> /etc/apt/sources.list
# Examples:
# DRBL for RH9 in drbl.sourceforge.net, only DRBL packages, no RH9 packages
#rpm http://drbl.sourceforge.net/redhat/apt redhat/9/i386 drbl drbl-testing
#rpm-src http://drbl.sourceforge.net/redhat/apt redhat/9/i386 drbl drbl-testing

# DRBL for FC1 in drbl.sourceforge.net, only DRBL packages, no FC1 packages
#rpm http://drbl.sourceforge.net/fedora/apt/ fedora/linux/1/i386 drbl drbl-testing
#rpm-src http://drbl.sourceforge.net//fedora/apt/ fedora/linux/1/i386 drbl drbl-testing

# DRBL for RH9 in free.nchc.org.tw, including DRBL packages and RH9 pacakges
#rpm http://free.nchc.org.tw//redhat/apt/ redhat/9/i386 os updates drbl
#rpm-src http://free.nchc.org.tw//redhat/apt/ redhat/9/i386 os updates drbl

# DRBL for FC1 in linux.nchc.org.tw, including DRBL packages and FC1 pacakges
#rpm http://free.nchc.org.tw//fedora/apt/ fedora/linux/1/i386 core updates drbl  
#rpm-src http://free.nchc.org.tw//fedora/apt/ fedora/linux/1/i386 core updates drbl  

### Dag Apt Repository for Red Hat Fedora Core 
#rpm http://apt.sw.be fedora/3/en/i386 dag
#rpm http://apt.sw.be fedora/2/en/i386 dag
#rpm http://apt.sw.be fedora/1/en/i386 dag

### Dag Apt Repository for Red Hat 9 (rh90)
#rpm http://apt.sw.be redhat/9/en/i386 dag

### Dag Apt Repository for Red Hat 8.0 (rh80)
#rpm http://apt.sw.be redhat/8.0/en/i386 dag

### Fedora Legacy Respository
# Red Hat Linux 8.0:
# rpm http://download.fedoralegacy.org/apt redhat/8.0/i386 os updates legacy-utils
# Red Hat Linux 9:
# rpm http://download.fedoralegacy.org/apt redhat/9/i386 os updates legacy-utils
# Fedora Core 1:
# rpm http://download.fedoralegacy.org/apt fedora/1/i386 os updates legacy-utils
# Fedora Core 2:
# rpm http://download.fedoralegacy.org/apt fedora/2/i386 os updates legacy-utils
APT_END

} # end of append_RH_apt_sources_list_example

parse_os_to_set_repository() {
# If the OS_Version is not supported, exit
if [ -z "$OS_Version" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "$msg_not_determine_OS"
  echo "$msg_is_not_supported"
  echo "$msg_try_drblsrv_offline"
  echo -n "$msg_press_ctrl_c_stop! "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  read
  exit 1 
fi

case "$OS_Version" in
   "RH8.0"|"RH9")
     # set the official release number... 8.0, 9...
     RH_VER=${OS_Version:2}
     #DIST="redhat"
     echo "$msg_OS_version: $OS_Version"
     # core_dir is for the core program, for RH8 and 9, RPMS.os, set it as "os"
     core_dir="os"
     # RH_RPMS_os_update_dir indicates in apt repository, 
     # where OS and updates pakcage (rpms) exist,
     # We will use it to search glibc & openssl only, not
     # for apt repository (not for /etc/apt/sources.list)
     # freshrpms:
     #http://ayo.freshrpms.net/redhat/9/i386/updates/RPMS/
     #http://ayo.freshrpms.net/redhat/9/i386/os/RPMS
     # opensource (normal)
     #http://free.nchc.org.tw/redhat/linux/9/en/os/i386/RedHat/RPMS/
     #http://free.nchc.org.tw/redhat/linux/updates/9/en/os/i386
     #http://free.nchc.org.tw/redhat/linux/updates/9/en/os/i586
     #http://free.nchc.org.tw/redhat/linux/updates/9/en/os/i686
     RH_RPMS_os_update_dir="updates/$RH_VER/en/os/i386 updates/$RH_VER/en/os/i586 updates/$RH_VER/en/os/i686 updates/$RH_VER/en/os/noarch $RH_VER/en/os/i386/RedHat/RPMS $RH_VER/i386/updates/RPMS $RH_VER/i386/os/RPMS"
     ;;
   FC*)
     FC_VER=${OS_Version:2}
     #DIST="fedora"
     echo "$msg_OS_version: $OS_Version"
     # core_dir is for the core program, for FC1, RPMS.core, set it as "core". Note! variable core_dir is only used for apt repository
     core_dir="core"
     # RH_RPMS_os_update_dir indicates in apt repository, 
     # where OS and updates pakcage (rpms) exist,
     # We will use it to search glibc & openssl only, not
     # for apt repository (not for /etc/apt/sources.list)
     # freshrpms:
     # http://ayo.freshrpms.net/fedora/linux/3/i386/updates/RPMS/
     # http://ayo.freshrpms.net/fedora/linux/3/i386/core/RPMS/
     #
     # http://opensource.nchc.org.tw/fedora/linux/core/6/i386/
     # http://opensource.nchc.org.tw/fedora/linux/core/updates/6/i386/
     # Ex: for FC6, url_os_coreroot=http://opensource.nchc.org.tw/fedora/linux/core
     # From Fedora 7, the path is changed.
     # http://opensource.nchc.org.tw/fedora/linux/releases/7/Fedora/i386/os/
     # http://opensource.nchc.org.tw/fedora/linux/updates/7/i386
     # Ex: for FC7, url_os_coreroot=http://opensource.nchc.org.tw/fedora/linux/releases/
     # in fedora.repo, it's used as:
     # baseurl=$url_os_coreroot/\$releasever/\$basearch/os

     # From Fedora 8, the path is changed. (The last dir name 'Fedora' -> 'Packages')
     # http://opensource.nchc.org.tw/fedora/linux/releases/8/Fedora/i386/os/Packages/
     # http://opensource.nchc.org.tw/fedora/linux/releases/8/Everything/i386/os/Packages/
     # http://opensource.nchc.org.tw/fedora/linux/updates/8/i386/
     RH_RPMS_os_update_dir="../updates/$FC_VER/$ARCH/k/ ../updates/$FC_VER/$ARCH/ $FC_VER/Fedora/$ARCH/os/Packages/k/ $FC_VER/Fedora/$ARCH/os/Packages/ $FC_VER/Fedora/$ARCH/os/Fedora/ $FC_VER/Everything/$ARCH/os/Packages/ $FC_VER/Everything/$ARCH/os/Fedora/ updates/$FC_VER/$ARCH/ $FC_VER/$ARCH/os/Fedora/RPMS/ $FC_VER/$ARCH/updates/RPMS/ $FC_VER/$ARCH/os/RPMS/"
     ;;
   CO*)
     CO_VER=${OS_Version:2}
     echo "$msg_OS_version: $OS_Version"
     # RH_RPMS_os_update_dir indicates in apt repository, 
     # where OS and updates pakcage (rpms) exist,
     # We will use it to search kernel, glibc & openssl only, not
     # for apt repository (not for /etc/apt/sources.list)
     # 2007/05/06. For CentOS 5, it's centos/5/os/i386/CentOS/, no RPMS in the end.
     # 2011/08/22, For CentOS 6, it's centos/6/os/i386/Packages/
     # 2012/01/30, For CentOS 6, the updates is in centos/6/updates/i386/Packages
     RH_RPMS_os_update_dir="$CO_VER/updates/$ARCH/Packages/ $CO_VER/os/$ARCH/Packages/ $CO_VER/updates/$ARCH/RPMS/ $CO_VER/os/$ARCH/CentOS/ $CO_VER/os/$ARCH/CentOS/RPMS/"
     ;;
   MDK9.2|MDK10.0)
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_OS_version: $OS_Version"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     #http://free.nchc.org.tw/mandrake/apt/ mandrake/linux/9.2/i586 core updates drbl drbl-testing drbl-unstable

     # sources.list
     # rpm $url_os/official/$MDK_VER_PATH/i586/Mandrake/ base/hdlist RPMS
     # Mandrake updates
     # rpm $url_os/official/updates/$MDK_VER_PATH/ base/hdlist RPMS
     MDK_VER_PATH=${OS_Version:3}
     # The media_info directory in the same directory level with RPMS
     update_rpmdir_info="RPMS"
     # SHIT!!! why Mandrake use differnet name for 2005 (LE2005) in updates ?
     # To make it more clear, we add an empty variable update_extra_code_name
     update_extra_code_name=""
     # MDK_RPMS_os_update_dir indicates in rpm repository, 
     # where OS and update rpms exists
     # We will use MDK_RPMS_os_update_dir for search glibc & openssl only, not
     # for apt repository (not for /etc/apt/sources.list)
     MDK_RPMS_os_update_dir="updates/$MDK_VER_PATH/RPMS $MDK_VER_PATH/i586/Mandrake/RPMS"
     ;;
   MDK10.[1-2]|MDV2005|MDV2006.0)
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_OS_version: $OS_Version"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     # main
     # http://free.nchc.org.tw//mandrake/official/10.1/i586/media media_info/hdlist main
     # updates
     # rpm http://free.nchc.org.tw//mandrake/official/updates/10.1/ main_updates/media_info/hdlist main_updates
     # "MDV2006.0" -> "2006.0"
     MDK_VER_PATH=${OS_Version:3}
     # This update_rpmdir_info is for urpmi.addmedia: urpmi.addmedia MDK${MDK_VER_PATH}-updates $url_os/$os_ayo_path/updates/${update_extra_code_name}$MDK_VER_PATH/$update_rpmdir_info
     update_rpmdir_info="main_updates"

     # SHIT!!! why Mandrake use differnet name for 2005 (LE2005) in updates ?
     if [ "$OS_Version" = "MDV2005" ];then
	update_extra_code_name="LE"
     else
	update_extra_code_name=""
     fi
     # MDK_RPMS_os_update_dir indicates in rpm repository, 
     # where OS and update rpms exists
     # We will use MDK_RPMS_os_update_dir to search kernel, not for urpmi
     MDK_RPMS_os_update_dir="updates/$MDK_VER_PATH/main_updates $MDK_VER_PATH/$ARCH/media/main"
     ;;
   MDV*)
     # From MDV2007.0
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_OS_version: $OS_Version"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     # ftp://mdk.linux.org.tw//pub/mandrake-linux/official/2007.0/i586/media/main
     # updates
     # ftp://mdk.linux.org.tw//pub/mandrake-linux/official/updates/2007.0/i586/media
     # "MDV2007.0" -> "2007.0"
     MDK_VER_PATH=${OS_Version:3}
     # This update_rpmdir_info is for urpmi.addmedia: urpmi.addmedia MDK${MDK_VER_PATH}-updates $url_os/$os_ayo_path/updates/${update_extra_code_name}$MDK_VER_PATH/$update_rpmdir_info
     # For 2007.0, ftp://mdk.linux.org.tw//pub/mandrake-linux/official/updates/2007.0/i586/media/main/updates/media_info
     update_rpmdir_info="/$ARCH/media/main/updates"

     # MDK_RPMS_os_update_dir indicates in rpm repository, 
     # where OS and update rpms exists
     # We will use MDK_RPMS_os_update_dir to search kernel, not for urpmi
     # Note by Steven Shiau on 2006/09/26
     # For MDV2007.0, in 2007.0/i586/media/main, there are still 3 dirs:
     # release, testing, updates
     # Hence we have to include them.
     MDK_RPMS_os_update_dir="updates/$MDK_VER_PATH/$ARCH/media/main/updates $MDK_VER_PATH/$ARCH/media/main/release $MDK_VER_PATH/$ARCH/media/main/updates"
     ;;
   *)
     [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
     echo "$msg_is_not_supported: $OS_Version"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_try_drblsrv_offline"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     echo -n "$msg_press_ctrl_c_stop! "
     read
     exit 1 
esac

#
ask_if_setup_proxy

} # parse_os_to_set_repository
#
preset_client_archi_ans() {
  # The function to preset client_archi_ans for some specific arch or distribution release.
  # The return value is client_archi_ans or client_archi_ans_def
  # Apply the client_archi_ans for x86_64 and SMP
  # x86_64 added, we will use best_optimization for x86_64.
  if [ "$ARCH" = "x86_64" -o "$ARCH" = "amd64" ]; then
    client_archi_ans=2
    return 0
  fi
  
  # if SMP for client -> best_optimization.
  # Not SMP -> ask user.
  case "$drbl_SMP_answer" in
   [yY]|[yY][eE][Ss])
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_delimiter_star_line"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     echo "$msg_smp_optimization_kernel"
     client_archi_ans="2"
     echo -n "$msg_press_enter_to_continue"
     read 
     return 0
     ;;
  esac
  
  # Ugly! For CentOS 5.0, when it's initial release, only i686 kernel supported! For more info, check this: http://wiki.centos.org/QaWiki/CentOS5PentiumSupport
  # This will be removed when CentOS 5 officially supports i586 CPU.
  case "$OS_Version" in
    CO5*|CO6*)
      echo "$msg_delimiter_star_line"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "$msg_only_i686_kernel_available_in_centos_5"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      client_archi_ans="2"
      echo -n "$msg_press_enter_to_continue"
      read 
      return 0
      ;;
    FC*)
      client_archi_ans="2"
      return 0
      ;;
  esac

  [ -e /etc/lsb-release ] && . /etc/lsb-release
  if [ "$DISTRIB_ID" = "Ubuntu" ]; then
      echo "$msg_delimiter_star_line"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "This server is running Ubuntu Linux, so generic kernel is available."
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    case "$DISTRIB_RELEASE" in
      # Ubuntu 5.04 to 10.04
      [5-9].[01][460]|10.04) client_archi_ans_def="2" ;;
      *)
        echo "$msg_delimiter_star_line"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
        echo "$msg_only_kernel_same_with_srv_available"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        client_archi_ans="2"
        echo -n "$msg_press_enter_to_continue"
        read 
	;;
    esac
    return 0
  fi
} # end of preset_client_archi_ans

#
parse_client_archi_setting() {
  # 
  preset_client_archi_ans
  
  # if $client_archi_ans is not set (i.e. NOT x86_64, NOT SMP), ask user.
  [ -z "$client_archi_ans_def" ] && client_archi_ans_def="2"
  if [ -z "$client_archi_ans" ]; then
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_delimiter_star_line"
     echo "$msg_apt_optimization_question"
     echo "$msg_optimization_level_0"
     echo "$msg_optimization_level_1"
     echo "$msg_optimization_level_2"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
     echo "$msg_N_note"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_note!!! $msg_different_level_machine_prompt"
     echo "$msg_diff_arch_explain"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     echo "$msg_not_sure_better_1"
     echo -n "[$client_archi_ans_def] "
     read client_archi_ans
     echo "$msg_delimiter_star_line"
  fi
  [ -z "$client_archi_ans" ] && client_archi_ans="$client_archi_ans_def"
  case "$client_archi_ans" in
     [0-9])
         echo "The CPU arch option for your clients: $client_archi_ans"
         ;;
     *)
         client_archi_ans=1
         ;;
  esac
  
  # check if the answer fits to the distribution
  # This is based on the kernel rpm: kernel-.*.i[356]86.rpm
  # client_archi_ans: (1) i386 (2) i586 (3) optimization
  # for RH8/9, FC1 -> i386, i586, i686 are available
  # for FC2/3 -> i586, i686 are available, i386 is not available
  # for CO4/4.1/4.2 -> i586, i686 are available, i386 is not available
  # for MDK -> i586 and i686 (a bug) are available, i386 is not available.
  # for SUSE -> i586, i686 are available, i386 is not available
  # for Debian woody/sarge/breezy -> i386, i686 are available, i586 is not available
  # for Debian etch/dapper -> i486, i686 are available, i386 is not available
  if [ "$client_archi_ans" = "0" ]; then
    case "$OS_Version" in
    FC[2-9]|FC1[01]|MD[KV]*|SUSE*|CO*)
          [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
          echo "$msg_change_opt_from_i386_to_i586"
          [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
          client_archi_ans="1"
          ;;
    esac
  fi
  
  # for Debian woody/sarge/breezy-> i386 and i686 are available, i586 is not available.
  if [ "$client_archi_ans" = "1" ]; then
    case "$OS_Version" in
    DBN*)
          [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
          echo "$msg_change_opt_from_i586_to_i386"
          [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
          client_archi_ans="0"
          ;;
    esac
  fi
  # 
  case "$client_archi_ans" in
   2)
     # use default, let apt-rpm decide, so we do not have to change anything in
     # /etc/apt/apt.conf
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_same_optimization"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     client_archi_set="best_optimization"
     ;;
   0)
     # for i386... this is not available for FC2...
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_no_optimization"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     [ -f "/etc/apt/apt.conf" ] && perl -p -i -e "s/^.*Install-Options.*\"\".*;/    Install-Options \"\"; Architecture \"i386\";/" /etc/apt/apt.conf
     client_archi_set="i386"
     ;;
   *)
     # for apt to use upto i586, the change will be in /etc/apt/apt.conf
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_i586_optimization"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     [ -f "/etc/apt/apt.conf" ] && perl -p -i -e "s/^.*Install-Options.*\"\".*;/    Install-Options \"\"; Architecture \"i586\";/" /etc/apt/apt.conf
     client_archi_set="i586"
     ;;
  esac
} # end of parse_client_archi_setting

#
install_client_archi_glibc_openssl_RH() {
  local pkg_ARCH installed_pkg Is_i386_pkg_available ipkg pkg_ARCH_now lib_bk idir
  echo "$msg_delimiter_star_line"
  # x86_64 will be in best_optimization
  if [ "$client_archi_set" = "best_optimization" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "$msg_optimization_is_on"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    return 0
  fi
  # for i586 and i386, we force to use i386 version: glibc, openssl and opensssl-perl (for CentOS 4.3) and more...
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_optimization_is_off"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

  for ipkg in $i686_pkg_check_list_RH_like; do
    pkg_ARCH=""
    installed_pkg=""
    # ///NOTE/// Ex: In CentOS 5 x86_64, the output results:
    # >rpm -q --qf '%{arch}\n' glibc
    # x86_64
    # i686
    # i.e. we might have 2 archs
    rpm -q --qf '%{ARCH}' $ipkg > /dev/null && pkg_ARCH="$(rpm -q --qf '%{ARCH}\n' $ipkg)"
    # If the package is not installed of it's already i386, try next
    [ -z "$pkg_ARCH" ] && continue
    if [ -n "$(echo $pkg_ARCH | grep -Ew "i386")" ]; then
      echo "$ipkg is already for i386 arch."
      continue
    fi
    # From Fedora 9, the query output for rpm is different:
    # Ex: rpm -q glibc:
    # Before Fedora 9 the output is: glibc-2.7-2
    # After Fedora 9 the output is: glibc-2.8-3.i686
    # i.e. an extra .i686 will be append.
    # ///NOTE/// Ex: In CentOS 5 x86_64, the output results:
    # >rpm -q --qf '%{arch}\n' glibc
    # x86_64
    # i686
    # i.e. we might have 2 archs
    installed_pkg="$(rpm -q "${ipkg}" | sort | uniq | sed -e "s|\.i[3-6]86||g")"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "$msg_install_i386_of $installed_pkg..."
    echo "$msg_searching_pkg_in_ayo..."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    for irpm in $RH_RPMS_os_update_dir; do
      # Before installing, check if it's available. This is due to a bug in
      # FC4. rpm in FC4 will not able to return to command line if package
      # is not available. So we have to check it first.
      Is_i386_pkg_available="$(list_available_rpm $url_os_coreroot/$irpm/ | grep "^${installed_pkg}.i386.rpm" 2>/dev/null)"
      if [ -n "$Is_i386_pkg_available" ]; then
        echo "Installing i386 $ipkg from $url_os_coreroot/$irpm/${installed_pkg}.i386.rpm..." 
        # since rpm does not support file://..., we use get_inst_rpm
        #rpm -Uvh --force --quiet $url_os_coreroot/$irpm/${installed_pkg}.i386.rpm 2>/dev/null
        get_inst_rpm -f $url_os_coreroot/$irpm/${installed_pkg}.i386.rpm -r "rpm -Uvh --force --quiet"
      fi
      # it seems that even if rpm successfully install glibc with 
      # -Uvh --force, the return code is still 1... 
      # So we have to use rpm -q --qf to check
      #RETVAL=$?
      #[ $RETVAL -eq 0 ] && break
      rpm -q --qf '%{ARCH}' $ipkg > /dev/null && pkg_ARCH_now=`rpm -q --qf '%{ARCH}' $ipkg`
      [ "$pkg_ARCH_now" = "i386" ] && break
    done
    # add warning if i386 rpm is not installed
    if [ "$pkg_ARCH_now" != "i386" ]; then
       [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
       echo "Warning! Unable to find the i386 ${installed_glibc} in the repository!!! The DRBL client will fail to boot if different CPU arch from that of DRBL server!"
       [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    fi
    # If it's glibc, we have to take care of the bug in RH9, i.e. stale files still exist, we have remove them.
    if [ "$ipkg" = "glibc" ]; then
      # sleep to make rpm db ok.
      sleep 5
      # to mv the i686 lib which should not exists in i386 glibc packags
      # if they are not cleaned.
      lib_bk="/lib/i686 /lib/tls /usr/lib/i686"
      for idir in $lib_bk; do
        if [ -d "$idir" ]; then
          [ -d "$idir.bak" ] && rm -rf $idir.bak
          mv -f $idir $idir.bak
        fi
      done
    fi
  done
} # end of install_client_archi_glibc_openssl_RH

#
install_client_archi_glibc_etc_SUSE() {
#
local url_tmp="" pkg_tmp=""
echo "$msg_delimiter_star_line"
case "$client_archi_set" in
    "best_optimization")
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "$msg_optimization_is_on"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      ;;
    *)
    # for i586 and i386, we force to use i586 version: glibc, db...
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "$msg_optimization_is_off"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      for ipkg in $i686_pkg_check_list_SUSE; do
        # i686_pkg_check_list_SUSE is: glibc glibc-devel db db-devel...
        # since "-" can not ba a variable name, replace it as "_"
        ipkg_arch_var="$(echo $ipkg | sed -e "s/-/_/g")"
        eval ipkg_ARCH=\$${ipkg_arch_var}_ARCH
        [ -z "$ipkg_ARCH" ] && continue
	echo "$ipkg arch: $ipkg_ARCH."
        if [ "$ipkg_ARCH" != "i586" ]; then 
         echo "Finding the i586 $ipkg in apt repository..."
         case "$installer" in
          apt)
           available_ipkg_rpm="$(parse_apt_url_get_rpm.sh -a RPMS.updates -a RPMS.base -r i586 --no-specific-ver ${ipkg} 2>/dev/null)"
	   # The obtained $available_ipkg_rpm is like: http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/inst-source/suse/i586/glibc-2.4-25.i586.rpm
           if [ -n "$available_ipkg_rpm" ]; then
             echo "Installing i586 ${ipkg}: $available_ipkg_rpm..."
	     # since rpm does not support file://..., we use get_inst_rpm
             #rpm -Uvh --force --quiet $available_ipkg_rpm 2>/dev/null
	     get_inst_rpm -f $available_ipkg_rpm -r "rpm -Uvh --force --quiet"
           fi
	   ;;
          yum|zypper)
	   available_ipkg_rpm="$(list_available_rpm $url_os_coreroot/$OS_PATH_IN_YUM_REPO/$REPO_PATH/suse/i586/ | grep -E "^$ipkg-[0-9]+.*.i586.rpm" 2>/dev/null)"
           if [ -n "$available_ipkg_rpm" ]; then
             echo "Installing i586 ${ipkg}: $available_ipkg_rpm..."
	     # since rpm does not support file://..., we use get_inst_rpm
             # rpm -Uvh --force --quiet $url_os_coreroot/$OS_PATH_IN_YUM_REPO/inst-source/suse/i586/$available_ipkg_rpm 2>/dev/null
	     get_inst_rpm -f $url_os_coreroot/$OS_PATH_IN_YUM_REPO/$REPO_PATH/suse/i586/$available_ipkg_rpm -r "rpm -Uvh --force --quiet"
           fi
	   ;;
         esac
        
         # it seems that even if rpm successfully install glibc with 
         # -Uvh --force, the return code is still 1... 
         # So we have to use rpm -q --qf to check, not by return code.
         rpm -q --qf '%{ARCH}' ${ipkg} > /dev/null && ipkg_ARCH_now=`rpm -q --qf '%{ARCH}' ${ipkg}`
         [ "$ipkg_ARCH_now" = "i586" ] && continue
         # add warning if i586 rpm is not installed
         if [ "$ipkg_ARCH_now" != "i586" ]; then
            [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
            echo "Warning! Unable to find the i586 ${ipkg} in the repository!!! The DRBL client will fail to boot if different CPU arch from that of DRBL server!"
            [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
         fi
         if [ "$ipkg" = "glibc" ]; then
          # sleep to make rpm db ok.
          sleep 5
          # to mv the i686 lib which should not exists in i386 glibc packags
          # if they are not cleaned.
          lib_bk="/lib/i686 /lib/tls /usr/lib/i686"
          for idir in $lib_bk; do
            if [ -d "$idir" ]; then
              [ -d "$idir.bak" ] && rm -rf $idir.bak
              mv -f $idir $idir.bak
            fi
          done
         fi
        fi
      done
esac
} # end of install_client_archi_glibc_etc_SUSE

#
parse_RH_os_yum_repo_answer() {
local os_name="$1"
local answer="$2"
case "$answer" in
    1)
      # download.fedora.redhat.com
      url_os="http://download.fedora.redhat.com"
      os_ayo_path="/pub/fedora/linux/core"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    2)
      # mirrors.kernel.org
      url_os="http://mirrors.kernel.org"
      os_ayo_path="/fedora/core"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    4)
      # skip the yum config, but we will use the following url_os... for glibc/kernel inst
      use_existing_yum_config="yes"

      url_os="http://free.nchc.org.tw"
      os_ayo_path="/fedora/linux/core"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    5)
      #enter by user
      url_os_default="http://mirrors.kernel.org"
      os_ayo_path_default="/fedora/core/"
      echo "$msg_enter_FQDN_IP_yum_repository"
      echo -n "[$url_os_default] "
      read url_os
      [ -z "$url_os" ] && url_os="$url_os_default"

      echo "$msg_enter_the_path_for_dir_ver_updates: \"$FC_VER\" $msg_and \"updates\"..."
      echo -n "[$os_ayo_path_default] "
      read os_ayo_path
      [ -z "$os_ayo_path" ] && os_ayo_path="$os_ayo_path_default"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_os="http://free.nchc.org.tw"
      case "$os_name" in
	FC*)
          os_ayo_path="/fedora/linux/core"
	  ;;
	CO*)
          os_ayo_path="/centos"
	  ;;
      esac
      url_os_coreroot="$url_os/$os_ayo_path"
esac
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_os, $os_ayo_path"
echo "------------------------------------------------------"

} # end of parse_RH_os_yum_repo_answer

#
parse_RH_drbl_yum_repo_answer() {
local answer="$1"
# TODO x86_64: change i386 -> x86_64 ? or it works ?
case "$answer" in
    1)
      # ftp.twaren.net
      url_drbl="http://ftp.twaren.net"
      drbl_ayo_path="local-distfiles/drbl-core/i386"
      ;;
    2)
      # drbl.sf.net
      url_drbl="http://drbl.sourceforge.net"
      drbl_ayo_path="drbl-core/i386"
      ;;
    5)
      #enter by user
      url_drbl_default="http://drbl.sourceforge.net"
      drbl_ayo_path_default="/drbl-core/i386/"
      echo "Please enter the hostname or IP address of yum reposity with http or ftp prefix where DRBL packages exists..."
      echo -n "[$url_drbl_default] "
      read url_drbl
      [ -z "$url_drbl" ] && url_drbl="$url_drbl_default"

      echo "Enter the path where directory \"RPMS.drbl\" exists..." 
      echo -n "[$drbl_ayo_path_default] "
      read drbl_ayo_path
      [ -z "$drbl_ayo_path" ] && drbl_ayo_path="$drbl_ayo_path_default"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_drbl="http://free.nchc.org.tw"
      drbl_ayo_path="drbl-core/i386"
esac
url_drbl_rpmroot="$url_drbl/$drbl_ayo_path/"
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_drbl, $drbl_ayo_path"
echo "------------------------------------------------------"

} # parse_RH_drbl_yum_repo_answer

#
parse_RH_os_apt_repo_answer() {
local os_ver="$1"
local answer="$2"
local ver_no="${os_ver:2}"
case "$answer" in
    1)
      # freshrpms
      url_os="http://ayo.freshrpms.net"
      case "$os_ver" in
        RH*)
          url_os_coreroot="$url_os/redhat"
          os_ayo_path="redhat/$RH_VER/i386"
          ;;
        FC*)
          url_os_coreroot="$url_os/fedora/linux"
          os_ayo_path="fedora/linux/$FC_VER/i386"
  	  ;;
      esac
      FRESHRPMS="freshrpms"
      ;;
    3) 
      # local repository
      url_os="/"
      url_os_coreroot_default="/opt/mirror/fedora/linux/core"
      os_ayo_path_default="/opt/mirror/fedora/apt/fedora/linux/$ver_no/i386"
      echo "Enter the path where directory \"$ver_no\" and \"updates\" exist."
      echo -n "[$url_os_coreroot_default] "
      read url_os_coreroot
      [ -z "$url_os_coreroot" ] && url_os_coreroot="$url_os_coreroot_default"
      while [ ! -e "$url_os_coreroot/$ver_no" ]; do
	echo "No such directory \"$url_os_coreroot/$ver_no\" or \"$url_os_coreroot/updates/$ver_no\"!" 
	echo "Please enter it again!"
        read url_os_coreroot
      done

      echo "Enter the path where directory RPMS.$core_dir, RPMS.updates... exist."
      echo -n "[$os_ayo_path_default] "
      read os_ayo_path
      [ -z "$os_ayo_path" ] && os_ayo_path="$os_ayo_path_default"
      while [ ! -e "$os_ayo_path/RPMS.$core_dir" ] || [ ! -e "$os_ayo_path/RPMS.drbl" ]; do
	echo "No such directory $os_ayo_path/RPMS.$core_dir or $os_ayo_path/RPMS.drbl!" 
	echo "Please enter it again!"
        read os_ayo_path
      done
      ;;
    5) 
      # enter by user
      url_os_default="http://ayo.freshrpms.net"
      os_coreroot_default="/fedora/linux/"
      os_ayo_path_default="/fedora/linux/$ver_no/i386"
      echo "Enter the hostname or IP address of $os_ver packages reposity with http or ftp prefix."
      echo -n "[$url_os_default] "
      read url_os
      [ -z "$url_os" ] && url_os="$url_os_default"

      echo "Enter the path where directory \"$ver_no\" and \"updates\" exist."
      echo -n "[$os_coreroot_default] "
      read os_coreroot
      [ -z "$os_coreroot" ] && os_coreroot="$os_coreroot_default"
      url_os_coreroot="$url_os/$os_coreroot"

      echo "Enter the path where directory RPMS.$core_dir, RPMS.updates... exist."
      echo -n "[$os_ayo_path_default] "
      read os_ayo_path
      [ -z "$os_ayo_path" ] && os_ayo_path="$os_ayo_path_default"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_os="http://free.nchc.org.tw"
      case "$os_ver" in
        RH*)
          url_os_coreroot="$url_os/redhat/linux"
          os_ayo_path="redhat/apt/redhat/linux/$RH_VER/i386"
          ;;
        FC*)
          url_os_coreroot="$url_os/fedora/linux/core"
          os_ayo_path="fedora/apt/fedora/linux/$FC_VER/i386"
  	  ;;
      esac
esac
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_os, $url_os_coreroot, $os_ayo_path"
echo "------------------------------------------------------"

} # parse_RH_os_apt_repo_answer

parse_RH_drbl_apt_repo_answer() {
# url_drbl_rpmroot is where the RPMS.drbl, RPMS.drbl-testing, RPMS.drbl-unstable... exist
# url_os_rpmroot is where the RPMS.os, RPMS.core, RPMS.update... exist
# try to set url_drbl_rpmroot and url_os, we need 
# url_drbl_rpmroot to get wget and apt
# url_os_rpmroot to get glibc/openssl/kernel
# os_ayo_path is under $url_os where RPMS.os/RPMS.core exists
# drbl_ayo_path is under $url_drbl where RPMS.drbl exists
local answer="$1"
case "$answer" in
    1)
      # ftp.twaren.net
      url_drbl="http://ftp.twaren.net"
      drbl_ayo_path="local-distfiles/drbl-core/i386"
      ;;
    2)
      # drbl.sf.net
      url_drbl="http://drbl.sourceforge.net"
      drbl_ayo_path="drbl-core/i386"
      ;;
    3) 
      # local repository
      url_drbl_default="/"
      drbl_ayo_path_default="/drbl-core/i386"

      echo "Enter the path where directory RPMS.$core_dir, RPMS.updates... exist."
      echo -n "[$drbl_ayo_path_default] "
      read drbl_ayo_path
      [ -z "$drbl_ayo_path" ] && os_ayo_path="$drbl_ayo_path_default"
      while [ ! -e "$drbl_ayo_path/RPMS.$core_dir" ] || [ ! -e "$drbl_ayo_path/RPMS.drbl" ]; do
	echo "No such directory $drbl_ayo_path/RPMS.$core_dir or $drbl_ayo_path/RPMS.drbl!" 
	echo "Please enter it again!"
        read drbl_ayo_path
      done
      ;;
    5) 
      # enter by user
      url_drbl_default="http://drbl.sourceforge.net"
      drbl_ayo_path_default="/drbl-core/i386"

      echo "Enter the hostname or IP address of DRBL packages reposity with http or ftp prefix."
      echo -n "[$url_drbl_default] "
      read url_drbl
      [ -z "$url_drbl" ] && url_drbl="$url_drbl_default"
      echo "Enter the path where directory RPMS.$core_dir, RPMS.updates... exist."
      echo -n "[$drbl_ayo_path_default] "
      read drbl_ayo_path
      [ -z "$drbl_ayo_path" ] && drbl_ayo_path="$drbl_ayo_path_default"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_drbl="http://free.nchc.org.tw"
      drbl_ayo_path="drbl-core/i386"
esac
url_drbl_rpmroot="$url_drbl/$drbl_ayo_path/"
case "${OS_Version}" in
   FC*)
      dist="fedora"
      ;;
   RH*)
      dist="redhat"
      ;;
esac
url_drbl_collect_root="$url_drbl/drbl-core/pre-drbl/${dist}/linux/${OS_Version:2}/i386"
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_drbl, $drbl_ayo_path"
echo "------------------------------------------------------"
} # parse_RH_drbl_apt_repo_answer

#
parse_MDK_os_ayo_repo_answer() {
# url_drbl_rpmroot is where the RPMS.drbl, RPMS.drbl-testing, RPMS.drbl-unstable... exist
# url_os_rpmroot is where the rpm files (lernel-2.6.8-24mdk-1-1mdk.i586.rpm)... exist
# try to set url_drbl_rpmroot and url_os, we need 
# url_drbl_rpmroot to get wget and apt
# url_os_rpmroot to get glibc/openssl/kernel
# url_os_rpmroot is to the path where "official" exists.
local answer="$1"
case "$answer" in
    1)
      # ftp.twaren.net
      url_os="http://ftp.twaren.net"
      os_ayo_path="/Linux/Mandrake/official"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    2)
      # http://mirrors.kernel.org/mandrake/Mandrakelinux/official/
      # mirrors.kernel.org
      url_os="http://mirrors.kernel.org"
      os_ayo_path="/mandrake/Mandrakelinux/official"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    3)
      # ftp://ftp.isu.edu.tw/pub/Linux/Mandriva/official/
      url_os="ftp://ftp.isu.edu.tw"
      os_ayo_path="/pub/Linux/Mandriva/official"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    4)
      # TODO... make $use_existing_yum_config work in urpmi.addmedia
      # skip the setting of yum, but we will add this for glibc/kernel inst.
      use_existing_urpmi_config="yes"
      url_os="http://free.nchc.org.tw"
      os_ayo_path="/mandrake/official"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    5)
      #enter by user
      url_os_default="http://mirrors.kernel.org"
      os_ayo_path_default="/mandrake/Mandrakelinux/official/"
      echo "Please enter the hostname or IP address of OS packages reposity with http or ftp prefix..."
      echo -n "[$url_os_default] "
      read url_os
      [ -z "$url_os" ] && url_os="$url_os_default"

      echo "Enter the path where directories \"${OS_Version:3}\" and \"updates\"exist..." 
      echo -n "[$os_ayo_path_default] "
      read os_ayo_path
      [ -z "$os_ayo_path" ] && os_ayo_path="$os_ayo_path_default"

      url_os_coreroot=$url_os/$os_ayo_path
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_os="http://free.nchc.org.tw"
      os_ayo_path="/mandrake/official"
      url_os_coreroot="$url_os/$os_ayo_path"
esac
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_os, $url_os_coreroot"
echo "------------------------------------------------------"

} # end of parse_MDK_os_ayo_repo_answer

#
parse_MDK_drbl_ayo_repo_answer() {
# url_drbl_rpmroot is where the RPMS.drbl, RPMS.drbl-testing, RPMS.drbl-unstable... exist
# url_os_rpmroot is where the rpm files (lernel-2.6.8-24mdk-1-1mdk.i586.rpm)... exist
# try to set url_drbl_rpmroot and url_os, we need 
# url_drbl_rpmroot to get wget and apt
# url_os_rpmroot to get glibc/openssl/kernel
# url_os_rpmroot is to the path where "official" exists.
local answer="$1"
case "$answer" in
    1)
      # ftp.twaren.net
      url_drbl="http://ftp.twaren.net"
      drbl_ayo_path="local-distfiles/drbl-core/i386"
      ;;
    2)
      # drbl.sf.net
      url_drbl="http://drbl.sourceforge.net"
      drbl_ayo_path="drbl-core/i386"
      ;;
    5)
      #enter by user
      url_drbl_default="ftp://free.nchc.org.tw"
      drbl_ayo_path_default="/drbl-core/i386"
      echo "Please enter the hostname or IP address of apt reposity with http or ftp prefix..."
      echo -n "[$url_drbl_default] "
      read url_drbl
      [ -z "$url_drbl" ] && url_drbl="$url_drbl_default"

      echo "Enter the path where directory RPMS.drbl exist..." 
      echo -n "[$drbl_ayo_path_default] "
      read drbl_ayo_path
      [ -z "$drbl_ayo_path" ] && drbl_ayo_path="$drbl_ayo_path_default"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_drbl="http://free.nchc.org.tw"
      drbl_ayo_path="drbl-core/i386"
esac
url_drbl_rpmroot="$url_drbl/$drbl_ayo_path/"
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_drbl, $drbl_ayo_path"
echo "------------------------------------------------------"

} # end of parse_MDK_drbl_ayo_repo_answer
#

generate_fedora_yum_conf() {
# backup old file
echo "$msg_delimiter_star_line"
echo "Backuping the repo files..."
if [ "$use_existing_yum_config" = "yes" ]; then
  # we won't touch the existing $FC_CORE_REPO fedora-updates.repo
  for ifile in $drbl_yum_repo_list; do
   [ -f "/etc/yum.repos.d/$ifile" ] && mv -f /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
  done
else
  for ifile in $FC_CORE_REPO fedora-updates.repo $drbl_yum_repo_list; do
   [ -f "/etc/yum.repos.d/$ifile" ] && mv -f /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
  done
fi
echo "done!"

# Turn off the testing and devel repository
echo -n "Turn off the testing and devel repository..."
for ifile in fedora-updates-testing.repo fedora-devel.repo; do
 [ -f "$/etc/yum.repos.d/$ifile" ] &&  perl -p -i -e "s/^enabled=.*/enabled=0/g" /etc/yum.repos.d/$ifile
done
echo "done!"

if [ "$use_existing_yum_config" != "yes" ]; then
  # overwrite the existing $FC_CORE_REPO and fedora-updates.repo
  #
  echo "Creating the fedora core packages repository list..."
  cat <<-YUM_END > /etc/yum.repos.d/$FC_CORE_REPO
[core]
name=Fedora Core \$releasever - \$basearch - Base
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/\$releasever/\$basearch/os/
#mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-core-\$releasever
baseurl=$url_os_coreroot/\$releasever/\$basearch/os
enabled=1
gpgcheck=1
YUM_END

  #
  echo "Creating the fedora updates packages repository list..."
  cat <<-YUM_END > /etc/yum.repos.d/fedora-updates.repo
[updates-released]
name=Fedora Core \$releasever - \$basearch - Released Updates
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/updates/\$releasever/\$basearch/
#mirrorlist=http://fedora.redhat.com/download/mirrors/updates-released-fc\$releasever
baseurl=$url_os_coreroot/updates/\$releasever/\$basearch/
enabled=1
gpgcheck=1
YUM_END

fi
} # end of generate_fedora_yum_conf

#
generate_drbl_yum_conf() {
# copy those drbl template yum config
cp -f $DRBL_SCRIPT_PATH/setup/yum-repos/drbl*.repo /etc/yum.repos.d/
echo "$msg_delimiter_star_line"

# DRBL stable
if [ -n "$DRBL_STABLE" ]; then
  echo "Preparing the DRBL stable packages repository list..."
  perl -pi -e 's|(^[[:space:]]*mirrorlist=.*)|#$1|g' /etc/yum.repos.d/drbl.repo
  perl -pi -e "s|^#*[[:space:]]*baseurl=.*|baseurl=$url_drbl/$drbl_ayo_path/RPMS.drbl|g" /etc/yum.repos.d/drbl.repo
  perl -pi -e "s|^[[:space:]]*enabled=.*|enabled=1|g" /etc/yum.repos.d/drbl.repo
else
  echo "Disabling the DRBL stable packages repository list..."
  perl -pi -e "s|^[[:space:]]*enabled=.*|enabled=0|g" /etc/yum.repos.d/drbl.repo
fi

# DRBL testing
if [ -n "$DRBL_TEST" ]; then
  echo "Preparing the DRBL testing packages repository list..."
  perl -pi -e 's|(^[[:space:]]*mirrorlist=.*)|#$1|g' /etc/yum.repos.d/drbl-testing.repo
  perl -pi -e "s|^#*[[:space:]]*baseurl=.*|baseurl=$url_drbl/$drbl_ayo_path/RPMS.drbl-testing|g" /etc/yum.repos.d/drbl-testing.repo
  perl -pi -e "s|^[[:space:]]*enabled=.*|enabled=1|g" /etc/yum.repos.d/drbl-testing.repo
else
  echo "Disabling the DRBL testing packages repository list..."
  perl -pi -e "s|^[[:space:]]*enabled=.*|enabled=0|g" /etc/yum.repos.d/drbl-testing.repo
fi

# DRBL unstable
if [ -n "$DRBL_UNSTABLE" ]; then
  echo "Preparing the DRBL unstable packages repository list..."
  perl -pi -e 's|(^[[:space:]]*mirrorlist=.*)|#$1|g' /etc/yum.repos.d/drbl-unstable.repo
  perl -pi -e "s|^#*[[:space:]]*baseurl=.*|baseurl=$url_drbl/$drbl_ayo_path/RPMS.drbl-unstable|g" /etc/yum.repos.d/drbl-unstable.repo
  perl -pi -e "s|^[[:space:]]*enabled=.*|enabled=1|g" /etc/yum.repos.d/drbl-unstable.repo
else
  echo "Disabling the DRBL unstable packages repository list..."
  perl -pi -e "s|^[[:space:]]*enabled=.*|enabled=0|g" /etc/yum.repos.d/drbl-unstable.repo
fi

echo "done!"
} # end of generate_drbl_yum_conf

#
do_upgrade_system_for_RH() {
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_glibc_openssl_upgrade_question"
echo "$msg_glibc_openssl_keep"
echo "$msg_keep_glibc_upgrade_openssl"
echo "$msg_upgrade_glibc_keep_openssl"
echo "$msg_upgrade_glibc_openssl"
echo "$msg_warning_glibc_upgrade"
echo "$msg_upgrade_glibc_make_you_cry"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[3] "
read RH_upgrade_option

echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_upgrade_whole_system"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"

# we must keep drbl 
APT_KEEP_OPT='-o RPM::Hold::=drbl'
YUM_KEEP_OPT='--exclude=drbl'
case "$RH_upgrade_option" in
  0)
     # keep glibc and openssl
     APT_KEEP_OPT="$APT_KEEP_OPT -o RPM::Hold::=openssl -o RPM::Hold::=glibc"
     YUM_KEEP_OPT="$YUM_KEEP_OPT --exclude=openssl --exclude=glibc --exclude=glibc-common --exclude=glibc-devel --exclude=glibc-headers"
     ;;
  1)
     # upgrade openssl, keep glibc
     APT_KEEP_OPT="$APT_KEEP_OPT -o RPM::Hold::=glibc"
     YUM_KEEP_OPT="$YUM_KEEP_OPT --exclude=glibc --exclude=glibc-common --exclude=glibc-devel --exclude=glibc-headers"
     ;;
  2) 
     # upgrade glibc, keep openssl
     APT_KEEP_OPT="$APT_KEEP_OPT -o RPM::Hold::=openssl"
     YUM_KEEP_OPT="$YUM_KEEP_OPT --exclude=openssl"
     ;;
  *)
     # upgrade glibc and openssl
     echo "We will upgrade glibc and openssl."
     ;;
esac

# do it
[ "$installer" = "apt" ] && apt-get dist-upgrade $APT_KEEP_OPT 
[ "$installer" = "yum" ] && yum $YUM_KEEP_OPT update
} # end of do_upgrade_system_for_RH

#
import_rpm_key() {
echo "$msg_delimiter_star_line"
case "$OS_Version" in
   FC*)
    # import GPG key if not imported.
    if ! rpm -qi --quiet gpg-pubkey-db42a60e-37ea5438; then
      # For FC[45], there are 2 dirs containing key, but they are same:
      # /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora
      # /usr/share/doc/fedora-release-5/RPM-GPG-KEY-fedora
      # We choose either one.
      RH_GPG_KEY="$(rpm -ql fedora-release | grep -E "RPM-GPG-KEY$" | head -n 1)"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "Importing the RPM-GPG-KEY from $RH_GPG_KEY..."
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      rpm --import $RH_GPG_KEY
    else
      echo "Red Hat GPG public key gpg-pubkey-db42a60e-37ea5438 was already imported."
    fi
    if ! rpm -qi --quiet gpg-pubkey-4f2a6fd2-3f9d9d3b; then
      FC_GPG_KEY="$(rpm -ql fedora-release | grep -E "RPM-GPG-KEY-fedora$" | head -n 1)"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "Importing the RPM-GPG-KEY-fedora from $FC_GPG_KEY..."
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      rpm --import $FC_GPG_KEY
    else
      echo "Fedora Project GPG public key gpg-pubkey-4f2a6fd2-3f9d9d3b was already imported."
    fi
    ;;
  CO*)
    if ! rpm -qi --quiet gpg-pubkey-443e1821-421f218f; then
      # /usr/share/doc/centos-release-4/RPM-GPG-KEY is all the same with
      # /usr/share/doc/centos-release-4/RPM-GPG-KEY-centos4
      # So we just use the one RPM-GPG-KEY-centos4
      CO_GPG_KEY="$(rpm -ql centos-release | grep -E "RPM-GPG-KEY-centos4$")"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo -n "Importing the RPM GPG KEY from $CO_GPG_KEY..."
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      rpm --import $CO_GPG_KEY
    fi
    ;;
esac
} # end of import_rpm_key

#
do_upgrade_system_for_MDK() {
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_delimiter_star_line"
echo "$msg_upgrade_whole_system"
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

APT_KEEP_OPT='-o RPM::Hold::=drbl'
case "$installer" in
  "apt")
    apt-get upgrade $APT_KEEP_OPT
    ;;
  "urpmi")
    # backup the original skip.list
    [ -f /etc/urpmi/skip.list ] && mv -f /etc/urpmi/skip.list /etc/urpmi/skip.list.drblsave
    # generate the skip list, we want to keep drbl
    cat <<EOF > /etc/urpmi/skip.list
drbl
EOF
    urpmi --auto-select

    [ -f /etc/urpmi/skip.list.drblsave ] && mv -f /etc/urpmi/skip.list.drblsave /etc/urpmi/skip.list
    ;;
esac
} # end of do_upgrade_system_for_MDK

#
do_upgrade_system_for_DBN() {
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_upgrade_whole_system"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"
# do it
[ "$installer" = "apt" ] && apt-get -y dist-upgrade
} # end of do_upgrade_system_for_RH

# For Debian
install_drbl_DBN() {
# function to install Debian DRBL
# /etc/-release is like:
# DISTRIB_ID=Ubuntu
# DISTRIB_RELEASE=6.06
# DISTRIB_CODENAME=dapper
# DISTRIB_DESCRIPTION="Ubuntu 6.06 LTS"
[ -e /etc/lsb-release ] && . /etc/lsb-release
#case "$OS_Version" in
#  DBN-TU)
#    # Ubuntu breezy, Debian Etch, Sid or...
#    # Force to set LC_ALL=C, otherwise in Ubuntu, it will show a lot warning messages in debconf like
#    # perl: warning: Setting locale failed.
#    # perl: warning: Please check that your locale settings:
#    # LANGUAGE = "zh_TW:zh:en_US:en",
#    # LC_ALL = "zh_TW.Big5",
#    # LANG = "zh_TW.UTF-8"
#    # are supported and installed on your system.
#    # perl: warning: Falling back to the standard locale ("C").
#    export LC_ALL=C
#    ;;
#esac

# Some checkings:
# It seems sarge running kernel 2.4 will have NFS problem...
case "$OS_Version" in
  DBN3.1)
    if [ -n "$(uname -r | grep -E "^2\.4\.*")" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "Kernel 2.4 is too old in DRBL environment for Debian Sarge, you have to run kernel 2.6. Please install kernel 2.6 and use it."
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop"
      exit 1
    fi
    ;;
esac
# clean the stale files and make necessary directories
[ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
# create the directory for diskless image
[ ! -d "/tftpboot/nbi_img" ] && mkdir -p /tftpboot/nbi_img

# 1.
# User has to setup apt/yum first by them self, so skip this.

# ask some setting for client, such as SMP, serial console...
ask_and_parse_client_extra_setting

# 
parse_client_archi_setting

# In Debian, the installer is apt.
inst_prog="apt-get"
inst_opt="-y $apt_extra_opt"
inst_action="install"

# 2.
# It's important to remove the cache of apt, otherwise maybe something like
# the RPM::Architecture will not take effect. check this:
# http://distro2.conectiva.com.br/pipermail/apt-rpm/2003-November/002015.html
echo "$msg_delimiter_star_line"
echo "$msg_clean_apt_cache"
for ifile in /var/cache/apt/*.bin; do
   rm -f /var/cache/apt/$ifile
done

# Update the apt data with repository.
$inst_prog update

# UGLY
obsolete_pkgs="mkinitrd-net partimage-server knoppix-terminalserver"
# mkinitrd-net is replaced by mkpxeinitrd-net in this version. Although 
# mkinitrd-net is set as one of the obsoletes in mkpxeinitrd-net.spec.
# However, we use alien to convert drbl rpm to drbl deb, the obsolete is not 
# converted. 
# Same for partimage-server (The partimage released by DRBL containing partimaged), knoppix-terminalserver
for i in $obsolete_pkgs; do
  if [ -n "$(dpkg -s $i 2>/dev/null | grep -Ei "^Status: install ok installed")" ]; then
    dpkg --purge $i
  fi
done

# Shall We try to upgrade system first.?
# Since there are many debian based distributions, it's might not be a good
# idea to upgrade the system. So let user decided.
echo "$msg_delimiter_star_line"
if [ -z "$upgrade_system_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_upgrade_system_question"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read upgrade_system_answer
fi
case "$upgrade_system_answer" in 
   y|Y|[yY][eE][sS])
      do_upgrade_system_for_DBN
      ;;
esac

# 3.
# Install the necessary packages for DRBL 
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_delimiter_star_line"
echo "$msg_install_DRBL_necesary_files"
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
# package from Debian
# mkinitrd is in initrd-tools
# dos2unix is in sysutils
# Supposed "udpcast" is put in the rpm spec's requirement of clonezilla. But
# Alien fails to make that deps, so we add here.
# We need gawk instead of mawk (the original awk)
# $PKG_FROM_DBN, $PKG_TO_QUERY and $PKG_FROM_DRBL are loaded from conf/drbl.conf

# Check if lvm2 is available, if so, add it, we need this for clonezilla to
# save or restore LVM2 PV/VG/LV.
echo "Searching if $PKG_TO_QUERY available... "
for ipkg in $PKG_TO_QUERY; do
  #if [ -n "$(LC_ALL=C apt-cache pkgnames $ipkg 2>/dev/null | grep -i "^${ipkg}$")" ]; then
  if [ -n "$(LC_ALL=C apt-cache show $ipkg 2>/dev/null)" ]; then
    echo "Package $ipkg exists in repository."
    PKG_FROM_DBN="$PKG_FROM_DBN $ipkg"
  fi
done
# Detect if discover/discover1 is installed or not, if installed, 
# do not install it. If not, we need discover (not discover1) for clients. 
# The package name "discover" exists both in woody and sarge.
# The reason we have to check is that we prefer to keep the installed package, maybe discover or discover1.
if ! chk_deb_installed discover && ! chk_deb_installed discover1; then
   PKG_FROM_DBN="$PKG_FROM_DBN discover"
fi
# For Breezy, Etch, udev is necessary ? not hotplug ? udev and hotplug conflict.# For Breezy, Etch, we just won't touch the server, if hotplug is unstalled (breezy), ust that, if udev is installed (ethc), use that.
case "$OS_Version" in
  DBN3.1|DBN3.0)
    # Genuine Debian sarge/woody
    PKG_FROM_DBN="$PKG_FROM_DBN hotplug"
    ;;
esac

# For Ubuntu 9.10, xz-utils conflicts with lzma, and lzma is essential
if [ "$DISTRIB_ID" = "Ubuntu" -a "$DISTRIB_RELEASE" = "9.10" ]; then
  if [ -n "$(echo $PKG_FROM_DBN | grep -Ew "lzma")" -a \
       -n "$(echo $PKG_FROM_DBN | grep -Ew "xz-utils")" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "In Ubuntu 9.10, xz-utils conflicts with lzma, and lzma is essential. Exclude installing xz-utils!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    PKG_FROM_DBN="$(echo $PKG_FROM_DBN | sed -e "s/xz-utils//g")"
  fi
fi

# From ntfs-3g 1:2011.4.12AR.4-2, it will provide all the files of ntfsprogs.
# Package: ntfs-3g
# Version: 1:2011.4.12AR.4-2   
# Installed-Size: 1492
# Maintainer: Daniel Baumann <daniel.baumann@progress-technologies.net>
# Architecture: i386
# Replaces: libntfs-3g75, libntfs-3g804, ntfsprogs
# Provides: ntfsprogs
# Therefore we do not have to install ntfsprogs. Otherwise we should.
# 2012/10/17, ntfs-3g package info does not show "Provides: ntfsprogs" on Debian Sid, therefore we have to check if it exists on repository before adding it to PKG_FROM_DBN.
if [ -z "$(LC_ALL=C apt-cache show ntfs-3g | grep -E "^Provides:.*ntfsprogs")" -a \
     -n "$(LC_ALL=C apt-cache search ntfsprogs | grep -Ew "^ntfsprogs")" ]; then
  echo "Package ntfs-3g does not provide the files of old package ntfsprogs, so ntfsprogs will be installed."
  PKG_FROM_DBN="$PKG_FROM_DBN ntfsprogs"
fi

# package from DRBL,
# $PKG_FROM_DRBL will be loaded from conf/drbl.conf

# We need to preconfig some packages, such as dhcp3-server, tftpd-hpa, nis so
# it will not ask user to config. We will configure later in drblpush.
deb-preconf-drbl

# Install them
$inst_prog $inst_opt $inst_action $PKG_FROM_DBN $PKG_FROM_DRBL
rc_apt="$?"
if [ "$rc_apt" -gt 0 ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_some_necessary_pkgs_not_installed"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "$msg_press_ctrl_c_stop Or $msg_press_enter_to_continue "
  read
fi

#
echo "$msg_delimiter_star_line"
# 4. dev for client machines
# For Debian woody & B2D pureKDE20050603..It's necessary to make one before run
# this... (Just create the tarball from some Debian machine)
# It's ugly here...
# The solution: Make udev as the 1st priority
[ ! -d $drbl_pkgdir ] && mkdir -p $drbl_pkgdir

# try to install udev...
if ! chk_deb_installed udev; then
  echo "$msg_delimiter_star_line"
  echo "Try to install udev..."
  $inst_prog $inst_opt $inst_action udev &>/dev/null
fi
if ! chk_deb_installed udev; then
  # udev is not available, use tarball
  if [ -f $drbl_setup_path/files/${OS_type}/${OS_Version}/dev.$OS_Version.tgz ]; then
    echo "Udev is not available, use pre-packaged device files."
    cp -f $drbl_setup_path/files/${OS_type}/${OS_Version}/dev.$OS_Version.tgz $drbl_pkgdir/dev.tgz
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "No udev, and no pre-packaged device files!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    echo "$msg_program_stop"
    exit 1
  fi
fi

# 5.
# ugly...might be not my fault...
# for some distribution some version, we have to force upgrading some packages.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_try_to_update_some_pkgs"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
case "$OS_Version" in
  DBN3.0)
    # We need "grep -o", and the grep in woody does not have that option.
    # so use the one from backports.
    echo "$msg_OS_version: $OS_Version."
    echo "$msg_force_to_upgrade_some_pkgs"
    apt-get $inst_opt $inst_action grep
    ;;
esac

# 6.
# netinstall
if [ "$DRBL_NETINSTALL" = "yes" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_install_net_install_images"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_delimiter_star_line"
  drbl-netinstall -i all -s -v
fi

# 7.
# Force to install all the kernel selected.
echo "$msg_delimiter_star_line"
echo "$msg_search_kernel_in_ayo ${SMP_OPTION:1} kernel ..."

# Install the kernel
# Note! This must be done after gawk is installed, since we need gawk to parse
# the latest kernel.
if [ "$client_archi_set" = "best_optimization" ]; then
  # same CPU archi with server,  maybe for i686, or i386 (for AMD K7 ?)
  KARCH_LIST="$(drbl-check-kernel-cpu-arch `uname -r`)"
  # KARCH_LIST will be nothing if we can not find the kernel config file, so we choose to use the same arch of server for client.
  [ -z "$KARCH_LIST" ] && KARCH_LIST="$(uname -m)"
  # convert the arch name from server to client, 
  # This is the lists for Debian Sarge:
  # -----------------------------------------------
  # kernel-image-2.6.8-3-686-smp 2.6.8-16sarge2
  # kernel-image-2.6.8-3-686 2.6.8-16sarge2
  # kernel-image-2.6.8-3-386 2.6.8-16sarge2
  # kernel-image-2.6.8-2-k7-smp 2.6.8-16sarge1
  # kernel-image-2.6.8-2-k7 2.6.8-16sarge1
  # kernel-image-2.6.8-12-em64t-p4-smp 2.6.8-16sarge2
  # kernel-image-2.6.8-12-em64t-p4 2.6.8-16sarge2
  # kernel-image-2.6.8-12-amd64-k8-smp 2.6.8-16sarge2
  # kernel-image-2.6.8-12-amd64-k8 2.6.8-16sarge2
  # kernel-image-2.6.8-12-amd64-generic 2.6.8-16sarge2
  # kernel-image-2.6.8-11-em64t-p4-smp 2.6.8-16sarge1
  # kernel-image-2.6.8-11-em64t-p4 2.6.8-16sarge1
  # kernel-image-2.6.8-11-amd64-k8-smp 2.6.8-16sarge1
  # kernel-image-2.6.8-11-amd64-k8 2.6.8-16sarge1
  # kernel-image-2.6.8-11-amd64-generic 2.6.8-16sarge1
  # -----------------------------------------------
  # i386/i486/i586 -> i686 since only i686-smp and k7-smp exists in Debian
  # For k7, keep what it is.
  # If found nothing, let KARCH_LIST=i686
  # TODO: How about em64t ? Can drbl-check-kernel-cpu-arch show correct messages ?
  # -----------------------------------------------
  # Updated on 2006/10/28, for Ubuntu 6.10, there is no name tag for 686 or amd64-k8, they are like:
  # -----------------------------------------------
  # linux-image-2.6.17-10-386_2.6.17-10.33_i386.deb             
  # linux-image-2.6.17-10-generic_2.6.17-10.33_amd64.deb        
  # linux-image-2.6.17-10-generic_2.6.17-10.33_i386.deb         
  # linux-image-2.6.17-10-powerpc-smp_2.6.17-10.33_powerpc.deb  
  # linux-image-2.6.17-10-powerpc64-smp_2.6.17-10.33_powerpc.deb
  # linux-image-2.6.17-10-powerpc_2.6.17-10.33_powerpc.deb      
  # linux-image-2.6.17-10-server-bigiron_2.6.17-10.33_i386.deb  
  # linux-image-2.6.17-10-server_2.6.17-10.33_amd64.deb         
  # linux-image-2.6.17-10-server_2.6.17-10.33_i386.deb          
  # linux-image-2.6.17-10-sparc64-smp_2.6.17-10.33_sparc.deb    
  # -----------------------------------------------
  # and "apt-cache pkgnames linux-image | grep -E "^linux-image-[0-9]+\.[0-9]+\.[0-9]+" | sort" will give:
  # linux-image-2.6.15-23-386 <-- forget the stale one
  # linux-image-2.6.15-23-686 <-- forget the stale one
  # linux-image-2.6.15-23-k7  <-- forget the stale one
  # linux-image-2.6.15-27-686 <-- forget the stale one
  # linux-image-2.6.17-10-386
  # linux-image-2.6.17-10-generic
  # linux-image-2.6.17-10-server
  # linux-image-2.6.17-10-server-bigiron
  # Hence now basically only linux-image-2.6.17-10-386 (for i386) or linux-image-2.6.17-10-generic (for i586/686)
  # -----------------------------------------------
  # However, for Debian etch in testing (on 2006/10/28), it's like:
  # linux-image-2.6-486_2.6.18+3_i386.deb           
  # linux-image-2.6-686-bigmem_2.6.18+3_i386.deb    
  # linux-image-2.6-686-smp_2.6.18+3_i386.deb       
  # linux-image-2.6-686_2.6.18+3_i386.deb           
  # linux-image-2.6-alpha-generic_2.6.18+3_alpha.deb
  # linux-image-2.6-alpha-legacy_2.6.18+3_alpha.deb 
  # linux-image-2.6-alpha-smp_2.6.18+3_alpha.deb    
  # linux-image-2.6-amd64-generic_2.6.18+3_amd64.deb
  # linux-image-2.6-amd64-k8-smp_2.6.18+3_amd64.deb 
  # linux-image-2.6-amd64-k8_2.6.18+3_amd64.deb     
  # linux-image-2.6-amd64_2.6.18+3_amd64.deb        
  # linux-image-2.6-em64t-p4-smp_2.6.18+3_amd64.deb 
  # linux-image-2.6-em64t-p4_2.6.18+3_amd64.deb     
  case "$KARCH_LIST" in
  i[0-9]86)
     KARCH_LIST="i686"
     # skip the leading "i"
     client_archi=${KARCH_LIST:1}
     ;;
  x86_64)
     KARCH_LIST="amd64"
     client_archi=${KARCH_LIST}
     ;;
  *)
     KARCH_LIST="i686"
     # skip the leading "i"
     client_archi=${KARCH_LIST:1}
     ;;
  esac
  # UGLY! Any better method ?
  # For Ubuntu 6.10 or later, there is no name tag for 686 or amd64-k8, 
  # it's generic, for earlier version, it uses 386, 686 or amd64-k8
  # We will not use this for Debian etch.
  if [ "$DISTRIB_ID" = "Ubuntu" ]; then
    if [ "$DISTRIB_RELEASE" != "5.10" -a "$DISTRIB_RELEASE" != "6.06" ]; then
       echo "The kernel image in Ubuntu $DISTRIB_RELEASE \"uses generic\" for i686/amd64 CPU."
       KARCH_LIST="generic"
       client_archi="generic"
    fi
  fi
  # For Linux Mint, by Ceasar Sun
  if [ "$DISTRIB_ID" = "LinuxMint" -a -z "$(echo "$DISTRIB_DESCRIPTION" | grep -iE '^LMDE')" ]; then
    # LinuxMint follow Ubuntu policy exclude  LMDE
    echo "The kernel image in Ubuntu $DISTRIB_RELEASE \"uses generic\" for i686/amd64 CPU."
    KARCH_LIST="generic"
    client_archi="generic"
  fi
else
  # For Debian, only i386 is available we decided. Maybe in the future, we 
  # will add K7 ?
  # For Debian Etch/Ubuntu Dapper... the kernel is like linux-image-2.6.15-1-486, only 486
  KARCH_LIST="i[345]86"
  # skip the leading "i"
  client_archi=${KARCH_LIST:1}
fi
cpu_filter="grep -iE $client_archi"

if [ -n "$SMP_OPTION" ]; then
  # enable SMP filter
  smp_filter="grep -i smp"
else
  # "NOT SMP" filter
  smp_filter="grep -iv smp"
fi

# This is for Debian etch, to make thing easier, we exclude some kernel
# linux-image-2.6.17-2-486
# linux-image-2.6.17-2-686
# linux-image-2.6.17-2-686-bigmem   <--
# linux-image-2.6.17-2-k7
# linux-image-2.6.17-2-vserver-686  <--
# linux-image-2.6.17-2-vserver-k7   <--
# linux-image-2.6.17-2-xen-686      <--
# linux-image-2.6.17-2-xen-k7       <--
# For Lenny, openvz:
# linux-image-2.6.26-2-openvz-686   <-- 
# i.e. exclude: xen, vserver, openvz, rt, dbg...
seldom_kernel_filter="grep -Ev (xen|vserver|openvz|rt|dbg)"

# Ugly!!!
# There is no SMP tag kernel image from Ubuntu Dapper (6.06), so we force to set not smp
# Actually this is useless, since in ask_and_parse_client_extra_setting(), for DBN-TU, we will not ask SMP or not. Therefore maybe we should remove this!
if [ -n "$SMP_OPTION" -a "$DISTRIB_ID" = "Ubuntu" ]; then
  if [ "$DISTRIB_RELEASE" != "5.10" ]; then
     echo "Since all the kernel image in Ubuntu $DISTRIB_RELEASE supports SMP, we do not have to specify the kernel with SMP tag for clients."
     smp_filter="grep -iv smp"
  fi
fi

# The kernel image name: linux-iamge (such as Etch) or kernel-image (such as Sarge)
# Kernel package name before (<=) Debian Wheezy (7.0) is like: linux-image-3.2.0-4-amd64
# While for >= Debian jessie (8.0) is like: linux-image-3.8-1-amd64
for ikmg in linux-image kernel-image; do
  latest_drbl_kernel_in_rep="$(apt-cache pkgnames $ikmg | grep -E "$ikmg-[0-9]+\.[0-9]+(\.[0-9]+|)-.*" | $cpu_filter | $smp_filter | $seldom_kernel_filter | pkg-ver-latest)"
  if [ -n "$latest_drbl_kernel_in_rep" ]; then
     kernel_img_name="$ikmg"
     break
  fi
done
echo "$msg_latest_kernel_in_ayo $latest_drbl_kernel_in_rep"

# Get the selected kernel arch (i686/i386/i486)
# Ex:
# linux-image-2.6.12-10-686 -> 686
# linux-image-2.6.12-10-686-smp -> 686
# linux-image-2.6.12-10-amd64-xeon -> x86_64
# linux-image-2.6.12-10-amd64-k8-smp -> x86_64
# linux-image-2.6.12-10-amd64-k8 -> x86_64
# linux-image-2.6.12-10-amd64-generic -> x86_64
# For Etch or Dapper, there is an issue in Dapper, check
# https://lists.ubuntu.com/archives/ubuntu-devel/2006-May/018185.html
# for more details.
# linux-image-2.6.16-1-486_2.6.16-12_i386.deb (Etch) -> 486, there is a "486", not "386", to show this is for i486 arch.
# linux-image-2.6.15-23-386_2.6.15-23.39_i386.deb (Dapper beta) -> the name implies 386, but actually it's 486... Ha...
case "$latest_drbl_kernel_in_rep" in
  *-[0-9]86*)
    KARCH="$(echo $latest_drbl_kernel_in_rep | sed -e "s/.*-\([0-9]86\).*$/i\1/" | sed -e "s/-smp//g")"
    ;;
  *-amd64-*|*-em64t-*)
    KARCH=x86_64
    ;;
esac

# UGLY!!! For Ubuntu Dapper, about the issue mentioned above, we have to use the ugly rule! Since although the name is 386, but actually it's 486.
# Actually from Ubuntu dapper (6.06) or later (not only 6.06), as mentioned above, the kernel image name is like linux-headers-2.6.20-12_2.6.20-12.20_i386.deb (7.04 still), but actually it's 486 (CONFIG_M486=y)
# Maybe it will change in the future...
if [ "$KARCH" = "i386" -a \
     "$DISTRIB_ID" = "Ubuntu" -a \
     "$DISTRIB_RELEASE" != "5.10" ]
then
  KARCH="i486"
fi

# Another available kernel maybe is from server, check it:
running_karch="$(drbl-check-kernel-cpu-arch `uname -r`)"
running_kernel_in_server="$(echo $(uname -r) $running_karch | $cpu_filter | $smp_filter)"

use_kernel_from=
if [ -n "$running_kernel_in_server" ]; then
  # Server running kernel matches client's requirement
  if [ -z "$chosen_client_kernel_from" ]; then
    echo "$msg_2_kernel_availables"
    echo "[1]: kernel $running_kernel_in_server ($msg_from_this_drbl_server)"
    echo "[2]: $latest_drbl_kernel_in_rep ($msg_from_apt_repository)"
    echo -n "[1] "
    read chosen_client_kernel_from
  fi
  case "$chosen_client_kernel_from" in
    2)
      echo "Clients will use the kernel $latest_drbl_kernel_in_rep from apt repository."
      use_kernel_from="apt-repository"
      ;;
    *)
      echo "Clients will use the kernel $running_kernel_in_server from server."
      use_kernel_from="local"
      ;;
  esac
else
  # Use the kernel in the repository
  echo "Use the kernel $latest_drbl_kernel_in_rep from apt repository!"
  use_kernel_from="apt-repository"
fi

rc_install_kernel=1
case "$use_kernel_from" in
  "apt-repository")
    # use with client_kernel_install_mode and client_kernel_tmp_dir
    client_kernel_install_mode="from_downloaded_pkg"

    # get the version and release of kernel, strip the kernel[#-] or kernel-smp[#-]
    # $drbl_kernel_ver will be like "2.6.8-2-386", "2.6.8-2-686", or "2.6.8-2-686-smp"
    drbl_kernel_ver="$(echo $latest_drbl_kernel_in_rep | sed -e s/$kernel_img_name-//g)"
    if [ -z "$drbl_kernel_ver" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "Linux kernel $kernel_img_name not available!"
      echo "You can run this program $0 again, and try to choose different CPU architecture (such as i386, i686, no SMP...)!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop"
      exit 1
    fi
    
    # download it
    apt-get $inst_opt --reinstall --download-only install $latest_drbl_kernel_in_rep
    rc_get_kernel=$?
    if [ $rc_get_kernel -gt 0 ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "Failed to download $latest_drbl_kernel_in_rep."
      echo "Maybe you should check /etc/apt/sources.list or remove the unnecessary kernel block in /var/lib/dpkg/status so that "apt-cache pkgnames $kernel_img_name" can get the downloadable kernel! Then try to run this program again."
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop"
      exit 1
    fi
    # Download the restricted modules for Ubuntu, here we assume if server use that, then client should use that.
    drblsrv_offline_extra_opt=""
    if [ -x /etc/init.d/linux-restricted-modules-common ]; then
      apt-get $inst_opt --reinstall --download-only install linux-restricted-modules-$drbl_kernel_ver
      # find the restricted kernel
      # The "_" is important, since the file names in the cache dir are like
      # linux-restricted-modules-2.6.17-10-386_2.6.17.5-11_i386.deb
      # linux-restricted-modules-2.6.17-10-generic_2.6.17.5-11_i386.deb
      if [ -n "$(ls /var/cache/apt/archives/linux-restricted-modules-${drbl_kernel_ver}_*.deb 2>/dev/null)" ]; then
        latest_drbl_restricted_kernel_in_rep_deb="$(unalias ls 2>/dev/null; ls /var/cache/apt/archives/linux-restricted-modules-${drbl_kernel_ver}_*.deb 2>/dev/null | pkg-ver-latest)"
        drblsrv_offline_extra_opt="$drblsrv_offline_extra_opt -e $latest_drbl_restricted_kernel_in_rep_deb"
      fi
    fi
    # Download the extra modules from Ubuntu, here we assume if server use that, then client should use that.
    # Ex: /lib/modules/2.6.22-14-generic/ubuntu is from deb linux-ubuntu-modules-2.6.22-14-generic
    if [ -n "$(unalias ls 2>/dev/null; ls /lib/modules/*/ubuntu 2>/dev/null)" ]; then
      apt-get $inst_opt --reinstall --download-only install linux-ubuntu-modules-$drbl_kernel_ver
      # find the ubuntu kernel
      # The "_" is important, since the file names in the cache dir are like
      # linux-ubuntu-modules-2.6.22-14-generic_2.6.22-14.38_i386.deb 
      # linux-ubuntu-modules-2.6.22-14-386_2.6.22-14.38_i386.deb
      if [ -n "$(ls /var/cache/apt/archives/linux-ubuntu-modules-${drbl_kernel_ver}_*.deb 2>/dev/null)" ]; then
        latest_drbl_ubuntu_kernel_in_rep_deb="$(unalias ls 2>/dev/null; ls /var/cache/apt/archives/linux-ubuntu-modules-${drbl_kernel_ver}_*.deb 2>/dev/null | pkg-ver-latest)"
        drblsrv_offline_extra_opt="$drblsrv_offline_extra_opt -e $latest_drbl_ubuntu_kernel_in_rep_deb"
      fi
    fi

    # Download the linux-image-extra package from Ubuntu. This is for Ubuntu 12.04 or later (12.10, 13.04...)
    # Ref: https://lists.ubuntu.com/archives/kernel-team/2012-May/020448.html
    if [ -n "$(apt-cache search linux-image-extra-$drbl_kernel_ver)" ]; then
      apt-get $inst_opt --reinstall --download-only install linux-image-extra-$drbl_kernel_ver
      # find the ubuntu kernel
      # The "_" is important, since the file names in the cache dir are like
      # linux-image-extra-3.5.0-17-generic_3.5.0-17.28_i386.deb
      if [ -n "$(ls /var/cache/apt/archives/linux-image-extra-${drbl_kernel_ver}_*.deb 2>/dev/null)" ]; then
        latest_drbl_ubuntu_kernel_in_rep_deb="$(unalias ls 2>/dev/null; ls /var/cache/apt/archives/linux-image-extra-${drbl_kernel_ver}_*.deb 2>/dev/null | pkg-ver-latest)"
        drblsrv_offline_extra_opt="$drblsrv_offline_extra_opt -e $latest_drbl_ubuntu_kernel_in_rep_deb"
      fi
    fi
    
    # The "_" is important, since the file names in the cache dir are like
    # kernel-image-2.6.8-2-386_2.6.8-16_i386.deb, 
    # kernel-image-2.6.8-2-686_2.6.8-16_i386.deb, 
    # kernel-image-2.6.8-2-686-smp_2.6.8-16_i386.deb
    if [ -n "$(unalias ls 2>/dev/null; ls /var/cache/apt/archives/${latest_drbl_kernel_in_rep}_*.deb 2>/dev/null)" ]; then
      client_kernel_tmp_dir="/var/cache/apt/archives"
    fi
    ;;
  "local")
    echo "$msg_install_kernel_might_take_several_minutes"
    drbl_kernel_ver="$(uname -r)"
    # Since local is chosen, here we force to use the KARCH from the server, otherwise maybe the kernel in the repository has different CPU arch.
    # KARCH="$running_karch"

    client_kernel_install_mode="from_server"
    client_kernel_ver="$drbl_kernel_ver"
    echo "$msg_done!"
    ;;
esac

# 8.
echo "$msg_delimiter_star_line"
# Force to install all the kernel selected.
echo "$msg_install_kernel_for_clients ..."
echo "$msg_search_kernel_in_ayo kernel${SMP_OPTION} ..."
echo "$msg_delimiter_star_line"
run_drblsrv_offline

} # end of install_drbl_DBN

#
install_drbl_SUSE() {
# function to install SuSE Linux
# Now the installer is only apt, maybe someday we will find a way to use yast.
# Note: Here we use apt instead of "apt-get" since apt can accept "--no-checksig"
case "$installer" in
  "yum")
        inst_prog="yum"
        inst_opt="-y"
        inst_action="install"
        ;;
  "zypper")
        # zypper [--global-opts] <command> [--command-opts] [command-arguments]
	# E.g. zypper --non-interactive install foo
        # --non-interactive (or -n) is in --global-opts
        inst_prog="zypper"
        inst_opt="-n"
        inst_action="install"
        ;;
  *)
        # Default is apt
        inst_prog="apt"
        inst_opt="-y --no-checksig $apt_extra_opt"
        inst_action="install"
        ;;
esac

# Check the running kernel
echo "$msg_delimiter_star_line"
# ugly...might be not my fault...
# for some distribution some version, we have to force upgrading some packages.

# SuSE/OpenSuSE 10.0 with kernel 2.6.13-15-default is BUGGY 
# in DRBL server!!! Since it's a kernel upgrade, we wont' do that for
# user, just prompt them
if [ "$(uname -r)" = "2.6.13-15-default" ]; then
  echo "$msg_OS_version: $OS_Version."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "$msg_kernel_2_6_13_15_defaul_is_buggy."
  echo "$msg_u_have_to_upgrade_kernel_in_this_machine"
  case "$ARCH" in
    i386)
        echo "rpm -ivh http://free.nchc.org.tw/SuSE/SuSE/i386/update/10.0/rpm/i586/kernel-default-2.6.13-15.7.i586.rpm"
        ;;
    x86_64)
        echo "rpm -ivh http://free.nchc.org.tw/SuSE/SuSE/i386/update/10.0/rpm/x86_64/kernel-default-2.6.13-15.7.x86_64.rpm"
        ;;
  esac
  echo "$msg_then_reboot_this_server"
  echo "Ref: https://bugzilla.novell.com/show_bug.cgi?id=128784"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

# check if installer exists
if [ "$installer" = "apt" ] && ! rpm -q apt &>/dev/null; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "You must install apt4rpm first in this SuSE server!"
  echo "Check http://linux01.gwdg.de/apt4rpm for more details!"
  echo "Or run 'rpm -Uvh http://linux01.gwdg.de/~scorot/install-apt4suse.rpm', then run install-apt4suse to setup it."
  echo "Remeber to add the following line in /etc/apt/sources.list"
  echo "rpm http://free.nchc.org.tw/drbl-core i386 drbl-unstable drbl-testing drbl-stable"
  echo "Note: drbl-unstable or drbl-testing is optional, use at your own risk!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  exit 1
fi
if [ "$installer" = "yum" ] && ! rpm -q yum &>/dev/null; then
  # From SuSE 10.0, yum is available
  if [ "$OS_Version" != "SUSE9.3" ]; then 
    echo "$msg_delimiter_star_line"
    echo "Try to install yum first..."
    yast2 -i yum
    echo "$msg_delimiter_star_line"
    if ! rpm -q yum &>/dev/null; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "You must manually install yum first in this SuSE server!"
      echo "Use yast2 to install that, and remember to set the yum repository in /etc/yum.repos.d/"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      exit 1
    fi
  fi
fi
if [ "$installer" = "zypper" ] && ! rpm -q zypper &>/dev/null; then
  echo "$msg_delimiter_star_line"
  echo "Try to install zypper first..."
  yast2 -i zypper
  echo "$msg_delimiter_star_line"
  if ! rpm -q zypper &>/dev/null; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "You must manually install package 'zypper' first!"
    echo "Use yast2 to install that, and remember to set the zypper repository!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    exit 1
  fi
fi

# clean the stale files and make necessary directories
[ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
# create the directory for diskless image
[ ! -d $pxecfg_pd ] && mkdir -p $pxecfg_pd
[ ! -d $drbl_common_root ] && mkdir -p $drbl_common_root
[ ! -d $drblroot ] && mkdir -p -m 700 $drblroot

# Setup yum respository
if [ "$installer" = "yum" ]; then
  find_yum_repos_dir_path_for_opensuse
  # Backup the old repo file, since 1.6.4-7, we will use opensuse-${OS_VER_NO}.repo instead of opensuse.repo
  [ -f $yum_conf_dir_path/opensuse.repo ] && mv -fv $yum_conf_dir_path/opensuse.repo $yum_conf_dir_path/opensuse.repo.drblsave
  [ -f $yum_conf_dir_path/opensuse-nonoss.repo ] && mv -fv $yum_conf_dir_path/opensuse-nonoss.repo $yum_conf_dir_path/opensuse-nonoss.repo.drblsave
  if [ ! -f $yum_conf_dir_path/drbl.repo -o ! -f $yum_conf_dir_path/opensuse-${OS_VER_NO}.repo ]; then
    echo "Try to backup the original yum repository settings if they exist..."
    # we won't touch the existing one, just copied by drbl.
    repo_lists="$drbl_yum_repo_list opensuse-${OS_VER_NO}.repo opensuse-updates-${OS_VER_NO}.repo"
    for ifile in $repo_lists; do
      [ -f "$yum_conf_dir_path/$ifile" ] && mv -v $yum_conf_dir_path/$ifile $yum_conf_dir_path/$ifile.drblsave
    done
    if [ ! -f $DRBL_SCRIPT_PATH/setup/yum-repos/opensuse-${OS_VER_NO}.repo ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "$DRBL_SCRIPT_PATH/setup/yum-repos/opensuse-${OS_VER_NO}.repo does NOT exist!"
      echo "Maybe this is NOT a supported version!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop"
      exit 1
    fi
    cp -f $DRBL_SCRIPT_PATH/setup/yum-repos/{drbl*.repo,opensuse*-${OS_VER_NO}.repo} $yum_conf_dir_path/
  fi
  # ask some RPM repository options, such as drbl-testing, drbl-unstable
  ask_and_parse_rpm_ayo_repo_options
  # turn on some branches if set
  if [ -n "$DRBL_STABLE" ]; then
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" $yum_conf_dir_path/drbl.repo
  else
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=0/gi" $yum_conf_dir_path/drbl.repo
  fi
  if [ -n "$DRBL_TEST" ]; then
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" $yum_conf_dir_path/drbl-testing.repo
  else
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=0/gi" $yum_conf_dir_path/drbl-testing.repo
  fi
  if [ -n "$DRBL_UNSTABLE" ]; then
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" $yum_conf_dir_path/drbl-unstable.repo
  else
    perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=0/gi" $yum_conf_dir_path/drbl-unstable.repo
  fi
fi

# Setup zypper respository
if [ "$installer" = "zypper" ]; then
  # Ask some RPM repository options, such as drbl-testing, drbl-unstable
  ask_and_parse_rpm_ayo_repo_options

  echo "$msg_delimiter_star_line"
  # Remove DRBL branches first.
  echo "Removing existing drbl repository if exists..."
  for i in drbl drbl-testing drbl-unstable; do
    if [ -n "$(LANG=C zypper lr | awk -F "|" '{print $2}' | grep -iEw "$i[[:space:]]")" ]; then
      zypper rr $i
    fi
  done
  # Add drbl branches
  zypper ar $rpm_md_conf_dir_path/drbl.repo
  [ -n "$DRBL_TEST" ] && zypper ar $rpm_md_conf_dir_path/drbl-testing.repo
  [ -n "$DRBL_UNSTABLE" ] && zypper ar $rpm_md_conf_dir_path/drbl-unstable.repo
  zypper refresh
  echo "Zypper reopsitory:"
  echo "$msg_delimiter_star_line"
  zypper lr
  echo "$msg_delimiter_star_line"
fi

# ask some setting for client, such as SMP, serial console...
ask_and_parse_client_extra_setting

# parse the setting of client archi.
parse_client_archi_setting

# It's important to remove the cache of apt, otherwise 
# the RPM::Architecture will not take effect. check this:
# http://distro2.conectiva.com.br/pipermail/apt-rpm/2003-November/002015.html
# ... RPM::Architecture, is what he's looking for. His problem was that the cache was built before he set the architecture. Doing "rm -f /var/cache/apt/*.bin; apt-get update" would make it work.
if [ "$installer" = "apt" ]; then
  echo "$msg_clean_apt_cache"
  $inst_prog clean
  for ifile in /var/cache/apt/*.bin; do
     rm -f /var/cache/apt/$ifile
  done
fi

# If user specifies the client CPU arch is i586, we must put the setting in
# apt so that different arch program will NOT be upgraded.
if [ "$installer" = "apt" ]; then
  if [ "$client_archi_set" = "i586" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "Setting the \"Architecture "i586"\" in apt config file /etc/apt/apt.conf.d/drbl-arch.conf."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    cat <<-APTCFG_END > /etc/apt/apt.conf.d/drbl-arch.conf
// necessary for DRBL client
RPM
{
   Architecture "i586";
}
APTCFG_END
  fi
fi

# Update the apt data with repository.
# if it's yum, skip this, later yum will do it. Because yum update will also
# do upgrade...
[ "$installer" = "apt" ] && $inst_prog update

# 2.
# check if apt is integrity... Ex: original MDK 9.2 is buggy...
if [ "$installer" = "apt" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_check_apt_integrity"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  check_apt_integrity
fi

echo "$msg_delimiter_star_line"
# 3.
# We will upgrade system first.
# Try to find if the i386 glibc and openssl are installed or not
# If they are installed, hold them not to be upgraded to i686
if [ -z "$upgrade_system_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_upgrade_system_question"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read upgrade_system_answer
fi

case "$upgrade_system_answer" in 
   y|Y|[yY][eE][sS])
      [ "$installer" = "apt" ] && $inst_prog $inst_opt dist-upgrade
      [ "$installer" = "yum" ] && yum upgrade
      [ "$installer" = "zypper" ] && zypper update
      ;;
esac

# 4.
# lftp & curl are necessary here, we install it first
if ! rpm -q --quiet lftp || ! rpm -q --quiet curl || ! rpm -q --quiet wget ; then
  echo "lftp and curl are necessary so that we can find some program in apt or yum repository. Install them now if not installed..."
  # we need lftp to parse the repo setting if it's in ftp://
  if [ "$ARCH" = "x86_64" -a "$installer" = "yum" ]; then
    lftp_curl_etc="lftp.x86_64 curl.x86_64 wget.x86_64"
  else
    lftp_curl_etc="lftp curl wget"
  fi
  $inst_prog $inst_opt install $lftp_curl_etc
fi
# After the dist upgrade, check the ARCH for glibc, db... which are different 
# from i586 and are found in ftp://ftp.twaren.net/Linux/SuSE/i386/9.3/suse/i686
for ipkg in $i686_pkg_check_list_SUSE; do
  # i686_pkg_check_list_SUSE is: glibc glibc-devel db db-devel...
  # since "-" can not ba a variable name, replace it as "_"
  ipkg_arch_var="$(echo $ipkg | sed -e "s/-/_/g")"
  rpm -q --qf '%{ARCH}' $ipkg > /dev/null && eval ${ipkg_arch_var}_ARCH="$(rpm -q --qf '%{ARCH}' $ipkg)"
done
# We need to get url_os os_ayo_path url_os_coreroot if it's yum.
if [ "$installer" = "yum" ]; then
  url_tmp="$(find-url-in-yum-set $yum_conf_dir_path/opensuse-${OS_VER_NO}.repo $OS_VER_NO $ARCH 2>/dev/null)"
  if [ -n "$url_tmp" ]; then
    url_os="$(echo $url_tmp | awk -F' ' '{print $1}')"
    # We have to remove the last "/" in the path,
    # Ex: fedora/linux/releases/ -> fedora/linux/releases
    # otherwise for example, this command will have problem:
    # list_available_rpm http://free.nchc.org.tw/fedora/linux/releases//../updates/7/i386
    # The place "//../" is the key problem. It should be
    # list_available_rpm http://free.nchc.org.tw/fedora/linux/releases/../updates/7/i386
    os_ayo_path="$(echo $url_tmp | awk -F' ' '{print $2}' | sed -e "s|/$||g")"
    url_os_coreroot=$url_os/$os_ayo_path
    echo "$msg_delimiter_star_line"
    echo "Use yum repository in $url_os_coreroot"
    echo "$msg_delimiter_star_line"
  fi
  url_update_tmp="$(find-url-in-yum-set $yum_conf_dir_path/opensuse-updates-${OS_VER_NO}.repo $OS_VER_NO $ARCH 2>/dev/null)"
  if [ -n "$url_update_tmp" ]; then
    # Ex:
    # url_updates_root will be like:
    # http://opensource.nchc.org.tw/suse/SuSE/update
    # Then the updates RPM will be in http://opensource.nchc.org.tw/suse/SuSE/update/10.0/rpm/i586/
    url_updates="$(echo $url_update_tmp | awk -F' ' '{print $1}')"
    # We have to remove the last "/" in the path,
    # Ex: fedora/linux/releases/ -> fedora/linux/releases
    # otherwise for example, this command will have problem:
    # list_available_rpm http://free.nchc.org.tw/fedora/linux/releases//../updates/7/i386
    # The place "//../" is the key problem. It should be
    # list_available_rpm http://free.nchc.org.tw/fedora/linux/releases/../updates/7/i386
    updates_ayo_path="$(echo $url_update_tmp | awk -F' ' '{print $2}' | sed -e "s|/$||g")"
    url_updates_root=$url_updates/$updates_ayo_path
    echo "$msg_delimiter_star_line"
    echo "Use yum updates repository in $url_updates_root"
    echo "$msg_delimiter_star_line"
  fi
elif [ "$installer" = "zypper" ]; then
  echo "$msg_delimiter_star_line"
  # We have to export the zypper repository to rpm-md format.
  opensuse_rpm_md_repo="$(mktemp /tmp/rpm_md_repo.XXXXXX)"
  # //NOTE// The output file is ${opensuse_rpm_md_repo}.repo
  LC_ALL=C zypper lr -e $opensuse_rpm_md_repo
  # Modify the output rpm md repo file, since some blocks might come with "space" # E.g.
  # [openSUSE 11.1-0] <--- replace " " with "-" so that we can use find-url-in-rpm-md-set to parse the url. find-url-in-rpm-md-set can only work with the block name without "space"
  # name=openSUSE 11.1-0
  # enabled=1
  # autorefresh=1
  # baseurl=http://free.nchc.org.tw/opensuse/distribution/11.1/repo/oss
  # path=/
  # type=yast2
  # keeppackages=0
  lines="$(grep -E -n "^\[.*\]" ${opensuse_rpm_md_repo}.repo | cut -d":" -f1)"
  for i in $lines; do
   begin_line="$i"
   end_line="$i"
   sub_cmd="if ($begin_line..$end_line) {s/ /-/g}"
   LC_ALL=C perl -pi -e "$sub_cmd" ${opensuse_rpm_md_repo}.repo
  done
  
  # Find the possible blocks
  # We will get something like:
  # [drbl]
  # [drbl-testing]
  # [drbl-unstable]
  # [openSUSE-11.1-0]
  # [openSUSE-11.1-Non-Oss]
  # [openSUSE-11.1-Oss]
  # [repo-debug]
  # [repo-source]
  # [repo-update]
  
  possible_blks="$(LC_ALL=C grep -E "^\[.*\]" ${opensuse_rpm_md_repo}.repo | sed -e "s/\[//g" -e "s/\]//g")"

  # The export file is ${opensuse_rpm_md_repo}.repo
  # (1) Find release repo
  for ib in $possible_blks; do
    # skip drbl-related, source, debug repos
    [ -n "$(echo $ib | grep -iE "(drbl|debug|source|update)")" ] && continue
    url_tmp="$(find-url-in-rpm-md-set ${opensuse_rpm_md_repo}.repo $ib $OS_VER_NO 2>/dev/null)"
    # Sometimes maybe the update list is shown in the higer priority, so we might get this:
    # ========================
    # [http-download.opensuse.org-adb379f6]
    # name=Updates for openSUSE 11.2-0
    # enabled=1
    # autorefresh=1
    # baseurl=http://download.opensuse.org/update/11.2/
    # path=/
    # type=rpm-md
    # keeppackages=0
    # ========================
    # In this case, we will get:
    # url_tmp='http://download.opensuse.org update'
    # we should skip this one, and find the next one.
    [ -n "$(echo $url_tmp | grep -Ew "update")" ] && continue 
    if [ -n "$url_tmp" ]; then
      url_os="$(echo $url_tmp | awk -F' ' '{print $1}')"
      # We have to remove the last "/" in the path,
      # Ex: fedora/linux/releases/ -> fedora/linux/releases
      # otherwise for example, this command will have problem:
      # list_available_rpm http://free.nchc.org.tw/fedora/linux/releases//../updates/7/i386
      # The place "//../" is the key problem. It should be
      # list_available_rpm http://free.nchc.org.tw/fedora/linux/releases/../updates/7/i386
      os_ayo_path="$(LC_ALL=C echo $url_tmp | awk -F' ' '{print $2}' | sed -e "s|/$||g")"
      url_os_coreroot=$url_os/$os_ayo_path
      echo "$msg_delimiter_star_line"
      echo "Use zypper repository in $url_os_coreroot"
      echo "$msg_delimiter_star_line"
      break
    fi
  done
  # (2) Find update repo
  for ib in $possible_blks; do
    url_update_tmp="$(find-url-in-rpm-md-set ${opensuse_rpm_md_repo}.repo $ib $OS_VER_NO 2>/dev/null)"
    # Only update-related, e.g.
    # ========================
    # [http-download.opensuse.org-adb379f6]
    # name=Updates for openSUSE 11.2-0
    # enabled=1
    # autorefresh=1
    # baseurl=http://download.opensuse.org/update/11.2/
    # path=/
    # type=rpm-md
    # keeppackages=0
    # ========================
    # In this case, we will get:
    # url_tmp='http://download.opensuse.org update'
    [ -z "$(echo $url_update_tmp | grep -Ew "update")" ] && continue 
    if [ -n "$url_update_tmp" ]; then
      # Ex:
      # url_updates_root will be like:
      # http://opensource.nchc.org.tw/suse/SuSE/update
      # Then the updates RPM will be in http://opensource.nchc.org.tw/suse/SuSE/update/10.0/rpm/i586/
      url_updates="$(echo $url_update_tmp | awk -F' ' '{print $1}')"
      # We have to remove the last "/" in the path,
      # Ex: fedora/linux/releases/ -> fedora/linux/releases
      # otherwise for example, this command will have problem:
      # list_available_rpm http://free.nchc.org.tw/fedora/linux/releases//../updates/7/i386
      # The place "//../" is the key problem. It should be
      # list_available_rpm http://free.nchc.org.tw/fedora/linux/releases/../updates/7/i386
      updates_ayo_path="$(LC_ALL=C echo $url_update_tmp | awk -F' ' '{print $2}' | sed -e "s|/$||g")"
      url_updates_root=$url_updates/$updates_ayo_path
      echo "$msg_delimiter_star_line"
      echo "Use zypper updates repository in $url_updates_root"
      echo "$msg_delimiter_star_line"
      break
    fi
  done
  [ -e "$opensuse_rpm_md_repo" ] && rm -f $opensuse_rpm_md_repo
  [ -e "${opensuse_rpm_md_repo}.repo" ] && rm -f ${opensuse_rpm_md_repo}.repo
fi

if [ -z "$url_os_coreroot" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "SuSE RPM release repository NOT found!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "$msg_press_enter_to_continue"
  read 
fi
if [ -z "$url_updates_root" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "SuSE RPM updates repository NOT found!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "$msg_press_enter_to_continue"
  read 
fi

install_client_archi_glibc_etc_SUSE

# 5. prepare the kernel for clients.
echo "$msg_delimiter_star_line"
echo "$msg_prepare_kernel_for_clients"
prepare_install_client_kernel_in_SUSE

# 6.
# wget maybe not installed, we will check it is installed only when apt is not installed, so if apt/yum is installed while wget is not installed, get_inst_rpm() will failed! Add wget here.
# Note, for SUSE, nfs-utils is kernel nfs server, nfs-server is user space.
# $PKG_FROM_SUSE, $PKG_TO_QUERY and $PKG_FROM_DRBL are loaded from conf/drbl.conf
echo "$msg_delimiter_star_line"
echo "Searching if $PKG_TO_QUERY available... "
case "$installer" in
  zypper)
     for ipkg in $PKG_TO_QUERY; do
       if [ -n "$(LC_ALL=C zypper search $ipkg 2>/dev/null | grep -iEw "$ipkg")" ]; then
         echo "Package $ipkg exists in repository."
         PKG_FROM_SUSE="$PKG_FROM_SUSE $ipkg"
       fi
     done
     ;;
  yum)
     for ipkg in $PKG_TO_QUERY; do
       if [ -n "$(yum list $ipkg 2>/dev/null | grep -E "^$ipkg")" ]; then
         echo "Package $ipkg exists in repository."
         PKG_FROM_SUSE="$PKG_FROM_SUSE $ipkg"
       fi
     done
     ;;
  apt)
     for ipkg in $PKG_TO_QUERY; do
       if [ -n "$(apt-cache pkgnames $ipkg 2>/dev/null | grep -i "^${ipkg}$")" ]; then
         echo "Package $ipkg exists in repository."
         PKG_FROM_SUSE="$PKG_FROM_SUSE $ipkg"
       fi
     done
     ;;
esac

# Since the repodata in yum repository of opensuse include all the arch, they do not separate them, so we have to put the specific arch, especially for x86_64. Otherwise the i586 will be always the first priority, that will case the problem:
# http://sourceforge.net/mailarchive/forum.php?thread_id=9549930&forum_id=41969
if [ "$ARCH" = "x86_64" -a "$installer" = "yum" ]; then
    PKG_TO_INSTALL=""
    for ipkgs in $PKG_FROM_SUSE; do
      PKG_TO_INSTALL="$PKG_TO_INSTALL ${ipkgs}.x86_64"
    done
else
    PKG_TO_INSTALL="$PKG_FROM_SUSE"
fi
$inst_prog $inst_opt install $PKG_TO_INSTALL $PKG_FROM_DRBL

# TODO
RETVAL=$?
check_apt_install $RETVAL

# 7.
# Creat dev tarball in $drbl_pkgdir,
# which will be copied to /tftpboot/node_root/etc/diskless-image 
# in deploy script
[ ! -d $drbl_pkgdir ] && mkdir -p $drbl_pkgdir

echo "$msg_delimiter_star_line"
# get dev
echo "The DRBL client will use udev..."
# we must install udev...
if ! rpm -q --quiet udev; then
  echo -n "Try to install udev..."
  $inst_prog $inst_opt install udev
  echo "done!"
fi
if ! rpm -q --quiet udev; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "No udev, and no pre-packaged devices file!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

# 8.
# netinstall
if [ "$DRBL_NETINSTALL" = "yes" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_install_net_install_images"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_delimiter_star_line"
  drbl-netinstall -i all -s -v
fi

# 9.
echo "$msg_delimiter_star_line"
run_drblsrv_offline

} # end of install_drbl_SUSE

get_url_ocs_then_install_curl_etc_via_yum_if_necessary() {
  # we need $url_os, "$os_ayo_path"and "$url_os_coreroot for kernel and dev
  # try to parse the yum config in the system
  # We will search CentOS-base.repo, $FC_CORE_REPO in /etc/yum.repos.d/
  # TODO: maybe search all the *.repo in /etc/yum.repos.d/ ?
  echo "$msg_delimiter_star_line"
  echo "Try to find yum repository url..."
  for i in CentOS-Base fedora-core fedora; do
    if [ -f "/etc/yum.repos.d/${i}.repo" ]; then
      if ! rpm -q --quiet curl || ! rpm -q --quiet lftp || ! rpm -q --quiet wget; then
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	echo "$msg_need_curl_etc_to_parse_config"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
	# we need lftp to parse the yum repo config if the repo is in ftp://
	$installer -y install lftp curl wget
      fi
      # Once we have curl, we can get url_os, os_ayo_path...
      if [ "$i" = "CentOS-Base" ]; then
        # CentOS 4.3 uses the new method to get the mirrorlist system.
	# Ref: http://lists.centos.org/pipermail/centos/2006-April/063312.html
	# We can only query releasever=4 for 4.3
	# Since earlier version 4.2/4.1 is no more, forget about them.
	# Actually, for CentOS, the latest version in 4.x is linked to 4, so use 4 is the latest release.
        #QUERY_OS_VER="$(echo ${OS_Version:2} | sed -e "s/\.[[:digit:]]*//g")"
	# Note by Steven Shiau on 2006/10/10, now we should query releasever=4.4 for 4.4, then this will be consistent with find-url-in-yum-set. 
	# Now with ?release=4
	# curl 'http://mirrorlist.centos.org/?release=4&arch=i386&repo=os' 
	# We got:
	# http://ftp2.tnc.edu.tw/pub1/centos/4.4/os/i386/
	# http://ftp.cs.pu.edu.tw/Linux/CentOS/4.4/os/i386/
	# It's not consistent, then "find-url-in-yum-set /etc/yum.repos.d/CentOS-Base.repo 4 i386" will have problem, it will give "http://ftp2.tnc.edu.tw pub1/centos/4." (extra "." in the end, since we are replacing "4" (release=4 is what we assign), not 4.4)
        QUERY_OS_VER="${OS_Version:2}"
      else
        QUERY_OS_VER="${OS_Version:2}"
      fi
      url_tmp="$(find-url-in-yum-set /etc/yum.repos.d/${i}.repo $QUERY_OS_VER $ARCH 2>/dev/null)"
      if [ -n "$url_tmp" ]; then
        url_os="$(echo $url_tmp | awk -F' ' '{print $1}')"
	# We have to remove the last "/" in the path,
	# Ex: fedora/linux/releases/ -> fedora/linux/releases
	# otherwise for example, this command will have problem:
	# list_available_rpm http://free.nchc.org.tw/fedora/linux/releases//../updates/7/i386
	# The place "//../" is the key problem. It should be
	# list_available_rpm http://free.nchc.org.tw/fedora/linux/releases/../updates/7/i386
        os_ayo_path="$(echo $url_tmp | awk -F' ' '{print $2}' | sed -e "s|/$||g")"
        url_os_coreroot=$url_os/$os_ayo_path
        echo "$msg_delimiter_star_line"
        echo "Use yum repository in $url_os_coreroot"
        check_if_repository_in_blacklist $url_os
	break
      fi
    fi
  done
} # end of get_url_ocs_then_install_curl_etc_via_yum_if_necessary
#
install_curl_etc_via_apt_if_necessary() {
  # we need $url_os, "$os_ayo_path"and "$url_os_coreroot for kernel and dev
  echo "$msg_delimiter_star_line"
  if ! rpm -q --quiet curl || ! rpm -q --quiet lftp || ! rpm -q --quiet wget; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "$msg_need_curl_etc_to_parse_config"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    apt-get update
    # we need lftp to parse the repo config if the repo is in ftp://
    # wget is already installed when setup apt, but who cares, add it in case
    apt-get -y install lftp curl wget
  fi
} # end of install_curl_etc_via_apt_if_necessary

#
prepare_install_client_kernel_in_SUSE() {
# Prepare to install the kernel of clients in SuSE
#
local pkg_name pkg_ver pkg_2_be_inst_for_cli suse_rpm_list_tmp pkg_2_be_inst_for_cli_ver
if [ "$client_archi_set" = "best_optimization" ]; then
  # same CPU archi with server,  maybe for i686, or i386 (for AMD K7 ?)
  [ -z "$KARCH" ] && KARCH="$(uname -m)"
  KARCH="$(drbl-check-kernel-cpu-arch `uname -r`)"
  # KARCH will be nothing if we can not find the kernel config file, so we choose to use the same arch of server for client.
  [ -z "$KARCH" ] && KARCH="$(uname -m)"
  # convert the arch name from server to client, 
  # Note:
  # "KARCH_RPM_URL_PATH" is for RPM repository PATH use only, like 
  # http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/inst-source/suse/i586/
  # of
  # http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/inst-source/suse/x86_64/
  case "$KARCH" in
  i[0-9]86)
     KARCH="i686"
     # skip the leading "i"
     client_archi=${KARCH:1}
     KARCH_RPM_URL_PATH="i586"
     ;;
  x86_64)
     KARCH="x86_64"
     client_archi=${KARCH}
     KARCH_RPM_URL_PATH="x86_64"
     ;;
  *)
     KARCH="i686"
     # skip the leading "i"
     client_archi=${KARCH:1}
     KARCH_RPM_URL_PATH="i586"
     ;;
  esac
else
  # For SuSE, only i586 is available we decided. Maybe in the future, we 
  # will add K7 ?
  KARCH="i586"
  # skip the leading "i"
  client_archi=${KARCH:1}
  KARCH_RPM_URL_PATH="i586"
fi
cpu_filter="grep -i $client_archi"
if [ -n "$SMP_OPTION" ]; then
  # enable SMP filter
  smp_filter="grep -i smp"
  kernel_img_name="kernel-smp"
else
  # Before OpenSuSE 11.1, only one kernel package (e.g. kernel-default-2.6.25.5-1.1.i586.rpm or kernel-pae-2.6.25.5-1.1.i586.rpm).
  # From OpenSuSE 11.1 or later, we need more kernel packages, e.g.:
  # -----------
  # kernel-default-2.6.27.7-9.1.i586.rpm (modules)
  # kernel-default-base-2.6.27.7-9.1.i586.rpm (vmlinuz and base modules)
  # kernel-default-extra-2.6.27.7-9.1.i586.rpm (old modules ?)
  # -----------
  # Or
  # PAE (Physical Address Extension) kernel (PAE kernel is available from OpenSuSE 11.0 or later):
  # -----------
  # kernel-pae-2.6.27.7-9.1.i586.rpm
  # kernel-pae-base-2.6.27.7-9.1.i586.rpm
  # kernel-pae-extra-2.6.27.7-9.1.i586.rpm          
  # -----------
  # "NOT SMP" filter
  smp_filter="grep -iv smp"
  # Here we test on the server to see if kernel-pae is available. If yes,  use it.
  for i in kernel-pae kernel-default; do
    if rpm -q $i &>/dev/null; then
      kernel_img_name="$i"
      break
    fi
  done
  # If we can not find the name we want, use the default one "kernel-default"
  [ -z "$kernel_img_name" ] && kernel_img_name="kernel-default"
fi
# Since there is only i586 kernel for SuSE, we do not have to filter KARCH here
# latest_drbl_kernel_in_rep is (1) package name with .i586.rpm, like: kernel-default-2.6.16.21-0.13.i586.rpm when the installer is yum (2) name without i586.rpm, like kernel-default-2.6.16.21-0.13 when the installer is apt

# Flag to download kernel-pae-base or kernel-default-base:
download_suse_kernel_base="no"
drblsrv_offline_extra_opt=""
if rpm -q ${kernel_img_name}-base &>/dev/null; then
  echo "Kernel package ${kernel_img_name}-base exists..."
  download_suse_kernel_base="yes"
fi
suse_rpm_list_tmp="$(mktemp /tmp/suse_rpm_list.XXXXXX)"
if [ "$installer" = "apt" ]; then
  latest_drbl_kernel_in_rep="$(apt-cache pkgnames $kernel_img_name | grep -E "\-default$" | pkg-ver-latest)"
elif [ "$installer" = "yum" -o "$installer" = "zypper" ]; then
  # Find the latest kernel in updates first (like http://opensource.nchc.org.tw/suse/SuSE/update/10.0/rpm/i586/)
  list_available_rpm $url_updates_root/$OS_VER_NO/rpm/$KARCH_RPM_URL_PATH/ > $suse_rpm_list_tmp
  latest_drbl_kernel_in_rep="$(cat $suse_rpm_list_tmp | grep -E "${kernel_img_name}-[0-9]+" | pkg-ver-latest)"
  if [ -n "$latest_drbl_kernel_in_rep" ]; then
    # Keep the url, we will use it to download
    suse_latest_kernel_url="$url_updates_root/$OS_VER_NO/rpm/$KARCH_RPM_URL_PATH/$latest_drbl_kernel_in_rep"
  # elif [ -z "$latest_drbl_kernel_in_rep" ]; then
  else
    list_available_rpm $url_os_coreroot/$OS_PATH_IN_YUM_REPO/$REPO_PATH/suse/$KARCH_RPM_URL_PATH/ > $suse_rpm_list_tmp
    # If we can not find it in updates, use the one in release (like http://opensource.nchc.org.tw/opensuse/distribution/SL-10.0-OSS/inst-source/suse/i586/)
    latest_drbl_kernel_in_rep="$(cat $suse_rpm_list_tmp | grep -E "${kernel_img_name}-[0-9]+" | pkg-ver-latest)"
    suse_latest_kernel_url="$url_os_coreroot/$OS_PATH_IN_YUM_REPO/$REPO_PATH/suse/$KARCH_RPM_URL_PATH/$latest_drbl_kernel_in_rep"
  fi
fi
suse_latest_kernel_base_url=""
if [ "$download_suse_kernel_base" = "yes" ]; then
  # For kernel like "kernel-default-base-2.6.27.7-9.1.i586.rpm"
  suse_latest_kernel_base_url="$(echo $suse_latest_kernel_url | sed -e "s|$kernel_img_name|${kernel_img_name}-base|g")"
fi

echo "$msg_latest_kernel_in_ayo $latest_drbl_kernel_in_rep"

# Another available kernel maybe is from server, check it:
running_karch="$(drbl-check-kernel-cpu-arch `uname -r`)"
running_kernel_in_server="$(echo $(uname -r) $running_karch | $cpu_filter | $smp_filter)"

use_kernel_from=
if [ -n "$running_kernel_in_server" ]; then
  # Server running kernel matches client's requirement
  if [ -z "$chosen_client_kernel_from" ]; then
    echo "$msg_2_kernel_availables"
    echo "[1]: kernel $running_kernel_in_server ($msg_from_this_drbl_server)"
    echo "[2]: $latest_drbl_kernel_in_rep ($msg_from_ayo_repository)"
    echo -n "[1] "
    read chosen_client_kernel_from
  fi
  case "$chosen_client_kernel_from" in
    2)
      echo "Clients will use the kernel $latest_drbl_kernel_in_rep from ayo repository."
      use_kernel_from="ayo-repository"
      ;;
    *)
      echo "Clients will use the kernel $running_kernel_in_server from server."
      use_kernel_from="local"
      ;;
  esac
else
  # Use the kernel in the repository
  echo "Use the kernel $latest_drbl_kernel_in_rep from ayo repository!"
  use_kernel_from="ayo-repository"
fi

case "$use_kernel_from" in
  "ayo-repository")
    case "$installer" in
     apt)
      # use with client_kernel_install_mode and client_kernel_tmp_dir
      client_kernel_install_mode="from_downloaded_pkg"

      # Before download it, clean the kernel-image in the cache dir so that we can fin
      # the download one by ls.
      rm -f /var/cache/apt/archives/${latest_drbl_kernel_in_rep}*.rpm 2>/dev/null
      
      # download it
      $inst_prog $inst_opt --reinstall --download-only install $latest_drbl_kernel_in_rep
      RC=$?
      if [ $RC -gt 0 ]; then
        [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
        echo "Failed to download $latest_drbl_kernel_in_rep"
        echo "Maybe you should check /etc/apt/sources.list so that "apt-cache pkgnames $kernel_img_name" can get the downloadable kernel! Then try to run this program again."
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        echo "$msg_program_stop"
        exit 1
      fi
      
      # The "_" is important, since the file names in the cache dir are like
      # kernel-default_2.6.11.4-21.7_i586.rpm
      if [ -n "$(ls /var/cache/apt/archives/${latest_drbl_kernel_in_rep}_*.rpm 2>/dev/null)" ]; then
        # Note! Do not put / in the end, we will use it to compare something.
	client_kernel_tmp_dir="/var/cache/apt/archives"
      fi
      ;;
     yum|zypper)
      # use with client_kernel_install_mode and client_kernel_tmp_dir
      client_kernel_install_mode="from_downloaded_pkg"
      client_kernel_tmp_dir=`mktemp -d /tmp/SuSE_RPM.XXXXXXX`
      echo "Downloading the kernel from $suse_latest_kernel_url..."
      wget --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$client_kernel_tmp_dir" $suse_latest_kernel_url
      if [ -n "$suse_latest_kernel_base_url" ]; then
        echo "Downloading the ${kernel_img_name}-base rpm from $suse_latest_kernel_base_url..."
        wget --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$client_kernel_tmp_dir" $suse_latest_kernel_base_url
        if [ -n "$(ls "$client_kernel_tmp_dir/$(basename $suse_latest_kernel_base_url)" 2>/dev/null)" ]; then
          drblsrv_offline_extra_opt="$drblsrv_offline_extra_opt -e $client_kernel_tmp_dir/$(basename $suse_latest_kernel_base_url)"
        fi
      fi
      # For OpenSuSE 11.2 or later, some kernel packages are not inside kernel package, they are separate, like:
      # compcache-kmp-default-0.5.3_2.6.31.5_0.1-4.4.i586.rpm
      # compcache-kmp-desktop-0.5.3_2.6.31.5_0.1-4.4.i586.rpm
      # preload-kmp-default-1.1_2.6.31.5_0.1-6.8.1.i586.rpm
      # preload-kmp-desktop-1.1_2.6.31.5_0.1-6.8.1.i586.rpm
      # So if the client kernel is either:
      # kernel-default-2.6.31.5-0.1.1.i586.rpm
      # or
      # kernel-desktop-2.6.31.5-0.1.1.i586.rpm
      # We have to find it's "default" or "desktop"
      suse_client_kernel_tag="$(echo ${kernel_img_name} | grep -Eo "(desktop|default)")"
      # First we find those installed on the server.
      install_kmp_flag=""
      pkg_2_be_inst_for_cli_url=""
      kmp_list="$(LC_ALL=C rpm -qa | grep -E "\-kmp-")"
      suse_latest_kernel_url_path="$(dirname $suse_latest_kernel_url)"
      for ipkg in $kmp_list; do
        # E.g: package: preload-kmp-desktop-1.1_2.6.31.5_0.1-6.8.1.i586
        # pkg_name=preload-kmp-desktop
        # For "pkg_ver" we have to use the version number same with kernel of client
        pkg_name="$(LC_ALL=C rpm -q --qf '%{NAME}\n' $ipkg)"
	echo "Found $pkg_name installed on the server, will try to install it with client's kernel version for clients later..."       
        # The version should be consistent with kernel
        pkg_ver="$(LC_ALL=C rpm -qp --qf '%{VERSION}\n' $client_kernel_tmp_dir/$(basename $suse_latest_kernel_url))"
        pkg_2_be_inst_for_cli="$(echo $pkg_name | sed -r -e "s/(-desktop|-default)/-${suse_client_kernel_tag}/g")"
        if [ -n "$pkg_2_be_inst_for_cli" ]; then 
          # Find the kernel module package on the repository...
          pkg_2_be_inst_for_cli_ver="$(cat $suse_rpm_list_tmp | grep -E "${pkg_2_be_inst_for_cli}" | grep -E "${pkg_ver}" | pkg-ver-latest)"
          if [ -n "$pkg_2_be_inst_for_cli_ver" ]; then
            pkg_2_be_inst_for_cli_url="$pkg_2_be_inst_for_cli_url $(dirname $suse_latest_kernel_url)/$pkg_2_be_inst_for_cli_ver"
            install_kmp_flag="yes"
          fi
        fi
      done
      if [ "$install_kmp_flag" = "yes" ]; then
       for i in $pkg_2_be_inst_for_cli_url; do
        echo "Downloading the kernel module package $i .."
        wget --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$client_kernel_tmp_dir" $i
        if [ -n "$(ls "$client_kernel_tmp_dir/$(basename $i)" 2>/dev/null)" ]; then
          drblsrv_offline_extra_opt="$drblsrv_offline_extra_opt -e $client_kernel_tmp_dir/$(basename $i)"
        else
          echo "$i does not exist. Will skip installing it for clients."
        fi
       done
      fi
      ;;
    esac  
    ;;
  "local")
    # kernel for client already exists in /tftpboot/node_root.
    client_kernel_install_mode="from_server"
    client_kernel_ver="$(uname -r)"
    ;;
esac
[ -e "$suse_rpm_list_tmp" ] && rm -f $suse_rpm_list_tmp

} # end of prepare_install_client_kernel_in_SUSE

#
Usage() {
  echo "Usage:"
  echo "$0 [Option]"
  echo "Option:"
  echo "-f, --force-yes:  force yes, only for Debian-like distribution. It  should  not  be used except in very special situations. Using force-yes can potentially destroy your system!" 
  echo "-i, --install:    install DRBL." 
  echo "-u, --uninstall:  uninstall DRBL." 
  echo "-v, --verbose:    verbose mode."
  echo "-t, --testing [y/n]:    use packages in testing branch or not."
  echo "-a, --unstable [y/n]:   use packages in unstable branch or not."
  echo "-n, --netinstall [y/n]: install the network installation program or not."
  echo "-m, --smp-client [y/n]: use SMP kernel for DRBL clients or not."
  echo "-x, --set-proxy [y/n]:  set proxy or not."
  echo "-c, --console-output [y/n]: set console output for client or not."
  echo "-g, --upgrade_system [y/n]: upgrade system or not."
  echo "-k, --client_archi [0/1/2]: set the client's CPU arch, 0 for i386, 1 for i586, 2 for same level with DRBL server (for x86_64, use 2)."
  echo "-o, --client_kernel_from [1/2]: choose client's kernel image from, 1 from DRBL server running kernel (if available), 2 from ayo repository."
  echo "-s, --skip-select-repository: skip the question for selecting repository."
  echo "-l, --language N  Set the language to be shown, where N:"
  echo "                  0: English,"
  echo "                  1: Traditional Chinese (Big5) - Taiwan,"
  echo "                  2: Traditional Chinese (UTF-8, Unicode) - Taiwan."
}

#############
### main ####
#############
check_if_root

# main
while [ $# -gt 0 ]; do
  case "$1" in
   -f|--force-yes)
        shift; apt_extra_opt="--force-yes"
	;;
   -l|--language)
	shift; specified_lang="$1"
	shift;;
   -i|--install)
        shift; mode="install"
	;;
   -u|--uninstall)
        shift; mode="uninstall"
	;;
   -t|--testing)
        shift; drbl_test_answer="$1"
	shift;;
   -a|--unstable)
        shift; drbl_unstable_answer="$1"
	shift;;
   -n|--netinstall)
        shift; drbl_netinstall_answer="$1"
	shift;;
   -m|--smp-client)
        shift; drbl_SMP_answer="$1"
	shift;;
   -s|--skip-select-repository)
        shift; select_repository="no"
	;;
   -x|--set-proxy)
        shift; set_proxy_answer="$1"
	shift;;
   -c|--console-output)
        shift; console_output_answer="$1"
	shift;;
   -g|--upgrade_system)
        shift; upgrade_system_answer="$1"
	shift;;
   -k|--client_archi)
        shift; client_archi_ans="$1"
	shift;;
   -o|--client_kernel_from)
        shift; chosen_client_kernel_from="$1"
	shift;;
   -y|--yum)
        shift; preferred_installer="yum"
	;;
   -d|--apt)
        shift; preferred_installer="apt"
	;;
   -p|--urpmi)
        shift; preferred_installer="urpmi"
	;;
   -v|--verbose)
	verbose="on"
	verbose_opt="-v"
        shift ;;
   *)
	Usage
	exit 1
  esac
done
     
# mode is essential
[ -z "$mode" ] && Usage && exit 1

#
ask_and_load_lang_set $specified_lang

#
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_hint_for_answer"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

echo "$msg_delimiter_star_line"
# get the distribution name and type: OS_Version and OS_type
check_distribution_name
echo "$msg_delimiter_star_line"

#
case "$OS_Version" in
   RH*)
      # For RH/FC, $client_archi_set will be decided by user or system.
      installer="apt"
      ARCH="i386"
      ;;
   FC*|CO*)
      # For RH/FC, $client_archi_set will be decided by user or system.
      # Yum is better is FC3 then that in FC2/FC1, we can use it as one of our
      # installer.
      case "$OS_Version" in
        FC[3-9]|FC[12][0123456789]|CO*)
         # TODO x86_64: CPU arch -> rpm package, which one is real ?
         # "uname -m" or "rpm -q --qf '%{arch\n}' kernel..."
         if [ "$(uname -m)" = "x86_64" ]; then
             ARCH="x86_64"
         else
             ARCH="i386"
         fi
	 #
	 if [ "$preferred_installer" = "apt" ]; then
           installer="apt"
         else
           installer="yum"
         fi
	 ;;
	*)
	 # we only support x86 version and use apt for FC2/FC1
         ARCH="i386"
         installer="apt"
	 ;;
      esac
      ;;
   MDK*|MDV*)
      # default installer is urpmi now, no more apt
      installer="urpmi"
      # For MDK, the arch is only i586 or x86_64
      if [ "$(uname -m)" = "x86_64" ]; then
          ARCH="x86_64"
      else
          ARCH="i586"
      fi
      ;;
   DBN*)
      # For Debian, $client_archi_set will be decided by user or system.
      installer="apt"
      if [ "$(uname -m)" = "x86_64" ]; then
          ARCH="amd64"
      else
          ARCH="i386"
      fi
      ;;
   SUSE*)
      #
      case "$OS_Version" in
        SUSE9.3)
          installer="apt"
	  ;;
	SUSE10.[0123]|SUSE11.0)
	  # For SuSE 10.0 or later, let yum be the default installer
          if [ "$preferred_installer" = "apt" ]; then
            installer="apt"
          else
            installer="yum"
          fi
	  ;;
	*) # From OpenSuSE 11.1, yum is not available any more. We use zypper.
          installer="zypper"
	  ;;
      esac

      if [ "$(uname -m)" = "x86_64" ]; then
          ARCH="x86_64"
      else
          ARCH="i386"
      fi
      # Get the version no, OS_Version is like "SUSE10.0", then OS_VER_NO=10.0
      OS_VER_NO=${OS_Version:4}
      # For OpenSuSE 10.0, the path in yum repo is like
      # http://opensource.nchc.org.tw/opensuse/distribution/SL-10.0-OSS/inst-source/suse/
      # For OpenSuSE 10.1, it's like (no -OSS):
      # http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/inst-source/suse/ 
      # For OpenSuSE 10.2, it's like (no SL-):
      # http://opensource.nchc.org.tw/opensuse/distribution/10.2/repo/oss/suse/i586/
      # Hope SuSE will keep this rule.
      # For SuSE 9,3 this variable $OS_PATH_IN_YUM_REPO is useless.
      case "$OS_VER_NO" in
        10.0)
            OS_PATH_IN_YUM_REPO="SL-${OS_VER_NO}-OSS"
	    REPO_PATH="inst-source"
	    ;;
        10.1)
            OS_PATH_IN_YUM_REPO="SL-${OS_VER_NO}"
	    REPO_PATH="inst-source"
	    ;;
        *)
            OS_PATH_IN_YUM_REPO="${OS_VER_NO}"
	    REPO_PATH="repo/oss"
	    ;;
      esac
      ;;
   *)
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "This version in this distribution is NOT supported by DRBL, maybe you can try to use the drbl in testing or unstable repository."
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "$msg_program_stop"
      exit 1 
esac

#
case "$installer" in
  "apt") inst_prog="apt-get" ;;
  "yum") inst_prog="yum" ;;
  "urpmi") inst_prog="urpmi" ;;
  "zypper") inst_prog="zypper" ;;
  *)
     [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
     echo "Unknown installer!"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     echo "$msg_program_stop"
     exit 1
     ;;
esac

# select_repository is for RH-like and CentOS repository selection.
# for CentOS, we force to make select_repository="no", we will not put centos 
# yum config in this file
# for some distributions, we have to force user select the repository
# for FC3-6, the question is open. For FC7 and later, make select_repository="no". i.e. let use handle the yum repository by himself. Otherwise it's too complicated, it's better to use the one in the system.
# in the future, maybe we will set all the distribution select_repository="no"
case "$OS_Version" in
     RH*|FC[1-2]|MD[KV]*) select_repository="yes" ;;
     FC[3456]) select_repository="" ;;
     *)   select_repository="no" ;; # for CO, FC7 or later...
esac

# do it
case "$mode" in
   install)
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	eval msg=\$msg_install_${OS_type}
	echo $msg
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
	install_drbl_${OS_type}
	;;
   uninstall)
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	eval msg=\$msg_uninstalling_DRBL_${OS_type}
	echo $msg
	echo "$msg_analyzing_rpm_info"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
	uninstall_drbl_${OS_type}
	;;
   *)
	Usage
	exit 1
esac