import gspread
import sys
from oauth2client.service_account import ServiceAccountCredentials
import datetime

#1 = tid
#2 = appname1
#3 = lut
#4 = reg
#5 = ram
#6 = uram
#7 = dsp
#8 = lut as logic
#9 = lut as mem
#10 = synth time
#11 = backend

tid = sys.argv[1]

json_key = '/home/mattfel/regression/zynq/key.json'
scope = [
    'https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive'
]
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key, scope)

gc = gspread.authorize(credentials)

if (sys.argv[11] == "Zynq"):
	sh = gc.open("Zynq Regression") # Open by name
	word="Slice"
elif (sys.argv[11] == "F1"):
	sh = gc.open("AWS Regression") # Open by name
	word="CLB"

# Get column
worksheet = sh.get_worksheet(0) # Select worksheet by index
lol = worksheet.get_all_values()
if (sys.argv[2] in lol[0]):
	col=lol[0].index(sys.argv[2])+1
else:
	col=len(lol[0])+1
	for x in range(0,5):
		worksheet = sh.get_worksheet(x)
		worksheet.update_cell(1,col,sys.argv[2])		


# Page 0 - Timestamps
stamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

worksheet = sh.worksheet('Timestamps') # Select worksheet by index
worksheet.update_cell(tid,col, stamp)

# Page 1 - Slice LUT
worksheet = sh.worksheet(word + ' LUTs') # Select worksheet by index
worksheet.update_cell(tid,col,sys.argv[3])

# Page 2 - Slice Reg
worksheet = sh.worksheet(word + ' Regs') # Select worksheet by index
worksheet.update_cell(tid,col,sys.argv[4])

# Page 3 - Mem
worksheet = sh.worksheet('BRAMs') # Select worksheet by index
worksheet.update_cell(tid,col,sys.argv[5])

if (sys.argv[11] == "F1"):
	# Page 4 - URAM
	worksheet = sh.worksheet('URAMs') # Select worksheet by index
	worksheet.update_cell(tid,col,sys.argv[6])

# Page 5 - DSP
worksheet = sh.worksheet('DSPs') # Select worksheet by index
worksheet.update_cell(tid,col,sys.argv[7])

# Page 6 - LUT as Logic
worksheet = sh.worksheet('LUT as Logic') # Select worksheet by index
worksheet.update_cell(tid,col,sys.argv[8])

# Page 7 - LUT as Memory
worksheet = sh.worksheet('LUT as Memory') # Select worksheet by index
worksheet.update_cell(tid,col,sys.argv[9])

# Page 8 - Synth time
worksheet = sh.worksheet('Synth Time') # Select worksheet by index
worksheet.update_cell(tid,col,sys.argv[10])


@@ -900,16 +900,50 @@
 l%5B0%5D)+1%0A
+%09numsheets = len(sh.worksheets())%0A
 %09for x i
@@ -952,17 +952,25 @@
 range(0,
-5
+numsheets
 ):%0A%09%09wor

fix appname thing