#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# color_depth_info
#
# This file is part of Checkbox.
#
# Copyright 2012 Canonical Ltd.
#
# Authors: Alberto Milone <alberto.milone@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.

import os
import re

from glob import glob


def get_color_depth(log_dir='/var/log/'):
    '''Return color depth and pixmap format'''
    depth = 8
    pixmap_format = 8
    # find the most recent X.org log
    log = None
    max_time = 0
    for f in glob(os.path.join(log_dir, 'Xorg.*.log')):
        mtime = os.stat(f).st_mtime
        if mtime > max_time:
            max_time = mtime
            log = f

    with open(log, errors='ignore') as f:
        for match in re.finditer(
            '\(==\) Depth (.+) pixmap format is (.+) bpp',
            f.read()):
            depth = match.group(1)
            pixmap_format = match.group(2)

    return (depth, pixmap_format)


def main():
    print('Color Depth: %s\nPixmap Format: %s bpp' % get_color_depth())

if __name__ == '__main__':
    main()
