#!/usr/bin/python3

from reman import ReMan
import argparse
import sys
import importlib.util

reman = ReMan.ReMan()


parser = argparse.ArgumentParser()
parser.add_argument("action", help="The action to perform: getUserChannels getChannel createConanData createVariants createPlatforms")
parser.add_argument("-r", "--releaseName", type=str, help="The release name")
parser.add_argument("-u", "--userName", type=str, help="The user name")
parser.add_argument("-c", "--channelName", type=str, help="The channel name")
parser.add_argument("-t", "--targetName", type=str, help="target name")

def checkReleaseName():
	if (args.releaseName is None):
		print(f"Release name not specified")
		sys.exit(1)

def checkUserName():
	if (args.userName is None):
		print(f"User name not specified")
		sys.exit(1)

def checkChannel():
	if (args.version is None):
		print(f"Channel not specified")
		sys.exit(1)

def checkTargetName():
	if (args.targetName is None):
		print(f"Target name not specified")
		sys.exit(1)

def getUserChannels(releaseName):
	print(reman.getUserChannels(releaseName))

def getChannel(releaseName, userName):
	print(reman.getChannel(releaseName, userName))

def createConanData(releaseName):
	data = reman.getUserChannels(releaseName)
	print("create conandata.yml")
	f = open("conandata.yml", "w")
	f.write("repositories:\n")
	for row in data:
		f.write(f"  {row[0]}: \"{row[1]}\"\n")
	f.close()

def createVariants(target):
	print("create variants.yml")
	spec = importlib.util.spec_from_file_location("conanfile", "./conanfile.py")
	if  spec:
		module = importlib.util.module_from_spec(spec)
		spec.loader.exec_module(module)
		f = open("variants.yml", "w")
		f.write("variants:\n")
		if  hasattr( module, "build_variants" ):
			# some specific build are defined
			for key in module.build_variants:
				variant = module.build_variants[key]
				# if target is present
				if target in variant['targets']:
					for option in variant['options']:
						f.write(f"  - \"{option}\"\n")
		else:
			# Add default build  ( no options )
 			f.write(f"  - \"\"\n")
		f.close()

def createBoards(target):
	print("create boards.yml")
	spec = importlib.util.spec_from_file_location("conanfile", "./conanfile.py")
	if  spec:
		module = importlib.util.module_from_spec(spec)
		spec.loader.exec_module(module)
		f = open("boards.yml", "w")
		f.write("boards:\n")
		if  hasattr( module, "build_variants" ):
			# some specific build are defined
			for key in module.build_boards:
				f.write(f"  - \"{key}\"\n")
	        # Add default build  ( no options )
		f.write(f"  - \"\"\n")
		f.close()


args = parser.parse_args()

if (args.action == "getUserChannels"):
	checkReleaseName()
	getUserChannels(args.releaseName)
elif (args.action == "getChannel"):
	checkReleaseName()
	checkUserName()
	getChannel(args.releaseName, args.userName)
elif (args.action == "createConanData"):
	checkReleaseName()
	createConanData(args.releaseName)
elif (args.action == "createVariants"):
        checkTargetName()
        createVariants(args.targetName)
elif (args.action == "createBoards"):
        checkTargetName()
        createBoardss(args.targetName)

