summaryrefslogtreecommitdiff
path: root/components/services/install/lib/eazel-install-logic2.c
blob: 7e4b3759e2e36d6b54b8cdc3733896d980bdeb01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* 
 * Copyright (C) 2000 Eazel, Inc
 *
 * 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 of the
 * License, 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.
 *
 * Authors: Eskil Heyn Olsen  <eskil@eazel.com>
 *          Robey Pointer <robey@eazel.com>
 */

#include "eazel-install-logic2.h"
#include "eazel-install-public.h"
#include "eazel-install-private.h"

#include <libtrilobite/trilobite-core-utils.h>
#include <libtrilobite/trilobite-md5-tools.h>

#include <libnautilus-extensions/nautilus-debug.h>

#ifndef EAZEL_INSTALL_NO_CORBA
#include <libtrilobite/libtrilobite.h>
#else
#include <libtrilobite/libtrilobite-service.h>
#include <libtrilobite/trilobite-root-helper.h>
#endif

/*
  0x1 enables post check_dependencies treedumps
  0x2 enables post dedupe tree dumps
  0x4 enables random spewing
  0x8 enables dumping the final tree
 */
#define EI2_DEBUG 0xFF

#define PATCH_FOR_SOFTCAT_BUG 1
#define MUST_HAVE PACKAGE_FILL_NO_DIRS_IN_PROVIDES
#define UPDATE_MUST_HAVE PACKAGE_FILL_NO_DEPENDENCIES|PACKAGE_FILL_NO_TEXT|PACKAGE_FILL_NO_DIRS_IN_PROVIDES
#define OWNS_MUST_HAVE PACKAGE_FILL_NO_DEPENDENCIES|PACKAGE_FILL_NO_TEXT|PACKAGE_FILL_NO_DIRS_IN_PROVIDES
#define MODIFY_MUST_HAVE PACKAGE_FILL_NO_DEPENDENCIES|PACKAGE_FILL_NO_TEXT|PACKAGE_FILL_NO_DIRS_IN_PROVIDES

enum {
	DEPENDENCY_OK = 1,
	DEPENDENCY_NOT_OK = 2
};

/***********************************************************************************/
/* 
   this is a debug thing that'll dump the memory addresses of all packages
   in trees, and thereby let you manually check whether the dedupe worked or not.
*/
#if EI2_DEBUG & 0x10
static void
dump_tree (GList *packages)
{
	char *out;
	char **lines;
	int i;

	out = packagedata_dump_tree (packages, 2);
	lines = g_strsplit (out, "\n", 0);
	for (i = 0; lines[i] != NULL; i++) {
		trilobite_debug ("%s", lines[i]);
	}
	g_strfreev (lines);
	g_free (out);
}
#endif


/***********************************************************************************/

/* This is code to check the md5 of downloaded packages */


gboolean
check_md5_on_files (EazelInstall *service, 
		    GList *packages)
{
	gboolean result = TRUE;
	GList *iterator;
	GList *flat_packages;

	result = eazel_install_lock_tmp_dir (service);

	if (!result) {
		g_warning (_("Failed to lock the downloaded file"));
		return FALSE;
	}

	flat_packages = flatten_packagedata_dependency_tree (packages);

	for (iterator = flat_packages; iterator; iterator = g_list_next (iterator)) {
		PackageData *pack = (PackageData*)iterator->data;
		
		if (pack->md5) {
			char pmd5[16];
			char md5[16];

			trilobite_md5_get_digest_from_file (pack->filename, md5);
			trilobite_md5_get_digest_from_md5_string (pack->md5, pmd5);

			if (memcmp (pmd5, md5, 16) != 0) {
				g_warning (_("MD5 mismatch, package %s may be compromised"), pack->name);
#if EI2_DEBUG & 0x4				
				/* get_readable_name is leaked */
				trilobite_debug ("read md5 from file %s", pack->filename);
				trilobite_debug ("for package %s", packagedata_get_readable_name (pack));
#endif
				eazel_install_emit_md5_check_failed (service, 
								     pack, 
								     trilobite_md5_get_string_from_md5_digest (md5));
				result = FALSE;
			} else {
#if EI2_DEBUG & 0x4				
				trilobite_debug ("md5 match on %s", pack->name);
#endif
			}
		} else {
			g_message (_("No MD5 available for %s"), pack->name);
		}
	}	

	g_list_free (flat_packages);
	eazel_install_unlock_tmp_dir (service);

	return result;
}

/***********************************************************************************/

/* This is code to removed failed trees from the tree */

void prune_failed_packages (EazelInstall *service, GList **packages);
void prune_failed_packages_helper (EazelInstall *service, PackageData *root, 
				   PackageData *pack, GList *packages, GList **path, GList **result);

typedef enum {
	PRUNE_ACTION_NORMAL,
	PRUNE_ACTION_ALLOW
} PruneAction;

void 
prune_failed_packages_helper (EazelInstall *service, 
			      PackageData *root, 
			      PackageData *pack, 
			      GList *packages,
			      GList **path,
			      GList **result)
{
	GList *iterator;
	PruneAction action = PRUNE_ACTION_NORMAL;

#if EI2_DEBUG & 0x4
	trilobite_debug ("entering subpruner %p %s %s", 
			 pack, pack->name, 
			 packagedata_status_enum_to_str (pack->status));
#endif
	/* If package is already installed, check if the service
	   settings requires us to fail it */
	if (pack->status == PACKAGE_ALREADY_INSTALLED) {
		gboolean cancel_package;
		cancel_package = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (pack), "cancelled"));
#if EI2_DEBUG & 0x4
		trilobite_debug ("Package cancel status == %d", cancel_package);
#endif
		if (cancel_package == 0) {
			action = PRUNE_ACTION_ALLOW;
		}
	}

	/* If it's a suite and no dependencies, cancel it */
	if (pack->suite_id && g_list_length (pack->depends)==0 && pack->status == PACKAGE_PARTLY_RESOLVED) {
		pack->status = PACKAGE_ALREADY_INSTALLED;
	}

	/* Recursion check */
	if (g_list_find (*path, pack)) {
#if EI2_DEBUG & 0x4
		trilobite_debug ("... %p %s recurses .., softcat is probably in flux", pack, pack->name);
#endif
		return;
	}
	
	if (action == PRUNE_ACTION_NORMAL && pack->status == PACKAGE_PARTLY_RESOLVED) {
		action = PRUNE_ACTION_ALLOW;
	}

	if (action != PRUNE_ACTION_ALLOW) {
#if EI2_DEBUG & 0x4
		trilobite_debug ("subpruner kill root %p %s because of %p %s", 
				 root, root->name, pack, pack->name);
#endif
		if (g_list_find (*result, root)==NULL) {
			(*result) = g_list_prepend (*result, root);
		}
	} else {
		for (iterator = pack->depends; iterator; iterator = g_list_next (iterator)) {
			PackageDependency *dep = PACKAGEDEPENDENCY (iterator->data);
			(*path) = g_list_prepend (*path, pack);
			prune_failed_packages_helper (service, root, dep->package, packages, path, result);
			(*path) = g_list_remove (*path, pack);
		}
	}
}

/* If the calls to prune_failed_packages are removed, no
  install_failed signals are emitted, and the client gets the entire
  tree list.  This is for bug 5267 */

void 
prune_failed_packages (EazelInstall *service, 
		       GList **packages)
{
	GList *iterator;
	GList *result = NULL;

	for (iterator = *packages; iterator; iterator = g_list_next (iterator)) {
		PackageData *pack = PACKAGEDATA (iterator->data);
		GList *path = NULL;
		prune_failed_packages_helper (service, pack, pack, *packages, &path, &result);
	}

	for (iterator = result; iterator; iterator = g_list_next (iterator)) {
		PackageData *pack = PACKAGEDATA (iterator->data);

#if EI2_DEBUG & 0x4
		trilobite_debug ("removing %p %s", pack, pack->name);
#endif
		(*packages) = g_list_remove (*packages, pack);
		eazel_install_emit_install_failed (service, pack);
		gtk_object_unref (GTK_OBJECT (pack));
	}
	if (g_list_length (*packages) == 0) {
#if EI2_DEBUG & 0x4
		trilobite_debug ("packages is empty after prune");		
#endif		
		g_list_free (*packages);
		(*packages) = NULL;
	}
	g_list_free (result);

#if EI2_DEBUG & 0x2
	dump_tree (*packages);
#endif
}


/***********************************************************************************/

/* Code to check already installed packages */

EazelInstallStatus
eazel_install_check_existing_packages (EazelInstall *service, 
				       PackageData *pack)
{
	GList *existing_packages;
	GList *borked_packages = NULL;
	EazelInstallStatus result;

#if EI2_DEBUG & 0x4
	trilobite_debug ("check_existing %p %s", pack, pack->name);
#endif
	result = EAZEL_INSTALL_STATUS_NEW_PACKAGE;
	/* query for existing package of same name */
	existing_packages = eazel_package_system_query (service->private->package_system,
							service->private->cur_root,
							pack->name,
							EAZEL_PACKAGE_SYSTEM_QUERY_MATCHES,
							MODIFY_MUST_HAVE);

	/* If error, handle it */
	if (service->private->package_system->err) {
#if EI2_DEBUG & 0x4
		switch (service->private->package_system->err->e) {
		case EazelPackageSystemError_DB_ACCESS:
			if (strcmp (service->private->package_system->err->u.db_access.path, 
				    service->private->cur_root)==0) {
				trilobite_debug ("some package dbs is locked by another process", 
						 service->private->package_system->err->u.db_access.pid);
				pack->status = PACKAGE_PACKSYS_FAILURE;
				g_free (service->private->package_system->err);
				return EAZEL_INSTALL_STATUS_QUO;
				break;
			}
		}
#endif
	}
	if (existing_packages) {
		/* Get the existing package, set it's modify flag and add it */
		GList *iterator;
		PackageData *survivor = NULL;
		gboolean abort = FALSE;
		int res;

		if (g_list_length (existing_packages)>1) {
#if EI2_DEBUG & 0x4
			trilobite_debug ("there are %d existing packages called %s",
					 g_list_length (existing_packages),
					 pack->name);
#endif
			/* Verify them all, to find one that is not damaged. Mark the rest
			   as invalid so the client can suggest the user should delete them */
			for (iterator = existing_packages; iterator; iterator = g_list_next (iterator)) {
				PackageData *existing_package = PACKAGEDATA (iterator->data);
				char *name = packagedata_get_readable_name (existing_package);
				GList *foo = NULL;
				
				foo = g_list_prepend (foo, existing_package);
				if (eazel_package_system_verify (service->private->package_system,
								 service->private->cur_root,
								 foo)) {
					/* I18N note: "%is is ok" in the sense that %s=package name and the
					   package is intact */
					g_message (_("%s is ok"), name);
					if (survivor == NULL) {
						survivor = existing_package;
					} else {						
						abort = TRUE;
					}
				} else {
					/* I18N note: "%is is not ok" in the sense that %s=package name and the
					   package is not intact */
					g_message ("%s is not ok", name);
					existing_package->status = PACKAGE_INVALID;
					/* I add the borked packages later, so they'll show up
					   earlier in the tree */
					borked_packages = g_list_prepend (borked_packages, existing_package);
				}
				g_list_free (foo);
				g_free (name);
			}
		} else {
			survivor = PACKAGEDATA (g_list_first (existing_packages)->data);
		}
		
		if (abort) {
			trilobite_debug ("*********************************************************");
			trilobite_debug ("This is a bad bad case, see bug 3511");
			trilobite_debug ("To circumvent this problem, as root, execute this command");
			trilobite_debug ("(which is dangerous by the way....)");
			trilobite_debug ("rpm -e --nodeps `rpm -q %s`", pack->name);
			
			g_list_free (borked_packages);

			/* Cancel the package, mark all the existing as invalid */			   
			pack->status = PACKAGE_CANCELLED;
			for (iterator = existing_packages; iterator; iterator = g_list_next (iterator)) {
				PackageData *existing_package = PACKAGEDATA (iterator->data);
				existing_package->status = PACKAGE_INVALID;
				packagedata_add_pack_to_modifies (pack, existing_package);
				gtk_object_unref (GTK_OBJECT (existing_package));
			}
		}

		if (abort==FALSE && survivor) {
			g_assert (pack->version);
			g_assert (survivor->version);

			res = eazel_package_system_compare_version (service->private->package_system,
								    pack->version, 
								    survivor->version);

			if (survivor->epoch > 0) {
				g_warning ("modified package has epoch %d, new package has %d",
					   survivor->epoch,
					   pack->epoch);
				gtk_object_set_data (GTK_OBJECT (service->private->package_system),
						     "ignore-epochs", GINT_TO_POINTER (1));
			}
			
			/* check against minor version */
			if (res==0) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("versions are equal (%s), comparing minors", pack->version);
#endif
				if (pack->minor && survivor->minor) {
#if EI2_DEBUG & 0x4
					trilobite_debug ("minors are %s for new and %s for installed)", 
							 pack->minor, survivor->minor);
#endif
					res = eazel_package_system_compare_version (service->private->package_system,
										    pack->minor, survivor->minor);
				} else if (!pack->minor && survivor->minor) {
					/* If the given packages does not have a minor,
					   but the installed has, assume we're fine */
					/* FIXME: bugzilla.eazel.com
					   This is a patch, it should be res=1, revert when
					   softcat is updated to have revisions for all packages 
					   (post PR3) */
					res = 1;
				} else {
					/* Eh, do nothing just to be safe */
					res = 0;
				}
			}

			/* Set the modify_status flag */
			if (res == 0) {
				survivor->modify_status = PACKAGE_MOD_UNTOUCHED;
			} else if (res > 0) {
				survivor->modify_status = PACKAGE_MOD_UPGRADED;
			} else {
				survivor->modify_status = PACKAGE_MOD_DOWNGRADED;
			}

			/* Calc the result */
			if (res == 0) {
				result = EAZEL_INSTALL_STATUS_QUO;
			} else if (res > 0) {
				result = EAZEL_INSTALL_STATUS_UPGRADES;
			} else {
				result = EAZEL_INSTALL_STATUS_DOWNGRADES;
			}
		
#if EI2_DEBUG & 0x4
			/* Debug output */ 
			switch (result) {
			case EAZEL_INSTALL_STATUS_QUO: {
				if (pack->minor && survivor->minor) {
					trilobite_debug (_("%s-%s version %s-%s already installed"), 
							 pack->name, pack->minor, 
							 survivor->version, survivor->minor);
				} else {
					trilobite_debug (_("%s version %s already installed"), 
							 pack->name, 
							 survivor->version);
				}
			} 
			break;
			case EAZEL_INSTALL_STATUS_UPGRADES: {
				/* This is certainly ugly as helll */
				if (pack->minor && survivor->minor) {
					trilobite_debug (_("%s upgrades from version %s-%s to %s-%s"),
							 pack->name, 
							 survivor->version, survivor->minor, 
							 pack->version, pack->minor);
				} else {
					trilobite_debug (_("%s upgrades from version %s-%s to %s-%s"),
							 pack->name, 
							 survivor->version, survivor->minor, 
							 pack->version, pack->minor);
				}
			}
			break;
			case EAZEL_INSTALL_STATUS_DOWNGRADES: {
				if (pack->minor && survivor->minor) {
					trilobite_debug (_("%s downgrades from version %s-%s to %s-%s"),
							 pack->name, 
							 survivor->version, survivor->minor, 
							 pack->version, pack->minor);
				} else {
					trilobite_debug (_("%s downgrades from version %s to %s"),
							 pack->name, 
							 survivor->version, 
							 pack->version);
				}
			}
			break;
			default:
				break;
			}
#endif		
/*
			if (g_list_find_custom (pack->modifies, 
						survivor->name,
						(GCompareFunc)eazel_install_package_name_compare)) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("%s already marked as modified", survivor->name);
#endif
				gtk_object_unref (GTK_OBJECT (survivor));
				survivor = NULL;
				continue;
			}
*/
		
			/* Set modifies list */
			if (result != EAZEL_INSTALL_STATUS_QUO) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("%p %s modifies %p %s",
						 pack, pack->name,
						 survivor, survivor->name);
#endif

				packagedata_add_pack_to_modifies (pack, survivor);
				survivor->status = PACKAGE_RESOLVED;
			} 
			pack->status = PACKAGE_ALREADY_INSTALLED;
			gtk_object_unref (GTK_OBJECT (survivor));
		}

		/* Now add the borked packages */
		for (iterator = borked_packages; iterator; iterator = g_list_next (iterator)) {
			PackageData *existing_package = PACKAGEDATA (iterator->data);
			packagedata_add_pack_to_modifies (pack, existing_package);
			gtk_object_unref (GTK_OBJECT (existing_package));

		}
		g_list_free (borked_packages);

		/* Free the list structure from _simple_query */
		g_list_free (existing_packages);
	} else {
#if EI2_DEBUG & 0x4
		if (pack->minor) {
			trilobite_debug (_("%s installs version %s-%s"), 
					 pack->name, 
					 pack->version,
					 pack->minor);
		} else {
			trilobite_debug (_("%s installs version %s"), 
					 pack->name, 
					 pack->version);
		}

#endif
	}

	return result;
}

