summaryrefslogtreecommitdiff
path: root/tb/tb_internals.sh
blob: da8dd71f64c32267e7238a686423653babe72ef3 (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
#!/usr/bin/env bash
# -*- tab-width : 4; indent-tabs-mode : nil -*-
#
#    Copyright (C) 2011-2013 Norbert Thiebaud
#    License: GPLv3
#
#
# Naming convention/Namespace
#
# lowercase variable: local variable. must be declared as 'local'
#
# tb_UPPER_CASE : global variable internal to the script. not to be set directly by config.
#
# TB_UPPER_CASE : global variable that can or must be set by config
#
# in general use readable variable name, use _ to separate the part of the variables name
#
# Exception: P : project name
#            B : current branch name. gerrit_* are reserved branch names for gerrit works
#            R : build result indicator ( 0=OK 1=KO 2=False positive )
#            V : verbose messages (V=1 => verbose message V= => no verbose message, iow: [ $V ] && log_msgs ....
#         MAKE : environement variable is use if set to point to a gnu-make
#                otherwise overriden to a gne-make found in the PATH
#
# profile_* reserved for functions specific to .../<profile_name>/phases.sh
# branches_* reserved for functions specific to .../<branch_name>/phases.sh
# canonical_* reserverved for phase implementation in tb_phases.sh
# canonical_[pre|do|post]_<phase> is garanteed to exist, even if it is a no-op function.
#
# The rational for these namespace is to allow lower-level overload to still call
# the implementation at higher level.
#
# for instance if a branche phase.sh want derefine the TMPDIR and clean it up
# in the pre-clean phase, but still want to do what-ever the tb_phase.sh normally do
# it can implement
# pre_clean()
# {
#    do what I need to do
#    canonical_pre_clean() to invoke the defautl impelmentation
# }
#
# similarely at the profile level one can override pre-clean in this fashion:
#
# profile_pre_clean()
# {
#     profile override implementation fo pre-clean()
# }
#
# pre_clean()
# {
#     profile_pre_clean()
# }
#
# that way a branch's phase.sh can invoke profile_pre_clean in it's own implemenation of pre_clean()
#
# ATTENTION: do not abuse this scheme by having defferent level invoking different phase
# at higher level... so a branch's pre_clean() for instance shall not invoke canonical_do_clean()
# or any other phase than *_pre_clean()
# Obviously profile level phase.sh shall not invoke any branche_* functions.
#
# Configuration files layout
#
#  ~/.tb/config
#       /meta/
#       /phases.sh
#       /profiles/<profile_name>/autogen.lastrun
#       /profiles/<profile_name>/autogen.lastrun_gerrit
#       /profiles/<profile_name>/autogen.lastrun_tb
#       /profiles/<profile_name>/branches/<branch_name>/autogen.lastrun
#       /profiles/<profile_name>/branches/<branch_name>/autogen.lastrun_gerrit
#       /profiles/<profile_name>/branches/<branch_name>/autogen.lastrun_tb
#       /profiles/<profile_name>/branches/<branch_name>/config
#       /profiles/<profile_name>/branches/<branch_name>/config_gerrit
#       /profiles/<profile_name>/branches/<branch_name>/config_tb
#       /profiles/<profile_name>/branches/<branch_name>/false_negatives
#       /profiles/<profile_name>/config
#       /profiles/<profile_name>/false_negatives
#       /profiles/<profile_name>/phases.sh

# Note: config are accumulated from high to low.
#       autogen are 'lowest level prime'.

# XRef :
#
# tb_BIN_DIR :
# tb_BRANCHES :
# tb_BRANCH_AUTHOR :
# tb_BUILD_COMMIT :
# tb_BUILD_TRIGGERED : "1" if the build was controlled by a trigger file
# tb_BUILD_TYPE :
# tb_CONFIG_DIR :
# tb_GERRIT_BRANCHES :
# tb_GERRIT_PLATFORM :
# tb_KEEP_AUTOGEN :
# tb_LOGFILE :
# tb_MODE :
# tb_NEXT_PRIORITY :
# tb_ONE_SHOT :
# tb_OPT_DIR :
# tb_PRIORITY :
# tb_PRODILE_DIR :
# tb_PROFILE_DIR :
# tb_PUSH_NIGHTLIES :
# tb_REPORT_LOG :
# tb_REPORT_MSGS :
# tb_SEND_MAIL :
# tb_TB_BRANCHES :
# tb_TINDERBOX_BRANCH :


B=
P=
V=
tb_LOGFILE="/dev/null"


# please keep the function declaration in alphabetical order


bibisect_post()
{
    pushd ${TB_BIBISECT_DIR?} > /dev/null
    if [ "${TB_BIBISECT_GC}" = "1" ] ; then
        git gc
    fi
    if [ "${TB_BIBISECT_PUSH}" = "1" ] ; then
        git push
    fi
    popd > /dev/null
}

#
# Check if a branch is suitably configured
# to be build for tb and/or gerrit
#
check_branch_profile()
{
    local b="$1"
    local type=
    local ret=
    local rb=

    if [ ! -d "${tb_PROFILE_DIR?}/branches/${b?}" ] ; then
        log_msgs "No branch specific config for branch '${b?}', using default from profile"
    fi
    if [ "${tb_MODE?}" = "dual" -o "${tb_MODE?}" = "tb" ] ; then
        rb=$(check_branch_profile_tb "$b")
        ret=$?
        if [ "$ret" = "0" ] ; then
            if [ -z "${tb_TB_BRANCHES}" ] ; then
                tb_TB_BRANCHES="${rb?}"
            else
                tb_TB_BRANCHES="${tb_TB_BRANCHES?} ${rb?}"
            fi
        fi
    fi
    if [ "${tb_MODE?}" = "dual" -o "${tb_MODE?}" = "gerrit" ] ; then
        rb=$(check_branch_profile_gerrit "${b?}")
        ret=$?
        if [ "${ret?}" = "0" ] ; then
            if [ -z "${tb_GERRIT_BRANCHES}" ] ; then
                tb_GERRIT_BRANCHES="${rb?}"
            else
                tb_GERRIT_BRANCHES="${tb_GERRIT_BRANCHES?} ${rb?}"
            fi
        fi
    fi
}

#
# Checks common to both tb and gerrit
# buildability of a branch profile
#
check_branch_profile_common()
{
    local b="$1"

    if [ -z "${TB_GIT_DIR}" ] ; then
        die "Missing TB_GIT_DIR for branch ${b?}"
    else
        if [ ! -d "${TB_GIT_DIR?}" ] ; then
            die "TB_GIT_DIR:${TB_GIT_DIR?} for branch ${b?} is not a directory"
        fi
        if [ ! -d "${TB_GIT_DIR?}/.git" ] ; then
            die "TB_GIT_DIR:${TB_GIT_DIR?} for branch ${b?} is not a git repository"
        fi
    fi
    if [ -n "${TB_BUILD_DIR}" ] ; then
        if [ ! -d "${TB_BUILD_DIR?}" ] ; then
            die "TB_BUILD_DIR:${TB_BUILD_DIR?} for branch ${b?} does not exist"
        fi
    fi
}

#
# Check specific to gerrit builability
# of a branch profile
#
check_branch_profile_gerrit()
{
    local b="$1"

    # unset higher level CCACHE_DIR setting
    unset CCACHE_DIR

    source_branch_level_config "${b?}" "gerrit"

    # if CCACHE_DIR is set it has been set by the branch's profile
    # if TB_CCACHE_SIZE is set make sure the cache is as big as specified
    # note: no need to restore the old CCACHE value
    # since check_branches is run in a sub-shell
    if [ -n "${CCACHE_DIR}" ] ; then
        if [ -n "${TB_CCACHE_SIZE}" ] ; then
            ccache -M "${TB_CCACHE_SIZE?}" > /dev/null
        fi
    fi

    # if we did not die yet... we are good for this branch: print it
    echo "${b?}"

}

#
# Checks psecific to tb buildability
# of a branch
#
check_branch_profile_tb()
{
    local b="$1"
    local sha=

    # unset higher level CCACHE_DIR setting
    unset CCACHE_DIR

    source_branch_level_config "${b?}" "tb"

    if [ -z "${TB_TINDERBOX_BRANCH}" ] ; then
        TB_TINDERBOX_BRANCH=$(determine_default_tinderbox_branch "${b?}")
        # FIXME: determine if we can derive that value
        # from ${b}
        if [ -z "${TB_TINDERBOX_BRANCH}" ] ; then
            die "Missing TB_TINDERBOX_BRANCH to associate a BRANCH name on the tiderbox server to the branch ${b?}"
        fi
    fi

    if [ "${TB_BIBISECT}" == "1" ] ; then
        if  [ -z "${TB_BIBISECT_DIR}" ] ; then
            die "To do bibisect you must define TB_BIBISECT_DIR to point to your bibisect git repo"
        fi
        if [ ! -d "${TB_BIBISECT_DIR?}" -o ! -d "${TB_BIBISECT_DIR}/.git" ] ; then
            die "TB_BIBISECT_DIR:${TB_BIBISECT_DIR?} is not a git repository"
        fi
        pushd "${TB_BIBISECT_DIR?}" > /dev/null || die "Cannot cd to ${TB_BIBISECT_DIR?} for branch ${b?}"
        sha=$(git rev-parse "${b?}")
        if [ "$?" != "0" ] ; then
            die "Branch ${b?} does not exist in the bibisect repo, Cannot collect the requested bibisect"
        fi
    fi

    # if CCACHE_DIR is set it has been set by the branch's profile
    # if TB_CCACHE_SIZE is set make sure the cache is as big as specified
    # note: no need to restore the old CCACHE value
    # since check_branches is run in a sub-shell
    if [ -n "${CCACHE_DIR}" ] ; then
        if [ -n "${TB_CCACHE_SIZE}" ] ; then
            ccache -M "${TB_CCACHE_SIZE?}" > /dev/null
        fi
    fi

    # if we did not die yet... we are good for this branch: print it
    echo "${b?}"
}


#
# Check all the branches under consideration
# for suitable configuration
#
check_branches_profile()
{
local b

    tb_TB_BRANCHES=""
    tb_GERRIT_BRANCHES=""

    for b in ${tb_BRANCHES?} ; do
        if [ -z "$b" ] ; then
            die "Internal Error: trying to process and empty branch name"
        fi
        check_branch_profile "$b"
    done

    # Accumulate valid branches for tb and gerrit
    # depending of the mode
    if [ "${tb_MODE?}" = "dual" -o "${tb_MODE?}" = "gerrit" ] ; then
        if [ -z "$tb_GERRIT_BRANCHES" ] ; then
            die "No branches are configured properly for gerrit"
        fi
    fi
    if [ "${tb_MODE?}" = "dual" -o "${tb_MODE?}" = "tb" ] ; then
        if [ -z "$tb_TB_BRANCHES" ] ; then
            die "No branches are configured properly for tb"
        fi
    fi
}

#
# Determine if there are new commits
# on a given branch
#
check_for_commit()
{
    local b="$1"
    local err_msgs=
    local rev=
    local refspec=
    local r="-1"

    [ $V ] && echo "Checking for new commit for tb-branch ${b?}"

    source_branch_level_config "${b?}" "tb"

    pushd "${TB_GIT_DIR?}" > /dev/null || die "Cannot cd to git repo ${TB_GIT_DIR?} for tb-branche ${b?}"

    err_msgs="$( $tb_TIMEOUT git fetch 2>&1)"
    if [ "$?" -ne "0" ] ; then
        printf "Git repo broken - error is:\n\n$err_msgs" > error_log.log
        report_error owner "$(print_date)" error_log.log
        exit -1
    else
        refspec="origin/${b?}"
        if [ -n "${TB_BRANCH_REMOTE_REFSPEC}" ] ; then
            refspec="${TB_BRANCH_REMOTE_REFSPEC?}"
        fi
        [ $V ] && echo "collect current head for branch ${b?} refspec ${refspec?}"
        rev=$(git rev-parse ${refspec?})
        if [ "$?" = "0" ] ; then
            echo "${rev?}" > "${TB_METADATA_DIR?}/${P}_${b?}_current-git-head.log"
            print_date > "${TB_METADATA_DIR?}/${P}_${b?}_current-git-timestamp.log"

            if [ ! -f "${TB_METADATA_DIR?}/prev-${P?}_${b?}_current-git-head.log" ] ; then
                [ $V ] && echo "New commit for tb-branch ${b?} (no primer)"
                r=0
            elif [ "$(cat "${TB_METADATA_DIR?}/${P}_${b?}_current-git-head.log")" != "$(cat "${TB_METADATA_DIR?}/prev-${P?}_${b?}_current-git-head.log")" ] ; then
                [ $V ] && echo "New commit for tb-branch ${b?}"
                r=0
            else
                [ $V ] && echo "No New commit for tb-branch ${b?}"
                r=2
            fi
        else
            log_msgs "Git error while checking for commit on ${TB_GIT_REPO?} for branch ${b?}"
            printf "Git repo broken - error is:\n\n$err_msgs" > error_log.log
            report_error owner "$(print_date)" error_log.log
            exit -1
        fi
    fi
    [ $V ] && echo "pulling from the repo ${TB_GIT_REPO?} for branch ${b?} -> r=${r?}"
    if [ "${r?}" = "0" ] ; then
        if [ -n "${TB_TRIGGER_FILE}" -a -f "${TB_TRIGGER_FILE}" ] ; then
            r=1
            [ $V ] && echo "Trigger file ${TB_TRIGGER_FILE} detected for branch ${b?} -> r=${r?}"
        else
            r=3
            [ $V ] && echo "Trigger file ${TB_TRIGGER_FILE} missing for branch ${b?} -> r=${r?}"
        fi
    fi
    exit ${r?}
}

#
# Save the sha assoicated with the HEAD of the current branch
#
collect_current_head()
{
    [ $V ] && echo "collect_current_head"
    echo "core:$(git rev-parse HEAD)" > "${TB_METADATA_DIR?}/${P}_${B?}_current-git-head.log"
    print_date > "${TB_METADATA_DIR?}/${P}_${B?}_current-git-timestamp.log"
}

#
# Copy the autogen.lastrun in the builddir
# this assume that the cwd is the builddir
# and that B and tb_BUILD_TYPE are set
# This is notmally called fromthe do_autogen()
# phase.
#
copy_autogen_config()
{
    if [ -f "${tb_PROFILE_DIR?}/branches/${B}/autogen.lastrun_${tb_BUILD_TYPE?}" ] ; then
        cp "${tb_PROFILE_DIR?}/branches/${B}/autogen.lastrun_${tb_BUILD_TYPE?}" autogen.lastrun
    elif [ -f "${tb_PROFILE_DIR?}/branches/${B}/autogen" ] ; then
        cp "${tb_PROFILE_DIR?}/branches/${B}/autogen.lastrun" autogen.lastrun
    elif [ -f "${tb_PROFILE_DIR?}/autogen.lastrun_${tb_BUILD_TYPE?}" ] ; then
        cp "${tb_PROFILE_DIR?}/autogen.lastrun_${tb_BUILD_TYPE?}" autogen.lastrun
    elif [ -f "${tb_PROFILE_DIR?}/autogen.lastrun" ] ; then
        cp "${tb_PROFILE_DIR?}/autogen.lastrun" autogen.lastrun
    fi

}

deliver_lo_to_bibisect()
{
    # copy the content of lo proper to bibisect
    # this is  separate function so it can easily be overriden
    cp -fR ${tb_OPT_DIR?} ${TB_BIBISECT_DIR?}/

}

deliver_to_bibisect()
{
    local cc=""
    local oc=""

    [ $V ] && echo "deliver_to_bibisect()"

    if [ -n ${tb_OPT_DIR} ] ; then
        # verify that someone did not screw-up bibisect repo
        # while we were running
        if [ "${TB_BIBISECT}" != "0" ] ; then
            # note: this function will exit if something is wrong

            # avoid delivering the same build twice to bibisect
            cc=$(git rev-list -1 HEAD)
            if [ -f  "${TB_BIBISECT_DIR?}/commit.hash" ] ; then
                oc="$(cat ${TB_BIBISECT_DIR?}/commit.hash)"
            fi
            if [ "${cc}" != "${oc}" ] ; then
                deliver_lo_to_bibisect

                git log -1 --pretty=format:"source-hash-%H%n%n" ${tb_BUILD_COMMIT?} > "${TB_BIBISECT_DIR?}/commitmsg"
                git log -1 --pretty=fuller ${tb_BUILD_COMMIT?} >> "${TB_BIBISECT_DIR?}/commitmsg"

                [ $V ] && echo "Bibisect: Include interesting logs/other data"
                # Include the autogen log.
                cp tb_${B?}_autogen.log "${TB_BIBISECT_DIR?}/."

                # Include the build, test logs.
                cp tb_${B?}_build.log "${TB_BIBISECT_DIR?}/."

                # Make it easy to grab the commit id.
                git rev-list -1 HEAD > "${TB_BIBISECT_DIR?}/commit.hash"

                # Commit build to the local repo and push to the remote.
                [ $V ] && echo "Bibisect: Committing to local bibisect repo"
                pushd "${TB_BIBISECT_DIR?}" >/dev/null
                git add -A
                git commit -q --file=commitmsg
                popd > /dev/null

                bibisect_post

            fi
        fi
    fi
    [ $V ] && echo "unlock ${lock_file?}.bibisect"
    [ $V ] && echo "unlock ${lock_file?}.bibisect"
}


determine_default_tinderbox_branch()
{
    local b="$1"

    case "$b" in
        master)
            echo 'MASTER'
            ;;
        libreoffice-3-4)
            echo "${b?}"
            ;;
        libreoffice-3-5)
            echo "${b?}"
            ;;
        libreoffice-3-6)
            echo "${b?}"
            ;;
        libreoffice-4-0)
            echo "${b?}"
            ;;
    esac
}
#
# Find a gnu make
#
determine_make()
{
    ## Determine how GNU make is called on the system
    if [ -n "$MAKE" ] ; then
        $MAKE --version 2> /dev/null | grep -q GNU
        if test $? -eq 0;  then
            return
        else
            MAKE=
        fi
    fi

    for _g in make gmake gnumake; do
        $_g --version 2> /dev/null | grep -q GNU
        if test $? -eq 0;  then
            MAKE=$_g
            break
        fi
    done
    if [ -z "$MAKE" ] ; then
        die "Could not find a Gnu Make"
    fi
}

