#!/usr/bin/env python
# -*- coding: utf-8 -*-

# xfce4-terminal-font.py -- programmatically change the font size of an
# xfce4-terminal.
#
# Copyright © 2013 Noah K. Tilton <noahktilton@gmail.com>
# Copyright © 2017 Edoardo Negri <edne@gmx.com>
#
# License: GPLv2 or later.

from __future__ import print_function
from sys import argv, exit
import os
try:
    from configparser import RawConfigParser  # python3
except ImportError:
    from ConfigParser import RawConfigParser  # python2


if __name__ == '__main__':
    try:
        config_root = os.environ['XDG_CONFIG_HOME']
    except KeyError:
        config_root = os.path.join(os.environ['HOME'], '.config')

    config_file = os.path.join(config_root, 'xfce4', 'terminal', 'terminalrc')

    config = RawConfigParser()
    config.optionxform = str  # make keys case-sensitive
    config.read(config_file)

    font = " ".join(config.get('Configuration', 'FontName').split()[:-1])
    size = config.get('Configuration', 'FontName').split()[-1]

    if len(argv) == 1:
        print(size)
        exit(0)

    elif argv[1] in ['+', '-']:
        delta = int('{}1'.format(argv[1]))
        new_size = float(size) + delta

    else:
        try:
            new_size = float(argv[1])
        except ValueError:
            print('Invalid size', argv[1])
            exit(1)

    config.set('Configuration', 'FontName', '{} {}'.format(font, new_size))

    with open(config_file, 'w') as f:
        config.write(f)