/***********************************************************************************/

/* Info collector */

typedef enum {
	NO_SOFTCAT_HIT = 0,
	SOFTCAT_HIT_OK  = 1,
	PACKAGE_SKIPPED = 2
} GetSoftCatResult;

void get_package_info (EazelInstall *service, GList *packages);
GetSoftCatResult get_softcat_info (EazelInstall *service, PackageData **pack);

static void
add_to_dedupe_hash (EazelInstall *service,
		    PackageData *package)
{
#if EI2_DEBUG & 0x4
	trilobite_debug ("(adding %p %s %s to dedupe)", 
			 package, package->name, package->md5);
#endif
	gtk_object_ref (GTK_OBJECT (package));
	g_hash_table_insert (service->private->dedupe_hash, 
			     package->md5, 
			     package);
}

static void
fail_modified_packages (EazelInstall *service, 
			PackageData *package)
{
	GList *iterator;

	for (iterator = package->modifies; iterator; iterator = g_list_next (iterator)) {
		PackageData *pack = PACKAGEDATA (iterator->data);
		if (pack->status == PACKAGE_RESOLVED) {
			pack->status = PACKAGE_CANCELLED;
		}
	}
}

static GetSoftCatResult
post_get_softcat_info (EazelInstall *service, 
		       PackageData **package,
		       GetSoftCatResult result)
{
	PackageData *p1 = NULL;
	
#if EI2_DEBUG & 0x4
	trilobite_debug ("-> post_get_softcat_info %p %s", 
			 (*package), (*package)->name);
#endif

	g_assert ((*package)->md5);
	p1 = g_hash_table_lookup (service->private->dedupe_hash, (*package)->md5);
	
	if (p1 != NULL) {
		/* Don't dedupe yourself */
		if (p1 != (*package)) {
#if EI2_DEBUG & 0x4
			trilobite_debug ("\tdeduping(a) %p %s to %p", *package, (*package)->name, p1);
#endif		
			
			gtk_object_ref (GTK_OBJECT (p1));
			gtk_object_unref (GTK_OBJECT (*package)); 
			(*package) = p1;
		} else {
#if EI2_DEBUG & 0x4
			trilobite_debug ("\tnot deduping(a) myself %p %s", *package, (*package)->name, p1);
#endif		
		}
	}

	if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT ((*package)), "have-checked-existing")) == 0) {
		EazelInstallStatus status;
		
		status = eazel_install_check_existing_packages (service, *package);
		gtk_object_set_data (GTK_OBJECT ((*package)), "have-checked-existing", GINT_TO_POINTER (1));
		
#if 0
		/* HACK to always fail gnome-libs for fun... */
		if (strcmp ((*package)->name, "gnome-libs")==0) {
			trilobite_debug ("\t%s:%d MATCHED gnome-libs", __FILE__, __LINE__);
			status = EAZEL_INSTALL_STATUS_DOWNGRADES;
		}
#endif
		
		switch (status) {
		case EAZEL_INSTALL_STATUS_NEW_PACKAGE:
			add_to_dedupe_hash (service, *package);
			break;
		case EAZEL_INSTALL_STATUS_UPGRADES:
			if (eazel_install_get_upgrade (service)) {
				add_to_dedupe_hash (service, *package);
			} else {
				fail_modified_packages (service, *package);
				gtk_object_set_data (GTK_OBJECT (*package), 
						     "cancelled", 
						     GINT_TO_POINTER (1));
			}
			break;
		case EAZEL_INSTALL_STATUS_DOWNGRADES:
			if (eazel_install_get_downgrade (service)) {
				add_to_dedupe_hash (service, *package);
			} else {
				fail_modified_packages (service, *package);
				gtk_object_set_data (GTK_OBJECT (*package), 
						     "cancelled", 
						     GINT_TO_POINTER (1));
			}
			break;
		case EAZEL_INSTALL_STATUS_QUO:
			fail_modified_packages (service, *package);
			gtk_object_set_data (GTK_OBJECT (*package), 
					     "cancelled", 
					     GINT_TO_POINTER (1));
			result = PACKAGE_SKIPPED;
			break;
		}
	}

#if EI2_DEBUG & 0x4
	trilobite_debug ("<- post_get_softcat_info %p %s", 
			 (*package), (*package)->name);
#endif

	return result;
}

GetSoftCatResult
get_softcat_info (EazelInstall *service, 
		  PackageData **package)
{
	GetSoftCatResult result = NO_SOFTCAT_HIT;

	g_assert (*package);
	g_assert (IS_PACKAGEDATA (*package));
	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));

	if ((*package)->fillflag == MUST_HAVE) {
		/* Package is already filled, set result to OK and toggle packages status */
		result = SOFTCAT_HIT_OK;
		(*package)->status = PACKAGE_PARTLY_RESOLVED;
	} else {
		if ((*package)->filename) {
			/* Package already has a filename, load info from disk */
			if (g_file_test ((*package)->filename, G_FILE_TEST_ISFILE) &&
			    access ((*package)->filename, R_OK)==0) {
				PackageData *loaded_package;
#if EI2_DEBUG & 0x4
				trilobite_debug ("%p %s load from disk", *package, (*package)->name);
#else
				g_message (_("Loading package info from file %s"), (*package)->filename);
#endif		
				loaded_package = eazel_package_system_load_package (service->private->package_system,
										    *package,
										    (*package)->filename,
										    MUST_HAVE);
				if (loaded_package==NULL) {
					(*package)->status = PACKAGE_CANNOT_OPEN;
				} else {
					(*package)->status = PACKAGE_PARTLY_RESOLVED;
				}
				result = SOFTCAT_HIT_OK;
			}
		} else {
			/* Otherwise get info from SoftCat */
			EazelSoftCatError err;
			
			/* if package->version is set, we want get_info to get the exact 
			   version */
			err = eazel_softcat_get_info (service->private->softcat,
						      *package,
						      EAZEL_SOFTCAT_SENSE_EQ,
						      MUST_HAVE);

			/* if ((err==EAZEL_SOFTCAT_SUCCESS) && ((*package)->fillflag & MUST_HAVE)) { */
			if (err==EAZEL_SOFTCAT_SUCCESS) {
				result = SOFTCAT_HIT_OK;
				(*package)->status = PACKAGE_PARTLY_RESOLVED;
			} else {
				result = NO_SOFTCAT_HIT;
				(*package)->status = PACKAGE_CANNOT_OPEN;
			}
		}
	}
	if ((result != NO_SOFTCAT_HIT) && ((*package)->suite_id == NULL)) {
		post_get_softcat_info (service, package, result);
	}
	return result;
}

static GetSoftCatResult
get_package_info_foreach (EazelInstall *service,
			  PackageData *package)
{
	GetSoftCatResult result;

	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));
	g_assert (package);
	g_assert (IS_PACKAGEDATA (package));

	result = get_softcat_info (service, &package);
        switch (result) {
	case NO_SOFTCAT_HIT:
		g_message ("Could not get info from SoftwareCatalog ");
		break;
	case PACKAGE_SKIPPED:
#if EI2_DEBUG & 0x4
		trilobite_debug ("Package skipped");
#endif
		break;
	default:
#if EI2_DEBUG & 0x4
		trilobite_debug ("Softcat check ok");
#endif 		
		break;
	}
	return result;
}

void
get_package_info (EazelInstall *service, 
		  GList *packages)
{
	GList *iterator;

	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));

	for (iterator = packages; iterator; iterator = g_list_next (iterator)) {
		get_package_info_foreach (service, PACKAGEDATA (iterator->data));
	}
}

/***********************************************************************************/

/* This is the dedupe magic, which given a tree, links dupes (same eazel-id) to
   point to the same PackageData* object and unrefs the dupes */

void dedupe_foreach (PackageData *pack, EazelInstall *service);
void dedupe_foreach_depends (PackageDependency *d, EazelInstall *service);
void dedupe (EazelInstall *service, GList *packages);

void
dedupe_foreach_depends (PackageDependency *d,
			 EazelInstall *service) 
{
	PackageData *p1;
	PackageData *p11;

	g_assert (d);
	g_assert (IS_PACKAGEDEPENDENCY (d));
	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));
	
	p1 = d->package;

	if (p1->md5 == NULL) {
		/* Package info not received from SoftCat */
		return;
	}

	p11 = g_hash_table_lookup (service->private->dedupe_hash, p1->md5);
	
	if (p11) {
		if (p11 != p1) {
#if EI2_DEBUG & 0x4
			trilobite_debug ("\tdeduping(b) %p %s to %p", p11, p11->name, p1);
#endif		
			gtk_object_ref (GTK_OBJECT (p11));
			gtk_object_unref (GTK_OBJECT (p1)); 
			d->package = p11;
		} else {
#if EI2_DEBUG & 0x4
			trilobite_debug ("\tnot deduping(b) myself %p %s", p11, p11->name, p1);
#endif		
		}			
	} else {
		add_to_dedupe_hash (service, p1);
		dedupe_foreach (p1, service);
	}
}