#
# Display an error message and exit
# tb or a sub-shell
#
die()
{
    echo "[$(print_date) ${P}:${B}] Error:" "$@" | tee -a ${tb_LOGFILE?}
    R=-1
    exit -1;
}

epoch_from_utc()
{
    local utc="$@"

    date -u '+%s' -d "$utc UTC"
}

epoch_to_utc()
{
    date -u -d @${1}
}

#
# Fetch a gerrit changeset and check it out
#
fetch_gerrit()
{
    GERRIT_PREV_B=`git branch | grep '^\*' | sed 's/^..//' | sed 's/\//_/g'`
    [ $V ] && echo "fetching gerrit path from ssh://${GERRIT_HOST?}/core ${GERRIT_TASK_REF}"
    git fetch -q ssh://${GERRIT_HOST?}/core ${GERRIT_TASK_REF}
    if [ "$?" != "0" ] ; then
        retval="3"
    else
        git checkout -q FETCH_HEAD || die "fatal error checking out gerrit ref"
        git submodule -q update
        [ $V ] && echo "fetched gerrit path from ssh://${GERRIT_HOST?}/core ${GERRIT_TASK_REF}"
        retval="0"
    fi
}

find_dev_install_location()
{
    find . -name opt -type d
}

generate_cgit_link()
{
    local line="$1"
    local repo=$(echo $line | cut -f 1 -d \:)
    local sha=$(echo $line | cut -f 2 -d \:)

    echo "<a href='http://cgit.freedesktop.org/libreoffice/${repo}/log/?id=$sha'>$repo</a>"
}

