#  S1 := detail files
#  S2 := revenue summaries
#
#  The 8-digit code is the content-owner ID  (aka Distributor ID)
#  
# S1 files have an aggregated total at the bottom of the files, spearated by a row that says "rowcount"
# S2 files have a stack of reports in a txt file

#  
#  distributor_id,distributor_name,distributor_ds
#  80027963,Blind Pig,BLPG
#  80028547,Iris,IRIS
#  80028967,Orchard,ORCD
#  80029727,IODA,IODA
#  80031727,Reader's Digest,RDNA
#  80031813,BFM,BFM
#  80032084,Relapse,RLPS
#  80032149,Naive,NVE
#  80035150,Priddis,PRI
#  80037290,TVT,TVT
#  85356999,Fan Club,FNCB
#  85420853,BFMA,BFMA
#  85744662,Rotana,ROT


setScience("AppleMusic", subProj="finance", load=FALSE, subl=FALSE)

folders <- ingest.p("ftp_sync", "iTunes Apple Music DONE") %>% dir(full=TRUE)
folder <- max(folders)

files.ww <- extract("WW", dir(folder)) %>% as.path(folder, .)



header_rows <- fread(files.ww[[1]], nrows=4, skip=0)
body_rows   <- fread(files.ww[[1]], nrows=4, skip=4)


files.all <- dir(folder) %>% {.[!grepl("TEST - ", .)]}
DT.filenames <- files.all %>% gsub("\\.(.{3,8})$", "_\\1", .) %>% gsub("^S(\\d)(_| )*", "S\\1_", .) %>% strsplit("_") %>% do.call(rbind, .) %>% as.data.table %>% setnames(c("FileType", "distributor_id", "date", "country_code", "ext"))
DT.filenames[, file_name := files.all]

f.test <- DT.filenames[, tail(file_name, 5)] %>% as.path(folder, .) %>% head(1)
catnn(readLines(f.test))


files.all
f <- extract("80028967", files.ww)[[1]]
catnn(readLines(f, n=20))