void
dedupe_foreach (PackageData *package,
		 EazelInstall *service)
{
	g_assert (package);
	g_assert (IS_PACKAGEDATA (package)); 
	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));

	g_list_foreach (package->depends, (GFunc)dedupe_foreach_depends, service);
} 

void
dedupe (EazelInstall *service,
	 GList *packages)
{
	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));

	g_list_foreach (packages, (GFunc)dedupe_foreach, service);
#if EI2_DEBUG & 0x2
	dump_tree (packages);
#endif
}

/***********************************************************************************/

/* This is the code to remove already satisfied dependencies */

void check_dependencies (EazelInstall *service, GList *packages);
void check_dependencies_foreach (EazelInstall *service, GList *packages, PackageData *package);
gboolean is_satisfied (EazelInstall *service, GList *packages, PackageDependency *dep);
gboolean is_satisfied_features (EazelInstall *service, GList *packages, PackageData *package);
gboolean is_satisfied_from_package_list (EazelInstall *service, GList *packages, PackageDependency *dep);

/*
  FIXME: bugzilla.eazel.com 6809
  is_satisfied_from_package_list is flawed since it 1) looks also at unresolved deps,
  should only look at partly_resolved 2) is only looks at the current workset, and not the complete
  (ie. everything at the current level, but now upwards */

gboolean
is_satisfied (EazelInstall *service, 
	      GList *packages, 
	      PackageDependency *dep)
{
	char *key;
	int previous_check_state = 0;
	char *sense_str;
	gboolean result = FALSE;

	g_assert (dep);
	g_assert (IS_PACKAGEDEPENDENCY (dep));
	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));

	/* FIXME:
	   this checks that the package isn't already filled, but what if
	   it is, but later fails, will we loose a dependency ? */
	/* First check if we've already downloaded the package */
	if (dep->package->fillflag == MUST_HAVE) {
#if EI2_DEBUG & 0x4
		trilobite_debug ("is_satisfied? %p %s", 
				 dep->package, dep->package->name);
		trilobite_debug ("\t -> already filled, must be ok");
#else
		g_message ("\tcached : %s", 
			   dep->package->name);
#endif
		
		return FALSE;
	}

	/* If the dependency has a version, but no sense, something is terribly
	   wrong with the xml */
#if EI2_DEBUG & 0x4
	if (dep->version && dep->sense == 0) {
		trilobite_debug ("I'm going to die now, because softcat is making no sense");
		trilobite_debug ("Or rather, the xml returned a invalid dependency sense");
	}
#endif
	if (dep->version) { g_assert (dep->sense!=0); }

	sense_str = eazel_softcat_sense_flags_to_string (dep->sense);
#if EI2_DEBUG & 0x4
	trilobite_debug ("is_satisfied? %p %s %s %s", 
			 dep->package, dep->package->name, sense_str,
			 (dep->version != NULL ? dep->version : ""));
#endif
	key = g_strdup_printf ("%s/%s/%s", dep->package->name, sense_str,
			       (dep->version != NULL ? dep->version : ""));

	if (key != NULL && strcmp (key, "(null)//")!=0) {
		previous_check_state = GPOINTER_TO_INT (g_hash_table_lookup (service->private->dep_ok_hash, key));
	}
#if EI2_DEBUG & 0x4
	trilobite_debug ("\t--> key is %s", key);
#endif
	switch (previous_check_state) {
	case DEPENDENCY_OK: {
#if EI2_DEBUG & 0x4
		trilobite_debug ("\t--> dep hash ok");
#endif
		result = TRUE;
		break;
	}
	case DEPENDENCY_NOT_OK: {
#if EI2_DEBUG & 0x4
		trilobite_debug ("\t--> dep hash failed");
#endif
		result = FALSE;
		break;
	}
	default: {				
		/* If dependency has name and version, first check workset, then packages on system,
		   and use the dependency's version/sense */
		if (dep->package->name && dep->version) {
			if (is_satisfied_from_package_list (service, packages, dep)) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("\t--> present with version in workset");
#endif
				result = TRUE;
			} else if (eazel_package_system_is_installed (service->private->package_system,
								      service->private->cur_root,
								      dep->package->name,
								      dep->version,
								      NULL,
								      dep->sense)) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("\t--> installed with version");
#endif
				result = TRUE;
			}
		} else if (dep->package->features && is_satisfied_features (service, packages, dep->package)) {
			/* If package dependency didn't have a name/sense/version, but 
			   has features, check if features are installed */
#if EI2_DEBUG & 0x4
			trilobite_debug ("\t--> features of package are satisfied");
#endif
			result = TRUE;
		} else if (dep->package->name) {
			/* Else check for name in workset and then amongst installed packages,
			   using sense ANY */
			if (is_satisfied_from_package_list (service, packages, dep)) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("\t--> present in workset");
#endif
				result = TRUE;
			} else if (eazel_package_system_is_installed (service->private->package_system,
								      service->private->cur_root,
								      dep->package->name,
								      NULL, 
								      NULL,
								      EAZEL_SOFTCAT_SENSE_ANY)) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("\t--> installed");
#endif
				result = TRUE;
			}
		}

		if (result) {
#if EI2_DEBUG & 0x4
			trilobite_debug ("\t--> feature is satisfied");
#endif
			g_hash_table_insert (service->private->dep_ok_hash, 
					     key,
					     GINT_TO_POINTER (DEPENDENCY_OK));
		} else {
#if EI2_DEBUG & 0x4
			trilobite_debug ("\t--> feature not satisfied");
#endif
			g_hash_table_insert (service->private->dep_ok_hash, 
					     key,
					     GINT_TO_POINTER (DEPENDENCY_NOT_OK));
		}
	}
	}

#if ~EI2_DEBUG & 0x4	
	g_message ("\t%8.8s : %s %s %s", 
		   /* I18N note: ok is for "package dependencies are ok", "not ok".. well guess... */
		   result ? _("ok") : _("not ok"),
		   dep->package->name, sense_str,
		   (dep->version != NULL ? dep->version : ""));
#endif
	g_free (sense_str);
	return result;
}

gboolean 
is_satisfied_from_package_list (EazelInstall *service, 
				GList *packages, 
				PackageDependency *dep)
{
	GList *iterator;
	GList *flat_packages;
	gboolean result = FALSE;

	flat_packages = flatten_packagedata_dependency_tree (packages);
	
#if EI2_DEBUG & 0x4
	trilobite_debug ("\t--> is_satisfied_from_packages %s from %d packages",
			 dep->package->name, g_list_length (packages));
#endif
	for (iterator = flat_packages; iterator; iterator = g_list_next (iterator)) {
		PackageData *p = PACKAGEDATA (iterator->data);
		/*
		trilobite_debug ("\t\t --> checking %s %s %s", 
				 p->name, 
				 packagedata_status_enum_to_str (p->status),
				 p->fillflag == MUST_HAVE ? "filled" : "not filled");
		*/
		if (p->status == PACKAGE_PARTLY_RESOLVED && 
		    (p->fillflag == MUST_HAVE) && 
		    p->name && 
		    (strcmp (p->name, dep->package->name)==0)) {
			trilobite_debug ("\t\t --> foo");
			if (dep->version) {
				int res;
				res = eazel_package_system_compare_version (service->private->package_system,
									    dep->version,p->version);
				if (res==0 && dep->sense & EAZEL_SOFTCAT_SENSE_EQ) {
					result = TRUE;
					break;
				} else if (res < 0 && dep->sense & EAZEL_SOFTCAT_SENSE_LT) {
					result = TRUE;
					break;
				} else if (res > 0 && dep->sense & EAZEL_SOFTCAT_SENSE_GT) {
					result = TRUE;
					break;
				}
			} else {
				result = TRUE;
				break;
			}
		}
	}
	
	g_list_free (flat_packages);
	return result;
}

gboolean
is_satisfied_features (EazelInstall *service, 
		       GList *packages, 
		       PackageData *package)
{
	gboolean result = TRUE;
	GList *iterator;
	GList *features;

	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));
	g_assert (package);
	g_assert (IS_PACKAGEDATA (package));

	features = package->features;

#if EI2_DEBUG & 0x4
	trilobite_debug ("\t -> is_satisfied_features %d features", g_list_length (features));
#endif

	for (iterator = features; iterator && result; iterator = g_list_next (iterator)) {
		GList *query_result;
		char *f = (char*)iterator->data;

#if EI2_DEBUG & 0x4
		trilobite_debug ("\t -> %s", f);
#endif
		query_result = eazel_package_system_query (service->private->package_system,
							   service->private->cur_root,
							   f,
							   EAZEL_PACKAGE_SYSTEM_QUERY_PROVIDES,
							   PACKAGE_FILL_MINIMAL);
		if (g_list_length (query_result) == 0) {
			query_result = eazel_package_system_query (service->private->package_system,
								   service->private->cur_root,
								   f,
								   EAZEL_PACKAGE_SYSTEM_QUERY_OWNS,
								   PACKAGE_FILL_MINIMAL);
			if (g_list_length (query_result) == 0) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("\t -> noone owns or provides %s", f);
#endif
				result = FALSE;

/*    This is a workaround for a softcat bug "speciality, where packagedependencies
      with no sense are reported as a <FEATURE>foo</FEATURE>. When bug 1129
      is closed, remove this evilness */
#if PATCH_FOR_SOFTCAT_BUG
				if (eazel_package_system_is_installed (service->private->package_system,
								       service->private->cur_root,
								       f, NULL, NULL,
								       EAZEL_SOFTCAT_SENSE_ANY)) {
#if EI2_DEBUG & 0x4
					trilobite_debug ("\t -> but a package called %s exists", f);
#endif
					result = TRUE;
				}
#endif
			}
		}
		g_list_foreach (query_result, (GFunc)gtk_object_unref, NULL); 
		g_list_free (query_result);
	}

	return result;
}

void 
check_dependencies_foreach (EazelInstall *service, 
			    GList *packages,
			    PackageData *package)
{
	GList *remove = NULL;
	GList *iterator;
	
	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));
	g_assert (package);
	g_assert (IS_PACKAGEDATA (package));

	/* If the package was cancelled, don't process it's dependencies */
	if (package->status == PACKAGE_CANCELLED || 
	    (package->status == PACKAGE_ALREADY_INSTALLED && package->modifies==NULL)) {
		g_list_foreach (package->depends, (GFunc)packagedependency_destroy, NULL);
		g_list_free (package->depends);
		package->depends = NULL;
		return;
	}

	if (package->suite_id!=NULL) {
		for (iterator = package->depends; iterator; iterator = g_list_next (iterator)) {
			PackageDependency *dep = PACKAGEDEPENDENCY (iterator->data);
			dep->package->fillflag = PACKAGE_FILL_INVALID;
			g_list_foreach (dep->package->depends, (GFunc)packagedependency_destroy, NULL);
			dep->package->depends = NULL;
		}
#if EI2_DEBUG & 0x4
		trilobite_debug ("skipping suite %p %s", package, package->suite_id);
#endif
	}


#if EI2_DEBUG & 0x4
	trilobite_debug ("check deps for %p %s", package, package->name);
#else
	g_message (_("Checking dependencies for %s"), package->name);
#endif

	for (iterator = package->depends; iterator; iterator = g_list_next (iterator)) {
		PackageDependency *dep = PACKAGEDEPENDENCY (iterator->data);		

		if (dep->package->name && package->name && strcmp (dep->package->name, package->name)==0) {
			char *name_a, *name_b;

			name_a = packagedata_get_readable_name (package);
			name_b = packagedata_get_readable_name (dep->package);

			g_warning ("Possible inconsistency!");
			g_warning ("%s depends on %s", name_a, name_b);

			g_free (name_a);
			g_free (name_b);

			package->status = PACKAGE_CIRCULAR_DEPENDENCY;
		} else {
			if (is_satisfied (service, packages, dep)) {
				remove = g_list_prepend (remove, dep);
			} else {
				eazel_install_emit_dependency_check (service, package, dep);
			}
		}
	}

	for (iterator = remove; iterator; iterator = g_list_next (iterator)) {
		PackageDependency *dep = PACKAGEDEPENDENCY (iterator->data);
		package->depends = g_list_remove (package->depends, dep);
#if EI2_DEBUG & 0x4
		trilobite_debug ("removing %p %s from %p %s", 
				 dep->package, dep->package->name, 
				 package, package->name);
#endif
		packagedependency_destroy (dep);
	}
	g_list_free (remove);
}

