# screen -xRR create_lookml
# R

setScience(proj="Looker", subProj="LookML_Creation", load=FALSE, subl=FALSE)
setPrompt(full=TRUE)

setGitBranchToSystem(); .g(); .us()

## MANUAL FLAGS
print_to_screen <- TRUE
print_to_screen <- FALSE

wh <- getSnowflakeWH() %>% {ifelse(. == "", "LOOKER_WH_LARGE", .)}
dbname <- getSnowflakeDB() %>% {ifelse(. == "", "PROD", .)}

tbls <- c("accounting", "analytics_track_level")
schema <- "bi"

setSnowflake(wh=wh, dbname=dbname)
# file.outs <- sapply(tbls, create_lookml_from_tbl, schema=schema, wh=wh, dbname=dbname, print_to_screen=print_to_screen, TEST_RUN=FALSE)

## FOR bi FILES
files.outs <- create_lookml_model_from_schema(schema=schema, only_include_these_tbls=NULL, ignore_these_tbls=tbls, dbname=dbname, verbose=TRUE)
files.outs <- create_lookml_model_from_schema(schema=schema, only_include_these_tbls=tbls, ignore_these_tbls=NULL, dbname=dbname, verbose=TRUE)
files.outs <- create_lookml_model_from_schema(schema=schema, dbname=dbname, verbose=TRUE)

### ~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ ###

## DEBUGGING IF NEEDED
if (FALSE) {
  debug(create_lookml_from_tbl)
  create_lookml_from_tbl("analytics_track_level", "bi", wh=wh, dbname=dbname, TEST_RUN=TRUE)
  create_lookml_from_tbl("accounting", "bi", wh=wh, dbname=dbname, TEST_RUN=TRUE)
}

## LOCAL
if (FALSE) 
{
  setScienceIfNot(proj='Looker')
  sourceSupportFns(proj='Looker')
  bringAndCopyLookerFiles(f='~rsaporta/git/orch/out/Looker/auto_generated_lookmls/bi')
  
  .o('/Applications/Tower 2.0.app')
}


### ~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ ###




### run this on local machine to pull files.  
##  Then manually copy them over to 
##     ~/Development/looks/
##


if (.Pfm == "Darwin") 
{
  ## Folders on local machine
  folder <- "~/git/orch/out/Looker/auto_generated_lookmls/"
  folder_looks <- "~/Development/looks"

  ## Sync over the files
  warning("Will not sync if files exist and newer ones are at remote")
  bringme(folder)

  ## Files to copy
  ret.copy <- c("analytics_track_level", "analytics_release_level", "accounting") %>% sprintf("bi.%s.view.lookml", .) %>% as.path(folder, .) %>% {.[file.exists(.)]} %>% file.copy(to=folder_looks, recursive=FALSE, overwrite=TRUE)

  .o(c(folder_looks, folder))
  .o("/Applications/Tower 2.0.app")
}

