// same hhash and count as previous entry; start grouping
        if ($prev_count === $bib[3]) {
            $bibs[$bib[2]] = $bib[2];
        }
    } else {
        if (count($bibs) > 1) {
            do_fix_dupe($bibs);
            // print join(",", array_keys($bibs)) . "\n";
        }
        $prev_hhash = $bib[0];
        $prev_count = $bib[3];
        $bibs = array($bib[1] => $bib[1]);
    }
}
if (count($bibs) > 1) {
    do_fix_dupe($bibs);
}
print "</table>";
mysql_query("commit");
function do_fix_dupe($bibIDs)
{
    /*
     * $bibIDs is an array of IDs of records records that are all exactly the SAME.
     * We try to identify which of the records is the most important, keep that as the master,
     * and retrofit all pointers to the other records to point at that one.
     */
    $bibIDlist = join(",", $bibIDs);
    $mostPopular = mysql_fetch_row(mysql_query("select rec_ID from Records where rec_ID in ({$bibIDlist}) order by rec_Popularity desc limit 1"));
    $mostPopular = $mostPopular[0];
    // remove mostPopular from the bibID list
    $bibIDlist = preg_replace("/\\b{$mostPopular},|,{$mostPopular}\\b/", "", $bibIDlist);
require_once dirname(__FILE__) . '/../../common/connect/applyCredentials.php';
require_once dirname(__FILE__) . '/../../common/php/dbMySqlWrappers.php';
require_once dirname(__FILE__) . '/../../common/php/utilsTitleMask.php';
require_once dirname(__FILE__) . '/../../admin/verification/fetchRecordDetails.php';
if (!is_admin()) {
    return;
}
session_start();
$do_merge_details = false;
if (@$_REQUEST['keep'] && @$_REQUEST['duplicate']) {
    //user has select master and dups- time to merge details
    $do_merge_details = true;
    $_REQUEST['bib_ids'] = join(',', array_merge($_REQUEST['duplicate'], array($_REQUEST['keep'])));
    //copy only the selected items
} elseif (@$_REQUEST['commit']) {
    do_fix_dupe();
    return;
}
$finished_merge = false;
if (@$_SESSION['finished_merge']) {
    unset($_SESSION['finished_merge']);
    $finished_merge = true;
}
if (@$_REQUEST['keep']) {
    $_SESSION['master_rec_id'] = $master_rec_id = $_REQUEST['keep'];
}
//store the master record id in the session
if (!@$_REQUEST['bib_ids']) {
    return;
}
mysql_connection_select(DATABASE);