void
check_dependencies (EazelInstall *service,
		    GList *packages) 
{
	GList *iterator;

	g_assert (service);
	g_assert (EAZEL_INSTALL (service));
	
	for (iterator = packages; iterator; iterator = g_list_next (iterator)) {
		check_dependencies_foreach (service, packages, PACKAGEDATA (iterator->data));
	}

#if EI2_DEBUG & 0x1
	trilobite_debug ("post dep_check tree");
	dump_tree (packages);
#endif
}

/***********************************************************************************/

/* This is the main dependency check function */

void do_dep_check_internal (EazelInstall *service, GList *packages);
void do_dep_check (EazelInstall *service, GList **packages);
void do_requirement_consistency_check (EazelInstall *service, GList *packages);
void do_requirement_consistency_check_internal (EazelInstall *service, GList *packages);

void
do_dep_check_internal (EazelInstall *service,
		       GList *packages)
{
	GList *iterator;
	GList *K = NULL;

	g_assert (service);
	g_assert (EAZEL_IS_INSTALL (service));

	if (packages==NULL) {
		return;
	}

#if EI2_DEBUG & 0x1
	trilobite_debug ("do_dep_check tree");
	dump_tree (packages);
#endif

	get_package_info (service, packages);
	dedupe (service, packages);
	check_dependencies (service, packages);

	/* we might have received a "stop" callback recently */
	if (service->private->cancel_download) {
		return;
	}

	/* Build the K list, consisting of packages without must_have set... */
	for (iterator = packages; iterator; iterator = g_list_next (iterator)) {
	        GList *diterator;
		for (diterator = PACKAGEDATA(iterator->data)->depends; diterator; diterator = g_list_next (diterator)) {
			PackageDependency *dep = PACKAGEDEPENDENCY (diterator->data);
			if (((~dep->package->fillflag & MUST_HAVE) ||
			    (PACKAGEDATA(iterator->data)->suite_id)) && 
			    (g_list_find (K, dep->package)==NULL)) {
				K = g_list_prepend (K, dep->package);
			}
		}
	}

	/* ... and process them */
	if (K) {
		do_dep_check_internal (service, K);
	}
}

void
do_dep_check (EazelInstall *service,
	      GList **packages)
{
	/* Shift to the internal. We want to do a prune_failed_packages
	   after completion, but since the do_dep_check algorithm is 
	   recursive, we call an internal function here. */
	do_dep_check_internal (service, *packages);
	prune_failed_packages (service, packages);
}

/* This function is used by do_requirement_consistency_check_package.
   "pack" is a package which modified "updated", and "dep" is a 
   dependency that someone else has on updated.

   To accept :
   calculate version compare value x for cmp (dep->version, pack->version)
   if upgrading the package and sense is any, accept
   if upgrading the package and sense has >, accept
   if upgrading the package and sense has <, accept if x > 0
   if upgrading the package and sense has =, accept if x = 0
   vice versa for downgrade (flipping the ><'s)
   
   This will not be called with dep->sense = ANY, since these are always ok.
*/
static gboolean
check_if_modification_is_ok (EazelInstall *service, 
			     PackageData *pack, 
			     PackageData *updated,
			     PackageDependency *dep)
{
	gboolean accept = FALSE;
	int x;

	/* But just in case.... */
	if (dep->sense == EAZEL_SOFTCAT_SENSE_ANY) {
		return TRUE;
	}

	x = eazel_package_system_compare_version (service->private->package_system, 
						  dep->version,
						  pack->version);

	switch (updated->modify_status) {
	case PACKAGE_MOD_UPGRADED:
#if EI2_DEBUG & 0x4
		trilobite_debug ("\t\t\t\t -> checking if upgrade is ok");
#endif
		if (dep->sense & EAZEL_SOFTCAT_SENSE_GT) {
			/* is sense it has > in it, upgrading is safe */
			accept = TRUE;
		} else if (dep->sense & EAZEL_SOFTCAT_SENSE_LT) {
			/* if sense it has < in it, upgrade is
			   safe if dep->version is > pack->version*/
			if (x > 0) {
				accept = TRUE;
			}
		} else if (dep->sense & EAZEL_SOFTCAT_SENSE_EQ) {
			/* if sense is =, upgrade is safe if
			   x == 0 */
			if (x == 0) {
				accept = TRUE;
			}
		} 
		break;
	case PACKAGE_MOD_DOWNGRADED:
#if EI2_DEBUG & 0x4
		trilobite_debug ("\t\t\t\t -> checking if downgrade is ok");
#endif
		if (dep->sense & EAZEL_SOFTCAT_SENSE_LT) {
			/* is sense it has < in it, 
			   downgrading is safe */
			accept = TRUE;
		} else if (dep->sense & EAZEL_SOFTCAT_SENSE_GT) {
			/* if sense it has > in it, upgrade is
			   safe if pack->version is > dep->version */
			if (x < 0) {
				accept = TRUE;
			}
		} else if (dep->sense & EAZEL_SOFTCAT_SENSE_EQ) {
			/* if sense is =, upgrade is safe if
			   x == 0 */
			if (x == 0) {
				accept = TRUE;
			}
		}
		break;
	default:
		break;
	}

#if EI2_DEBUG & 0x4
	trilobite_debug ("\t\t\t\t -> %s", accept ? "yarh" : "argh");
#endif

	return accept;
}

static int
find_break_by_package_name (PackageBreaks *breaks, PackageData *pack)
{
	return eazel_install_package_compare (packagebreaks_get_package (breaks), 
					      pack);
}

/*
  Traverse the packages modifies and query for packages R that require them
  foreach r in R 
      foreach d in R->depends
          if d->version && d->package is set (and sense isn'any) 
             if modification not ok, add breaks structure
 */
static void 
do_requirement_consistency_check_package (EazelInstall *service, 
					  GList *packages,
					  PackageData *pack)
{
	GList *iterator;

#if EI2_DEBUG & 0x4
	trilobite_debug ("\t -> do_requirement_consistency_check_package (%p %s)", pack, pack->name);
#endif

	if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (pack), "do_requirement_consistency_check"))!=0) {
		return;
	}
	gtk_object_set_data (GTK_OBJECT (pack), "do_requirement_consistency_check", GINT_TO_POINTER (1));
	
	/* Check all the modifies of the package */
	for (iterator = pack->modifies; iterator; iterator = g_list_next (iterator)) {
		PackageData *updated = PACKAGEDATA (iterator->data);
		GList *requiredby_list = NULL;
		GList *requiredby_it;

		GList *feature_cache;
		GList *provide_cache;

#if EI2_DEBUG & 0x4
		trilobite_debug ("\t -> querying for who requires %s", updated->name);
#endif
		/* I temporarily omit the provides, since we later do a provide consistency check */
		feature_cache = updated->features;
		updated->features = NULL;
		provide_cache = updated->provides;
		updated->provides = NULL;

		/* Get the packages that require the modified package */
		requiredby_list = eazel_package_system_query (service->private->package_system,
							      service->private->cur_root,
							      updated,
							      EAZEL_PACKAGE_SYSTEM_QUERY_REQUIRES,
							      PACKAGE_FILL_NO_PROVIDES);
		updated->features = feature_cache;
		updated->provides = provide_cache;

		/* Now check all the dependencies of the packages that requires the modified package */
		for (requiredby_it = requiredby_list; requiredby_it; requiredby_it = g_list_next (requiredby_it)) {
			PackageData *requiredby = PACKAGEDATA (requiredby_it->data);
			GList *dep_it;

#if EI2_DEBUG & 0x4
			trilobite_debug ("\t\t -> scanning deps for %s", requiredby->name);
#endif
			/* If the package that requries something from the modified package is
			   in the current workset, assume the other parts of the dependency voodoo
			   works and we can there just continue... */

			if (g_list_find_custom (packages, 
						requiredby->name, 
						(GCompareFunc)eazel_install_package_name_compare)) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("\t\t -> since it's in packages, it's fine...");
#endif
			} else for (dep_it = requiredby->depends; dep_it; dep_it = g_list_next (dep_it)) {
				PackageDependency *dep = PACKAGEDEPENDENCY (dep_it->data);
				if (dep->package && dep->version) {
					/* if this is a dep for the updated package, and sense
					   isn't ANY, check if modification is safe.
					   Note, sense will never be ANY (since you can't have ANY sense
					   and a version, but just for purity, I check for it */
					if (dep->package->name &&
					    strcmp (dep->package->name, updated->name)==0 &&
					    dep->sense != EAZEL_SOFTCAT_SENSE_ANY) {
#if EI2_DEBUG & 0x4
						trilobite_debug ("\t\t\t -> %p %s requires %s %s %s",
								 requiredby, requiredby->name,
								 dep->package->name,
								 eazel_softcat_sense_flags_to_string (dep->sense),
								 dep->version);
#endif
						if (check_if_modification_is_ok (service, 
										 pack, 
										 updated, 
										 dep)
						    == FALSE) {
							/* If we fail, mark pack as breaking requiredby */
							if (g_list_find_custom (pack->breaks, 
										updated,
										(GCompareFunc)find_break_by_package_name)==NULL) {
								PackageFeatureMissing *breakage = 
									packagefeaturemissing_new ();
								PackageDependency *req_dep = packagedependency_new();

								/* Ref mojo */
								gtk_object_ref (GTK_OBJECT (updated));
								gtk_object_ref (GTK_OBJECT (requiredby));
								req_dep->package = updated;
								req_dep->sense = dep->sense;
								req_dep->version = g_strdup (dep->version);
								packagedata_add_pack_to_depends (requiredby, req_dep);
								pack->status = PACKAGE_BREAKS_DEPENDENCY;
								packagebreaks_set_package (PACKAGEBREAKS (breakage), 
											   requiredby);
								packagedata_add_to_breaks (pack, PACKAGEBREAKS (breakage));
							}
						}
					}
				}
			}
		}
		g_list_foreach (requiredby_list, (GFunc)gtk_object_unref, NULL);
		g_list_free (requiredby_list);
	}
}

void 
do_requirement_consistency_check_internal (EazelInstall *service, 
					   GList *packages)
{
	GList *iterator;
	
#if EI2_DEBUG & 0x4
	trilobite_debug ("--> do_requirement_consistency_check_internal");
#endif

	for (iterator = packages; iterator; iterator = g_list_next (iterator)) {
		PackageData *pack = PACKAGEDATA (iterator->data);
		if (pack->modifies) {		      
#if EI2_DEBUG & 0x4
			trilobite_debug ("\t -> checking %p %s", pack, pack->name);
#endif
			do_requirement_consistency_check_package (service, packages, pack);
			
		}
	}

#if EI2_DEBUG & 0x4
	trilobite_debug ("<-- do_requirement_consistency_check_internal");
#endif
}

void 
do_requirement_consistency_check (EazelInstall *service, 
				  GList *packages)
{
	GList *flat_packages;

	flat_packages = flatten_packagedata_dependency_tree (packages);
	do_requirement_consistency_check_internal (service, flat_packages);
	g_list_free (flat_packages);
}

/***********************************************************************************/

/* This is the main file conflicts check hell */

void do_file_conflict_check (EazelInstall *service, GList **packages, GList **extra_packages);
void check_no_two_packages_has_same_file (EazelInstall *service, GList *packages);
void check_feature_consistency (EazelInstall *service, GList *packages);
void check_conflicts_against_already_installed_packages (EazelInstall *service, GList *packages);
void check_tree_for_conflicts (EazelInstall *service, GList **packages, GList **extra_packages);
gboolean check_if_modified_related_package (EazelInstall *service, PackageData *package, PackageData *dep);
gboolean check_if_related_package (EazelInstall *service, PackageData *package, PackageData *dep);

/*
  This function tests whether "package" and "dep"
  seems to be related in some way.
  This is done by checking the package->modifies list for
  elements that have same version as dep->version.
  I then compare these elements against dep->name,
  and if one matches the x-y-z vs dep->name=x-y scheme,
  I declare that "package" and "dep" are related
*/
gboolean
check_if_related_package (EazelInstall *service,
			  PackageData *package,
			  PackageData *dep)
{
	/* Pessimisn mode = HIGH */
	gboolean result = FALSE;
	char **dep_name_elements;
	char **mod_name_elements;
	char *dep_name_iterator;
	char *mod_name_iterator;
	int cnt = 0;

#if EI2_DEBUG & 0x4
	trilobite_debug ("check_if_related_package (%p %s-%s, %p %s-%s)",
			 package, package->name, package->version,
			 dep, dep->name, dep->version);
#endif 	

	dep_name_elements = g_strsplit (dep->name, "-", 80);		
	mod_name_elements = g_strsplit (package->name, "-", 80);
		
	for (cnt=0; TRUE;cnt++) {
		dep_name_iterator = dep_name_elements[cnt];
		mod_name_iterator = mod_name_elements[cnt];
#if 0
		trilobite_debug ("dep name iterator = \"%s\"", dep_name_iterator);
		trilobite_debug ("mod name iterator = \"%s\"", mod_name_iterator);
#endif
		if ((dep_name_iterator==NULL) ||
		    (mod_name_iterator==NULL)) {
			break;
		}
		if ((strlen (dep_name_iterator) == strlen (mod_name_iterator)) &&
		    (strcmp (dep_name_iterator, mod_name_iterator)==0)) {
			continue;
		}
		break;
	}
	if (cnt >= 1) {
		if (!result) {
			result = TRUE;
		} else {
			trilobite_debug ("but what blows is, the previous also did!!");
			package->status = PACKAGE_CANCELLED;
			result = FALSE;
		}				
	} else {
#if EI2_DEBUG & 0x4
		trilobite_debug ("%s-%s is not related to %s-%s", 
				 dep->name, dep->version, 
				 package->name, package->version);
#endif
	}

	g_strfreev (mod_name_elements);			
	g_strfreev (dep_name_elements);
	return result;
}

