aboutsummaryrefslogtreecommitdiffstats
path: root/iurt2
blob: 36d51e8a7745ee7d71fa03e6983fb63c1089b5eb (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
#!/usr/bin/perl
#
# Copyright (C) 2005 Mandrakesoft
# Copyright (C) 2005,2006 Mandriva
# 
# Author: Florent Villard <warly@mandriva.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# compare and rebuild packages on different architecture
#
# TODO
#
# - use a cache (rpmctl cache for example) to find maintainer
# - add icecream compilation support
# - add a --set option to compile a set of packages
#
use strict;
use Hdlist;
use Data::Dumper;
use URPM;
use File::NCopy qw(copy);
use MIME::Words qw(encode_mimewords);
# I did not manage to make locks work over the network
#use File::lockf;
use Mkcd::Commandline qw(parseCommandLine usage);
use Filesys::Df qw(df);

my $program_name = 'iurt2';
# sessing parameters
my $arg = @ARGV;
my (@params, %run);
$run{todo} = [ ];
@params = ( 
    #    [ "one letter option", "long name option", "number of args (-X means ´at least X´)", "help text", "function to call", "log info"]
    #
    # no_rsync, config_help and copy_srpm kept for compatibility reasons
    #
    [ "", "$program_name", 0, "[--cache] [--chrooted-urpmi <media prefix>] [--concurrent-run] [--config foo value] [--warn] [--verbose integer]
            [--copy-srpm] [--debug] [--distro] [--no-rsync] [--clean user1 user2 user3] [--clean-all] [--shell] [--stop {p|c|i|l|b|a|s}]
 	    [--use-system-distrib] [--dir] [--help foo?] [--log filename] [--group] [--unionfs]
 	    [--upload [--markrelease] [--source]] [--dir] [--help foo?] [--log filename]  [--unionfs]
 	    {--config_help |
	    --chroot --arch {i586|x86_64|ppc} --distro {cooker|2006.0|community/2006.0|...} } |
 	    --rebuild {cooker|2006.0|community/2006.0|...} {i586|x86_64|ppc|...} {filename1.src.rpm} {filename2.src.rpm} ... {filenamen.src.rpm} }", 
    "$program_name is a perl script to rebuild automatically several rpm in chroot, given a sourcerpm repository, and mail authors or rebuilder when problems occurs.", 
    sub { $arg or usage($program_name, \@params) }, "" ],
    [ "", "distro", 1, "<distro>", 
    "Set the distribution",
    sub { ($run{distro}) = @_; 1 }, "Setting the distribution" ],
    [ "a", "arch", 1, "<architecture>", 
    "Set the architecture",
    sub { ($run{my_arch}) = @_; 1 }, "Setting architecture" ],
    [ "", "cache", 0, "", 
    "Use the global cache file",
    sub { $run{use_cache} = 1 }, "Activating cache use" ],
    [ "", "copy-srpm", 0, "", 
    "Copy also the regenerated SRPM",
    sub { $run{copy_srpm} = 1 }, "Activating the copy_srpm mode" ],
    [ "", "copy_srpm", 0, "", 
    "Copy also the regenerated SRPM",
    sub { $run{copy_srpm} = 1 }, "Activating the copy_srpm mode" ],
    [ "c", "chroot", 0, "", 
    "Check chroot and update it if needed",
    sub { $run{chroot} = 1 }, "Activating chroot updating" ],
    [ "", "chrooted-urpmi", 1, "<media prefix>", 
    "Create urpmi media inside the chroot instead of using --root (media prefix is like http:///server.mandriva.com/dis/)",
    sub { $run{chrooted_urpmi} = shift }, "Activating chroot updating" ],
    [ "", "clean-all", 0, "", 
    "Clean all remaining chroots for all the users",
    sub { $run{clean_all} = 1 }, "Activating clean chroot flag" ],
    [ "", "clean", -1, "<user 1> <user 2> ... <user n>", 
    "Clean remaining chroot before runing",
    sub { $run{clean} = \@_ }, "Activating clean chroot flag" ],
    [ "", "concurrent-run", 0, "", 
    "Allow several iurt to run on different machines (slower)",
    sub { $run{concurrent_run} = 1 }, "Activating concurrent run checks" ],
    [ "d", "dir", -1, "", 
    "Directory where to find packages to rebuild", 
    sub { $run{extra_dir} = \@_; 1 }, "Adding extra source packages directories" ],
    [ "", "config", 2, "<configuration keyword> <value>", 
    "Override a configuration file variable",
    sub { my ($key, $value) = @_; $run{config}{$key} = $value }, "Overriding configuration variable" ],
    [ "", "config-help", 0, "", 
    "Explain configuration files keywords", 
    sub { $run{config_usage} = 1 }, "Activating debug mode" ],
    [ "", "config_help", 0, "", 
    "Explain configuration files keywords", 
    sub { $run{config_usage} = 1 }, "Activating debug mode" ],
    [ "", "debug", 0, "", 
    "Activate debug mode", 
    sub { $run{debug} = 1 }, "Activating debug mode" ],
    [ "g", "group", 0, "", 
    "Activate group mode, packages will be compiled as a global set, not as individual packages", 
    sub { $run{group} = 1 }, "Activating group mode to compile packages as a set" ],
    [ "u", "unionfs", 0, "", 
    "Activate unionfs mode", 
    sub { $run{unionfs} = 1 }, "Activating unionfs mode" ],
    [ "l", "log", 1, "<log file>", 
    "Log file.", 
    sub { 
	$run{log} = pop @_; 
	open $run{LOG}, ">$run{log}" or die "unable to open $run{log}\n";
	print *{$run{LOG}}, "command line: @ARGV\n";
	1
    }, "Log file" ],
    [ "m", "media", -1, "", 
    "Media to rebuild", 
    sub { ($run{media}) = @_; 1 }, "Adding a media to rebuild" ],
    [ "r", "rebuild", -2, "<distro> <architecture> <srpm 1> <srpm 2> ... <srpm n>", 
    "Rebuild the packages, e.g. $program_name -r cooker x86_64 /home/foo/rpm/SRPMS/foo-2.3-12mdv2007.0.src.rpm", 
    sub { 
	$run{rebuild} = 1; 
	$run{distro} = shift @_;
       	$run{my_arch} = shift @_;
	foreach (@_) {
	    my ($path, $srpm);
	    if (m,(.*/)([^/]*.src.rpm)$, && -f $_) {
		($path, $srpm) = ( $1, $2 )
	    } elsif (m,([^/]*.src.rpm)$, && -f $_) {
		($path, $srpm) = ( './', $1 )
	    } else {
		die "FATAL iurt: $_ does not seems to be a SRPM\n"
	    }
	    my $hdr = rpm2header($_);
	    if (check_arch($hdr)) {
		print {$run{LOG}} "iurt: force build for $2 (from $1)\n";
		push @{$run{todo}}, [ $path, $srpm, 1 ]
	    } else {
		print {$run{LOG}} "ERROR iurt: $_ could not be build on $run{my_arch}, ignored.\n"
	    }
	}
	1
	}, "Activating rebuild mode" ],
    [ "", "upload", [
	["", "upload", 0, "[options]", 
	"Upload the rebuild packages", 
	sub {   my ($tmp) = @_;
	    $tmp->[0] ||= {};
	    1
	}, "Setting upload options"],
	[ "m", "markrelease", 0, "", 
	"Mark SVN directory when uploading the packages", 
	sub { $run{markrelease} = 1 }, "Adding markrelease repsys option" ],
	[ "s", "source", 0, "", 
	"Upload the source package as wells", 
	sub { $run{source_upload} = 1 }, "Setting source flag for upload" ],
    ], "[options]", 
    "Upload the rebuild packages", 
    sub { $run{upload} = 1 }, "Setting the upload flag" ],
    [ "", "no_rsync", 0, "", 
    "Do not send build log to the distant rsync server", 
    sub { $run{no_rsync} = 1 }, "Setting the no rsync warn flag" ],
    [ "", "no-rsync", 0, "", 
    "Do not send build log to the distant rsync server", 
    sub { $run{no_rsync} = 1 }, "Setting the no rsync warn flag" ],
    [ "", "use-system-distrib", 1, "<media>", 
    "Use the current system urpmi configuration", 
    sub { $run{use_system_distrib} = shift; 1 }, "Setting system distrib for urpmi configuration" ],
    [ "v", "verbose", 1, "<verbose level>", 
    "Give more info messages about what is going on (level from 1 to 10)", 
    sub { $run{verbose} = $_[0]; 1 }, "Setting verbose level" ],
    [ "w", "warn", 0, "", 
    "Warn maintainer of the packages about problem in the rebuild", 
    sub { $run{warn} = 1 ; 1 }, "Setting warn flag to warn maintainers" ],
    [ "", "shell", 0, "", 
    "Dump to a shell into the newly created chroot with sudo on rpm, urpmi, urpme and urpmi.addmedia", 
    sub { 
	($run{shell}) = 1; 
	1 }, "Setting option to dump to a shell" ],
    [ "", "stop", 1, "<rpm step>", 
    "Perform rpm -b<rpm step> (p c i l b a s) instead of rpm -ba and then open a shell in the chroot", 
    sub { 
	($run{stop}) = @_; 
	1 
    }, "Setting rpm build option" ],
);

open(my $LOG, ">&STDERR");
$run{LOG} = $LOG;

my $todo = parseCommandLine($program_name, \@ARGV, \@params);
@ARGV and usage($program_name, \@params, "@ARGV, too many arguments");
foreach my $t (@$todo)  {
    print {$run{LOG}} "$program_name: $t->[2]\n" if $run{verbose} > 5;
    &{$t->[0]}(@{$t->[1]}) or print {$run{LOG}} "ERROR: $t->[2]\n";
}

$run{distro_tag} = $run{distro};
$run{distro_tag} =~ s,/,-,g;

my $real_arch = `uname -m`;
chomp $real_arch;
my %arch_comp = (
    'i586' => { 'i386' => 1, 'i486' => 1, 'i586' => 1 },
    'i686' => { 'i386' => 1, 'i486' => 1, 'i586' => 1, 'i686' => 1 },
    'x86_64' => { 'i386' => 1, 'i486' => 1, 'i586' => 1, 'i686' => 1, 'x86_64' => 1 },
    'ppc' => { 'ppc' => 1 },
    'ppc64' => { 'ppc' => 1, 'ppc64' => 1 },
);

