'''
Created on 21 lut 2020

@author: fpawlak
'''

from __future__ import print_function
from Classes.Config import Config
from string import rsplit
import os
import sys

config = Config(os.path.dirname(__file__) + '\\config.cfg');

print('Initializing library')
print('Setting project info', end='')
sys.stdout.flush()

project = projects.primary
projectInfo = project.get_project_info()

filename = os.path.basename(project.path)
path = os.path.dirname(project.path)

projectInfo.title = rsplit(filename, '.', 1)[0]
projectInfo.values['Placeholder'] = rsplit(filename, '.', 1)[0]
projectInfo.company = config.main.company
projectInfo.version = config.main.libraryStartingVersion

project.save()

print(' -> OK')


print('Creating .gitignore file', end='')
sys.stdout.flush()

with open('{}\\.gitignore'.format(path), 'w') as fp:
    fp.write('*.opt\n')
    fp.write('*.~u\n')

print(' -> OK')


print('Creating .gitlab-ci.yml file', end='')
sys.stdout.flush()

with open('{}\\.gitlab-ci.yml'.format(path), 'w') as fp:
    fp.write('variables:\n')
    fp.write('  PROJECT_FILE: {}\n'.format(filename))
    fp.write('include:\n')
    fp.write('  - project: \'IQVitroClima/Codesys3/CodesysLibsScript\'\n')
    fp.write('    file: \'runner/DeployLibrary.yml\'\n')

print(' -> OK')


print('Library initialization done')