/*
  This function tests whether "package" has modifieds that are related to "dep" in any way.
*/
gboolean
check_if_modified_related_package (EazelInstall *service,
				   PackageData *package,
				   PackageData *dep)
{
	/* Pessimisn mode = HIGH */
	gboolean result = FALSE;
	GList *potiental_mates;

	potiental_mates = package->modifies;

	while ((result == FALSE) && 
	       (potiental_mates = 
		g_list_find_custom (potiental_mates, 
				    dep->version, 
				    (GCompareFunc)eazel_install_package_version_compare))!=NULL) {
		PackageData *modpack = PACKAGEDATA (potiental_mates->data);
#if EI2_DEBUG & 0x4
		trilobite_debug ("checking against %p %s-%s %p %s-%s %p %s-%s", 
				 package, 
				 package->name, package->version,
				 modpack,
				 modpack->name,
				 modpack->version,
				 dep, dep->name, dep->version);
#endif
		if (check_if_related_package (service, 
					      modpack, 
					      dep) == TRUE) {
#if EI2_DEBUG & 0x4
			trilobite_debug ("%s-%s seems to be a related package of %s-%s, which %s-%s modifies",
					 dep->name, dep->version, 
					 modpack->name, modpack->version,
					 package->name, package->version);
#endif
			result = TRUE;			
		}
		/* Skip one ahead */
		potiental_mates = g_list_next (potiental_mates);
	}

	return result;
}

static gboolean
check_update_for_no_more_file_conflicts (PackageFileConflict *conflict, 
					 PackageData *pack_update)
{
	GList *iterator;

	g_assert (IS_PACKAGEFILECONFLICT (conflict));
	g_assert (conflict->files);
	g_assert (g_list_length (conflict->files));

	for (iterator = conflict->files; iterator; iterator = g_list_next (iterator)) {
		char *filename = (char*)iterator->data;
		if (g_list_find_custom (pack_update->provides, 
					filename,
					(GCompareFunc)strcmp)) {
			return FALSE;
		}
	}
	return TRUE;
}

static gboolean
check_for_no_more_missing_features (PackageFeatureMissing *missing, 
				    PackageData *pack_update)
{
					/* FIXME: bugzilla.eazel.com 6811 */
					/* 
					   tjek that the update no longer requires 
					   missing->features;
					   for each F in missing->features {
					       for each D in pack_update->depends {
					           if F occurs in D->features pack_update still needs feature F
					       }
					   }
					   
					 */
	GList *iterator;

	for (iterator = missing->features; iterator; iterator = g_list_next (iterator)) {
		char *feature = (char*)iterator->data;
		GList *dep_iterator;

		for (dep_iterator = pack_update->depends; dep_iterator; dep_iterator = g_list_next (dep_iterator)) {
			PackageDependency *dep = PACKAGEDEPENDENCY (dep_iterator->data);
			PackageData *pack = dep->package;
			
			if (pack==NULL) { continue; }
			if (g_list_find (pack->features, feature)) {
				return FALSE;
			}
		}		
	}

	return TRUE;
}


static void
check_tree_helper (EazelInstall *service, 
		   PackageData *pack,
		   GList **extra_packages,
		   GList **path)
{
	GList *iterator;
	GList *remove = NULL;

	if (g_list_find (*path, pack)) {
#if EI2_DEBUG & 0x4
		trilobite_debug ("... %p %s recurses ..", pack, pack->name);
#endif
		return;
	}

	if (pack->status == PACKAGE_FILE_CONFLICT ||
	    pack->status == PACKAGE_BREAKS_DEPENDENCY) {
		if (eazel_install_get_upgrade (service)==FALSE) {
#if EI2_DEBUG & 0x4
			trilobite_debug ("cannot revive %s, update not allowed", pack->name);
#endif			
		}
#if EI2_DEBUG & 0x4
		trilobite_debug ("trying to revive %s", pack->name);
#else
		g_message ("trying to revive %s", pack->name);
#endif
		for (iterator = pack->breaks; iterator; iterator = g_list_next (iterator)) {
			PackageBreaks *breakage = PACKAGEBREAKS (iterator->data);

			PackageData *pack_broken = packagebreaks_get_package (breakage);
			PackageData *pack_update = NULL;
			gboolean update_available = FALSE;
			
			if (check_if_modified_related_package (service,
							       pack,
							       pack_broken)) {
				char *a, *b;
				a = packagedata_get_readable_name (pack);
				b = packagedata_get_readable_name (pack_broken);
				/* I18N note: both %s' are package names.
				   related is in the sense that the two packages apparently
				   are connected somehow, eg. foo and foo-devel */
				g_message (_("%s is related to %s"),
					   a, b);
				
					/* Create the pack_update */
				pack_update = packagedata_new ();
				pack_update->name = g_strdup (pack_broken->name);
				pack_update->version = g_strdup (pack->version);
				pack_update->distribution = pack_broken->distribution;
				pack_update->archtype = g_strdup (pack_broken->archtype);
				
					/* Try and get the info */
				if (eazel_softcat_get_info (service->private->softcat,
							    pack_update,
							    EAZEL_SOFTCAT_SENSE_EQ,
							    MUST_HAVE) 
				    != EAZEL_SOFTCAT_SUCCESS) {
					update_available = FALSE;
					        /* if we failed, delete the package object */
					gtk_object_unref (GTK_OBJECT (pack_update));
				} else {
						update_available = TRUE;
				}
				
				g_free (a);
				g_free (b);
			} else {
				update_available = 
					eazel_softcat_available_update (service->private->softcat,
									pack_broken,
									&pack_update,
									MUST_HAVE);
			}
			
			if (update_available) {
				if (post_get_softcat_info (service, &pack_update, SOFTCAT_HIT_OK) !=
				    SOFTCAT_HIT_OK) {
					update_available = FALSE;
				}								
			} 

			if (update_available) {
				gboolean proceed = TRUE;
				if (IS_PACKAGEFILECONFLICT (breakage)) {
					PackageFileConflict *conflict = PACKAGEFILECONFLICT (breakage); 
					
					if (!check_update_for_no_more_file_conflicts (conflict, pack_update)) {
						proceed = FALSE;
					}
				} else if (IS_PACKAGEFEATUREMISSING (breakage)) {
					PackageFeatureMissing *missing = PACKAGEFEATUREMISSING (breakage);
					
					if (!check_for_no_more_missing_features (missing, pack_update)) {
						proceed = FALSE;
					}					
				} 
				if (proceed) {
#if EI2_DEBUG & 0x4
					trilobite_debug ("adding %s to packages to be installed", 
							 pack_update->name);
#else
					g_message (_("updating %s to version %s-%s solves conflict"),
						pack_update->name, pack_update->version,
						pack_update->minor);
#endif
					/* ref the package and add it to the extra_packages list */
					gtk_object_ref (GTK_OBJECT (pack_update));
					(*extra_packages) = g_list_prepend (*extra_packages, 
									    pack_update);
					pack_update->status = PACKAGE_PARTLY_RESOLVED;
					remove = g_list_prepend (remove, breakage);
					/* reset pack_broken to some sane values */
					pack_update->toplevel = TRUE;
				} else {
#if EI2_DEBUG & 0x4
					trilobite_debug ("%s still has conflict", pack_update->name);
#else
					g_message (_("available update to %s (%s-%s) does not solves conflict"),
						   pack_update->name,
						   pack_update->version, pack_update->minor);
#endif
					gtk_object_unref (GTK_OBJECT (pack_update));
				}
			} else {
				g_message (_("could not revive %s"), pack->name);
			} 
		}

		/* Now nuke the successfully revived PackageBreaks */
		for (iterator = remove; iterator; iterator = g_list_next (iterator)) {
			PackageBreaks *breakage = PACKAGEBREAKS (iterator->data);
			gtk_object_unref (GTK_OBJECT (breakage));
			pack->breaks = g_list_remove (pack->breaks, breakage);
		}
		g_list_free (remove);

		/* if no breaks were unrevived, null out the list */
		if (g_list_length (pack->breaks)==0) {
			pack->status = PACKAGE_PARTLY_RESOLVED;
			g_list_free (pack->breaks);
			pack->breaks = NULL;
		}
	}
	
	for (iterator = pack->depends; iterator; iterator = g_list_next (iterator)) {
		PackageDependency *dep = PACKAGEDEPENDENCY (iterator->data);
		(*path) = g_list_prepend (*path, pack);
		check_tree_helper (service, dep->package, extra_packages, path);
		(*path) = g_list_remove (*path, pack);
	}
}

/* 
   Scan the tree for file conflicts. Add the broken packages to extra_packages
   and reset the status flag on the offending package. Since the prune_failed_packages
   is called at every step, we won't risk resetting a borked package to ok.
 */
void 
check_tree_for_conflicts (EazelInstall *service, 
			  GList **packages, 
			  GList **extra_packages)
{
	GList *iterator;
#if EI2_DEBUG & 0x4
	trilobite_debug ("-> check_tree_for_conflicts");
#endif
	for (iterator = g_list_first (*packages); iterator != NULL; iterator = g_list_next (iterator)) {
		PackageData *pack = PACKAGEDATA (iterator->data);
		GList *path = NULL;
		check_tree_helper (service, pack, extra_packages, &path);
	}
#if EI2_DEBUG & 0x4
	trilobite_debug ("<- check_tree_for_conflicts");
#endif
}

static gboolean
add_file_conflict (EazelInstall *service, 
		   PackageData *pack, 
		   PackageData *broken,
		   char *filename)
{
	GList *prev;

	if (check_if_related_package (service, pack, broken) == TRUE) {
#if EI2_DEBUG & 0x4				
		trilobite_debug ("\%p %s %p %s are related, ignoring conflict",
				 pack, pack->name,
				 broken, broken->name);
#endif	
		return FALSE;
	}

#if EI2_DEBUG & 0x4				
	trilobite_debug ("\tadding file conflict between %p %s %p %s",
			 pack, pack->name,
			 broken, broken->name);
#endif	

	prev = g_list_find_custom (pack->breaks, 
				   broken,
				   (GCompareFunc)find_break_by_package_name);
	pack->status = PACKAGE_FILE_CONFLICT;

	if (prev) {
		PackageFileConflict *conflict = PACKAGEFILECONFLICT (prev->data);
		conflict->files = g_list_prepend (conflict->files, g_strdup (filename));
	} else {
		PackageFileConflict *conflict = packagefileconflict_new ();
		
		pack->status = PACKAGE_FILE_CONFLICT;
		broken->status = PACKAGE_FILE_CONFLICT;
	
		packagebreaks_set_package (PACKAGEBREAKS (conflict), broken);
		conflict->files = g_list_prepend (conflict->files, g_strdup (filename));
		packagedata_add_to_breaks (pack, PACKAGEBREAKS (conflict));
		gtk_object_unref (GTK_OBJECT (conflict));
	}
	
	return TRUE;
}