get_commits_since_last_good()
{
    local mode=$1
    local head=
    local repo=
    local sha=

    if [ -f "${TB_METADATA_DIR?}/${P?}_${B?}_last-success-git-head.txt" ] ; then
        head=$(head -n1 "${TB_METADATA_DIR?}/${P?}_${B?}_last-success-git-head.txt")
        repo=$(echo ${head?} | cut -d : -f 1)
        sha=$(echo ${head?} | cut -d : -f 2)
        if [ "${mode?}" = "people" ] ; then
            git log '--pretty=tformat:%ce' ${sha?}..HEAD
        else
            echo "==== ${repo} ===="
            git log '--pretty=tformat:%h  %s' ${sha?}..HEAD | sed 's/^/  /'
        fi
    else
        if [ "${mode?}" = "people" ] ; then
            echo "$TB_OWNER"
        else
            echo "==== ${repo} ===="
            echo "no primer available, can't extract the relevant log"
        fi
    fi
}

get_committers()
{
    echo "get_committers: $(get_commits_since_last_good people)" 1>&2
    get_commits_since_last_good people | sort | uniq | tr '\n' ','
}

get_first_branche()
{
    local top="$1"

    echo "$top"

}

interupted_build()
{
    log_msgs "Interrupted by Signal"
    if [ "$tb_BUILD_TYPE" = "gerrit" ] ; then
        if [ -n "${GERRIT_TASK_TICKET}" ] ;then
            # repport a cancellation if we already acquired the ticket
            R=2
            report_gerrit
        fi
    elif [ "$tb_BUILD_TYPE" = "tb" ] ; then
        if [ -n "${tb_LAST_CHECKOUT_DATE?}" ] ; then
            # repport a cancellation if we already notified a start
            report_to_tinderbox "${tb_LAST_CHECKOUT_DATE?}" "fold" "no"
        fi
    fi
    # propagate the stop request to the main loop
    touch ${tb_CONFIG_DIR?}/stop

    exit 4
}

