import os, sys

from librarymanager.LibraryManager import MochaLibraryManager

if __name__ == "__main__":

	mlm = MochaLibraryManager()
	path = None
	dbname = None
	dbuser = None
	dbpass = None
	tenantName = 'super'

	if len(sys.argv) == 2:
		path = sys.argv[1]
		print("installing mocha from path: " + path)

	elif len(sys.argv) == 5:
		path = sys.argv[4]
		dbname = sys.argv[1]
		dbuser = sys.argv[2]
		dbpass = sys.argv[3]
		print("installing mocha from path: " + path)
		print("using dbname = " + dbname + ", dbuser = " + dbuser)

	elif len(sys.argv) == 6:
		tenantName = sys.argv[1]
		path = sys.argv[5]
		dbname = sys.argv[2]
		dbuser = sys.argv[3]
		dbpass = sys.argv[4]
		print("installing mocha from path: " + path)
		print("using dbname = " + dbname + ", dbuser = " + dbuser)

	else:
		print("usage: mocha-install-library TENANTNAME [DATABASENAME USERNAME PASSWORD] PATH_TO_LIBRARY")
		exit(3)

	mlm.connect('localhost', dbname, dbuser, dbpass)
	mlm.select_tenant(tenantName)
	result = mlm.install_from_path(path)
	
	if result:
		mlm.commit()

	mlm.close()