# 00 Hidden files from DL to Ingest Dir.r

setScienceIfNot(proj="ContentAnalysis", subProj="Hidden")


## This was an attempt to read the files right from Google Spreadsheets
## ------------------------------------------------------------------------- ##
if (FALSE) {
    require("googlesheets")

    keys <- c(
      Orchard = "0Ag_AhktJtCY6dENTdW8wWnhJOWg3THdjMGRhN0k0cGc"
    , IODA = "0Ag_AhktJtCY6dHR4T3FMd09jSlhMc19BdVl0bnVxcWc"
    , IRIS = "0Ag_AhktJtCY6dGxnMThhR3lkeHBjQXdqdDliN0RaRHc"
    )
}
## ------------------------------------------------------------------------- ##


## -------------------------------------------------------------------------------------- ##
## ON LOCAL ----------------------------------------------------------------------------- ##
## ON LOCAL ----------------------------------------------------------------------------- ##
## ON LOCAL ----------------------------------------------------------------------------- ##
## -------------------------------------------------------------------------------------- ##
if (.Pfm == "Darwin") {

      ## Extract files from the Downloads folder
      files_in_dl_folder <- extractFilesFromFolder(folder="~/Downloads", pattern="iTunes Hidden", recursive=TRUE, ext=c("tsv", "csv"), full=TRUE, showWarnings=FALSE)

      if (length(files_in_dl_folder)) {
          verboseMsg(verbose, sprintf("Moving %i files:", length(files_in_dl_folder)), pasteC("\n\t'", files_in_dl_folder, "'"))

          ## New location
          destinationFiles <- basename(files_in_dl_folder) %>% ingest.p()

          ## Check for existing files and zArchive them
          currentFiles <- destinationFiles %>% {.[file.exists(.)]}
          for (.curfile in currentFiles) {
            zArchive(.curfile, addTimeStamp=TRUE)
          }

          ## Copy the files over
          file.rename(files_in_dl_folder, destinationFiles)

          ## Confirm
          stopifnot(!file.exists(files_in_dl_folder))
          stopifnot(file.exists(destinationFiles))
      } else {
        verboseMsg(verbose, "No files found to move")
      }

      syncIngestDir(rsync=TRUE)
} else {

## -------------------------------------------------------------------------------------- ##
## ON AWS   ----------------------------------------------------------------------------- ##
## ON AWS   ----------------------------------------------------------------------------- ##
## ON AWS   ----------------------------------------------------------------------------- ##
## -------------------------------------------------------------------------------------- ##

    ## -------------------------------- ##
    ##       EXECUTE                    ##
    ## -------------------------------- ##
    if (FALSE) 
    {
      warning("Do you want to do something about the name for  :       Any Event in First90days")

      setGitBranchToSystem(); .g()
      setScienceIfNot(proj="ContentAnalysis", subProj="Hidden")
      if (!exists("wh"))  wh <- getWH_already_on("looker_wh_large")
      setSnowflake(wh=wh)
      source("~/git/orch/src/ContentAnalysis/pull DT.releases_with_metadata.r")
      source("~/git/orch/src/ContentAnalysis/01 Hidden Content Modelling.r")
      try(source("~/git/orch/src/ContentAnalysis/02 Hidden Content - Simple.r"), silent=FALSE)
      source("~/git/orch/src/ContentAnalysis/05 Ingest into Snowflake.r")
      jesusForData(DT.releases_with_metadata)
    }

}
