#!/usr/bin/python3

import os
from six.moves.configparser import RawConfigParser

from univention.appcenter.log import log_to_stream
from univention.appcenter.actions import get_action
from univention.appcenter.app_cache import Apps
from univention.appcenter.ucr import ucr_save

MAIN_APP_VERSION = os.getenv('MAIN_APP_VERSION', None)

log_to_stream()


update = get_action('update')
update.call()

app = Apps().find('relution', MAIN_APP_VERSION)

print('Changing default settings for', app)
parser = RawConfigParser()
with open(app.get_cache_file('settings'), 'r') as f:
	parser.read_file(f)
	parser.set('SYSTEM_ADMIN_PASSWORD', 'InitialValue', 'secret-password123')
	parser.set('ORGA_ADMIN_USERNAME', 'InitialValue', 'orga_admin')
	parser.set('ORGA_ADMIN_PASSWORD', 'InitialValue', 'secret-password123')
	parser.set('ORGA_ADMIN_GIVENNAME', 'InitialValue', 'John')
	parser.set('ORGA_ADMIN_SURNAME', 'InitialValue', 'Doe')
	parser.set('', 'InitialValue', '')

with open(app.get_cache_file('settings'), 'w') as f:
	parser.write(f)

with open(app.get_cache_file('env'), 'a') as f:
	f.write('\nRELUTION_SERVER_JWTSECUREFLAGENABLED=false\n')

Apps().clear_cache()
ucr_save({'appcenter/umc/update/always': 'false'})