my $HOME = $ENV{HOME};
my $configfile = "$HOME/.iurt.$run{distro_tag}.conf";
print {$run{LOG}} "iurt: loading config file $configfile\n" if $run{verbose} > 1;
my $config;
if (-f $configfile) {
    $config = do $configfile or die "FATAL iurt: syntax error in $configfile";
} else {
    $config = {}
}
my $urpmi_options = "-v --no-verify-rpm --nolock --auto --ignoresize";
if ($run{use_system_distrib}) {
    $config->{basesystem_media_root} ||= $run{use_system_distrib}
} elsif ($run{chrooted_urpmi}) {
    my ($host) = $run{chrooted_urpmi} =~ m,(?:file|http|ftp)://([^/]*),;
    my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname $host;
    my $ip = join '.', unpack('C4',$addrs[0]);
    $ip =~ /\d+\.\d+\.\d+\.\d+/ or die "FATAL iurt: could not resolve $host ip address";
    $run{chrooted_urpmi} =~ s/$host/$ip/;
    $run{chrooted_media} = "$run{chrooted_urpmi}/$run{distro}/$run{my_arch}";
    print {$run{LOG}} "iurt: using $run{chrooted_media} as insallation media\n" if $run{verbose} > 2
} else {
    $urpmi_options .= " --use-distrib $config->{repository}/$run{distro}/$run{my_arch}"
}

if (!$run{chrooted_urpmi} && $run{group}) {
    die "FATAL iurt: option --chrooted-urpmi is mandatory if --group is selected"
} 

my %config_usage = ( 
    admin => { desc => 'Mail of the administrator of packages builds', default => '' },
    all_media => { desc => 'List of known media', default => [ 'main', 'contrib' ] },
    basesystem_media_root => { desc => 'Name of the media holding basesystem packages', default => sub { my ($config, $run) = @_; "$config->{repository}/$run{distro}/$run{my_arch}/" } },
    basesystem_media => { desc => 'Where to find basesystem packages', default => 'main' },
    cache_home => { desc => 'Where to store the cache files', default => "$HOME/.bugs" },
    cache_min_size => { desc => 'Minimal size to consider a cache file valid', default => 1000000 },
    distribution => { desc => 'Name of the packages distribution', default => 'Mandriva Linux' },
    home => { desc => 'Home dir', default => $HOME },
    install_chroot_binary => { desc => 'Tool used to create initial chroot', default => 'install-chroot-tar.sh' },
    local_home => { desc => 'Where to build packages', default => $HOME },
    local_upload => { desc => 'Where to store build packages and log', default => '' },
    log_size_limit => { desc => 'Maximum authorized size for a log file', default => '100M' },
    log_size_date => { desc => 'Number of days log should be kept', default => '30' },
    log_url => { desc => 'Where the log can be seen', default => '' },
    minimum_package_number => { "Minimum number of packages in a synthesis file to consider it valid", default => 1000 },
    no_mail  => { desc => 'Hash table with people mail address where we should not send any mails', default => {} },
    packager => { desc => 'Name of the build bot', default => 'Iurt' },
    repository => { desc => 'Prefix of the repositories', default => '/mnt/BIG/dis/' },
    rsync_to => { desc => 'Server where the result of the builds should be rsynced (name@server:path format)', default => ''},
    sendmail => { desc => 'If the bot will send mail reports regarding build', default => 0 },
    supported_arch => { desc => 'Table of supported architecture', default => ['i586', 'x86_64'] },
    upload => { desc => 'Where to copy build packages', default => "$HOME/uploads/" },
    vendor => { desc => 'Name of the packages vendor', default => 'Mandriva' },
   );
config_usage() if $run{config_usage};
$run{my_arch} or usage($program_name, \@params, "no architecture given (media $run{media}, run{my_arch} $run{my_arch}, todo @{$run{todo}})");
if (!$arch_comp{$real_arch}{$run{my_arch}}) {
    die "FATAL iurt: could not compile $run{my_arch} binaries on a $real_arch"
}
foreach my $k (keys %config_usage) {
    ref $config_usage{$k}{default} eq 'CODE' and next;
    $config->{$k} ||= $run{config}{$k} || $config_usage{$k}{default}
}
foreach my $k (keys %config_usage) {
    ref $config_usage{$k}{default} eq 'CODE' or next;
    my $a = $config_usage{$k}{default}($config, \%run);
    $config->{$k} ||= $run{config}{$k} || $a
}

$config->{upload} .= $run{distro};
$config->{upload} =~ s/community//g;
if ($run{distro} ne 'cooker') {
    if ($run{media} ne 'main') {
	$config->{upload} .= "/$run{media}"
    }
} elsif ($run{media} eq 'contrib') {
    $config->{upload} =~ s/cooker/contrib/g;
}

-d $config->{upload} or usage($program_name, \@params, "$config->{upload} does not exist");

# cache file name is needed early to remove the manual lock file if the lock mechanism does not work
my $cachefile = "$config->{cache_home}/iurt.$run{distro_tag}.$run{my_arch}.$run{media}.cache";
$run{cachefile} = $cachefile;
if (!$run{debug} && $run{media} || $run{chroot}) {
    $run{pidfile_home} = "$config->{cache_home}/";
    $run{pidfile} = "iurt.$run{distro_tag}.$run{my_arch}.$run{media}";
    check_pid(\%run)
}

my $debug_tag = '_debug' if $run{debug};
my $chroot_name = "chroot_$run{distro_tag}$debug_tag";
my $chroot = "$config->{local_home}/$chroot_name";
$run{chroot_path} = $chroot;
my $chroot_tar = "$chroot.$run{my_arch}.tar.gz";
$run{chroot_tar} = $chroot_tar;
if ($run{chroot}) {
    check_chroot($chroot, $chroot_tar, \%run) or die "FATAL iurt: could not prepare initial chroot"
}

my $cache;
my $clear_cache = 1;
if (-f $cachefile && $run{use_cache}) {
    print {$run{LOG}} "iurt: loading cache file $cachefile\n" if $run{verbose} > 1;
    $cache = do $cachefile or print "FATAL iurt: could not load cache $cachefile ($!)\n";
    if (!$cache) {
	opendir my $cache_dir, $config->{cache_home};
	my $to_load;
	foreach my $file (readdir $cache_dir) {
	    (my $date) = $file =~ /iurt\.$run{distro_tag}\.$run{my_arch}\.$run{media}\.cache\.tmp\.(\d{8})/ or next;
	    if ($date > $to_load && -s "$config->{cache_home}/$file" > $config->{cache_min_size}) {
		$to_load = $date;
		$cachefile = "$config->{cache_home}/$file"
	    }
	}
	print {$run{LOG}} "iurt: loading alternate cache file $cachefile\n";
	$cache = do $cachefile or print "FATAL iurt: could not load cache $cachefile ($!)\n"
    }
    $clear_cache = 0 if $cache
}

if ($clear_cache) {
    $cache = { rpm_srpm => {}, failure => {}, queue => {}, warning => {}, run => 1, needed => {}, no_unionfs => {} }
}
$run{cache} = $cache;

my (%srpm_version, @wrong_rpm, %provides, %pack_provide, $to_compile, %maint);
$to_compile = @{$run{todo}};
$to_compile += check_media(\%run, $cache, $config, \%srpm_version, \@wrong_rpm, \%provides, \%pack_provide, \%maint) if $run{media};
$to_compile += search_packages(1, $cache, \%run, @{$run{extra_dir}}) if $run{extra};

dump_cache(\%run);

print {$run{LOG}} "iurt: will try to compile $to_compile packages\n" if $run{verbose} > 1;

my ($fulldate, $daydate) = get_date();
if ($run{use_cache}) {
    $run{run} = $cache->{run};
    $cache->{run}++
} else {
    $run{run} = "0.$fulldate"
}
print {$run{LOG}} "iurt: using $run{run} as chroot extension\n" if $run{verbose} > 4;
$run{user} = $ENV{SUDO_USER} || $ENV{USER};
$run{uid} = getpwnam $run{user};
print {$run{LOG}} "iurt: using local user $run{user}, id $run{uid}\n" if $run{verbose} > 3;
my $luser = $run{user} || 'builder';

my $unionfs_dir;
if ($run{unionfs}) {
    # FIXME need to grep /proc/modules not ot try to load it if already loaded
    open my $modules, '/proc/modules';
    my $ok;
    while (my $m = <$modules>) {
	if ($m =~ /unionfs/) {
	    $ok = 1;
	    last
	}
    }
    if (!$ok) {
	print {$run{LOG}} "iurt: adding unionfs module\n" if $run{verbose} > 0;
	system("sudo /sbin/depmod -a");
	system("sudo /sbin/modprobe -f unionfs");
    }
    $unionfs_dir = "$config->{local_home}/iurt_unionfs$debug_tag/";
    remove_chroot(\%run, $unionfs_dir, \&clean_all_unionfs);
    $unionfs_dir = "$unionfs_dir/$run{user}/";
    -d $unionfs_dir or mkdir $unionfs_dir
}

my (%done, $wait_limit, $done);
$run{done} = \%done;
my $home = $config->{local_home};
my $union_id = 1;
my $unionfs_tmp = $run{unionfs};	
my $chroot_tmp = "$config->{local_home}/chroot_tmp";
if (!-d $chroot_tmp) {
    mkdir $chroot_tmp
} else {
    remove_chroot(\%run, $chroot_tmp, \&clean_all_chroot_tmp, $chroot_name);
}
$chroot_tmp = "$config->{local_home}/chroot_tmp/$run{user}";
if (!-d $chroot_tmp) {
    mkdir $chroot_tmp
}
$chroot_tmp = "$config->{local_home}/chroot_tmp/$run{user}/$chroot_name.$run{run}";

# now exit if there is nothing to do and it was just a cleaning pass
if (!@{$run{todo}} && !$run{debug} && !$run{shell} && !$run{rebuild}) {
    print {$run{LOG}} "iurt: nothing to do\n";
    unlink "$run{pidfile_home}/$run{pidfile}" if $run{pidfile};
    exit
}
print {$run{LOG}} "iurt: running with pid $$\n";

my $df = df $home;
if ($df->{per} == 100) {
    die "FATAL iurt: not enough space on the filesystem, only $df->{bavail} KB on $home, full at $df->{per}%"
}

