'''
Created on 19 lut 2020

@author: fpawlak
'''

import os
import sys
from Classes.LibrariesInstaller import LibrariesInstaller
from Classes.CodesysLibsScriptsException import CodesysLibsScriptsException
from Classes.Config import Config
from Classes.AviableLibrariesParser import AviableLibrariesParser
from Classes.ProjectCompiller import ProjectCompiller
from Classes.LibraryRestrictions import LibraryRestrictions
import traceback

class MissingProjectParameter(CodesysLibsScriptsException):
    pass

config = Config(os.path.dirname(__file__) + '\\config.cfg');
aviableLibraries = AviableLibrariesParser(config.remoteRepository.path)
restrictions = LibraryRestrictions(config.main.company, True)

args = dict(arg.split('=') for arg in sys.argv[1:])

if 'project_file' in args:
    print('Using project file from argv: {}'.format(args.get('project_file')))
    projectPath = args.get('project_file')
else:
    raise MissingProjectParameter(None)

try:
    print('[INSTALLING LIBRARIES]')
    installer = LibrariesInstaller(librarymanager)
    repository = installer.getRepository(config.localRepository)
    installer.installLibs(repository, config.remoteRepository.path, aviableLibraries)
    print('')
    
    print('[COMPILING AND INSTALLING]')
    compiler = ProjectCompiller(projects, projectPath)
    libraryInfo = compiler.compile(config, config.remoteRepository.path, False)
    
    aviableLibraries.addLibrary(libraryInfo)
    print('')
    
    print('[DONE]')
    
except CodesysLibsScriptsException as e:
    print(e.mainException)
    traceback.print_exc()
    sys.exit(-1)
    