load_config()
{
    tb_CONFIG_DIR="$HOME/.tb"
    if [ ! -d "${tb_CONFIG_DIR?}" ] ; then
        die "You need to configure tb to use it"
    fi
    if [ -f "${tb_CONFIG_DIR?}/config" ] ; then
        source "${tb_CONFIG_DIR?}/config"
    fi
}

load_profile()
{
    local p=$1
    local rc=0
    local config_file=
    local old_ccache_dir=

    if [ -z "$p" ] ; then
        die "A profile is needed to run: use -p or configure one"
    else
        tb_PROFILE_DIR="${tb_CONFIG_DIR?}/profiles/${p}"
        if [ ! -d "${tb_PROFILE_DIR}" ] ; then
            die "You need to configure the profile ${p} to use it"
        fi

        #save the current CCACHE_DIR setting
        if [ -n "${CCACHE_DIR}" ] ; then
            old_ccache_dir="${CCACHE_DIR?}"
            unset CCACHE_DIR
        fi
        config_file="${tb_PROFILE_DIR?}/config"
        if [ -f "${config_file?}" ] ; then
            source "${config_file?}"
        fi
        # global level phase override
        if [ -f "${tb_CONFIG_DIR?}/phases.sh" ] ; then
            source "${tb_CONFIG_DIR?}/phases.sh"
        fi
        # project level phase override
        if [ -f "${tb_PROFILE_DIR?}/phases.sh" ] ; then
            source "${tb_PROFILE_DIR?}/phases.sh"
        fi

        # if we have a CCACHE_DIR here, it has been set by
        # the profile. if we also haev a TB_CCACHE_SIZE
        # make sure the cache is as big as indicated
        # if CCACHE_DIR is not set, restaure the potential
        # previous value
        if [ -n "${CCACHE_DIR}" ] ; then
            if [ -n "${TB_CCACHE_SIZE}" ] ; then
                ccache -M "${TB_CCACHE_SIZE?}" > /dev/null
            fi
        else
            if [ -n "${old_ccache_dir}" ] ; then
                CCACHE_DIR="${old_ccache_dir?}"
            fi
        fi
    fi
}

log_msgs()
{
    echo "[$(print_date) ${P?}:${B}]" "$@" | tee -a ${tb_LOGFILE?}
}

prepare_git_repo_for_gerrit()
{
    if [ -z "$TB_BUILD_DIR" ] ; then
        TB_BUILD_DIR="${TB_GIT_DIR?}"
    fi
    pushd ${TB_GIT_DIR?} > /dev/null || die "Cannot cd to build dir : ${TB_GIT_DIR?}"
    [ $V ] && echo "fetching gerrit path from ssh://${TB_GERRIT_HOST?}/core ${GERRIT_TASK_REF}"

    (
        git clean -fd && git fetch -q ssh://${TB_GERRIT_HOST?}/core ${GERRIT_TASK_REF}
        if [ "$?" = "0" ] ; then
            git checkout -q FETCH_HEAD
            git submodule -q update
        else
            exit -1
        fi
    ) 2>&1 > ${TB_BUILD_DIR}/error_log.log

    if [ "$?" != "0" ] ; then
        popd > /dev/null
        report_error owner "$(print_date)" error_log.log
        die "Cannot reposition repo ${TB_GIT_DIR} to the proper branch"
    fi
    popd > /dev/null

}

prepare_git_repo_for_tb()
{
    local remote_sha="$(cat "${TB_METADATA_DIR?}/${P}_${B?}_current-git-head.log")"
    local local_sha=
    local refspec=
    local remote_refspec=

    # by default the local branch name is the 'branch' name for the profile
    refspec="${B?}"
    if [ -n "${TB_BRANCH_LOCAL_REFSPEC}" ] ; then
        refspec="${TB_BRANCH_LOCAL_REFSPEC?}"
    fi

    # by default the remote branch name is identical to the local one with origin/ in front
    remote_refspec="origin/${refspec?}"
    if [ -n "${TB_BRANCH_REMOTE_REFSPEC}" ] ; then
        remote_refspec="${TB_BRANCH_REMOTE_REFSPEC?}"
    fi

    if [ -z "$TB_BUILD_DIR" ] ; then
        TB_BUILD_DIR="${TB_GIT_DIR?}"
    fi
    pushd ${TB_GIT_DIR?} > /dev/null || die "Cannot cd to build dir : ${TB_GIT_DIR?}"

    (
        local_sha=$(git rev-parse ${refspec?})
        if [ "$?" = "0" ] ; then
            cb=$(git rev-parse --abbrev-ref HEAD)
            if [ "$?" = "0" -a "${cb?}" = "${refspec?}" ] ; then
                git clean -fd &&  git reset --hard ${remote_refspec?} && git submodule update
            else
                git clean -fd && git checkout -q ${refspec?} && git reset --hard ${remote_refspec?} && git submodule update
            fi
        else
            git clean -fd && git checkout -b ${refspec?} ${remote_refspec?} && git submodule update
        fi
    ) 2>&1 > ${TB_BUILD_DIR}/error_log.log

    if [ "$?" != "0" ] ; then
        popd > /dev/null
        report_error owner "$(print_date)" error_log.log
        die "Cannot reposition repo ${TB_GIT_DIR} to the proper branch"
    fi
    popd > /dev/null

}

prepare_upload_manifest()
{
    local manifest_file="build_info.txt"

    echo "Build Info" > $manifest_file

    echo "tinderbox: administrator: ${TB_OWNER?}" >> $manifest_file
    echo "tinderbox: buildname: ${TB_NAME?}" >> $manifest_file
    echo "tinderbox: tree: ${tb_TINDERBOX_BRANCH?}" >> $manifest_file
    echo "tinderbox: pull time $(cat "${TB_METADATA_DIR?}/${P?}_${B?}_current-git-timestamp.log")" >> $manifest_file
    echo "tinderbox: git sha1s"  >> $manifest_file
    cat "${TB_METADATA_DIR?}/{P?}_${B?}_current-git-head.log"  >> $manifest_file
    echo ""  >> $manifest_file
    echo "tinderbox: autogen log"  >> $manifest_file
    cat tb_${P?}_${B?}_autogen.log  >> $manifest_file

}

print_date()
{
    date -u '+%Y-%m-%d %H:%M:%S'
}

print_local_date()
{
    date '+%Y-%m-%d %H:%M:%S'
}

position_bibisect_branch()
{
    pushd ${TB_BIBISECT_DIR?} > /dev/null
    git checkout -q ${B?}
    if [ "$?" -ne "0" ] ; then
        echo "Error could not position the bibisect repository to the branch $B" 1>&2
        exit 1;
    fi
    popd > /dev/null
}

push_bibisect()
{
    local curr_day=
    local last_day_upload=

    if [ ${TB_BIBISECT} = "1" -a -n "${tb_OPT_DIR}" ] ; then

        [ $V ] && echo "Push: bibisec builds enabled"
        curr_day=$(date -u '+%Y%j')
        last_day_upload="$(cat "${TB_METADATA_DIR?}/${P}_${B?}_last-bibisect-day.txt" 2>/dev/null)"
        if [ -z "$last_day_upload" ] ; then
            last_day_upload=0
        fi
        [ $V ] && echo "bibisect curr_day=$curr_day"
        [ $V ] && echo "bibisect last_day_upload=$last_day_upload"

        # If it has been less than a day since we pushed the last build
        # (based on calendar date), skip the rest of the push phase.
        if [ $last_day_upload -ge $curr_day ] ; then
            return 0;
        fi
        [ $V ] && echo "Record bibisect"
        deliver_to_bibisect

        echo "$curr_day" > "${TB_METADATA_DIR?}/${P}_${B?}_last-bibisect-day.txt"

    fi
}