if ($run{shell}) {
    ($union_id, my $unionfs_tmp, my $chroot_tmp) = create_temp_chroot(\%run, $cache, $unionfs_tmp, $unionfs_dir, $union_id) or die "FATAL iurt: could not create temporary chroot";
    add_local_user($chroot_tmp, \%run, $luser, $run{uid}) or die "FATAL iurt: could not add local user";
    $run{urpmi_command} = "urpmi $urpmi_options --root $chroot_tmp";
    add_packages(\%run, $config, $chroot_tmp, $luser, 'sudo', 'urpmi') or die "FATAL iurt: could not add urpmi and sudo in the chroot";
    add_sudoers(\%run, $luser);
    if ($run{shell}) {
	print {$run{LOG}} "iurt: dumping to a chrooted shell into $chroot_tmp\n";
	exec "sudo chroot $chroot_tmp /bin/su $luser -c bash";
	die "FATAL iurt: could not exec chroot to $chroot_tmp ($!)"
    }
}

$config->{local_upload} ||= $config->{local_home};
my $local_spool = "$config->{local_upload}/iurt/$run{distro_tag}/$run{my_arch}";
if (!-d $local_spool) {
    print {$run{LOG}} "iurt: creating local spool $local_spool\n" if $run{verbose} > 4;
    -d "$config->{local_upload}/iurt" or mkdir "$config->{local_upload}/iurt";
    my $d = "$config->{local_upload}/iurt/$run{distro_tag}";
    if (!-d $d) { mkdir $d or die "FATAL iurt: could not create local spool dir $d ($!)" }
    if (!-d $local_spool) {
	mkdir $local_spool;
	mkdir "$local_spool/log"
    }
}

# perform some cleaning before running to have some more space, rsync to the server too in case previous iurt crashed
if ($config->{rsync_to} && !$run{no_rsync}) { 
    	# remove some old and very big log files not to saturate the server
	system(qq|find $local_spool/log/ -name "*.log" \\( -size +$config->{log_size_limit} -or -mtime +$config->{log_size_date} \\) -exec rm -f {} \\;|);
	system("rsync --delete -alHPe 'ssh -xc arcfour' $local_spool/log/ $config->{rsync_to}/$run{distro_tag}/$run{my_arch}/log/");
}

print {$run{LOG}} "iurt: try to dump rpm macros to $chroot/home/builder/.rpmmacros\n" if $run{verbose} > 3;
if (!dump_rpmmacros("$chroot/home/builder/.rpmmacros")) {
     print "ERROR iurt: could not dump rpm macros to $chroot/home/builder/.rpmmacros, trying to rebuild the chroot";
     check_chroot($chroot, $chroot_tar, \%run) or die "FATAL iurt: could not prepare initial chroot";
     dump_rpmmacros("$chroot/home/builder/.rpmmacros") or die "FATAL iurt: could not dump rpm macros to $chroot/home/builder/.rpmmacros"
}

my $s = sub { 
    if ($run{main}) {
	print {$run{LOG}} "iurt: dumping cache...\n"; 
	dump_cache(\%run);
	$Data::Dumper::Indent = 0;
	$Data::Dumper::Terse = 1;
	print {$run{LOG}} "Running environment:\n", Data::Dumper->Dump([\%run]), "\n\n" if $run{verbose} > 5; 
	print {$run{LOG}} "Configuration:\n",Data::Dumper->Dump([$config]),"\n\n";
    }
    exit 
};
$SIG{TERM} = $s;
$SIG{INT} = $s;
$run{main} = 1;

my $local_media;
my $rebuild;
if ($run{group}) { 
    $rebuild = 1;
    order_packages(\%run) or die "FATAL iurt: could not order packages";
    $local_media = "$local_spool/$run{run}/"
}