/* make sure none of the packages we're installing will share files

   add code to check that no two packages provides the same file 

	hash<char*, PackageData> Hfile;

	for each p in packages {
		for each f in p->provides {
			if p'=Hfile[f] {	
				# f is provided by p'
				whine (about p' conflicting with p over file f)
				set: p breaks p'
			} else {
				Hfile[f] = p;
			}
		}
	}

   (rough draft by robey:)
*/
void 
check_no_two_packages_has_same_file (EazelInstall *service, 
				     GList *packages)
{
	GHashTable *file_table;		/* filename(char *) -> package(PackageData *) */
	GList *iter, *iter_file;
	PackageData *pack, *pack_other;
	char *filename;
	GList *flat_packages;

	flat_packages = flatten_packagedata_dependency_tree (packages);

	if (eazel_install_get_ignore_file_conflicts (service) ||
	    (g_list_length (flat_packages) == 1)) {
#if EI2_DEBUG & 0x4
		trilobite_debug ("(not performing duplicate file check)");
#endif
		g_list_free (flat_packages);
		return;
	}

	file_table = g_hash_table_new (g_str_hash, g_str_equal);

#if EI2_DEBUG & 0x4
	trilobite_debug ("-> no-two-packages conflict check begins (%d unique packages)", g_list_length (flat_packages));
#endif
	
	for (iter = g_list_first (flat_packages); iter != NULL; iter = g_list_next (iter)) {
		gboolean reported_yet = FALSE;
		int other_conflicts = 0;

		pack = PACKAGEDATA (iter->data);

		if (pack->suite_id) {
			continue;
		}

		g_message (_("file uniqueness checking %s"), pack->name);
		eazel_install_emit_file_uniqueness_check (service, pack);

		for (iter_file = g_list_first (pack->provides); iter_file != NULL; iter_file = g_list_next (iter_file)) {
			filename = (char *)(iter_file->data);

			pack_other = g_hash_table_lookup (file_table, filename);
			if (pack_other != NULL) {
				/* Dang, this file is provided by both 'pack' and 'pack_other' */
				/* Only report it once in the debug log or we'll spam to eternity on some
				 * large broken packages... */
				if (! reported_yet) {
#if EI2_DEBUG & 0x4
					trilobite_debug ("Duplicate file : %s occurs in %p %s and %p %s",
							 filename, pack, pack->name, 
							 pack_other, pack_other->name);
#else
					{
						char *a, *b;
						a = packagedata_get_readable_name (pack);
						b = packagedata_get_readable_name (pack_other);
						g_message (_("Duplicate file : %s occurs in %s and %s"),
							   filename, a, b);
						g_free (a);
						g_free (b);
					}
#endif
					reported_yet = TRUE;
				} else {
					other_conflicts++;
				}

				if (add_file_conflict (service, pack, pack_other, filename) == FALSE) {
					g_message (_("... but it's ok, the packages are related"));
				}
			} else {
				/* file is okay */
				g_hash_table_insert (file_table, filename, pack);
			}
		}
		if (other_conflicts) {
			g_message (_("(there were %d other conflicts)"), other_conflicts);
		}
	}

	/* let's free all this crap, unlike last time (cough cough) :) */
	/* elements in flat_packages are not to be unreffed */
	g_list_free (flat_packages);
	/* the hashentries point to strings inside the packagedata objects */
	g_hash_table_destroy (file_table);	

#if EI2_DEBUG & 0x4
	trilobite_debug ("<- no_two_packages conflict");
#endif
}

/* determine if package 'pack' is being upgraded in the 'packages' list,
 * and that upgrade no longer needs 'filename'
 */
static gboolean
package_is_upgrading_and_doesnt_need_file (PackageData *pack, GList *packages, const char *filename)
{
	PackageData *pack_upgrade;
	GList *item, *item2;

	/* Hmmm, would eazel_install_package_compare be better ? */
	item = g_list_find_custom (packages, pack->name,
				   (GCompareFunc)eazel_install_package_name_compare);
	if (item != NULL) {
		pack_upgrade = PACKAGEDATA (item->data);
		item2 = g_list_find_custom (pack_upgrade->provides, (char *)filename, (GCompareFunc)strcmp);
		if (item2 == NULL) {
			/* package IS in the package list, and does NOT provide this file anymore */
			return TRUE;
		}
	}
	return FALSE;
}

/* check if any of our packages contain files that would conflict with packages already installed.

   Add code to do conflict checking against already installed packages.

	for each p in packages {
		for each f in p->provides {
			L = query (PROVIDES, f)
			L -= p->modifies	# dont care about packages we're modifying
			if L.size > 0 {
				# someone else might own f
				foreach p' in L {
					next if p'->name == p->name
					if p' in packages (name compare && p->provides[f]==null) {
						# we're upgrading p' to p'' which doesn't have f, 
						# so all is well 
					} else {
						whine (p might break p' since they share f);
					}
				}
			} else {
				# f is fine, noone else owns it 
			}
		}
	}

    (rough draft by robey:)
*/
void 
check_conflicts_against_already_installed_packages (EazelInstall *service, 
						    GList *packages)
{
	GList *owners;
	GList *iter, *iter_file, *iter_pack;
	PackageData *pack, *pack_owner;
	char *filename;
	GList *flat_packages;

	if (eazel_install_get_ignore_file_conflicts (service)) {
#if EI2_DEBUG & 0x4
		trilobite_debug ("(not performing file conflict check)");
#endif
		return;
	}

	flat_packages = flatten_packagedata_dependency_tree (packages);

#if EI2_DEBUG & 0x4
	trilobite_debug ("-> file conflict check begins (%d unique packages)", g_list_length (flat_packages));
#endif

	for (iter = g_list_first (flat_packages); iter != NULL; iter = g_list_next (iter)) {
		pack = PACKAGEDATA (iter->data);

		if (pack->conflicts_checked || pack->suite_id) {
			continue;
		}

		g_message (_("file conflict checking %s"), pack->name);
		eazel_install_emit_file_conflict_check (service, pack);

		pack->conflicts_checked = TRUE;
		for (iter_file = g_list_first (pack->provides); iter_file != NULL; iter_file = g_list_next (iter_file)) {
			filename = (char *)(iter_file->data);

			/* If the file isn't on the system, no need to check for conflicts */
			if (g_file_test (filename, G_FILE_TEST_ISFILE) == 0) {
				continue;
			}

			owners = eazel_package_system_query (service->private->package_system,
							     service->private->cur_root,
							     filename,
							     EAZEL_PACKAGE_SYSTEM_QUERY_OWNS,
							     OWNS_MUST_HAVE);
			/* No need to check packages that we modify */	
			packagedata_list_prune (&owners, pack->modifies, TRUE, TRUE);

			for (iter_pack = g_list_first (owners); iter_pack != NULL; iter_pack = g_list_next (iter_pack)) {
				pack_owner = (PackageData *)(iter_pack->data);

				if (strcmp (pack_owner->name, pack->name) == 0) {
					/* can't conflict with self */
					gtk_object_unref (GTK_OBJECT (pack_owner));
					continue;
				}

#if EI2_DEBUG & 0x4
				trilobite_debug ("file conflict : package %p %s already provides %s also provided by %p %s",
						 pack_owner, pack_owner->name, filename, pack, pack->name);
#else
				{
					char *a, *b;
					a = packagedata_get_readable_name (pack_owner);
					b = packagedata_get_readable_name (pack);
					g_message (_("file conflict : package %s already provides file %s also provided by %s"),
						   a, filename, b);
					g_free (a);
					g_free (b);
				}
#endif
				if (package_is_upgrading_and_doesnt_need_file (pack_owner, 
									       flat_packages, filename)) {
					/* the owner of this file is a package that we're upgrading, and the
					 * new version no longer has this file, so everything's okay. */
					g_message (_("...but it is okay, we're upgrading %s and it ditched that file"),
						   pack_owner->name);
				} else {
					if (add_file_conflict (service, pack, pack_owner, filename) == FALSE) {
						g_message (_("... but it's ok, the packages are related"));
					}
				}
				gtk_object_unref (GTK_OBJECT (pack_owner));
			}
			g_list_free (owners);
		}
	}

#if EI2_DEBUG & 0x4
	trilobite_debug ("<- file conflict check ends");
#endif
	g_list_free (flat_packages);
}

/* 
   hash<char*, PackageData> Hfeat;
   
	for each p in packages {
		for each f in p->features {
			Hfeat[f] = p;
		}
	}

	for each p in packages {
		for each p' in p->modifies {
			for each f in p'->features {
				if Hfeat[f] {
					# all is well, feature is still on system 
				} else {
					# feature is lost 
					L = query (REQUIRES, f);
					if l.size > 0 {
						# and someone requires this feature 
						whine (p updates p' which breaks packages in L);
					}
				}
			}
		}
	}
*/
void 
check_feature_consistency (EazelInstall *service, 
			   GList *packages)
{
	GHashTable *feature_hash;
	GList *iterator;
	GList *flat_packages;

	flat_packages = flatten_packagedata_dependency_tree (packages);
#if EI2_DEBUG & 0x4
	trilobite_debug ("-> check_feature_consistency begins, %d packages", g_list_length (flat_packages));
#endif
	feature_hash = g_hash_table_new ((GHashFunc)g_str_hash,
					 (GCompareFunc)g_str_equal);

	for (iterator = flat_packages; iterator; iterator = g_list_next (iterator)) {
		PackageData *pack = PACKAGEDATA (iterator->data);
		GList *feature_it;

		if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (pack), "do_feature_consistency_check"))!=0) {
			continue;
		}

#if EI2_DEBUG & 0x4
		trilobite_debug ("%s provides %d features", pack->name, g_list_length (pack->features));
#endif
		for (feature_it = pack->features; feature_it; feature_it = g_list_next (feature_it)) {
			char *feature = (char*)feature_it->data;
			g_hash_table_insert (feature_hash, feature, pack);
#if EI2_DEBUG & 0x4
			trilobite_debug ("%s provides feature %s", pack->name, feature);
#endif
		}
	}

	for (iterator = flat_packages; iterator; iterator = g_list_next (iterator)) {
		PackageData *pack = PACKAGEDATA (iterator->data);
		GList *modify_it;

		if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (pack), "do_feature_consistency_check"))!=0) {
			continue;
		}
		gtk_object_set_data (GTK_OBJECT (pack), "do_feature_consistency_check", GINT_TO_POINTER (1));

#if EI2_DEBUG & 0x4
		trilobite_debug ("checking %s", pack->name);
#else
		g_message (_("checking feature consistency of %s"), pack->name);
#endif
		eazel_install_emit_feature_consistency_check (service, pack);

		for (modify_it = pack->modifies; modify_it; modify_it = g_list_next (modify_it)) {
			PackageData *pack_modified = PACKAGEDATA (modify_it->data);
			GList *feature_it;
#if EI2_DEBUG & 0x4
			trilobite_debug ("%s modifies %s", pack->name, pack_modified->name);
#endif

			for (feature_it = pack_modified->features; feature_it; feature_it = g_list_next (feature_it)) {
				char *feature = (char*)feature_it->data;

				if (g_hash_table_lookup (feature_hash, feature)) {
					/* Since feature was in hash, it is still provided by someone */
#if EI2_DEBUG & 0x4
					trilobite_debug ("feature %s is ok", feature);
#endif
				} else {
					GList *required_by;
					GList *break_it;
					/* Feature is lost */
#if EI2_DEBUG & 0x4
					trilobite_debug ("feature %s is lost", feature);
#endif
					required_by = eazel_package_system_query (service->private->package_system,
										  service->private->cur_root,
										  feature,
										  EAZEL_PACKAGE_SYSTEM_QUERY_REQUIRES_FEATURE,
										  PACKAGE_FILL_NO_DIRS_IN_PROVIDES);

#if EI2_DEBUG & 0x4
					if (g_list_length (required_by)==0) {
						trilobite_debug ("but noone uses it...");
					}
#endif
					for (break_it = required_by; break_it; break_it = g_list_next (break_it)) {
						PackageFeatureMissing *feature_missing = packagefeaturemissing_new ();
						PackageData *pack_broken = PACKAGEDATA (break_it->data);
						
						if (eazel_install_package_compare (pack_broken, pack_modified)==0) {
#if EI2_DEBUG & 0x4
							trilobite_debug ("%p %s, it's my child",
									 pack_broken, pack_broken->name);
#endif
							continue;
							
						}

						if (g_list_find_custom (pack->breaks, pack_broken,
									(GCompareFunc)find_break_by_package_name)) {
#if EI2_DEBUG & 0x4
							trilobite_debug ("%p %s, already marked as borked",
									 pack_broken, pack_broken->name);
#endif
							continue;

						}

						feature_missing = packagefeaturemissing_new ();

#if EI2_DEBUG & 0x4
						trilobite_debug ("%p %s is broken by %p %s modifying %p %s", 
								 pack_broken, pack_broken->name,
								 pack, pack->name,
								 pack_modified, pack_modified->name);
#else
						g_message (_("feature missing : %s breaks if %s is installed (feature %s would be lost"),
							   pack_broken->name,
							   pack->name,
							   feature);
#endif
						pack_broken->status = PACKAGE_DEPENDENCY_FAIL;
						pack->status = PACKAGE_BREAKS_DEPENDENCY;
						packagebreaks_set_package (PACKAGEBREAKS (feature_missing), pack_broken);
						feature_missing->features = g_list_prepend (feature_missing->features, g_strdup (feature));
						packagedata_add_to_breaks (pack, PACKAGEBREAKS (feature_missing));
						gtk_object_unref (GTK_OBJECT (pack_broken));
					}
					g_list_free (required_by);
				}
			}
		}
	}

	g_list_free (flat_packages);
	g_hash_table_destroy (feature_hash);

#if EI2_DEBUG & 0x4
	trilobite_debug ("<- check_feature_consistency");
	trilobite_debug ("POST-FEATURE-CHECK PACKAGE TREE");
	dump_tree (packages);
#endif
}