push_nightly()
{
    local curr_day=
    local last_day_upload=
    local upload_time=
    local inpath=
    local stage=
    local file=
    local target=
    local tag=

    # Push build up to the project server (if enabled).
    [ $V ] && echo "Push: Nightly builds enabled"
    curr_day=$(date -u '+%Y%j')
    last_day_upload="$(cat "${TB_METADATA_DIR?}/${P}_${B?}_last-upload-day.txt" 2>/dev/null)"
    if [ -z "$last_day_upload" ] ; then
        last_day_upload=0
    fi
    [ $V ] && echo "curr_day=$curr_day"
    [ $V ] && echo "last_day_upload=$last_day_upload"

    # If it has been less than a day since we pushed the last build
    # (based on calendar date), skip the rest of the push phase.
    if [ $last_day_upload -ge $curr_day ] ; then
        return 0;
    fi
    [ $V ] && echo "Push Nightly builds"
    prepare_upload_manifest

    upload_time="$(cat "${TB_METADATA_DIR?}/${P?}_${B?}_current-git-timestamp.log")"
    ssh upload@gimli.documentfoundation.org "mkdir -p \"/srv/www/dev-builds.libreoffice.org/daily/${B?}/${TB_NAME?}/${upload_time?}\"" || return 1

    if [ -f config_host.mk ] ; then
        inpath=$(grep INPATH= config_host.mk | sed -e "s/.*=//")
    else
        return 1
    fi
    if [ -z "${inpath?}" -o ! -d "instsetoo_native/${inpath?}" ] ; then
        return 1
    fi
    pushd instsetoo_native/${inpath?} > /dev/null
    mkdir push 2>/dev/null || return 1
    stage="./push"
    tag="${B?}~${upload_time?}"

    for file in $(find . -name "*.dmg" -o -name '*.apk' -o -name "Lib*.tar.gz" -o -name "Lib*.exe" -o -name "Lib*.zip" -o -path '*/native/install/*.msi' | grep -v "/push/")
    do
        target=$(basename $file)
        target="${tag}_${target}"
        mv $file "$stage/$target"
    done;

    rsync --bwlimit=${TB_BANDWIDTH_LIMIT} -avPe ssh ${stage}/${tag}_* "upload@gimli.documentfoundation.org:/srv/www/dev-builds.libreoffice.org/daily/${B?}/${TB_NAME?}/${upload_time?}/" || return 1
    if [ "$?" == "0" ] ; then
        ssh upload@gimli.documentfoundation.org "cd \"/srv/www/dev-builds.libreoffice.org/daily/${B?}/${TB_NAME?}/\" && { rm current; ln -s \"${upload_time}\" current ; }"
    fi
    echo "$curr_day" > "${TB_METADATA_DIR?}/${P}_${B?}_last-upload-day.txt"
    return 0
}

report_error ()
{
    local_to_mail=
    local tinder1=
    local tinder2=
    local error_kind="$1"
    shift
    local rough_time="$1"
    shift
    local error_log="$1"
    shift

    local last_success=$(cat "${TB_METADATA_DIR?}/${P}_${B?}_last-success-git-timestamp.txt")
    to_mail=
    if [ "${tb_SEND_MAIL?}" = "owner" -o "${tb_SEND_MAIL?}" = "debug" -o "${tb_SEND_MAIL?}" = "author" ] ; then
        to_mail="${TB_OWNER?}"
    else
        if [ "${tb_SEND_MAIL?}" = "all" ] ; then
            case "$error_kind" in
                owner) to_mail="${TB_OWNER?}"
                    message="box broken" ;;
                *)
                    if [ -z "$last_success" ] ; then
                        # we need at least one successful build to
                        # be reliable
                        to_mail="${TB_OWNER?}"
                    else
                        to_mail="$(get_committers)"
                    fi
                    message="last success: ${last_success?}" ;;
            esac
        fi
    fi
    if [ -n "$to_mail" ] ; then
        echo "$*" 1>&2
        echo "Last success: ${last_success}" 1>&2
        tinder1="`echo \"Full log available at http://tinderbox.libreoffice.org/${TB_TINDERBOX_BRANCH?}/status.html\"`"
        tinder2="`echo \"Box name: ${TB_NAME?}\"`"

        cat <<EOF | send_mail_msg "$to_mail" "Tinderbox failure, ${TB_NAME?}, ${TB_TINDERBOX_BRANCH?}, $message" "" "${TB_OWNER?}" ""
Hi folks,

One of you broke the build of LibreOffice with your commit :-(
Please commit and push a fix ASAP!

${tinder1}

 Tinderbox info:

 ${tinder2}
 Branch: $TINDER_BRANCH
 "starttime": $(epoch_from_utc "$rough_time")
 Machine: `uname -a`
 Configured with: `cat autogen.lastrun`

 Commits since the last success:

 $(get_commits_since_last_good commits)

 The error is:

 $(cat "$error_log")
EOF
    else
        cat $error_log
    fi
}

report_gerrit()
{
local log_type="$1"
local status=
local gzlog=

    [ $V ] && echo "report to gerrit retval=${R} log_type=${log_type}"
    if [ "$log_type" = "short"  -a "${R?}" = "0" ] ; then
        gzlog="tinder.log.gz"
        (
            echo "gerrit_task_ticket:${GERRIT_TASK_TICKET?}"
            echo "gerrit_task_branch:${GERRIT_TASK_BRANCH?}"
            echo "gerrit task_ref:${GERRIT_TASK_REF?}"
            echo ""
            echo "Build: OK"
            echo ""
            cat tb_${B}_autogen.log 2>/dev/null
        ) | gzip -c > "${gzlog}"
    else
        gzlog="tinder.log.gz"
        (
            echo "gerrit_task_ticket:${GERRIT_TASK_TICKET?}"
            echo "gerrit_task_branch:${GERRIT_TASK_BRANCH?}"
            echo "gerrit task_ref:${GERRIT_TASK_REF?}"
            echo ""
            if [ "${R?}" = "0" ] ; then
                echo "Build: OK"
            else
                echo "Build: FAIL"
            fi
            echo ""
            cat tb_${B}_autogen.log tb_${B}_clean.log tb_${B}_build.log tb_${B}_tests.log 2>/dev/null
        ) | gzip -c > "${gzlog}"
    fi

    if [ "${R?}" = "0" ] ; then
        log_msgs "Report Success for gerrit ref ${GERRIT_TASK_TICKET?}"
        status="success"
    elif [ "${R?}" = "1" ] ; then
        log_msgs "Report Failure for gerrit ref ${GERRIT_TASK_TICKET?}"
        status="failed"
    else
        log_msgs "Report Cancellation for gerrit ref ${GERRIT_TASK_TICKET?}"
        status="canceled"
    fi
    cat "${gzlog}" | ssh ${TB_GERRIT_HOST?} buildbot put --ticket "${GERRIT_TASK_TICKET?}" --status $status --log -
}