do { 
    foreach (my $i ; $i < @{$run{todo}}; $i++) {
	my ($dir, $srpm, $status) = @{$run{todo}[$i]};
	$status or next;
	$done{$srpm} and next;
	$done{$srpm} = 1;
	check_version($srpm) or next;
	if ($run{debug}) { $run{debug}++ == 2 and exit }
	$done++;
	mkdir "$local_spool/log/$srpm";
	print {$run{LOG}} "iurt: packages $srpm [$done/$to_compile]\n";
	# FIXME unfortunately urpmi stalls quite often
	my $retry = 0;
	# 
	# current rpm is sometime segfaulting, and iurt is them blocked and cannot 
	#
	# $cache->{failure}{$srpm} = 1;
	# dump_cache(\%run);
	retry:
	if (!$run{chrooted_urpmi}) {
	    my $match = "urpmi $urpmi_options --root $chroot_tmp";
	    if (!clean_process($match, $run{verbose})) {
		dump_cache(\%run);
		die "FATAL iurt: Could not have urpmi working !"
	    }
	}
	($union_id, my $unionfs_tmp, my $chroot_tmp) = create_temp_chroot(\%run, $cache, $unionfs_tmp, $unionfs_dir, $union_id, $srpm) or next;

	if ($run{chrooted_urpmi}) { 
	    $run{urpmi_command} = "urpmi $urpmi_options --root $chroot_tmp ";
	    add_packages(\%run, $config, $chroot_tmp, $luser, 'urpmi') or next;
	    add_media(\%run, $config, $chroot_tmp, 'Main', "--distrib $run{chrooted_media}") or next;
	    if (-d "$local_media/hdlist.cz") {
		mkdir("$chroot_tmp/iurt_media/");
		opendir my $dir, $local_media;
		my $next;
		foreach my $f (readdir $dir) {
		    $f =~ /(\.rpm|^hdlist.cz)$/ or next;
		    if (!link "$local_media/$f", "$chroot_tmp/iurt_media") {
			if (!copy "$local_media/$f", "$chroot_tmp/iurt_media") {
			    print {$run{LOG}} "ERROR iurt: could not copy file $local_media/$f to $chroot_tmp/iurt_media";
			    $next = 1;
			    last
			}
		    }
		}
		next if $next;
		add_media(\%run, $config, $chroot_tmp, 'iurt_group', "iurt_group file:///iurt_media") or next
	    }
	    $run{urpmi_command} = "chroot $chroot_tmp urpmi $urpmi_options "
	} else {
	    $run{urpmi_command} = "urpmi $urpmi_options --root $chroot_tmp"
	}
	my ($srpm_name) = $srpm =~ /(.*)-[^-]+-[^-]+\.src\.rpm$/ or next;
	my ($maintainer, $cc);
	if (!$run{warn}) {
	    $maintainer = `rpmmon -s -p $srpm_name`;
	    $cc = "$maint{$srpm}";#, maintainers\@mandriva.com";
	    chomp $maintainer;
	    if (!$maintainer || $maintainer eq 'NOT_FOUND') {
		$maintainer = $cc;
		#$cc = 'maintainers@mandriva.com'
	    }
	}
	#($maintainer, $cc) = ($config->{admin},'');

	print {$run{LOG}} "iurt: adding local user $luser into $chroot_tmp...\n" if $run{verbose};
	add_local_user($chroot_tmp, \%run, $luser, $run{uid}) or next;

	my $ret = recreate_srpm(\%run, $config, $chroot_tmp, $dir, $srpm, $luser, $retry);
	if ($ret == -1) {
	    $retry = 1;
	    goto retry
	} elsif (!$ret) {
	    next
	} 
	
	print {$run{LOG}} "iurt: installing build dependencies of $srpm...\n" if $run{verbose};
	my $wait_urpmi = sub { 
	    print {$run{LOG}} "WARNING iurt: urpmi database locked, waiting...\n" if $run{debug};
	    sleep 30; 
	    $wait_limit++; 
	    if ($wait_limit > 8) { 
		$wait_limit = 0; system(qq{sudo pkill -9 urpmi &>/dev/null}) 
	    } };
	my $path_srpm =  $run{chrooted_urpmi} ? "/home/$luser/rpm/SRPMS/" : "$chroot_tmp/home/$luser/rpm/SRPMS/";
	# 
	# on x86_64 the rpm database is getting corrupted and sometimes rpm do not found anymore 
	# installed packages, retrying several time to be sure something is really broken
	if (!perform_command("sudo $run{urpmi_command} $path_srpm/$srpm", 
		\%run, $config,
		mail => $config->{admin},
		error => "[REBUILD] install of build dependencies of $srpm failed on $run{my_arch}",  
		hash => "install_deps_$srpm", 
		timeout => 600, 
		srpm => $srpm,
		freq => 1,
		#cc => $cc, 
		retry => 2,
		debug_mail => $run{debug},
		error_regexp => 'cannot be installed',
		wait_regexp => { 
		'database locked' => $wait_urpmi, 
		'is needed by' => sub { 
		    print {$run{LOG}} "WARNING iurt: rpm database seems corrupted, retrying\n"; 
		    system("sudo chroot $chroot_tmp rm -rf /var/lib/rpm/__db* &> /dev/null");
		    foreach (1 .. 3) { system("sudo chroot $chroot_tmp rpm -qa &> /dev/null") } }, 
	         }, 
		 wait_callback => $wait_urpmi,
		 log => "$local_spool/log/$srpm/",
		 callback => sub { 
			my ($opt, $output) = @_;
			print {$run{LOG}} "calling callback for $opt->{hash}\n" if $run{debug};
# 20060614 it seems the is needed urpmi error is due to something else (likely a database corruption error).	    
# my @missing_deps = $output =~ /(?:(\S+) is needed by )|(?:\(due to unsatisfied ([^[ ]*)(?: (.*)|\[(.*)\])?\))/g;
			my @missing_deps = $output =~ /\(due to unsatisfied ([^[ ]*)(?: (.*)|\[(.*)\])?\)/g;
			# as it seems that rpm db corruption is making urpmi returning false problem on deps installation, try to compile anyway
			@missing_deps or return 1;
			while (my $missing_deps = shift @missing_deps) {
			    my $version = shift @missing_deps;
			    my $version2 = shift @missing_deps;
			    $version ||= $version2 || 0;
			    my $p = $pack_provide{$missing_deps} || $missing_deps;
			    my $other_maint = `rpmmon -p "$p"`;
			    chomp $other_maint;
			    print {$run{LOG}} "missing dep: $missing_deps ($other_maint)\n";
			    if ($other_maint && $other_maint ne 'NOT_FOUND') {
				$opt->{mail} = "$maintainer, $other_maint";
				$opt->{error} = "[MISSING] $missing_deps, needed to build $srpm, is not available on $run{my_arch}";
			    }
			    # remember what is needed, and do not try to recompile until it is available
			    $cache->{needed}{$srpm}{$missing_deps} = { version => $version, maint => $other_maint || $maintainer };
			} 
			0
		    },
		)) {
		$run{status}{$srpm} = 'install_deps_failure';
		next;
	    }
	# try to workarround the rpm -qa db4 error(2) from dbcursor->c_get: No such file or directory
	# running rpm -qa several time seems to fix the problem
	system("sudo chroot $chroot_tmp rm -rf /var/lib/rpm/__db* &> /dev/null");
	foreach (1 .. 3) { system("sudo chroot $chroot_tmp rpm -qa &> /dev/null") }
	perform_command("sudo chroot $chroot_tmp rpm -qa", 
	    \%run, $config,
	    hash => "rpm_qa_$srpm", 
	    timeout => 60, 
	    debug_mail => $run{debug},
	    log => "$local_spool/log/$srpm/"); # or next; As this failed quite often, do not stop
	print {$run{LOG}} "Compiling $srpm\n" if $run{verbose}; 
	my $command = "rpm --rebuild /home/$luser/rpm/SRPMS/$srpm";
	if ($run{stop}) {
	    add_packages(\%run, $config, $chroot_tmp, $luser, 'urpmi', 'sudo');
	    add_sudoers(\%run, $config, $chroot_tmp, $luser);
	    $command = "rpm -b$run{stop} /home/$luser/rpm/SPECS/*.spec"
	}
	if (!perform_command(qq{TMP=/home/$luser/tmp/ sudo chroot $chroot_tmp /bin/su $luser -c "$command"}, 
		\%run, $config,
		#	mail => $maintainer, 
		error => "[REBUILD] $srpm from $run{distro_tag} does not build correctly on $run{my_arch}", 
		hash => "build_$srpm", 
		timeout => 18000, 
		srpm => $srpm,
		debug_mail => $run{debug},
		cc => $cc, 
		log => "$local_spool/log/$srpm/", 
		error_regexp => 'rror.*ailed|Bad exit status|RPM build error',
		callback => sub { 
		    my ($opt, $output) = @_;
		    if ($run{stop}) {
			print {$run{LOG}} "iurt: dumping to a chrooted shell into $chroot_tmp (pid $$)\n";
			# exec does not work because it seems stdin and out are shared between children
			system("sudo chroot $chroot_tmp /bin/su $luser -c bash");
			exit
		    }
		    print {$run{LOG}} "iurt: calling callback for $opt->{hash}\n" if $run{debug};
		    if ($unionfs_tmp && $output =~ /no space left on device/i) {
			print {$run{LOG}} "ERROR iurt: running out of space to compile $srpm in unionfs mode, will recompile it in normal mode\n";
			$cache->{no_unionfs}{$srpm} = 1;
			return 1
		    } elsif ($unionfs_tmp && $output =~ m,$home,) {
			print {$run{LOG}} "ERROR iurt: seems like building $srpm needs to access /proc/self/exe, which is broken with unionfs, will try to recompile it in non unionfs mode\n";
			$cache->{no_unionfs}{$srpm} = 1;
			return 1
		    }
		    1
		}, 
		freq => 1)) {
	    # FIXME
	    # The simple algo used here is : 
	    #  try to compile it with unionfs, if it runs out of space, compile it without the next time
	    #
	    #  This could be improved in keeping this srpm name for future version, but if we compile it 
	    #  on a new machine with more ram, or if next version compiles just fine with unionfs, we will
	    #  loose the unionfs advantage. 
	    #
	    #  Maybe the right thing to do would be to first try to increase the tmpfs size (more than 50 % of the 
	    #  physical RAM), but this will lead to more swap usage, and slower compilation (and lost of the unionfs
	    #  plus). Or to keep the faulty package a unionfs exception for some time, to save some more extra builds.
	    # 
	    if (!glob "$chroot_tmp/home/$luser/rpm/RPMS/*/*.rpm") {
		if ($unionfs_tmp && $cache->{no_unionfs}{$srpm}) {
		    goto retry
		}
		$cache->{failure}{$srpm} = 1;
		# 20060615
		$run{status}{$srpm} = 'build_failure';
		dump_cache(\%run);
		dump_status($local_spool, \%run);
		next
	    }
	}
	# do some cleaning if the compilation is successful
	delete $cache->{needed}{$srpm} if defined $cache->{needed}{$srpm};
	if (!perform_command("sudo urpmi $urpmi_options --root $chroot_tmp $chroot_tmp/home/$luser/rpm/RPMS/*/*.rpm", 
		\%run, $config,
		#	mail => $maintainer, 
		error => "[REBUILD] binaries packages generated from $srpm do not install correctly", 
		hash => "binary_test_$srpm", 
		srpm => $srpm,
		timeout => 600, 
		debug_mail => $run{debug},
		freq => 1, 
		wait_regexp => { 'database locked' => $wait_urpmi },
		error_regexp => 'unable to access',
		log => "$local_spool/log/$srpm/")) {
	    $cache->{failure}{$srpm} = 1;
	    $run{status}{$srpm} = 'binary_test_failure';
	    next
	}
	$run{status}{$srpm} = 'ok';
	delete $cache->{failure}{$srpm};
	if ($run{debug}) {
	    print {$run{LOG}} "iurt: debug mode, skip other packages\n";
	    exit
	} elsif ($run{group}) {
	    print {$run{LOG}} "iurt: group mode, keep packages for local media\n";
	    $run{done}{$srpm} = $done;
	    system("cp $chroot_tmp/home/$luser/rpm/RPMS/*/*.rpm $local_media &>/dev/null") and print {$run{LOG}} "ERROR: could not copy rpm files from $chroot_tmp/home/$luser/rpm/RPMS/ to $local_media ($!)\n";
	    system("cp $chroot_tmp/home/$luser/rpm/SRPMS/$srpm $local_media &>/dev/null") and print {$run{LOG}} "ERROR: could not copy $srpm from $chroot_tmp/home/$luser/rpm/SRPMS/ to $local_media ($!)\n";
	} else {
	    print {$run{LOG}} "iurt: build successful, copying packages to $local_spool.\n";
	    system("cp $chroot_tmp/home/$luser/rpm/RPMS/*/*.rpm $local_spool &>/dev/null") and print {$run{LOG}} "ERROR: could not copy rpm files from $chroot_tmp/home/$luser/rpm/RPMS/ to $local_spool ($!)\n";
	    if ($run{copy_srpm}) {
		system("cp $chroot_tmp/home/$luser/rpm/SRPMS/$srpm $local_spool &>/dev/null") and print {$run{LOG}} "ERROR: could not copy $srpm from $chroot_tmp/home/$luser/rpm/SRPMS/ to $local_spool ($!)\n";
	    }
	    process_queue($config, \%run, \@wrong_rpm, 1)
	}
	# dymp_cache each time so that concurrent process can get updated
	dump_cache(\%run) if $run{concurrent_run};
    }
    if ($run{group}) {
	$rebuild = 1 if order_packages(\%run)
    }
} while ($rebuild);

print {$run{LOG}} "iurt: reprocess generated packages queue\n" if $run{verbose};
process_queue($config, \%run, \@wrong_rpm);

dump_cache(\%run);

print {$run{LOG}} "ERROR iurt: RPM with a wrong SRPM name\n" if @wrong_rpm;
if (open my $file, ">$local_spool/log/wrong_srpm_names.log") {
    foreach (@wrong_rpm) {
	print $file "$_->[1] -> $_->[0] (", $cache->{rpm_srpm}{$_->[1]},")\n";
    }
    close $file
}

dump_status($local_spool, \%run);

if ($config->{rsync_to} && !$run{no_rsync}) { 
    	# remove some old and very big log files not to saturate the server
	system(qq|find $local_spool/log/ -name "*.log" \\( -size +$config->{log_size_limit} -or -mtime +$config->{log_size_date} \\) -exec rm -f {} \\;|);
	system("rsync --delete -alHPe 'ssh -xc arcfour' $local_spool/log/ $config->{rsync_to}/$run{distro_tag}/$run{my_arch}/log/");
}

# one last try to clean
print {$run{LOG}} "iurt: try to clean remaining unionfs\n" if $run{verbose};
if ($run{unionfs}) {
    my ($dir) = $unionfs_dir =~ /(.*)\/[^\/]+\/?/;
    remove_chroot(\%run, $dir, \&clean_all_unionfs)
}
unlink "$run{pidfile_home}/$run{pidfile}" if $run{pidfile};
exit;

sub config_usage {
	print "
	
	Iurt configuration keywords:
	
";
    $Data::Dumper::Indent = 0;
    $Data::Dumper::Terse = 1;
    foreach my $k (sort keys %config_usage) {
	print "	$k: $config_usage{$k}{desc} 
			default: ", Data::Dumper->Dump([$config_usage{$k}{default}]), ", current: ", Data::Dumper->Dump([$config->{$k}]),"\n"
    }
    print "\n\n";
    exit
}

sub clean_all_unionfs {
    my ($unionfs_dir) = @_;
    print {$run{LOG}} "Cleaning old unionfs remaining dir in $unionfs_dir\n" if $run{verbose} > 1;
    my $dir;
    if (!opendir $dir, $unionfs_dir) {
	print {$run{LOG}} "FATAL iurt: could not open $unionfs_dir ($!)";
	return
    }
    foreach (readdir $dir) {
	/unionfs\.((?:0\.)?\d+)\.(\d+)$/ or next;
	clean_unionfs($unionfs_dir, \%run, $1, $2);
    }
    closedir $dir
}

sub clean_all_chroot_tmp {
    my ($chroot_dir, $prefix) = @_;
    print {$run{LOG}} "Cleaning old chroot remaining dir in $chroot_dir\n" if $run{verbose};
    my $dir;
    if (!opendir $dir, $chroot_dir) { 
	print "ERROR iurt: could not open $chroot_dir ($!)";
	return
    }
    foreach (readdir $dir) {
	/$prefix.*$/ or next;
	clean_chroot_tmp($chroot_dir, $_);
    }
    closedir $dir
}

sub clean_chroot {
    my ($chroot, $run, $only_clean) = @_;
    if (-d $chroot) {
	system("sudo umount $chroot/proc &> /dev/null");
	perform_command("sudo rm -rf $chroot", 
	    $run, $config,
	    mail => $config->{admin},
	    error => "[REBUILD] Deleting of old chroot $chroot failed", 
	    hash => 'chroot_deletion', 
	    debug_mail => $run->{debug},
	    die => 1);
    }
   return 1 if $only_clean;
   mkdir $chroot;
   perform_command("pushd $chroot && sudo tar xvf $chroot_tar", 
		$run, $config,
		mail => $config->{admin}, 
		error => "[REBUILD] creating the initial chroot $chroot failed", 
		hash => 'chroot_init', 
		debug_mail => $run->{debug},
		die => 1);

   dump_rpmmacros("$chroot/home/builder/.rpmmacros") or return;
   system("sudo mount none -t proc $chroot/proc &>/dev/null") and return;
   1
}

sub clean_process {
    my ($match, $verbose) = @_;
    return clean($match, "pgrep -u root -f", "sudo pkill -9 -u root -f", $verbose);
}

sub clean_mnt {
    my ($mount_point, $verbose) = @_;
    return clean($mount_point, "/sbin/fuser", "sudo /sbin/fuser -k", $verbose);
}

sub clean {
    my ($var, $cmd, $kill_cmd, $verbose) = @_;
    my $ps;
    my $i;
    use POSIX ":sys_wait_h";
    while ($ps = `$cmd "$var"`) {
	system(qq{$kill_cmd "$var" &>/dev/null});
	sleep 1;
	$ps =~ s/\n/,/g;
	print {$run{LOG}} "Trying to remove previous blocked processes for $var ($ps)\n" if $verbose > 1;
	waitpid(-1, WNOHANG);
	return 0 if $i++ > 10
    }
    1
}

sub clean_unionfs {
    my ($unionfs_dir, $run, $r, $union_id) = @_;
    -d "$unionfs_dir/unionfs.$r.$union_id" or return $union_id;
    print {$run->{LOG}} "Cleaning $unionfs_dir/unionfs.$r.$union_id\n" if $run->{verbose} > 1;
    my $nok = 1;
    my $proc = "$unionfs_dir/unionfs.$r.$union_id/proc";
    while ($nok) {
	$nok = 0;
	if (-d $proc && check_mounted($proc, 'proc')) {
	    print {$run->{LOG}} "iurt iurt: umounting $proc\n" if $run->{verbose} > 2;
	    if (system("sudo umount $proc &>/dev/null")) { 
		print {$run->{LOG}} "ERROR iurt: could not umount $proc\n";
		return $union_id + 1
	    }
	}
	foreach my $t ("unionfs",'tmpfs') {
	    # unfortunately quite oftem the unionfs is busy and could not be unmounted
	    my $d = "$unionfs_dir/$t.$r.$union_id";
	    my $last;
	    if (-d $d && check_mounted($d, $t)) {
		$nok = 1;
		system("sudo /sbin/fuser -k $d &> /dev/null");
		print {$run->{LOG}} "iurt: umounting $d\n" if $run->{verbose} > 2;
		if (system(qq{sudo umount $d &> /dev/null})) {
		    print {$run->{LOG}} "WARNING iurt: could not umount $d ($!)\n" if $run->{verbose} > 1;
		    return $union_id + 1;
		}
	    }
	}
    }
    foreach my $t ("unionfs",'tmpfs') {
	my $d = "$unionfs_dir/$t.$r.$union_id";
	print {$run->{LOG}} "iurt: removing $d\n" if $run->{verbose} > 1;
	if (system(qq{sudo rmdir $d})) {
	    print {$run->{LOG}} "ERROR iurt: removing $d failed ($!)\n";
	    return $union_id + 1
	}
    }
    $union_id
}

sub clean_chroot_tmp {
    my ($chroot_dir, $dir) = @_;
    my $d = "$chroot_dir/$dir";
    if (system("sudo umount $d/proc &>/dev/null") && $run{verbose} > 1) { 
	print {$run{LOG}} "ERROR iurt: could not umount /proc in $d/\n" 
    }
    print {$run{LOG}} "iurt: cleaning $d\n" if $run{verbose};
    system("sudo /sbin/fuser -k $d &> /dev/null");
    print {$run{LOG}} "iurt: removing $d\n" if $run{verbose};
    system(qq{sudo rm -rf $d});
}

sub check_mounted {
    my ($mount_point, $type) = @_;
    my $mount;
    if (!open $mount, '/proc/mounts') {
	print 'ERROR iurt: could not open /proc/mounts';
	return
    }
    $mount_point =~ s,//+,/,g;
    while (<$mount>) {
	return 1 if /^\w+ $mount_point $type /
    }
    0
}

sub check_needed {
    my ($srpm) = @_;
    if (!defined $cache->{needed}{$srpm} && !ref $cache->{needed}{$srpm}) { return 1 } 
    my $ok = 1;
    foreach my $name (keys %{$cache->{needed}{$srpm}}) {
	my ($version, $maint) = $cache->{needed}{$srpm}{'version','maint'};
	my ($p_version) = $provides{$name};
	if ($p_version) {
	    next if $version == $p_version;
	    next if URPM::ranges_overlap($version, $p_version)
	}
	$ok = 0;
	if ($version) {
	    $cache->{needed}{$srpm}{$name}{version} = $version;
	}
	my $v ||= $version;
	print {$run{LOG}} "ERROR iurt: $srpm needs $name $v to be compiled.\n";
	# try to recompile it once in a while
	last if $cache->{warning}{"install_deps_$srpm"}{$maint}++ % 72;
	return 1
    }
    delete $cache->{needed}{$srpm};
    $ok
}

sub process_queue {
    my ($config, $run, $wrong_rpm, $quiet) = @_;
    return if !$run->{upload} && $quiet;
    my $dir = "$config->{local_upload}/iurt/$run->{distro_tag}/$run->{my_arch}";
    opendir my $rpmdir, $dir or return;
    foreach my $rpm (readdir $rpmdir) {
	my ($rarch, $srpm) = update_srpm($dir, $rpm, $wrong_rpm);
	$rarch or next;
	print {$run{LOG}} "iurt: $rpm\n";
	next if !$run->{upload};
	# recheck if the package has not been uploaded in the meantime
	my $rpms_dir = "$config->{repository}/$run->{distro}/$run->{my_arch}/media/$run->{media}/";
	if (! -f "$rpms_dir/$rpm") {
	    my $ok = copy "$dir/$rpm", "$config->{upload}/$config->{extra_subdir}/RPMS/";
	    # try to keep the opportunity to prevent disk full
	    if (!$ok){
		print {$run{LOG}} "ERROR process_queue: cannot copy $dir/$rpm to $config->{upload}/$config->{extra_subdir}/RPMS/ ($!)\n";
		next
	    }
	}
	if ($run->{upload_source}) {

	}
	unlink "$dir/$rpm";
	$cache->{queue}{$srpm} = 1
    }
    closedir $rpmdir;
}

sub update_srpm {
	my ($dir, $rpm, $wrong_rpm) = @_;
	my ($arch) = $rpm =~ /([^\.]+)\.rpm$/ or return 0;
	my $srpm = $cache->{rpm_srpm}{$rpm};
	if (!$srpm) {
		my $hdr = rpm2header("$dir/$rpm");
		$hdr or return 0;
		$srpm = $hdr->queryformat("%{SOURCERPM}");
		$cache->{rpm_srpm}{$rpm} = $srpm
	}
	$srpm = fix_srpm_name($srpm, $rpm, $wrong_rpm);
	$arch, $srpm
}

sub dump_cache {
    my ($run) = @_;
    my $filename = $run->{cachefile};
    my $cache = $run->{cache};
    # Right now there are no mechanism of concurrent access/write to the cache. There is 
    # on global lock for one iurt session. A finer cache access would allow several iurt running
    # but the idea is more to have a global parrallel build than several local ones.
    return if $run->{debug} || !$run->{use_cache};
    open my $file, ">$filename.tmp.$daydate" or die "FATAL iurt dump_cache: cannot open $filename.tmp";
    if ($run{concurrent_run}) {
	print {$run{log}} "iurt: merging cache";
	my $old_cache;
	if (-f $filename) {
	    print {$run{LOG}} "iurt: loading cache file $cachefile\n" if $run{verbose} > 1;
	    $old_cache = do $cachefile;
	    foreach my $k ('rpm_srpm', 'failure', 'no_unionfs', 'queue', 'needed', 'warning') {
		foreach my $rpm (%{$old_cache->{$k}}) {
		    $cache->{$k}{$rpm} ||= $old_cache->{$k}{$rpm}
		}
	    }
	}
	#  $cache = { rpm_srpm => {}, failure => {}, queue => {}, warning => {}, run => 1, needed => {}, no_unionfs => {} }
    }
    $Data::Dumper::Indent = 1;
    $Data::Dumper::Terse = 1;
    print $file Data::Dumper->Dump([ $cache ], [ "cache" ]);
    # flock does not work on network files and lockf seems to fail too
    my $status = 1; #File::lockf::lock($file);
    if (!$status) {
	unlink $filename;
	link "$filename.tmp.$daydate", $filename;
	File::lockf::ulock($file)
    } else {
	print {$run{LOG}} "WARNING iurt: locking the cache file $cachefile failed (status $status $!), try to lock manually\n";
	if (-f "$filename.lock") {
	    print {$run{LOG}} "ERROR iurt: manual file lock exist, do not save the cache\n";
	} else {
	    open my $lock, ">$filename.lock";
	    print $lock $$;
	    close $lock;
	    unlink $filename;
	    link "$filename.tmp.$daydate", $filename;
	    unlink "$filename.lock"
	}
    }
}

sub sendmail {
	my ($to, $cc, $subject, $text, $from, $debug) = @_;
	do { print "Cannot find sender-email-address [$to]\n"; return } unless defined($to);
	$from ||= $config->{packager};
	my $MAIL;
	if (!$debug) { open $MAIL,  "| /usr/sbin/sendmail -t" or return } else { open $MAIL,  ">&STDOUT" or return }
	my $sender = encode_mimewords($to);
	my $subject = encode_mimewords($subject);
	print $MAIL "To: $sender\n";
	if ($cc) { $cc = encode_mimewords($cc); print $MAIL "Cc: $cc\n" }
	print $MAIL "From: $from\n";
	print $MAIL "Subject: $subject\n";
	print $MAIL "\n";
	print $MAIL $text; 
	close($MAIL)
}

sub check_arch {
	my ($hdr) = @_;
	my (@exclusive_arch) = $hdr->queryformat('%{EXCLUSIVEARCH}');
	return if ! grep { $_ eq $run{my_arch} || $_ eq '(none)' } @exclusive_arch;
	my (@exclude_arch) = $hdr->queryformat('%{EXCLUDEARCH}');
	return if grep { $_ eq $run{my_arch} } @exclude_arch;
	1
}

sub check_version {
	my ($srpm) = @_;
	my ($srpm_name) = $srpm =~ /(.*)-[^-]+-[^-]+\.src\.rpm/;
	if (URPM::ranges_overlap("= $srpm",">= $srpm_version{$srpm_name}")) {
		$srpm_version{$srpm_name} = $srpm;
		return 1
	}
	0
}

sub fix_srpm_name {
	my ($srpm, $rpm, $wrong_rpm) = @_;
	my $old_srpm = $srpm;
	if ($srpm =~ s/^lib64/lib/){
		push @$wrong_rpm, [ $old_srpm, $rpm ];
		$cache->{rpm_srpm}{$rpm} = $srpm
	}
	$srpm
}

sub perform_command {
    my ($command, $run, $config, %opt) = @_;
    $opt{timeout} ||= 300;
    $opt{freq} ||= 24;
    print {$run{LOG}} "Timeout $opt{timeout}\n" if $run{verbose} > 2;
    # from alarm perldoc
    my ($output, $fulloutput, $comment);
    my ($kill, $pipe);
    if ($opt{debug}) {
	print "Would have rum $command with a timeout of $opt{timeout}\n";
	return 1
    }
    local $SIG{PIPE} = sub { print "Broken pipe!\n"; $pipe = 1 };
    my $retry = $opt{retry} || 1;
    my $call_ret = 1;
    my ($err, $pid, $try);
    my $logfile = "$opt{log}/$opt{hash}.$run->{run}.log";
    while ($retry) {
	$try++;
	if ($opt{retry} > 1) {
	    $logfile = "$opt{log}/$opt{hash}-$try.$run->{run}.log";
	}
	if ($opt{log}) {
	    my $parent_pid = $$;
	    $pid = fork;
	    #close STDIN; close STDERR;close STDOUT;
	    my $tot_time;
	    if (!$pid) {
		print {$run{LOG}} "Forking to monitor log size\n" if $run{verbose} > 2;
		$run->{main} = 0;
		local $SIG{ALRM} = sub { exit };
		$tot_time += sleep 30;
		my $size_limit = $config->{log_size_limit};
		$size_limit =~ s/k/000/i;
		$size_limit =~ s/M/000000/i;
		$size_limit =~ s/G/000000000/i;
		while ($tot_time < $opt{timeout}) {
		    my (@stat) = stat $logfile;
		    if ($stat[7] > $size_limit) {
			print {$run{LOG}} "WARNING: killing current command because of log size exceeding limit ($stat[7] > $config->{log_size_limit})\n";
			kill 14, "-$parent_pid";
			exit
		    }
		    my $df = df $opt{log} ;
		    if ($df->{per} == 100) {
			print {$run{LOG}} "WARNING: killing current command because running out of disk space (only $df->{bavail}KB left)\n";
			kill 14, "-$parent_pid";
			exit
		    }
		    $tot_time += sleep 30;
		}
		exit
	    }
	}
	eval {
	    local $SIG{ALRM} = sub { print "Timeout!\n"; $kill = 1; die "alarm\n" }; # NB: \n required
	    alarm $opt{timeout};
	    print {$run{LOG}} "$command\n" if $run{verbose} > 2;
	    if ($opt{log}) {
		#$output = `$command 2>&1 2>&1 | tee $opt{log}/$opt{hash}.$run.log`;
		system("$command 2>&1 >> $logfile");
	    } else {
		$output = `$command 2>&1`;
	    }
	    alarm 0
	};
	$err = $?;
	# kill pid watching log file size
	if ($pid) {
	    kill_for_good($pid);
	}
	if ($@) {
	    # timed out
	    die "FATAL iurt: unexpected signal" unless $@ eq "alarm\n";   # propagate unexpected errors
	}
	# Keep the run first on the harddrive so that one can check the command status tailing it
	if ($opt{log} && open my $log, $logfile) {
	    local $/;
	    $output = <$log>
	}
	$fulloutput .= $output;
	if (ref $opt{callback}) {
	    $call_ret = $opt{callback}(\%opt, $output);
	    $call_ret == -1 and return 1;
	    $call_ret == -2 and return 0
	}
	if ($kill) {
	    $comment = "Command has been killed after $opt{timeout} seconds: $command\n";
	    my ($cmd_to_kill) = $command =~ /sudo(?: chroot \S+)? (.*)/;
	    clean_process($cmd_to_kill, $run{verbose})
	} elsif ($pipe) {
	    $comment = "Command receives a broken pipe: $command\n";
	    sendmail($config->{admin}, '' , "$opt{hash} on $run->{my_arch} for $run->{media}: broken pipe", "$comment\n$output", 0, 0, $opt{debug_mail});
	} else {
	    $comment = "Command failed: $command\n"
	}
	# Maybe this has to be put before all the commands altering the $output var
	my $inc;
	if ($opt{wait_regexp}) {
	    foreach my $wr (keys %{$opt{wait_regexp}}) {
		if ($output =~ /$wr/m) {
		    $inc = $opt{wait_callback}(\%opt, $output) if ref $opt{wait_callback};
		    print {$run->{LOG}} "ERROR iurt: $wr !\n";
		    sendmail($config->{admin}, '' , "$opt{hash} on $run->{my_arch} for $run->{media}: could not proceed", "$wr\n\n$comment\n$output", 0, 0, $opt{debug_mail});
		}
	    }
	}
	if ($inc) {
	    $retry += $inc	    
	} elsif ($call_ret && !$kill && !$err && !$opt{error_regexp} || $fulloutput !~ /$opt{error_regexp}/) {
	    $retry = 0
	} else {
	    $retry-- 
	}
    }
    if (!$call_ret || $kill || $err || $opt{error_regexp} && $fulloutput =~ /$opt{error_regexp}/) {
	if ($opt{log} && $config->{log_url}) {
	    $comment = qq|See $config->{log_url}/$run{distro_tag}/$run{my_arch}/log/$opt{srpm}/\n\n$comment|
	}
	my $out;
	if (length $fulloutput < 10000) {
	    $out = $fulloutput
	} else { 
	    $out = "Message too big, see http link for details\n"
	}

	if ($opt{mail} && $config->{sendmail} && !$config->{no_mail}{$opt{mail}}) {
	    if (! ($cache->{warning}{$opt{hash}}{$opt{mail}} % $opt{freq})) {
		my $cc = join ',', grep { !$config->{no_mail}{$_} } split ',', $opt{cc};
		sendmail($opt{mail}, $cc,  $opt{error} , "$comment\n$out", 0, 0, $opt{debug_mail});
	    } elsif ($config->{admin}) {
		sendmail($config->{admin}, '' , $opt{error}, "$comment\n$out", 0, 0, $opt{debug_mail});
	    }
	}
	$cache->{warning}{$opt{hash}}{$opt{mail}}++;
	print {$run->{LOG}} "\n$comment\n$fulloutput\n";
	if ($opt{die}) {
	    dump_cache($run);
	    die "FATAL iurt: $opt{error}."
	}
	return 0
    }
    1
}

sub kill_for_good {
    my ($pid) = @_;
    use POSIX ":sys_wait_h";
    kill 14, $pid;
    sleep 1;
    waitpid(-1, WNOHANG);
    if (getpgrp $pid != -1) {
	kill 15, $pid;
	sleep 1;
	waitpid(-1, WNOHANG);
	if (getpgrp $pid  != -1) {
	    print STDERR "WARNING iurt: have to kill -9 pid $pid\n";
	    kill 9, $pid;
	    sleep 1;
	    waitpid(-1, WNOHANG);
	}
    }
}

sub check_chroot {
    my ($chroot, $chroot_tar, $run, $opt) = @_;
    print {$run{LOG}} "iurt: checking basesystem tar\n" if $run{verbose};
    system(qq{sudo pkill -9 -u root -f "urpmi $urpmi_options --root $chroot" &> /dev/null});
    if (!clean_chroot($chroot, $run, 1)) {
	print "ERROR iurt: Could no prepare initial chroot";
	return
    }
    perform_command("sudo $config->{install_chroot_binary} $config->{basesystem_media_root} $config->{basesystem_media_root}/media/$config->{basesystem_media} $chroot_tar $chroot 501 basesystem tar rpm-build rpm-mandriva-setup-build", 
	$run, $config,
	mail => $config->{admin},
	error => "[REBUILD] Creating the inital chroot for $run->{distro_tag} on $run->{my_arch} failed", 
	hash => 'chroot_inititialization', 
	timeout => 600, 
	debug_mail => $run->{debug},
	die => 1);
}

sub dump_rpmmacros {
	my ($file) = @_;
	my $f;
	if (!open $f, qq{| sudo sh -c "cat > $file"}) {
	   print {$run{LOG}} "ERROR iurt: could not open $file ($!)\n";
	   return 0
	}
	print $f qq{\%_topdir                \%(echo \$HOME)/rpm
\%_tmppath               \%(echo \$HOME)/rpm/tmp/
\%distribution           $config->{distribution}
\%vendor                 $config->{vendor}
\%packager               $config->{packager}};
        close $f;
	-f $file or return 0;
	1
}

sub check_pid {
    my ($run) = @_;
    my $hostname = `hostname`;
    chomp $hostname;
    my $pidfile = $run->{pidfile};
    my $lockfile = "$run->{pidfile_home}/$pidfile.$hostname.pid.lock";
    print {$run->{LOG}} "iurt: trying to lock $lockfile\n";
    open my $lock, ">$lockfile";
    my $lock_ok;
    # lockf seems not to work, try to workarround, but this start to create lock on the lock for the lock of the file.
    my $status = 1; #File::lockf::lock($lock);
    if (!$status) {
	$lock_ok = 1;
    } else {
	print {$run->{LOG}} "ERROR iurt: could not lock pid file (status $status $!)\n";
	if (! -f "$lockfile.2") {
	    print {$run->{LOG}} "iurt: using $lockfile.2 as lock file\n";
	    open my $lock2, ">$lockfile.2" or die "FATAL iurt: could not open lock file $lockfile.2";
	    print $lock2 $$;
	    close $lock2;
	}
    }
    if (!$run->{concurrent_run}) {
	opendir my $dir, $run->{pidfile_home};
	foreach my $f (readdir $dir) {
	    my ($pid_host) = $f =~ /$pidfile\.pid\.(.*)\.pid$/ or next; 
	    if ($pid_host ne $hostname) {
		my $pf = "$run->{pidfile_home}/$f";
		open my $test_PID, $pf;
		my $pid = <$test_PID>;
		my (@stat) = stat $pf;
		my $time = $stat[9];
		my $diff = time - $time;
		my $msg = "iurt: an other iurt is running for $run->{my_arch} on $pid_host, pid $pid, since $diff seconds";
		if ($diff < 36000) {
		    print {$run->{LOG}} "$msg\n";
		    exit
		} else {
		    print {$run->{LOG}} "$msg, ignoring it\n"
		}
	    }
	}
	$run->{pidfile} .= ".pid";
    }
    $run->{pidfile} .= ".$hostname.pid";
    $pidfile = "$run->{pidfile_home}/$run->{pidfile}";
    if (-f $pidfile)  {
	my (@stat) = stat $pidfile;
	open my $test_PID, $pidfile;
	my $pid = <$test_PID>;
	close $test_PID;
	if (!$pid) {
	    print {$run->{LOG}} "ERROR iurt: invalid pidfile ($pid), should be <pid>";
	    unlink $pidfile
	}
	if ($pid && getpgrp $pid != -1) {
	    my $time = $stat[9];
	    if ($time < time - 36000) {
		print {$run->{LOG}} "iurt: an other iurt pid $pid is running for a very long time, killing it\n";
		my $i;
		while ($i < 5 && getpgrp $pid != -1) {
		    kill_for_good($pid);
		    $i++;
		    sleep 1
		}
	    } else  {
		print {$run->{LOG}} "iurt: an other iurt is running for $run->{my_arch}, pid $pid, since ",time - $time," seconds\n";
		exit
	    }
	} else {
	    print {$run->{LOG}} "iurt: a previous iurt for $run->{my_arch} seems dead, cleaning.\n";
	    unlink $pidfile
	}
    }
    print {$run->{LOG}} "iurt: setting $pidfile pid lock\n";
    open my $PID, ">$pidfile" or die "FATAL iurt: could not open pidfile $pidfile for writing";
    print $PID $$;
    close $PID;
    if ($lock_ok) { 
	File::lockf::ulock($lock);
    } else {
	unlink "$lockfile.2"
    }
    close $lock;
    unlink $lockfile
}

sub check_media {
    my ($run, $cache, $config, $srpm_version, $wrong_rpm, $provides, $pack_provide, $maint) = @_;
# We could rely on only parsing the synthesis, hoping that they are correct, however this scan is very fast, so...
    my $rpms_dir = "$config->{repository}/$run->{distro}/$run->{my_arch}/media/$run->{media}/";
    print {$run->{LOG}} "iurt: checking current packages in $rpms_dir\n";
    opendir my $rpmdir, $rpms_dir or die "Could not open $rpms_dir: $!"; 
    foreach my $rpm (readdir $rpmdir) {
	my ($rarch, $srpm) = update_srpm($rpms_dir, $rpm, $wrong_rpm);
	$rarch or next;
	$cache->{queue}{$srpm} = 1;
	$run{status}{$srpm} = 'ok';
	check_version($srpm)
    }
    closedir $rpmdir;

    foreach my $m (@{$config->{all_media}}) {
	my $synthesis_file = "$config->{repository}/$run->{distro}/$run->{my_arch}/media/$m/media_info/synthesis.hdlist.cz";
	if (-f $synthesis_file) {
	    print {$run->{LOG}} "Parsing $synthesis_file\n";
	    if (open my $syn, "zcat $synthesis_file |") { 
		my @prov;
		my $nb;
		while (<$syn>) {
		    if (/^\@provides@(.*)/) {
			foreach my $p (split '@', $1) {
			    $p =~ /([^[]+)(?:\[(.*)\])?/g;
			    push @prov, $1;
			    $provides->{$1} = $2 || 1
			}
		    } elsif (/\@info\@([^@]+)@/) {
			$nb++;
			my ($name) = $1 =~ /(.*)-[^-]+-[^-]+\..*$/;
			foreach (@prov) {
			    $pack_provide->{$_} = $name
			}
			@prov = ()
		    }
		}
		$nb < $config->{minimum_package_number} and die "FATAL iurt: synthesis files seems corrupted, only $nb packages found."
	    } else {
		die "FATAL iurt: Could not open $synthesis_file\n";
	    }
	}
    }
    search_packages(0, $cache, $run, $maint, "$config->{repository}/$run->{distro}/SRPMS/$run->{media}/")
}

sub search_packages {
    my ($clean, $cache, $run, $maint, @dir) = @_;
    my ($to_compile, %rep, %done_rpm);
    foreach my $dir (@dir) {
	print {$run->{LOG}} "iurt: checking SRPMS dir $dir\n";
	opendir my $rpmdir, $dir or next;
	foreach my $srpm (readdir $rpmdir) {
	    # this is for the output of the new svn system
	    if ($srpm =~ /^\@\d+:(.*)/) {
		link "$dir/$srpm", "$dir/$1";
		# unlink "$dir/$srpm";
		$srpm = $1
	    }
	    $srpm =~ /(.*)-[^-]+-[^-]+\.src\.rpm$/ or next;
	    $run->{status}{$srpm} ||= 0;
	    if ($config->{unwanted_packages} && $srpm =~ /$config->{unwanted_packages}/) { next }
	    my $ok = 1;
	    if (check_version($srpm)) { 
		if (defined $cache->{failure}{$srpm}) {
		    $run->{status}{$srpm} = 'build_failure';
		    next
		}
		my $check_needed = check_needed($srpm);
		$run->{status}{$srpm} = 'missing_buildrequires' if !$check_needed;
		if (!$cache->{queue}{$srpm} && $check_needed) {
		    my $hdr = rpm2header("$dir/$srpm");
		    if (!check_arch($hdr)) {
			$run->{status}{$srpm} = 'not_on_this_arch';
			next
		    }
		    my $changelog = $hdr->queryformat("%{CHANGELOGNAME}");
		    my ($mail) = $changelog =~ /<(.*@.*)>/;
		    $maint{$srpm} = $mail;
		    print "iurt: will try to compile $srpm\n";
		    $to_compile++;
		    push @{$run->{todo}}, [ $dir , $srpm, 1 ]
		}
		foreach my $arch (@{$config->{supported_arch}}) {
		    $ok &&= $cache->{queue}{$srpm}
		}
	    }
	    if ($clean && ($rep{$srpm} || $ok)) {
		print "iurt: cleaning $dir/$srpm\n";
		unlink "$dir/build/$srpm";
		unlink "$dir/$srpm"
	    }
	    $rep{$srpm} = 1
	}
	closedir $rpmdir
    }
    $to_compile
}    

sub get_date {
    my ($sec,$min,$hour,$mday,$mon,$year) = gmtime(time());
    $year += 1900;
    my $fulldate = sprintf "%4d%02d%02d%02d%02d%02d", $year, $mon+1, $mday, $hour, $min, $sec;
    my $daydate = sprintf "%4d%02d%02d", $year, $mon+1, $mday;
    $fulldate, $daydate
}

sub add_local_user {
    my ($chroot_tmp, $run, $luser, $uid) = @_;
    # change the builder user to the local user id
    # FIXME it seems that unionfs does not handle well the change of the uid of files
    # if (system(qq|sudo chroot $chroot_tmp usermod -u $run{uid} builder|)) {
    if (system(qq|sudo chroot $chroot_tmp useradd -M -u $uid $luser|) || system(qq|sudo chroot $chroot_tmp id $luser|)) {
	print {$run->{LOG}} "ERROR: setting userid $uid to $luser in $chroot_tmp failed, trying to check the chroot\n";
	check_chroot($run->{chroot_path}, $run->{chroot_tar}, $run);
	return
    }
    if (system(qq|sudo chroot $chroot_tmp cp -R /home/builder /home/$luser|)) {
	print "ERROR iurt: could not initialized $luser directory\n";
	return
    }
    if (system(qq|sudo chroot $chroot_tmp chown -R $uid /home/$luser|)) {
	die "ERROR iurt: could not initialized $luser directory\n"; 
	return
    }
    1
}
 
sub create_temp_chroot {
    my ($run, $cache, $unionfs_tmp, $unionfs_dir, $union_id, $srpm) = @_;
    if ($unionfs_tmp) {
	my $mount_point = "$unionfs_dir/unionfs.$run->{run}.$union_id";
	print {$run->{LOG}} "Cleaning $mount_point\n" if $run->{verbose} > 1;
	if (!clean_mnt($mount_point, $run->{verbose})) {
	    dump_cache($run);
	    die "FATAL iurt: could not kill remaining processes acceding $mount_point"
	}
	my $tmpfs;

	# we cannont just rm -rf $tmpfs, this create defunct processes afterwards (and lock particularly hard the urpmi database)
	$union_id = clean_unionfs($unionfs_dir, $run, $run->{run}, $union_id);
	$tmpfs = "$unionfs_dir/tmpfs.$run->{run}.$union_id";
	$chroot_tmp = "$unionfs_dir/unionfs.$run->{run}.$union_id";
	if (!-d $tmpfs) {
	    if (!mkdir $tmpfs) {
		print "ERROR iurt: Could not create $tmpfs ($!)";
		return
	    }
	}
	if (! -d $chroot_tmp) {
	    if (!mkdir $chroot_tmp) {
		print "ERROR iurt: Could not create $chroot_tmp ($!)";
		return
	    }
	}
	if ($cache->{no_unionfs}{$srpm}) {
	    $unionfs_tmp = 0;
	    clean_chroot($chroot_tmp, $run)
	} else {
	    # if the previous package has been built without unionfs, chroot need to be cleaned
	    clean_chroot($chroot_tmp, $run) if !$unionfs_tmp;
	    $unionfs_tmp = 1;
	    if (system(qq{sudo mount -t tmpfs none $tmpfs &>/dev/null})) {
		print "ERROR iurt: could not mount $tmpfs ($!)"; 
		return
	    }
	    if (system(qq|sudo mount -o dirs=$tmpfs=rw:$home/chroot_$run->{distro_tag}$debug_tag=ro -t unionfs none $chroot_tmp &>/dev/null|)) {
		print "ERROR iurt: could not mount $tmpfs and $home/chroot_$run->{distro_tag}$debug_tag with unionfs ($!)";
		return
	    }
	    if (system("sudo mount -t proc none $chroot_tmp/proc &>/dev/null")) {
		print "ERROR iurt: could not mount /proc in the chroot $chroot_tmp.";
		return
	    }
	}
    } else {
	print {$run->{LOG}} "iurt: installing a new chroot in $chroot_tmp\n" if $run->{verbose} > 1;
	clean_chroot($chroot_tmp, $run)
    }
    $union_id, $unionfs_tmp, $chroot_tmp
}

sub add_packages {
    my ($run, $config, $chroot, $user, @packages) = @_;
    my $f;
    if (!perform_command("sudo $run{urpmi_command} @packages", 
		$run, $config,
		mail => $config->{admin},
		timeout => 120, 
		freq => 1,
		retry => 2,
		debug_mail => $run->{debug},
		error_regexp => 'cannot be installed',
		wait_regexp => {
		    'is needed by' => sub { 
		        print {$run{LOG}} "WARNING iurt: rpm database seems corrupted, retrying\n"; 
		        system("sudo chroot $chroot rm -rf /var/lib/rpm/__db* &> /dev/null");
		        foreach (1 .. 3) { system("sudo chroot $chroot rpm -qa &> /dev/null") } 
		        1
		        }, 
		    'database locked' => sub { 
		        print {$run->{LOG}} "WARNING iurt: urpmi database locked, waiting...\n";
		        sleep 30; 
		        $wait_limit++; 
		        if ($wait_limit > 10) { 
		    	    $wait_limit = 0; system(qq{sudo pkill -9 urpmi &>/dev/null});
			    return
			} 
			1
		  } },)) {
	print {$run->{LOG}} "ERROR iurt: could not install @packages inside $chroot\n";
	return 0
    }
    1
}

sub add_sudoers {
    my ($run, $user) = @_;
    my $file = "$chroot/etc/sudoers";
    my $f;
    if (!open $f, qq{| sudo sh -c "cat > $file"}) {
	print {$run->{LOG}} "ERROR iurt: could not open $file ($!)\n";
	return 0
    }
    print $f qq{Cmnd_Alias RPM=/bin/rpm,/usr/sbin/urpmi,/usr/sbin/urpme,/usr/sbin/urpmi.addmedia,/usr/sbin/urpmi.update,/usr/sbin/urpmi.removemedia
root    ALL=(ALL) ALL
$user   ALL=(ALL) NOPASSWD:RPM
};
    close $f;
    print {$run->{LOG}} "iurt: adding sudo for /bin/rpm, /usr/sbin/urpmi and /usr/sbin/urpme\n";
    -f $file or return 0;
    1
}

sub remove_chroot {
    my ($run, $dir, $func, $prefix) = @_;
    if ($run->{clean_all}) {
	opendir my $chroot_dir, $dir;
	foreach (readdir $chroot_dir) {
	    next if !-d || /\.{1,2}/;
	    $func->("$dir/$_", $prefix)
	}
    } else {
	foreach my $user (@{$run->{clean}}) {
	    print {$run->{LOG}} "iurt: cleaning old chroot for $user in $dir\n";
	    $func->("$dir/$user", $prefix)
	}
    }
}

sub add_media {
    my ($run, $config, $chroot, $regexp, $media) = @_;
    print {$run->{LOG}} "iurt: adding media $run{chrooted_media} in chroot $chroot\n";
    if (!perform_command("sudo chroot $chroot urpmi.addmedia $media", 
		$run, $config,
		mail => $config->{admin},
		timeout => 120, 
		freq => 1,
		retry => 2,
		debug_mail => $run->{debug})) {
	}
    if (!check_media_added($chroot, $regexp)) { 
	print "ERROR iurt could not add media into the chroot\n";
	return
    } 
    1
}

sub check_media_added {
    my ($chroot, $media) = @_;
    my $medias = `sudo chroot $chroot urpmi.removemedia 2>&1`;
    $medias =~ /one of.* $media/m
}

sub get_local_provides {
    my ($run, $local_media) = @_;
    opendir my $dir, $local_media;
    my $urpm = new URPM;
    foreach my $d (readdir $dir) {
	$d =~ /\.rpm$/ or next;
	my $id = $urpm->parse_rpm("$local_media/$d");
	my $pkg = $urpm->{depslist}[$id];
        foreach ($pkg->provides, $pkg->files) {
	    print {$run->{LOG}} "iurt: adding $_ as provides of $d\n" if $run->{verbose} > 2;
	    $run{local_provides}{$_} = $d
	}
    }
}

sub recreate_srpm {
    my ($run, $config, $chroot_tmp, $dir, $srpm, $luser, $retry) = @_;
# recreate a new srpm for buildarch condition in the spec file
    print {$run->{LOG}} "iurt: copying $srpm to $chroot_tmp\n" if $run->{verbose} > 1;
    perform_command("sudo cp $dir/$srpm $chroot_tmp/home/$luser/rpm/SRPMS/", 
	\%run, $config,
	mail => $config->{admin}, 
	error => "[REBUILD] cannot copy $srpm to $chroot_tmp", 
	debug_mail => $run->{debug},
	hash => "copy_$srpm") or return;

    my %opt = ( mail => $config->{admin}, 
	error => "[REBUILD] cannot install $srpm in $chroot_tmp", 
	debug_mail => $run->{debug},
	hash => "install_$srpm",
	retry => $retry,
	wait_regexp => { 
	    'cannot write to %sourcedir /usr/src/rpm/SOURCES' => sub {
		print {$run->{LOG}} "ERROR iurt: chroot seems corrupted, try to rebuild it\n";
		check_chroot($run->{chroot_path}, $run->{chroot_tar}, \%run)
	    }		
	},
	callback => sub { 
	    my ($opt, $output) = @_;
	    print {$run->{LOG}} "calling callback for $opt->{hash}\n" if $run->{debug};
	    if ($output =~ /warning: (group|user) .* does not exist - using root/i) {
		return 1
	    } elsif ($output =~ /user $luser does not exist/) {
		print {$run->{LOG}} "WARNING iurt: chroot seems corrupted...\n" if $run->{verbose} > 1;
		$opt->{error} = "[CHROOT] chroot is corrupted";
		$opt->{retry} = 1 if !$opt->{retry};
		return 
	    }
	});
    print {$run->{LOG}} "iurt: recreating src.rpm...\n" if $run->{verbose};
    if (!perform_command(qq{sudo chroot $chroot_tmp su $luser -c "rpm -i /home/$luser/rpm/SRPMS/$srpm"}, 
	    \%run, $config, %opt)) {
	print {$run->{LOG}} "ERROR iurt: chrooting failed (retry $opt{retry}\n" if $run->{debug};
	if ($opt{retry}) {
	    check_chroot($run->{chroot_path}, $run->{chroot_tar}, \%run) or return;
	    return -1
	}
	return
    }
    # 20060515 This should not be necessairy any more if urpmi *.spec works, but it doesn't
    #
    perform_command(qq{sudo chroot $chroot_tmp su $luser -c "rpm --nodeps -bs /home/$luser/rpm/SPECS/*.spec"}, 
	\%run, $config,
	mail => $config->{admin}, 
	error => "[REBUILD] cannot create $srpm in $chroot_tmp", 
	debug_mail => $run->{debug},
	hash => "create_$srpm")
}

sub get_build_requires {
    my ($run, $config) = @_;
    $run{todo_requires} = {};
    my ($union_id, $unionfs_tmp, $chroot_tmp) = create_temp_chroot(\%run, $cache, $unionfs_tmp, $unionfs_dir, $union_id) or return;
    
    my $urpm = new URPM;
    foreach my $p (@{$run->{todo}}) {
	my ($dir, $srpm, $s) = @$p;
	recreate_srpm($run, $config, $chroot_tmp, $dir, $srpm, 'iurt') or return;
	$s or next;
	my $id = $urpm->parse_rpm("$dir/$srpm");
	my $pkg = $urpm->{depslist}[$id];
        foreach ($pkg->requires) {
	    print {$run->{LOG}} "iurt: adding $_ as requires of $srpm\n" if $run->{verbose} > 2;
	    $run{todo_requires}{$_} = $srpm
	}
    }
}

sub order_packages {
    my ($run, $config, $provides, $local_media) = @_;
    my @packages = @{$run{todo}};
    my $move;
    get_local_provides($run, $local_media) or return;
    if (!$run{todo_requires}) {
	get_build_requires($run, $config) or return
    }
    my %visit;
    my %status;
    do { 
	$move = 0;
	foreach my $p (@packages) {
	    my ($dir, $rpm, $status) = @$p;
	    defined $status{$rpm} && $status{$rpm} == 0 and next;
	    print {$run->{LOG}} "iurt: checking packages $rpm\n";
	    foreach my $r (@{$run->{todo_requires}{$rpm}}) {
		print {$run->{LOG}} "iurt: checking requires $r\n";
		if (!$run->{local_provides}{$r}) { 
		    if ($provides->{$r}) {
			$status = 1
		    } else {
			$status = 0
		    }
		} elsif ($visit{$rpm}{$r}) {
		    # to evit loops
		    $status = 0
		} elsif ($run->{done}{$rpm} && $run->{done}{$provides->{$r}}) {
		    if ($run->{done}{$rpm} < $run->{done}{$provides->{$r}}) {
			$move = 1;
			$status = $status{$provides->{$r}} + 1 
		    } else {
			$status = 0
		    }
		} elsif ($status < $status{$provides->{$r}}) {
		    $move = 1;
		    $status = $status{$provides->{$r}} + 1
		}
		$visit{$rpm}{$r} = 1;
	    }
	    $status{$rpm} = $status;
	    $p->[2] = $status
	}
    } while ($move);    
    $run->{todo} = [ sort { $a->[2] <=> $b->[2] } @packages ];
    @packages
}

sub dump_status {
    my ($local_spool, $run) = @_;
    if (open my $file, ">$local_spool/log/status.$run->{media}.log") {
	foreach my $srpm (sort keys %{$run->{status}}) {
	    print $file "$srpm: ";
	    if ($run{status}->{$srpm}) {
		print $file $run->{status}{$srpm}
	    } else {
		print $file "unknown"
	    }
	    print $file "\n"
	}
	close $file
    }
}