import sys

if len(sys.argv) != 3:
	print("Usage: mpProfile /path/to/input.csv /path/to/output.h")

inpath = sys.argv[1]
outpath = sys.argv[2]

# Print banner
print("----- mpParser -----")
print("By: FRC team 5024\n")

print("Checking files..")
# Check if the correct filetyoe was used
if not "csv_" in inpath:
	print("Incorrect filetype used. Please use files generated by vannaka's generator")
	exit(1)

# make sure the left file is used
if not "csv_left" in inpath:
	print("Please use the csv_left file")
	exit(1)

print("Loading template..")

template = """
// This file was generated using mpParser by Raider Robotics
// For more info, see: https://github.com/frc5024/mpParser

#ifndef _MP_{name}_HG_
#define _MP_{name}_HG_
//       Position (rotations)	Velocity (RPM)	Duration (ms)
const int kMotionProfileSz = {lines};

const double kMotionProfileLeft[][3] = {
{data-l}
};

const double kMotionProfileRight[][3] = {
{data-r}
};


#endif
"""

# print("Enter a name for this auto (Use underscores. No spaces)")
# name = input(">")

name = outpath.split("/")[len(outpath.split("/")) - 1].split(".")[0].upper()

print("Opening file 1..")
with open(inpath, "r") as f:
	in_csv_left = f.read()
	f.close()

data_left= ""

print("Parsing data..")
line_count = 0
for line in in_csv_left.split("\n"):
	if line == "":
		continue
	data_left += "{"+line+"},\n"
	line_count += 1

data_left = data_left[:-2] + "\n"

print("Loading file 2..")
try:
	with open(inpath.replace("csv_left", "csv_right"), "r") as f:
		in_csv_right = f.read()
		f.close()
except:
	print("Unable to open csv_right file. Makse sure it is in the same directory as the csv_left file")

data_right= ""

print("Parsing data..")
for line in in_csv_right.split("\n"):
	if line == "":
		continue
	data_right += "{"+line+"},\n"

data_right = data_right[:-2] + "\n"

template = template.replace("{lines}", str(line_count))
template = template.replace("{name}", str(name))
template = template.replace("{data-l}", str(data_left))
template = template.replace("{data-r}", str(data_right))

print("Writing file..")
with open(outpath, "w") as f:
	f.writelines(template)
	f.close

print("Done!")