report_to_tinderbox()
{
    [ $V ] && echo "report_to_tinderbox status=$2"
    if [ -z "${tb_SEND_MAIL}" -o "${tb_SEND_MAIL}" = "none" ] ; then
        return 0
    fi

    local start_date="$1"
    local status="$2"
    local log="$3"
    local start_line=
    local xtinder="X-Tinder: cookie"
    local subject="tinderbox build start notification"
    local gzlog=
    local message_content=

    start_line="tinderbox: starttime: $(epoch_from_utc ${start_date})"
    message_content="
tinderbox: administrator: ${TB_OWNER?}
tinderbox: buildname: ${TB_NAME?}
tinderbox: tree: ${TB_TINDERBOX_BRANCH?}
$start_line
tinderbox: timenow: `date -u '+%s'`
tinderbox: errorparser: unix
tinderbox: status: ${status?}
tinderbox: END
"

    if [ "$log" = "yes" ] ; then
        gzlog="tinder.log.gz"
        (
            echo "$message_content"
            cat "${TB_METADATA_DIR?}/${P}_${B?}_current-git-timestamp.log"
            for cm in $(cat ${TB_METADATA_DIR?}/${P?}_${B?}_current-git-head.log) ; do echo "TinderboxPrint: $(generate_cgit_link "core:${cm}")" ; done
            cat tb_${B?}_autogen.log tb_${B?}_clean.log tb_${B?}_build.log tb_${B?}_tests.log 2>/dev/null
        ) | gzip -c > "${gzlog}"
        xtinder="X-Tinder: gzookie"
        subject="tinderbox gzipped logfile"
    fi

    if [ "$SEND_MAIL" = "debug" ] ; then
        echo "$message_content" | send_mail_msg "${TB_OWNER?}" "${subject?}" "${xtinder?}" '' "${gzlog}"
    elif [ "$SEND_MAIL" = "author" ] ; then
        echo "$message_content" | send_mail_msg "${TB_OWNER?}" "${subject?}" "${xtinder?}" '' "${gzlog}"
        if [ -n "${tb_BRANCH_AUTHOR}" ] ; then
            echo "$message_content" | send_mail_msg "${tb_BRANCH_AUTHOR}" "${subject?}" "${xtinder?}" '' "${gzlog}"
        fi
    else
        echo "$message_content" | send_mail_msg "tinderbox@gimli.documentfoundation.org" "${subject?}" "${xtinder?}" '' "${gzlog}"
    fi
}

rotate_logs()
{
    if [ "${R?}" = "0" ] ; then
        cp -f "${TB_METADATA_DIR?}/${P}_${B?}_current-git-head.log" "${TB_METADATA_DIR?}/${P}_${B?}_last-success-git-head.txt" 2>/dev/null
        cp -f "${TB_METADATA_DIR?}/${P}_${B?}_current-git-timestamp.log" "${TB_METADATA_DIR?}/${P}_${B?}_last-success-git-timestamp.txt" 2>/dev/null
    elif [ "${R}" != "2" ]; then # do not count abandonned false_negative loop as failure
        cp -f "${TB_METADATA_DIR?}/${P}_${B?}_current-git-head.log" "${TB_METADATA_DIR?}/${P}_${B?}_last-failure-git-head.txt" 2>/dev/null
        cp -f "${TB_METADATA_DIR?}/${P}_${B?}_current-git-timestamp.log" "${TB_METADATA_DIR?}/${P}_${B?}_last-failure-git-timestamp.txt" 2>/dev/null
    fi
    for f in tb_${B}*.log ; do
        mv -f ${f} prev-${f} 2>/dev/null
    done
    pushd "${TB_METADATA_DIR?}" > /dev/null
    for f in ${P?}_${B}*.log ; do
        mv -f ${f} prev-${f} 2>/dev/null
    done
    popd > /dev/null

}

rotate_branches()
{
    local top="$1"

    shift
    if [ -n "$1" ] ; then
        echo "$@ ${top}"
    else
        echo "$top"
    fi
}

#
# Main loop
#
run_loop()
{
local s=0

    check_branches_profile

    while true; do

        # Check for stop request
        if [ -f ${tb_CONFIG_DIR?}/stop ] ; then
            break;
        else
            sleep ${s?}
        fi

        # Select the next task
        # this set tb_BUILD_TYPE and B
        select_next_task

        # based on the build type run the appropriate build
        case "$tb_BUILD_TYPE" in
            tb)
                run_one_tb
                s=${TB_POST_BUILD_DELAY?}
                ;;
            gerrit)
                run_one_gerrit
                s=${TB_POST_BUILD_DELAY?}
                ;;
            wait)
                if [ "${s?}" != "${TB_POLL_DELAY?}" ] ; then
                    log_msgs "Nothing to do. waiting ${TB_POLL_DELAY?} seconds."
                fi
                s=${TB_POLL_DELAY?}
                ;;
            *)
                die "Invalid next mode $tb_BUILD_TYPE."
                ;;
        esac

    done

    # if we were stopped by request, let's log that
    # clean the semaphore file
    if [ -f ${tb_CONFIG_DIR?}/stop ] ; then
        log_msgs "Stoped by request"
        rm ${tb_CONFIG_DIR?}/stop
    fi

}

#
# Run a gerrit build
#
# Run a a subshell to isolate Branch-level config
#
run_one_gerrit()
{
    R=0
    (
        trap 'interupted_build' SIGINT SIGQUIT

        log_msgs "Starting tb build gerrit ref:${GERRIT_TASK_TICKET?}"
        # source branch-level configuration
        source_branch_level_config "${B?}" "gerrit"

        if [ -z "$TB_BUILD_DIR" ] ; then
            TB_BUILD_DIR="${TB_GIT_DIR?}"
        fi

        # clean-up the git repo associated with the
        # branch and checkout the target sha
        prepare_git_repo_for_gerrit

        # gerrit build are not incremental
        # always use all the phases
        local phase_list="autogen clean make test push"

        pushd ${TB_BUILD_DIR?} > /dev/null || die "Cannot cd to build dir : ${TB_BUILD_DIR?}"

        # run the build
        do_build ${phase_list?}

        # tell teh gerrit buildbot of the result of the build
        # R contain the overall result
        report_gerrit

        popd > /dev/null
        exit ${R?}
    )
    R="$?"

    # check we we intercepted a signal, if so bail
    if [ "${R?}" = "4" ] ; then
        exit -1
    fi
}

