library (googlesheets4)
library(dplyr)
#gs_auth()
# 
#template_f = gs_url ("https://docs.google.com/spreadsheets/d/1rrl2QcBIQNIQ-D5ugH7FuaiVckItAQxRVlW-Jgb9sdo/edit?ts=5ee10875#gid=1035048464")

# 
# data= googlesheets::gs_read_csv(PIlist, skip =2)

template_data <- read_sheet("https://docs.google.com/spreadsheets/d/1rrl2QcBIQNIQ-D5ugH7FuaiVckItAQxRVlW-Jgb9sdo/edit?ts=5ee10875#gid=1035048464")





createtemplate <- function(dirname ="template1", template_data) {
  dir.create(dirname)
  setwd(dirname)
  template_dataf = template_data [(template_data$create != "NULL"),] %>% filter (type == "folder")
  for (i in c(nrow(template_dataf):1)){
    if (nchar(template_dataf$create[i])<2) {
      dir.create(template_dataf$alternative_name[i])
      file.create(paste0(template_dataf$alternative_name[i], "/gitdir"))
      #file.rename(template_dataf$alternative_name[i], paste0 ("0",template_dataf$create[i],"_",template_dataf$alternative_name[i]))
      } else {
      dir.create(paste0(template_dataf$create[i], "/",template_dataf$alternative_name[i]), recursive = TRUE)
      file.create(paste0(template_dataf$create[i], "/",template_dataf$alternative_name[i], "/gitdir"))
      }
  }
  template_datafile = template_data [(template_data$create != "NULL"),] %>% filter (type == "file") 
    for (i in c(nrow(template_datafile):1)){
      if (template_datafile$create[i] == "parent") {
        file.create(template_datafile$alternative_name[i])
      } else {
        file.create(paste0(template_datafile$create[i], "/",template_datafile$alternative_name[i]))
      }
    #rename folder with numbers  
    for (i in c(nrow(template_dataf):1)){
        if (nchar(template_dataf$create[i])<2) {
          file.rename(template_dataf$alternative_name[i], paste0 ("0",template_dataf$create[i],"_",template_dataf$alternative_name[i]))
        } 
      }   
    
  }  
  setwd("../")
}

setwd("gin-foldertemplate")

# template_data$create = as.character(template_data$upfolder_2)
# createtemplate(dirname ="template1",template_data)
# 
# template_data$create = as.character(template_data$unitial_upfolder)
# createtemplate(dirname ="template_ori",template_data)
# template_data$create = as.character(template_data$upfolder_3)
# createtemplate(dirname ="template_3",template_data)
# 
# template_data$create = as.character(template_data$upfolder_2min)
# createtemplate(dirname ="upfolder_2min",template_data)

template_data$create = as.character(template_data$hierachical)
createtemplate(dirname ="hierachical",template_data)

template_data$create = as.character(template_data$flat)
createtemplate(dirname ="flat",template_data)
