#!/usr/bin/env python
# vim: ft=python

import os

fixes = []
with open(os.environ["XDG_CONFIG_HOME"] + "/xournal_conf/ugly_fix", "r") as f:
    fixes = f.read().strip().split("\n")

with open(os.environ["XDG_CONFIG_HOME"] + "/xournalpp/settings.xml", "r") as f:
    text = f.read().strip().split("\n")

for i in range(len(text)):
    if "selectionBorderColor" in text[i]:
        text[i] = '<property name="selectionBorderColor" value="' + str(int('FF' + fixes[1], 16)) + '"/>'
    if "backgroundColor" in text[i] and "pageTemplate" not in text[i]:
        text[i] = '<property name="backgroundColor" value="' + str(int('FF' + fixes[2], 16)) + '"/>'
    if "selectionMarkerColor" in text[i]:
        text[i] = '<property name="selectionMarkerColor" value="' + str(int('FF' + fixes[3], 16)) + '"/>'
    if "pageTemplate" in text[i]:
        text[i] = '<property name="pageTemplate" value="xoj/template&#10;copyLastPageSize=false&#10;copyLastPageSettings=false&#10;size=611.999983x791.999983&#10;backgroundType=graph&#10;backgroundColor=#ff' + fixes[0] + '&#10;"/>'
    if  "defaultViewModeAttributes" in text[i]:
        text[i] = '<property name="defaultViewModeAttributes" value="showMenubar,showToolbar"/>'


text = "\n".join(text)

with open(os.environ["XDG_CONFIG_HOME"] + "/xournalpp/settings.xml", "w") as f:
    f.write(text)