#
# Run a tinderbox build
#
# Run as subshel to isolate Branch-level config
#
run_one_tb()
{
    R=0
    (
        trap 'interupted_build' SIGINT SIGQUIT

        log_msgs "Starting tb build for sha:$(cat "${TB_METADATA_DIR?}/${P}_${B?}_current-git-head.log")"
        source_branch_level_config "${B?}" "${tb_BUILD_TYPE?}"
        if [ -z "$TB_BUILD_DIR" ] ; then
            TB_BUILD_DIR="${TB_GIT_DIR?}"
        fi

        # for 'primer' build we expect the repo to be in a buildable
        # condition already
        if [ "${tb_ONE_SHOT?}" != "1" ] ; then
            prepare_git_repo_for_tb
        else
            collect_current_head
        fi

        local phase_list
        local retry_count=3

        tb_LAST_CHECKOUT_DATE="$(cat "${TB_METADATA_DIR?}/${P}_${B?}_current-git-timestamp.log")"
        report_to_tinderbox "${tb_LAST_CHECKOUT_DATE?}" "building" "no"
        tb_TB_BUILD_REPORTED=1

        if [ "$TB_INCREMENTAL" = "1" ] ; then
            phase_list="autogen make test push"
        else
            phase_list="autogen clean make test push"
        fi

        pushd ${TB_BUILD_DIR?} > /dev/null || die "Cannot cd to build dir : ${TB_BUILD_DIR?}"

        while [ "${phase_list}" != "" ] ; do

            do_build ${phase_list?}

            if [ "${R?}" = "0" ] ; then
                report_to_tinderbox "${tb_LAST_CHECKOUT_DATE}" "success" "yes"
                phase_list=
                log_msgs "Successful tb build for sha:$(cat "${TB_METADATA_DIR?}/${P}_${B?}_current-git-head.log")"
            elif [ "${R?}" = "2" ] ; then
                log_msgs "False negative build, skip reporting"
                    # false negative does not need a full clean build, let's just redo make and after
                retry_count=$((retry_count - 1))
                if [ "$retry_count" = "0" ] ; then
                    log_msgs "False Negative Failed tb build for sha:$(cat "${TB_METADATA_DIR?}/${P}_${B?}_current-git-head.log")"
                    phase_list=""
                    R=2
                    if [ "${tb_ONE_SHOT?}" != "1" ] ; then
                        report_to_tinderbox "${tb_LAST_CHECKOUT_DATE?}" "fold" "no"
                    fi
                else
                    log_msgs "False Negative Retry tb build for sha:$(cat "${TB_METADATA_DIR?}/${P}_${B?}_current-git-head.log")"
                    phase_list="make test push"
                    R=0
                fi
            else
                printf "${tb_REPORT_MSGS?}:\n\n" > report_error.log
                echo "======" >> report_error.log
                if [ "${tb_REPORT_LOG?}" == "tb_${B}_build.log" ] ; then
                    tail -n1000 ${tb_REPORT_LOG?} >> report_error.log
                else
                    cat ${tb_REPORT_LOG?} >> report_error.log
                fi
                report_error committer "${tb_LAST_CHECKOUT_DATE?}" report_error.log
                report_to_tinderbox "${tb_LAST_CHECKOUT_DATE?}" "build_failed" "yes"
                phase_list=""
                log_msgs "Failed tb build for sha:$(cat "${TB_METADATA_DIR?}/${P}_${B?}_current-git-head.log")"
            fi
        done
        tb_LAST_CHECKOUT_DATE=
        rotate_logs
        popd > /dev/null
        if [ "${R?}" = "0" -a "${tb_BUILD_TRIGERRED?}" = "1" ] ; then
            rm -f "${TB_TRIGGER_FILE?}"
        fi
        exit ${R?}
    )
    R="$?"

    # check we we intercepted a signal, if so bail
    if [ "${R?}" = "4" ] ; then
        exit -1
    fi
}

#
# run a one-shot tb run
#
run_primer()
{
    check_branches_profile

    # as a special case the select_next_task
    # if tb_ONE_SHOT=1 return the first branch
    # of the slected list
    # a primer build build one and one branch only.
    select_next_task

    # as a special case tun_one_tb() does not reset the repo
    # do it will just build what is there.
    # it is the user responsability to make sure that the state
    # of the repo is correct.
    # the rational is to allow 'primer' build to be use
    # to test uncommited changes and/or local commit
    run_one_tb
}

#
# Select a gerrit task from
# the gerrit buildbot plugin
#
select_next_gerrit_task()
{
    local result
    local has_task

    [ $V ] && echo "Try to get a task for gerrit buildbot"

    tb_BUILD_TYPE="wait"
    GERRIT_TASK_TICKET=""
    GERRIT_TASK_BRANCH=""
    GERRIT_TASK_REF=""
    GERRIT_TASK_FEATURE=""
    result=$(ssh ${TB_GERRIT_HOST?} buildbot get -p core -a ${tb_GERRIT_PLATFORM?} --format BASH ${tb_GERRIT_BRANCHES?})
    [ $V ] && echo "Get task result:${result}"

    has_task=$(echo "$result" | grep "^GERRIT_TASK_")
    if [ -n "${has_task}" ] ; then
        eval "${result}"
        if [ -z "${GERRIT_TASK_TICKET}" -o -z "${GERRIT_TASK_REF}" -o -z "${GERRIT_TASK_BRANCH}" ] ; then
            [ $V ] && echo "no valid task from gerrit buildbot"
        else
            tb_BUILD_TYPE="gerrit"
            B="${GERRIT_TASK_BRANCH?}"
            [ $V ] && echo "got task TASK_TICKET=$GERRIT_TASK_TICKET TASK_REF=$GERRIT_TASK_REF TASK_BRANCH=${B?} "
        fi
    else
        [ $V ] && echo "no task from gerrit buildbot"
    fi

}

#
# Select the next task to do
# either tb or gerrit depending
# on the mode of operation
#
select_next_task()
{

    if [ "${tb_MODE?}" = "tb" ] ; then
        select_next_tb_task
    elif [ "${tb_MODE?}" = "gerrit" ] ; then
        select_next_gerrit_task
    else
        if [ "${tb_NEXT_PRIORITY}" = "tb" ] ; then
            select_next_tb_task
            if [ "${tb_BUILD_TYPE?}" = "wait" ] ; then
                select_next_gerrit_task
            fi
        else
            select_next_gerrit_task
            if [ "${tb_BUILD_TYPE?}" = "wait" ] ; then
                select_next_tb_task
            fi
        fi

        # if we use a 'fair' priority
        # switch the order in which we try to get stuff
        if [ "${tb_MODE?}" = "fair" ] ; then
            if [ "${tb_BUILD_TYPE?}" = "tb" ] ; then
                tb_NEXT_PRIORITY="gerrit"
            elif [ "${tb_BUILD_TYPE?}" = "gerrit" ] ; then
                tb_NEXT_PRIORITY="tb"
            fi
        fi
    fi
}

#
# Select a Tinderbox task
# by seraching for new commits
# on on of the branches under consideration
#
select_next_tb_task()
{
    local b
    local r

    tb_BUILD_TYPE="wait"
    for b in ${tb_TB_BRANCHES?} ; do
        if [ "${tb_ONE_SHOT?}" = "1" ] ; then
            B="${b?}"
            tb_BUILD_TYPE="tb"
            break
        else
            ( check_for_commit "$b" )
            r="$?"
            if [ ${r?} = 0 -o ${r?} = 1 ] ; then
                B="${b?}"
                tb_TB_BRANCHES=$(rotate_branches ${tb_TB_BRANCHES?})
                tb_BUILD_TYPE="tb"
                tb_BUILD_TRIGGERED="${r?}"
                break
            fi
        fi
    done
}

send_mail_msg()
{
    local to="$1"
    local subject="$2"
    local headers="$3"
    local bcc="$4"
    local log="$5"
    local quiet="-q"
    local smtp_auth=""

    if [ -n "${TB_SMTP_USER}" ] ; then
        smtp_auth="-xu ${TB_SMTP_USER?} -xp ${TB_SMTP_PASSWORD?}"
    fi

    [ $V ] && log_msgs "send mail to ${to?} with subject \"${subject?}\""
    [ $V ] && quiet=""
    if [ -n "${log}" ] ; then
        ${tb_BIN_DIR?}/tb_send_email $quiet -f "${TB_OWNER?}" -s "${TB_SMTP_HOST?}" $smtp_auth -t "${to?}" -bcc "${bcc?}" -u "${subject?}" -o "message-header=${headers?}" -a "${log?}"
    elif [ -n "${headers?}" ] ; then
        ${tb_BIN_DIR?}/tb_send_email $quiet -f "${TB_OWNER?}" -s "${TB_SMTP_HOST?}" $smtp_auth -t "${to?}" -bcc "${bcc?}" -u "${subject?}" -o "message-header=${headers?}"
    else
        ${tb_BIN_DIR?}/tb_send_email $quiet -f "${TB_OWNER?}" -s "${TB_SMTP_HOST?}" $smtp_auth -t "${to?}" -bcc "${bcc?}" -u "${subject?}"
    fi
}

