if (sys.argv.len() < 1) {
	sys.stderr.println('module name needs to be specified')
	sys.exit(1)
}
name = path.basename(sys.argv[0])
fileBuild = 'build.az'
fileSrc = 'Module_' + name + '.cpp'
if (path.exists(fileBuild) || path.exists(fileSrc)) {
	println('file already exists')
	sys.exit(1)
}

open(fileBuild, 'w').print(e'''\
#!/usr/bin/env ascript
import(module_builder) {build}
build('${name}.azd', ['Module_${name}.cpp'])
''')

open(fileSrc, 'w').print(e'''\
//-----------------------------------------------------------------------------
// AScript ${name} module
//-----------------------------------------------------------------------------
#include <ascript.h>

AScript_BeginModule(${name})

//-----------------------------------------------------------------------------
// AScript module functions: ${name}
//-----------------------------------------------------------------------------
// ${name}.test(num1:number, num2:number)
AScript_DeclareFunction(test)
{
	SetMode(RSLTMODE_Normal, FLAG_None);
	DeclareArg(env, "num1", VTYPE_Number);
	DeclareArg(env, "num2", VTYPE_Number);
	SetHelp("adds two numbers and returns the result.");
}

AScript_ImplementFunction(test)
{
	return Value(args.GetNumber(0) + args.GetNumber(1));
}

// Module entry
AScript_ModuleEntry()
{
	// function assignment
	AScript_AssignFunction(test);
}

AScript_ModuleTerminate()
{
}

AScript_EndModule(${name}, ${name})

AScript_RegisterModule(${name})
''')

println(fileBuild, ' and ', fileSrc, ' were created')
/*
print(e'''\
To create a module binary file, execute the following command:
  ascript build.az --here
This would create ${name}.azd in the current directory.
''')
*/
sys.exit(0)
