
cleanRightsholderColumn_ <- function(DT, nm.newcol = "Rightholders tracks", colsToReplace=c("Rightholder's tracks", "Rightholder tracks"), browse_on_fail=TRUE)  {
## This function combines the colsToReplace columns into a single column named the value of nm.newcol
##
## Background:  
##   The same type of file, depending on when it was delivered, may have a different column name
##   Specifically, with and withou an apostrophe for "Rightholder's tracks" // "Rightholder tracks"
##   Thus we will combine them all into one

  if (length(colsToReplace) != 2)
    stop ("'colsToReplace' must have length exactly 2")


  if (!all(wh <- colsToReplace %in% names(DT))) {
    if (any(wh))
      setnames(DT, colsToReplace[wh], nm.newcol)
    else 
      warning ("columns not in (or no longer in) DT")
    return(invisible(DT))
  }

  orig.column <- min(sapply(colsToReplace, grep, names(DT)))
  setnames(DT, colsToReplace[[1]], "RT1")
  setnames(DT, colsToReplace[[2]], "RT2")

  ## Convert to integer64
  convert_to_int64_if_sum_breaks_numeric_(DT, cols=c("RT1", "RT2"))
  # DT[, "RT1" := as.integer64(RT1)]
  # DT[, "RT2" := as.integer64(RT2)]

  ## Confirm not both not-NA at once  [it IS okay for both to be NA at once.  The final column will thus be NA at that row]
  stopifnot(DT[, !any(!is.na(RT1) & !is.na(RT2))])

  ## create new column
  DT[, (nm.newcol) := RT1]
  DT[is.na(RT1), (nm.newcol) := RT2]

  browser(expr=inDebugMode("cleanRightsholderColumn_", "rights"), text="in cleanRightsholderColumn_() before the potential crash")

  ## Confirm values are full, by checking sum
  if (DT[, sum(c(RT1, RT2), na.rm=TRUE) == sum(get(nm.newcol), na.rm=TRUE)])
    DT[, c("RT1", "RT2") := NULL]
  else {
    # browser(expr=browse_on_fail, text="inside the else clause of cleanRightsholderColumn_() where the columns did not match and now will fail")
    stop ("RT1 and RT2 did not match up (The two Rightholders tracks columns)\nHINT:  run traceback() to see which line filetypes were being processed then execute: \n\t  debugOn(cleanRightsholderColumn_);  reveal(cleanRightsholderColumn_)\n ")
  }

  ## If the above crashed, this is the investigation into why
  if (FALSE) {
    ## These should be equal ... 
    DT[, sumn(RT1, RT2)]
    DT[, sumn(get(nm.newcol))]

    ## ... if they are off, where specifically do they not match
    sumn(DT[, as.numeric(get(nm.newcol))])
    DT[, sumn(RT1, RT2) == sumn(get(nm.newcol)), by="Report start date"]
    DT[1:2, sumn(RT1, RT2) ]
    # ... found it
  }

  setcolorderpt(DT, c(names(DT)[1:(orig.column-1)], nm.newcol))

  is(DT[["Total tracks"]])
  ## Confrim Percentages
  if ("Total tracks" %in% names(DT)) {
    if (!is.numeric(DT[["Total tracks"]]))
      DT[, ("Total tracks") := as.numeric(get("Total tracks"))]
    DT[`Total tracks` != 0, pro_rata_exact := get(nm.newcol) / `Total tracks`]
    DT[`Total tracks` == 0, pro_rata_exact := `Pro rata share` / 100]
    if ("Pro rata share" %in% names(DT)) {
      if (DT[, all(equals(pro_rata_exact, `Pro rata share` / 100, tol=.001))]) {
        cat("\tPro Rata Share confirmed\n")
        DT[, 'Pro rata share' := pro_rata_exact]
        DT[, pro_rata_exact := NULL]
      }
      else 
        cat("Pro Rata Share does NOT add up\n")
    }
  }

  return(invisible(DT))
}