#
# Setup factory default for variables
#
# this is invoked before the profile is known
# so it cannot be overriden in the profile's phases.sh
#
set_factory_default()
{
    TB_INCREMENTAL=
    TB_GERRIT_HOST="logerrit"
    TB_POLL_DELAY=120
    TB_POST_BUILD_DELAY=15
    TB_BIBISECT=0
    TB_PUSH_NIGHTLIES=0
    TB_BANDWIDTH_LIMIT=2000

    tb_ONE_SHOT=

}

#
# Setup default at the profile level
#
# Based on the content of the profile
# setup some interna variable
# assign some default and
# do some other housekeeping
#
# Verify that mandatory profile
# variables are defined
setup_profile_defaults()
{
    local os

    if [  -n "${TB_LOGFILE}" ] ; then
        if [ ! -f "${TB_LOGFILE?}" ] ; then
            touch "${TB_LOGFILE?}" || die "Creating ${TB_LOGFILE?}"
        fi
        tb_LOGFILE="${TB_LOGFILE?}"
    fi

    if [ -z "{TB_METADATA_DIR}" ] ; then
        TB_METADATA_DIR="${tb_CONFIG_DIR?}/meta/"
    fi
    if [ ! -d ${TB_METADATA_DIR} ] ; then
        mkdir -p "${TB_METADATA_DIR?}" || die "Creating ${TB_METADATA_DIR?}"
    fi

    if [ -z "$TB_ID" ] ; then
        die "Error: TB_ID is required to be configured"
    fi

    if [ -z "${TB_OWNER}" ] ; then
        die "Error: TB_OWNER is required to be configured"
    fi

    if [ -z "${TB_NAME}" ] ; then
        die "TB_NAME is required to be configured"
    fi

    if [ -z "${tb_GERRIT_PLATFORM}" ] ; then
        os=$(uname)
        case "$os" in
            *Linux*)
                tb_GERRIT_PLATFORM="Linux"
                ;;
            Darwin)
                tb_GERRIT_PLATFORM="MacOSX"
                ;;
            CYGWIN*)
                tb_GERRIT_PLATFORM="Windows"
                ;;
        esac
    fi

    if [ -z "${tb_MODE}" ] ; then
        tb_MODE="${TB_DEFAULT_MODE:-tb}"
    fi

    if [ -z "${tb_DUAL_PRIORITY}" ] ; then
        tb_DUAL_PRIORITY="${TB_DUAL_PRIORITY:-fair}"
    fi

    if [ -z "${tb_BRANCHES}" ] ; then
        tb_BRANCHES="${TB_BRANCHES}"
        if [ -z "${tb_BRANCHES}" ] ; then
            log_msgs "TB_BRANCHES and -b not specified. Default to 'master'"
            tb_BRANCHES="master"
        fi
    fi
}

source_branch_level_config()
{
    local b="$1"
    local t="$2"

    if [ -f "${tb_PROFILE_DIR?}/branches/${b?}/config" ] ; then
        source "${tb_PROFILE_DIR?}/branches/${B?}/config"
    fi
    if [ -f "${tb_PROFILE_DIR?}/branches/${b?}/config_${t?}" ] ; then
        source "${tb_PROFILE_DIR?}/branches/${b?}/config_${t?}"
    fi
    # we have verified the branch before so that should work
    if [ -z "${TB_TINDERBOX_BRANCH}" ] ; then
        TB_TINDERBOX_BRANCH=$(determine_default_tinderbox_branch "${b?}")
    fi
}

#
# Verify the coherence of the command line arguments
#
verify_command()
{
local rc

    case "${tb_MODE?}" in
        dual)
            if [ -z "$tb_GERRIT_PLATFORM" ] ; then
                die "tb_GERRIT_PLATFORM is required for mode involving gerrit"
            fi
            case "${tb_DUAL_PRIORITY?}" in
                fair|gerrit|tb)
                    ;;
                *)
                    log_msgs "TB_DUAL_PRIORITY:${tb_DUAL_PRIORITY?} is not a valid value, defaulting to 'fair'"
                    tb_DUAL_PRIORITY="fair"
                    ;;
            esac
            ;;
        gerrit)
            if [ -z "$tb_GERRIT_PLATFORM" ] ; then
                die "tb_GERRIT_PLATFORM is required for mode involving gerrit"
            fi
            ;;
        gerrit-patch)
            tb_SEND_MAIL="none"
            tb_PUSH_NIGHTLIES=0
            ;;
        tb)
            if [ "${tb_ONE_SHOT?}" = "1" ] ; then
                tb_SEND_MAIL="none"
                tb_TB_BRANCHES="${tb_BRANCHES?}"
                B=$(get_first_branche ${tb_BRANCHES?})
                if [ "${tb_PUSH_NIGHTLIES}" = "1" ] ; then
                    rm -f "${METADATA_DIR?}/${P?}_${B?}_last-upload-day.txt"
                fi
            fi
            ;;
        *)
            die "Invalid mode $tb_MODE"
            ;;
    esac


    if [ -z "${tb_SEND_MAIL}" ] ; then
        tb_SEND_MAIL="${TB_SEND_MAIL}"
    fi
    # if we want email to be sent, we must make sure that the required parameters are set in the profile (or in the environment)
    case "${tb_SEND_MAIL?}" in
        all|tb|owner|debug|author)
            if [ -z "${TB_SMTP_HOST}" ] ; then
                die "TB_SMTP_HOST is required in the config to send email"
            fi
            if [ -z "${TB_SMTP_USER}" ] ; then
                echo "Warning: missing SMTPUSER (can work, depends on your smtp server)" 1>&2
            fi
            if [ -n "${TB_SMTP_USER}" -a -z "${TB_SMTP_PASSWORD}" ] ; then
                die "TB_SMTP_PASSWORD is required with TB_SMTP_USER set"
            fi
            ;;
        none)
            ;;
        *)
            die "Invalid -m/TB_SEND_MAIL argument:${tb_SEND_MAIL}"
            ;;
    esac

}

# Do we have timeout? If yes, guard git pull with that - which has a
# tendency to hang forever, when connection is flaky
if which timeout > /dev/null 2>&1 ; then
	# std coreutils - timeout is two hours
	tb_TIMEOUT="$(which timeout) 2h"
fi

################
# ATTENTION:
# Nothing below this point can be overriden at the platform-level
# so you should probably add code above this point
# unless you have a darn good reason not to

# source the platform specific override

mo="$(uname -o 2>/dev/null)"
ms="$(uname -s 2>/dev/null)"
if [ -n "${mo}" -a -f "${tb_BIN_DIR?}/tb_internals_${mo}.sh" ] ; then
    source "${tb_BIN_DIR?}/tb_internals_${mo}.sh"
else
    if [ -n "${ms}" -a -f "${tb_BIN_DIR?}/tb_internals_${ms}.sh" ] ; then
        source "${tb_BIN_DIR?}/tb_internals_${ms}.sh"
    fi
fi
unset mo
unset ms


determine_make


# source the standard build phases
source ${tb_BIN_DIR?}/tb_phases.sh