## screen -xRR PRS

setScience("PerformanceRights", create=FALSE, subl=FALSE)
wh <- "Science"

setSnowflake(wh=wh)

if (FALSE) {
  sfShowFileFormat()
  sfCreateFileFormat.CSV(format_name="CSV_quoted_with_quotes_as_escape", delimiter=",", gzip=FALSE, header=TRUE, NULL_IF="", quote_string="\"", escape_string=NULL)
}

catheader(file.abramus);  catnn(readLines(file.abramus, n=150))
catheader(file.GVL_ISRC);  catnn(readLines(file.GVL_ISRC, n=150))
catheader(file.GVL_ArtistTrack);  catnn(readLines(file.GVL_ArtistTrack, n=150))

file.abramus <- ingest.p("abramus", "abramus 20160414 - ioda_arq1_2_3_4", ext="csv")
file.GVL_ISRC <- ingest.p("GVL", "GVL-ClaimsAgainstOtherDistributors_ISRC_2016-03-30", ext="csv")
file.GVL_ArtistTrack <- ingest.p("GVL", "GVL-ClaimsAgainstOtherDistributors_Artist-Track_2016-03-30", ext="csv")
file.GVL_Claims_ISRC <- ingest.p("GVL", "GVL-Claims_ISRC", ext="csv")

## Make sure files exist
stopifnot(file.exists(file.abramus))
stopifnot(file.exists(file.GVL_ISRC))
stopifnot(file.exists(file.GVL_ArtistTrack))
stopifnot(file.exists(file.GVL_Claims_ISRC))

## Convert all of the files to UTF-8
  file.abramus_utf8 <- gsub("\\.csv$", ".utf8.csv", file.abramus)
  if (!file.exists(file.abramus_utf8))
    convert_encoding_linux(from.file=file.abramus, to.file=file.abramus_utf8, from.encoding="WINDOWS-1252", to.encoding="UTF-8", ignore_errors=TRUE)

  file.GVL_ISRC_utf8 <- gsub("\\.csv$", ".utf8.csv", file.GVL_ISRC)
  if (!file.exists(file.GVL_ISRC_utf8))
    convert_encoding_linux(from.file=file.GVL_ISRC, to.file=file.GVL_ISRC_utf8, from.encoding="WINDOWS-1252", to.encoding="UTF-8", ignore_errors=TRUE)

  file.GVL_ArtistTrack_utf8 <- gsub("\\.csv$", ".utf8.csv", file.GVL_ArtistTrack)
  if (!file.exists(file.GVL_ArtistTrack_utf8))
    convert_encoding_linux(from.file=file.GVL_ArtistTrack, to.file=file.GVL_ArtistTrack_utf8, from.encoding="WINDOWS-1252", to.encoding="UTF-8", ignore_errors=TRUE)

  file.GVL_Claims_ISRC_utf8 <- gsub("\\.csv$", ".utf8.csv", file.GVL_Claims_ISRC)
  if (!file.exists(file.GVL_Claims_ISRC_utf8))
    convert_encoding_linux(from.file=file.GVL_Claims_ISRC, to.file=file.GVL_Claims_ISRC_utf8, from.encoding="WINDOWS-1252", to.encoding="UTF-8", ignore_errors=TRUE)

## Confirm file was created
stopifnot(file.exists(file.abramus_utf8))
stopifnot(file.exists(file.GVL_ISRC_utf8))
stopifnot(file.exists(file.GVL_ArtistTrack_utf8))
stopifnot(file.exists(file.GVL_Claims_ISRC_utf8))


schema <- "manualCSVloads"

tbl.abramus <- "abramus"
tbl.GVL_ISRC <- "GVL_ISRC"
tbl.GVL_ArtistTrack <- "GVL_ArtistTrack"
tbl.GVL_Claims_ISRC      <- "GVL_Claims_ISRC"


## NEW UTF-8 FILE
sfPopulateTable(file_name=file.abramus_utf8,          tbl=tbl.abramus,          schema=schema, format_name="CSV_quoted_with_quotes_as_escape", encoding_for_fread="UTF-8", clean_column_names=TRUE)
sfPopulateTable(file_name=file.GVL_ISRC_utf8,         tbl=tbl.GVL_ISRC,         schema=schema, format_name="CSV_quoted_with_quotes_as_escape", encoding_for_fread="UTF-8", clean_column_names=TRUE)
sfPopulateTable(file_name=file.GVL_ArtistTrack_utf8,  tbl=tbl.GVL_ArtistTrack,  schema=schema, format_name="CSV_quoted_with_quotes_as_escape", encoding_for_fread="UTF-8", clean_column_names=TRUE)
sfPopulateTable(file_name=file.GVL_Claims_ISRC_utf8,  tbl=tbl.GVL_Claims_ISRC,  schema=schema, format_name="CSV_quoted_with_quotes_as_escape", encoding_for_fread="UTF-8", clean_column_names=TRUE)


## OLD non-UTF8 files
sfPopulateTable(tbl=tbl.abramus,          schema=schema, file_name=file.abramus,          format_name="CSV_quoted_with_quotes_as_escape", encoding_for_fread="UTF-8")
sfPopulateTable(tbl=tbl.GVL_ISRC,         schema=schema, file_name=file.GVL_ISRC,         format_name="CSV_quoted_with_quotes_as_escape", encoding_for_fread="UTF-8")
sfPopulateTable(tbl=tbl.GVL_ArtistTrack,  schema=schema, file_name=file.GVL_ArtistTrack,  format_name="CSV_quoted_with_quotes_as_escape")