/* Main conflict entry point */
void
do_file_conflict_check (EazelInstall *service, 
			GList **packages,
			GList **extra_packages)
{
	g_assert (extra_packages);

	/* Check that no two packages in the work set provides the 
	   same files */
	check_no_two_packages_has_same_file (service, *packages);
	/* If so, prune them no, no point in trying to update */
	prune_failed_packages (service, packages); 

	/* Check for file conflicts against already installed packages,
	   if conflicts, try and revive */
	check_conflicts_against_already_installed_packages (service, *packages);
	do_requirement_consistency_check (service, *packages);
	check_tree_for_conflicts (service, packages, extra_packages);
	prune_failed_packages (service, packages); 

	/* If packages were revived (added to extra_packages,
	   exit and we'll be recursed, otherwise check
	   feature consistency */
	if (*extra_packages==NULL) {
		check_feature_consistency (service, *packages);
		check_tree_for_conflicts (service, packages, extra_packages);
		prune_failed_packages (service, packages);
	} else {
#if EI2_DEBUG & 0x4
		trilobite_debug ("extra_packages set, no doing feature consistency check");
#endif		
	}
}

/***********************************************************************************/

/* This is the download stuff */

gboolean download_packages (EazelInstall *service, GList *packages);

gboolean
download_packages (EazelInstall *service, 
		   GList *packages)
{
	GList *flat_packages;
	GList *iterator;
	gboolean result = TRUE;
	
	flat_packages = flatten_packagedata_dependency_tree (packages);
	g_message (_("downloading %d packages"), g_list_length (flat_packages));

	service->private->cancel_download = FALSE;
	for (iterator = flat_packages; iterator; iterator = g_list_next (iterator)) {
		PackageData *pack = PACKAGEDATA (iterator->data);
		if (pack->filename == NULL) {
			if (eazel_install_fetch_package (service, pack) == FALSE) {
				result = FALSE;
				break;
			}
			if (service->private->cancel_download) {
				result = FALSE;
				break;
			}
		}
	}

	g_list_free (flat_packages);

	return result;
}

/***********************************************************************************/

static gboolean
clean_up_dedupe_hash (const char *id, PackageData *pack)
{
	gtk_object_unref (GTK_OBJECT (pack));
	return TRUE;
}

static gboolean
clean_up_dep_ok_hash (char *key, gpointer unused)
{
	g_free (key);
	return TRUE;
}

unsigned long
eazel_install_get_total_size_of_packages (EazelInstall *service,
					  const GList *packages)
{
	const GList *iterator;
	unsigned long result = 0;
	for (iterator = packages; iterator; glist_step (iterator)) {
		PackageData *pack;

		pack = (PackageData*)iterator->data;
		result += pack->bytesize;
	}
	return result;
}

static gboolean
execute (EazelInstall *service,
	 GList *packages,
	 EazelPackageSystemOperation op,
	 int flags) 
{
	TrilobiteRootHelper *root_helper;
	GList *flat_packages;
	gboolean result = FALSE;

	flat_packages = flatten_packagedata_dependency_tree (packages);

	root_helper = gtk_object_get_data (GTK_OBJECT (service), "trilobite-root-helper");
	gtk_object_set_data (GTK_OBJECT (service->private->package_system), 
			     "trilobite-root-helper", root_helper);	

	if (eazel_install_get_test (service)) {
		flags |= EAZEL_PACKAGE_SYSTEM_OPERATION_TEST;
	}

	if (eazel_install_get_force (service)) {
		flags |= EAZEL_PACKAGE_SYSTEM_OPERATION_FORCE;
	}

	if (eazel_install_get_downgrade (service)) {
		flags |= EAZEL_PACKAGE_SYSTEM_OPERATION_DOWNGRADE;
	}

	if (eazel_install_get_upgrade (service)) {
		flags |= EAZEL_PACKAGE_SYSTEM_OPERATION_UPGRADE;
	}

	eazel_install_init_transaction (service);
	
	/* Init the hack var to emit the old style progress signals */
	service->private->infoblock [0] = 0; /* bytes of package completed */
	service->private->infoblock [1] = 0; /* total size of package */
	service->private->infoblock [2] = 0; /* number of packages */ 
	service->private->infoblock [3] = g_list_length (flat_packages);
	service->private->infoblock [4] = 0; /* total size completed */
	service->private->infoblock [5] = eazel_install_get_total_size_of_packages (service, flat_packages);

	switch (op) {
	case EAZEL_PACKAGE_SYSTEM_OPERATION_INSTALL:
		eazel_package_system_install (service->private->package_system,
					      service->private->cur_root,
					      flat_packages,
					      flags);
		break;
	case EAZEL_PACKAGE_SYSTEM_OPERATION_UNINSTALL:
		eazel_package_system_uninstall (service->private->package_system,
						service->private->cur_root,
						flat_packages,
						flags);
		break;
	case EAZEL_PACKAGE_SYSTEM_OPERATION_VERIFY:
		eazel_package_system_verify (service->private->package_system,
					      service->private->cur_root,
					      flat_packages);
		g_assert (0);
		break;
	}

	if (service->private->failed_packages == NULL) {
		result = TRUE;
		if (eazel_install_emit_save_transaction (service, packages) == TRUE) {
			eazel_install_save_transaction_report (service);
		}
	} 

	eazel_install_init_transaction (service);

	g_list_free (flat_packages);
	
	return result;
}

static void 
install_packages_helper (EazelInstall *service, 
			 GList **packages,
			 GList **extra_packages)
{
	g_assert (extra_packages);

#if EI2_DEBUG & 0x4
	trilobite_debug ("-> install_packages_helper");
#endif	
	do_dep_check (service, packages);
	if (service->private->cancel_download) {
		return;
	}
	do_file_conflict_check (service, packages, extra_packages);

#if EI2_DEBUG & 0x8
	trilobite_debug ("FINAL TREE BEGIN");
	dump_tree (*packages);
	if (*extra_packages) {
		trilobite_debug ("EXTRA PACKAGES BEGIN");
		dump_tree (*extra_packages);
	}
	trilobite_debug ("FINAL TREE END");
#endif
#if EI2_DEBUG & 0x4
	trilobite_debug ("<- install_packages_helper");
#endif	
	return;
}

static void
set_toplevel (PackageData *package,
	      EazelInstall *service)
{
	package->toplevel = TRUE;
}

#if 0
static void
expand_package_suites (EazelInstall *service, GList **packages)
{
	GList *iter, *newlist, *sublist;
	EazelSoftCatError err;
	PackageData *pack;

	newlist = NULL;
	for (iter = g_list_first (*packages); iter != NULL; iter = g_list_next (iter)) {
		pack = PACKAGEDATA (iter->data);
		if (pack->suite_id != NULL) {
			/* could be multiple packages */
			sublist = NULL;
			err = eazel_softcat_query (service->private->softcat, pack, 
						   EAZEL_SOFTCAT_SENSE_EQ, MUST_HAVE, &sublist);
			if (err != EAZEL_SOFTCAT_SUCCESS) {
				g_warning ("softcat query on suite (%s) failed", pack->suite_id);
				/* leave the package alone for now */
				newlist = g_list_prepend (newlist, pack);
			} else {
				gtk_object_unref (GTK_OBJECT (pack));
				newlist = g_list_concat (sublist, newlist);
			}
		} else {
			newlist = g_list_prepend (newlist, pack);
		}
	}
	*packages = newlist;
}
#endif

/***********************************************************************************/
/* This is the revert majick */

static GList *
get_packages_with_mod_flag (GList *packages,
			    PackageModification mod) 
{
	GList *it;
	GList *res;
	
	res = NULL;
	for (it = packages; it; it = g_list_next (it)) {
		PackageData *pack;
		if (IS_PACKAGEDATA (it->data)) {
			pack = PACKAGEDATA (it->data);
		} else if (IS_PACKAGEDEPENDENCY (it->data)) {
			pack = PACKAGEDEPENDENCY (it->data)->package;
		}

		if (pack->modify_status == mod) {
			res = g_list_prepend (res, pack);
		}
		if (pack->depends) {
			res = g_list_concat (res, 
					     get_packages_with_mod_flag (pack->depends, mod));
		}
		if (pack->modifies) {
			res = g_list_concat (res, 
					     get_packages_with_mod_flag (pack->modifies, mod));
		}

		pack->modify_status = PACKAGE_MOD_UNTOUCHED;
	}
	return res;
}

/* Function to prune the uninstall list for elements marked as downgrade */
static void
check_uninst_vs_downgrade (GList **inst, 
			   GList **down) 
{
	GList *it;
	GList *remove;
	
	remove = NULL;
	for (it = *inst; it; it = g_list_next (it)) {
		GList *entry;
		PackageData *pack;

		pack = (PackageData*)it->data;
		entry = g_list_find_custom (*down, pack->name, (GCompareFunc)eazel_install_package_name_compare);
		if (entry != NULL) {
			remove = g_list_prepend (remove, it->data);
		}
	}

	for (it = remove; it; it = g_list_next (it)) {
		(*inst) = g_list_remove (*inst, it->data);
	}
}

static void
debug_revert (PackageData *pack, gpointer modr)
{
	PackageModification mod = GPOINTER_TO_INT (modr);
	char *name = packagedata_get_readable_name (pack);

	switch (mod) {
	case PACKAGE_MOD_UPGRADED:
		g_message (_("will upgrade %s"), name);
		break;
	case PACKAGE_MOD_DOWNGRADED:
		g_message (_("will downgrade %s"), name);
		break;
	case PACKAGE_MOD_INSTALLED:
		g_message (_("will install %s"), name);
		break;
	case PACKAGE_MOD_UNINSTALLED:
		g_message (_("will uninstall %s"), name);
		break;
	default: break;
			
	};

	g_free (name);
}

/***********************************************************************************/
/* This is the uninstall dep check majick */

/*
static int
compare_break_to_package_by_name (PackageBreaks *breakage, PackageData *pack)
{
	PackageData *broken_package = packagebreaks_get_package (breakage);

	return eazel_install_package_compare (broken_package, pack);
}
*/

/* This traverses upwards in the deptree from the initial list, and adds
   all packages that will break to "breaks" */
static void
eazel_uninstall_upward_traverse (EazelInstall *service,
				 GList **packages,
				 GList **failed,
				 GList **breaks)
{
	GList *iterator;
	/*
	  Create set
	  add all packs from packages to set
	  dep check
	  for all break, add to packages and recurse
	 */
#if EI2_DEBUG & 0x4
	trilobite_debug ("--> eazel_uninstall_upward_traverse %d packages", g_list_length (*packages));
#endif

	g_assert (packages!=NULL);
	g_assert (*packages!=NULL);
	g_assert (breaks!=NULL);
	g_assert (*breaks==NULL);
	g_assert (failed!=NULL);

	/* Open the package system */

	/* Add all packages to the set */

	for (iterator = *packages; iterator; iterator = g_list_next (iterator)) {
		PackageData *pack = (PackageData*)iterator->data;
		GList *matches = NULL;
		GList *match_iterator;
		GList *tmp_breaks = NULL;
		GList *b_iterator = NULL;

		/* Get the packages required by pack */
#if EI2_DEBUG & 0x4
		trilobite_debug ("\t checking requirements by %p %s", pack, rpmname_from_packagedata (pack));
#endif
		matches = eazel_package_system_query (service->private->package_system,
						      service->private->cur_root,
						      pack,
						      EAZEL_PACKAGE_SYSTEM_QUERY_REQUIRES,
						      PACKAGE_FILL_NO_TEXT |
						      PACKAGE_FILL_NO_DIRS_IN_PROVIDES | 
						      PACKAGE_FILL_NO_DEPENDENCIES);
		
		/* For all of them, mark as a break conflict */
		for (match_iterator = matches; match_iterator; match_iterator = g_list_next (match_iterator)) {
			PackageData *requiredby = (PackageData*)match_iterator->data;;
			
			requiredby->status = PACKAGE_DEPENDENCY_FAIL;
			pack->status = PACKAGE_BREAKS_DEPENDENCY;
#if EI2_DEBUG & 0x4
			trilobite_debug ("\t %p %s requires %p %s", 
					 requiredby, requiredby->name, 
					 pack, pack->name);
#else
			{
				char *a, *b;
				a = packagedata_get_readable_name (requiredby);
				b = packagedata_get_readable_name (pack);
				g_message (_("%s requires %s"), a, b);
				g_free (a);
				g_free (b);
			}
#endif

			/* If the broken package is in packages, just continue */
			if (g_list_find_custom (*packages, requiredby->name,
						(GCompareFunc)eazel_install_package_name_compare)) {
#if EI2_DEBUG & 0x4
				trilobite_debug ("\t skip %p %s", requiredby, requiredby->name);
#endif
				continue;
			}

			/* only add to breaks if it's a new breakage */
			if (g_list_find_custom (*breaks, 
						(gpointer)requiredby, 
						(GCompareFunc)eazel_install_package_compare) == NULL) {
				PackageFeatureMissing *breakage = packagefeaturemissing_new ();
#if EI2_DEBUG & 0x4
				trilobite_debug ("\t Adding %p %s to breaks", requiredby, requiredby->name);
#endif
				(*breaks) = g_list_prepend ((*breaks), requiredby);
				packagebreaks_set_package (PACKAGEBREAKS (breakage), requiredby);
				packagedata_add_to_breaks (pack, PACKAGEBREAKS (breakage));
				gtk_object_unref (GTK_OBJECT (breakage));
			}

			/* If the pac has not been failed yet (and is a toplevel),
			   fail it */
			if (!g_list_find_custom (*failed, (gpointer)pack->name, 
						 (GCompareFunc)eazel_install_package_name_compare) &&
			    pack->toplevel) {
				(*failed) = g_list_prepend (*failed, pack);
			}
		}
		g_list_foreach (matches, (GFunc)gtk_object_unref, NULL);
		g_list_free (matches);

		/* Now check the packages that broke, this is where eg. uninstalling
		   glib begins to take forever */
		if (*breaks) {
			eazel_uninstall_upward_traverse (service, breaks, failed, &tmp_breaks);
		}
		
		/* Add the result from the recursion */
		for (b_iterator = tmp_breaks; b_iterator; b_iterator = g_list_next (b_iterator)) {
			(*breaks) = g_list_prepend ((*breaks), b_iterator->data);
		}
	}
	
	/* Remove the failed packages */
	for (iterator = *failed; iterator; iterator = g_list_next (iterator)) {
		(*packages) = g_list_remove (*packages, iterator->data);
	}

#if EI2_DEBUG & 0x1
	trilobite_debug ("post uninstall upward tree");
	dump_tree (*packages);
#endif
#if EI2_DEBUG & 0x4
	trilobite_debug ("<-- eazel_uninstall_upward_traverse");
#endif
}

