

### OLD --- only use this when manually having downloaded the file via email




















if (FALSE) {
  source("~rsaporta/git/orch/src/VidServData/01a Move files to folder.r")

#   export YTData="RScript -e 'source("'"~rsaporta/git/orch/src/VidServData/01a Move files to folder.r"'")'"
#   echo $YTData
#   $YTData
}

setScienceIfNot(proj="VidServData", subProj="IngestYTData", load=FALSE)

expected_subfolders <- c("Premium", "UGC")

## Grab the correct filedrop location
fold.zip <- ifelse(.Pfm == "Darwin", "~/Downloads", "/data/filedrop/VideoServices/YouTube_CMS_Data")

pat <- "YouTube CMS Data"
f.orig <- extractFilesFromFolder(folder=fold.zip, pattern=pat, ext="zip", recursive=TRUE, full=TRUE)

if (!length(f.orig))
  stop("\n\nf.orig has no length -- meaning there was no file matching '", pat, "' in '", fold.zip, "'")
if (length(f.orig) > 1)
  stop("\n\nf.orig should have length 1, but it has length ", length(f.orig))
if (!file.exists(f.orig))
  stop("\n\nfile f.orig does not exist -- how the hell did this happen if using extractFilesFromFolder() ??")

folder <- data.p("zips_from_vidser_team")
f.zip <- as.path(folder, basename(f.orig))
file.move(f.orig, f.zip)

stopifnot(file.exists(f.zip))

folder.new <- tar_unzip(f.zip)

## There should be exactly two folders in the unzipped file
if (!all(dir(folder.new) == expected_subfolders))
  stop("There was a problem with the zip file -- the contents is something other than Premium UGC")

for (sfold in expected_subfolders) {
  tmp.files <- as.path(folder.new, sfold) %>% dir(full=FALSE, recursive=TRUE, include.dirs=FALSE)
  if (length(tmp.files)) {
    verboseMsg(verbose, sprintf("%10s: Moving %i files", sfold, length(tmp.files)))
    from <- as.path(folder.new, sfold, tmp.files)
    to <- ingest.p(sfold, sub("Orchard", "Orch", tmp.files, ignore.case=TRUE))
    file.move(from, to)    
  } else {
    verboseMsg(verbose, sprintf("%10s: There are no files to move", sfold))
  }
}


## If on local machine, need to sync to remote RBox. 
## Note that since March 2016 (when FTP was setup) this is no longer needed.
if (.Pfm == "Darwin")  
{
    ## Sync ingest Dir
    catheader("syncing ingestDir  (make sure VPN'd if not at office)")
    syncIngestDir()

    ## Call next function from R Box
    catn("NEXT STEP (from Rbox):\n\n\tsource(\"~/git/orch/src/VidServData/01 Ingest YouTubeMGMT Data into Snowflake.r\")\n")  
}

TRUE