#!/usr/bin/python

import sys, json, random, os

def translate(filename, target, tab):
	with open(filename, 'r') as f:
		content = f.read()
	for k, v in tab.items():
		content = content.replace('__' + k + '__', v)
	with open(target, 'w') as f:
		f.write(content)

cid = raw_input('uoj container id: ')
ip = raw_input('uoj ip: ')
name = raw_input('judger name: ')

os.system("docker cp " + cid + ":/home/local_main_judger/judge_client/.conf.json conf.json")

with open('conf.json', 'r') as f:
	conf = json.load(f)

conf['uoj_host'] = ip
conf['judger_name'] = name
conf['judger_password'] = ''.join(random.choice('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') for i in range(32))

with open('conf.json', 'w') as f:
	json.dump(conf, f, indent=4, separators=(',', ': '))
	print >>f

translate_table = {
	'svn_cert': '--username %s --password %s' % (conf['svn_username'], conf['svn_password']),
	'uoj_host': ip
}

translate('install', 'cur_install', translate_table)

print "please modify the database after getting the judger server ready:"
print "insert into judger_info (judger_name, password, ip) values ('%s', '%s', '__judger_ip_here__');" % (name, conf['judger_password'])