static void
eazel_uninstall_check_for_install (EazelInstall *service,
				   GList **packages,
				   GList **failed)
{
	GList *iterator;
	GList *remove  = NULL;
	GList *result = NULL;

#if EI2_DEBUG & 0x4
	trilobite_debug ("--> eazel_uninstall_check_for_install");
#endif
	g_assert (packages);

	for (iterator = *packages; iterator; iterator = g_list_next (iterator)) {		
		PackageData *pack = (PackageData*)iterator->data;		

		if (eazel_package_system_is_installed (service->private->package_system,
						       service->private->cur_root,
						       pack->name,
						       pack->version,
						       pack->minor,
						       EAZEL_SOFTCAT_SENSE_EQ)) {			
			GList *qresult;
			GList *qresult_it;
			
			qresult = eazel_package_system_query (service->private->package_system,
							      service->private->cur_root,
							      pack->name,
							      EAZEL_PACKAGE_SYSTEM_QUERY_MATCHES,
							      PACKAGE_FILL_NO_DEPENDENCIES | 
							      PACKAGE_FILL_NO_TEXT);
			for (qresult_it = qresult; qresult_it; qresult_it = g_list_next (qresult_it)) {
				PackageData *match = PACKAGEDATA (qresult_it->data);
				gboolean proceed = FALSE;
				if (pack->version == NULL) {
					proceed = TRUE;
				}
				if (proceed == FALSE &&
				    eazel_package_system_compare_version (service->private->package_system,
									  pack->version,
									  match->version) == 0) {
					proceed = TRUE;
				}
				if (proceed) {					
					pack->toplevel = TRUE;
					packagedata_fill_in_missing (pack, match,
								     PACKAGE_FILL_NO_DEPENDENCIES | 
								     PACKAGE_FILL_NO_TEXT);
					result = g_list_prepend (result, pack);
					break;
				}
			}
			g_list_foreach (qresult, (GFunc)gtk_object_unref, NULL);
			g_list_free (qresult);
		} else {
#if EI2_DEBUG & 0x4
			trilobite_debug ("\t %p %s is not installed", pack, pack->name);
#endif
			pack->status = PACKAGE_CANNOT_OPEN;
			remove = g_list_prepend (remove, pack);
		}		
	}
	
	for (iterator = remove; iterator; iterator=g_list_next (iterator)) {
		(*packages) = g_list_remove (*packages, iterator->data);
		(*failed) = g_list_prepend (*failed, iterator->data);
	}
	g_list_free (remove);
	remove = NULL;
	
	g_list_free (*packages);
	(*packages) = result;
	
#if EI2_DEBUG & 0x4
	trilobite_debug ("<-- eazel_uninstall_check_for_install");
#endif
}

/* Calls the upward and downward traversal */
static void
eazel_uninstall_globber (EazelInstall *service,
			 GList **packages,
			 GList **failed)
{
	GList *iterator;
	GList *tmp;

	/*
	  call upward with packages
	  call downward with packages and &tmp
	  add all from &tmp to packages
	*/

#if EI2_DEBUG & 0x4
	trilobite_debug ("--> eazel_uninstall_globber %d packages", g_list_length (*packages));
#endif

	tmp = NULL;

	eazel_uninstall_check_for_install (service, packages, failed);
	for (iterator = *failed; iterator; iterator = g_list_next (iterator)) {
		eazel_install_emit_uninstall_failed (service, (PackageData*)iterator->data);
	}
	g_list_foreach (*failed, (GFunc)gtk_object_unref, NULL);
	g_list_free (*failed);
	(*failed)=NULL;

	/* If there are still packages and we're not forcing,
	   do upwards traversel */
	if (*packages && !eazel_install_get_force (service)) {
		eazel_uninstall_upward_traverse (service, packages, failed, &tmp);

#if EI2_DEBUG & 0x4
		if (g_list_length (*failed)) {
			trilobite_debug ("FAILED");
			dump_tree (*failed);
		}
#endif
		for (iterator = *failed; iterator; iterator = g_list_next (iterator)) {
			PackageData *pack = (PackageData*)iterator->data;
#if EI2_DEBUG & 0x4
			trilobite_debug ("failed %p %s", pack, pack->name);
#else
			{
				char *a;
				a = packagedata_get_readable_name (pack);
				g_message (_("failed %s"), a);
				g_free (a);
			}
#endif
			eazel_install_emit_uninstall_failed (service, pack);
		}
		g_list_foreach (*failed, (GFunc)gtk_object_unref, NULL);
		g_list_free (*failed);
		g_list_free (tmp);
	}

#if EI2_DEBUG & 0x4
	trilobite_debug ("<-- eazel_uninstall_glob");
#endif
}


/***********************************************************************************/

/* These are the methods exposed to the the rest of the service object */

EazelInstallOperationStatus 
install_packages (EazelInstall *service, GList *categories)
{
	EazelInstallOperationStatus result = EAZEL_INSTALL_NOTHING;       	
	GList *packages;
	GList *extra_packages = NULL;

	eazel_softcat_reset_server_update_flag (service->private->softcat);

	packages = packagedata_list_copy (categorylist_flatten_to_packagelist (categories), TRUE);

	/*
	expand_package_suites (service, &packages);
	*/

	g_list_foreach (packages, (GFunc)set_toplevel, service);
	do {
		extra_packages = NULL;
		install_packages_helper (service, &packages, &extra_packages);
		if (extra_packages) {
			GList *iterator;
			/* add the contents of extra_packages, but avoid inserting
			   dupes */
			for (iterator = extra_packages; iterator; iterator = g_list_next (iterator)) {
				PackageData *p = PACKAGEDATA (iterator->data);
				if (g_list_find_custom (packages, p,
							(GCompareFunc)eazel_install_package_compare)==NULL) {
					packages = g_list_prepend (packages, p);
				}
			}

		}
	} while (extra_packages != NULL);

#if EI2_DEBUG & 0x4
	trilobite_debug ("%d packages survived", g_list_length (packages));
#endif	

	if (packages) {
		if (eazel_install_emit_preflight_check (service, packages)) {
			int flags = 0;
			gboolean go_ahead = TRUE;
			
#if EI2_DEBUG & 0x4
			trilobite_debug ("emit_preflight returned true");
#endif	
			if (download_packages (service, packages)) {
				if (eazel_install_get_force (service) == FALSE) {
					if (!check_md5_on_files (service, packages)) {
						go_ahead = FALSE;
					}
				}
				if (go_ahead) {
					/* Execute the operation */
					if (execute (service, packages, EAZEL_PACKAGE_SYSTEM_OPERATION_INSTALL, flags)) {
						result = EAZEL_INSTALL_INSTALL_OK;
					} 
				}
			} else {
				/* FIXME: bugzilla.eazel.com 5722
				   download could fail, do the download func needs to
				   be able to fail the operation... */
			}
		}
	}

	g_hash_table_foreach_remove (service->private->dedupe_hash,
				     (GHRFunc)clean_up_dedupe_hash,
				     service);
	g_hash_table_foreach_remove (service->private->dep_ok_hash,
				     (GHRFunc)clean_up_dep_ok_hash,
				     service);
	g_list_foreach (packages, (GFunc)gtk_object_unref, NULL);

	return result;
}

EazelInstallOperationStatus 
uninstall_packages (EazelInstall *service, GList *categories)
{
	EazelInstallStatus result = EAZEL_INSTALL_NOTHING;
	GList *packages = NULL;
	GList *failed = NULL;	

	eazel_softcat_reset_server_update_flag (service->private->softcat);

#if EI2_DEBUG & 0x4
	trilobite_debug ("--> uninstall_packages");
#endif
	packages = packagedata_list_copy (categorylist_flatten_to_packagelist (categories), TRUE);	
	eazel_uninstall_globber (service, &packages, &failed);
	
	if (packages) {
		if (eazel_install_emit_preflight_check (service, packages)) {
			int flags = 0;
			if (execute (service, packages, EAZEL_PACKAGE_SYSTEM_OPERATION_UNINSTALL, flags)) {
				result = EAZEL_INSTALL_UNINSTALL_OK;
			}
		}
	}

	g_list_foreach (packages, (GFunc)gtk_object_unref, NULL);
	g_list_free (packages);

#if EI2_DEBUG & 0x4
	trilobite_debug ("\tuninstall returns returning %s", 
			 result == EAZEL_INSTALL_UNINSTALL_OK ? "OK" : "FAILED");
	trilobite_debug ("<-- uninstall_all_packages");
#endif
	return result;
}

EazelInstallStatus
revert_transaction (EazelInstall *service, 
		    GList *packages)
{
	GList *uninst, *inst, *upgrade, *downgrade;
	CategoryData *cat;
	GList *categories;
	EazelInstallStatus result = EAZEL_INSTALL_NOTHING;

	uninst = get_packages_with_mod_flag (packages, PACKAGE_MOD_INSTALLED);
	inst = get_packages_with_mod_flag (packages, PACKAGE_MOD_UNINSTALLED);
	upgrade = get_packages_with_mod_flag (packages, PACKAGE_MOD_DOWNGRADED);
	downgrade = get_packages_with_mod_flag (packages, PACKAGE_MOD_UPGRADED);

	check_uninst_vs_downgrade (&uninst, &downgrade);

	g_list_foreach (uninst, (GFunc)debug_revert, GINT_TO_POINTER (PACKAGE_MOD_UNINSTALLED));
	g_list_foreach (inst, (GFunc)debug_revert, GINT_TO_POINTER (PACKAGE_MOD_INSTALLED));
	g_list_foreach (downgrade, (GFunc)debug_revert, GINT_TO_POINTER (PACKAGE_MOD_DOWNGRADED));
	g_list_foreach (upgrade, (GFunc)debug_revert, GINT_TO_POINTER (PACKAGE_MOD_UPGRADED));

	cat = categorydata_new ();
	categories = g_list_prepend (NULL, cat);

	if (uninst) {
		eazel_install_set_uninstall (service, TRUE);
		eazel_install_set_downgrade (service, FALSE);
		eazel_install_set_upgrade (service, FALSE);
		cat->packages = uninst;
		if (uninstall_packages (service, categories) == EAZEL_INSTALL_UNINSTALL_OK) {
			result = EAZEL_INSTALL_REVERSION_OK;
		}
	}
	if (inst) {
		eazel_install_set_uninstall (service, FALSE);
		eazel_install_set_downgrade (service, FALSE);
		eazel_install_set_upgrade (service, FALSE);
		cat->packages = inst;
		if (install_packages (service, categories) == EAZEL_INSTALL_UNINSTALL_OK) {
			result = EAZEL_INSTALL_REVERSION_OK;
		}
	}
	if (downgrade) {
		eazel_install_set_uninstall (service, FALSE);
		eazel_install_set_downgrade (service, TRUE);
		eazel_install_set_upgrade (service, FALSE);
		cat->packages = downgrade;
		if (install_packages (service, categories) == EAZEL_INSTALL_UNINSTALL_OK) {
			result = EAZEL_INSTALL_REVERSION_OK;
		}
	}
	if (upgrade) {
		eazel_install_set_uninstall (service, FALSE);
		eazel_install_set_downgrade (service, TRUE);
		eazel_install_set_upgrade (service, TRUE);
		cat->packages = upgrade;
		if (install_packages (service, categories) == EAZEL_INSTALL_UNINSTALL_OK) {
			result = EAZEL_INSTALL_REVERSION_OK;
		}
	}


	categorydata_destroy (cat);
	g_list_free (categories);

	return result;
}