#!/usr/bin/env python3
""" amdgpu-ls  -  Displays details about installed AMD GPUs

    This utility displays most relevant parameters for installed and compatible AMD GPUs.
    The default behavior is to list relevant parameters by GPU.  OpenCL platform information
    is added when the *--clinfo* option is used.  The *--no_fan* can be used to ignore fan
    settings.  The *--pstate* option can be used to output the p-state table for each GPU
    instead of the list of basic parameters.  The *--ppm* option is used to output the table
    of available power/performance modes instead of basic parameters.

    Copyright (C) 2019  RueiKe

    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 3 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""
__author__ = 'RueiKe'
__copyright__ = 'Copyright (C) 2019 RueiKe'
__credits__ = ['Craig Echt - Testing, Debug, Verification, and Documentation']
__license__ = 'GNU General Public License'
__program_name__ = 'amdgpu-ls'
__version__ = 'v2.6.0'
__maintainer__ = 'RueiKe'
__status__ = 'Stable Release'

import argparse
import os
import sys
from GPUmodules import GPUmodule as GPU
from GPUmodules import env
from pathlib import Path


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--about', help='README',
                        action='store_true', default=False)
    parser.add_argument('--pstates', help='Output pstate tables instead of GPU details',
                        action='store_true', default=False)
    parser.add_argument('--ppm', help='Output power/performance mode tables instead of GPU details',
                        action='store_true', default=False)
    parser.add_argument('--clinfo', help='Include openCL with card details',
                        action='store_true', default=False)
    parser.add_argument('--no_fan', help='do not include fan setting options',
                        action='store_true', default=False)
    parser.add_argument('-d', '--debug', help='Debug output',
                        action='store_true', default=False)
    args = parser.parse_args()

    # About me
    if args.about:
        print(__doc__)
        print('Author: ', __author__)
        print('Copyright: ', __copyright__)
        print('Credits: ', __credits__)
        print('License: ', __license__)
        print('Version: ', __version__)
        print('Maintainer: ', __maintainer__)
        print('Status: ', __status__)
        sys.exit(0)

    env.gut_const.PATH = os.path.dirname(str(Path(__file__).resolve()))
    env.gut_const.DEBUG = args.debug
    if args.no_fan:
        env.gut_const.show_fans = False

    if env.gut_const.check_env() < 0:
        print('Error in environment. Exiting...')
        sys.exit(-1)

    # Check value of AMD Feature mask
    try:
        featuremask = env.gut_const.read_amdfeaturemask()
    except FileNotFoundError:
        print('Cannot read ppfeaturemask. Exiting...')
        sys.exit(-1)
    if featuremask == int(0xffff7fff) or featuremask == int(0xffffffff) or featuremask == int(0xfffd7fff):
        print('AMD Wattman features enabled: %s' % hex(featuremask))
    else:
        print('AMD Wattman features not enabled: %s, See README file.' % hex(featuremask))
        sys.exit(-1)

    env.gut_const.get_amd_driver_version()

    # Get list of AMD GPUs and get basic non-driver details
    gpu_list = GPU.GPU_LIST()
    gpu_list.get_gpu_list()
    gpu_list.read_allgpu_pci_info()

    # Check list of AMD GPUs
    num_amd_gpus = gpu_list.num_gpus()
    num_com_gpus = gpu_list.num_compatible_gpus()
    if num_amd_gpus == 0:
        print('No AMD GPUs detected, exiting...')
        sys.exit(-1)
    else:
        if num_com_gpus == 0:
            print('None are compatible, exiting...')
            sys.exit(-1)
        print(f'{num_amd_gpus} AMD GPUs detected, {num_com_gpus} may be compatible, checking...')

    # Read data static driver information for GPUs
    gpu_list.read_gpu_driver_info()
    gpu_list.read_gpu_sensor_static_data()

    # Read dynamic sensor and state data from GPUs
    gpu_list.read_gpu_sensor_data()
    gpu_list.read_gpu_state_data()

    # Check number of compatible GPUs again
    num_com_gpus = gpu_list.num_compatible_gpus()
    if num_com_gpus == 0:
        print('None are compatible, exiting...')
        sys.exit(-1)
    else:
        print(f'{num_com_gpus} are confirmed compatible.')
        print('')

    # Read report specific details
    if args.clinfo:
        if gpu_list.read_gpu_opencl_data() < 0:
            args.clinfo = False
    gpu_list.read_gpu_pstates()
    gpu_list.read_gpu_ppm_table()

    # Print out user requested details
    if args.pstates:
        gpu_list.print_pstates()
    if args.ppm:
        gpu_list.print_ppm_table()
    if not args.pstates and not args.ppm:
        gpu_list.print(args.clinfo)
    sys.exit(0)


if __name__ == '__main__':
    main()
