#!/usr/bin/python

# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# See the COPYING file for license information.
#
# Copyright (c) 2012 peo3 <peo314159265@gmail.com>

import sys
import os
import optparse

from cgutils import cgroup
from cgutils.version import VERSION


DEFAULT_SUBSYSTEM = 'cpu'

parser = optparse.OptionParser(version='check_supported_files '+VERSION)
parser.add_option('-o', action='store', type='string',
                  dest='target_subsystem', default=DEFAULT_SUBSYSTEM,
                  help='Specify a subsystem [cpu]')

options, args = parser.parse_args()

root_cgroup = cgroup.scan_cgroups(options.target_subsystem)

def check_files(_cgroup):
    files = os.listdir(_cgroup.fullpath)
    files = [ file.replace(options.target_subsystem + '.', '') \
              for file in files if options.target_subsystem + '.' in file ]

    supported_files = []
    supported_files.extend(_cgroup.subsystem.STATS.keys())
    supported_files.extend(_cgroup.subsystem.CONFIGS.keys())
    supported_files.extend(_cgroup.subsystem.CONTROLS.keys())
    #print(files)
    #print(supported_files)

    return list(set(files) - set(supported_files))

def check_files_recursive(_cgroup):
    _unsupported_files = check_files(_cgroup)
    for child in _cgroup.childs:
        _unsupported_files.extend(check_files_recursive(child))
    return _unsupported_files

unsupported_files = check_files_recursive(root_cgroup)
unsupported_files = sorted(set(unsupported_files))

if unsupported_files:
    sys.stderr.write("Unsupported files: %s\n" % unsupported_files)
    sys.exit(1)
else:
    sys.exit(0)
