#!/usr/bin/env python3

import sys
import argparse


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('filename',
                        action='store',
                        help='The output file from sleep tests to parse')
    parser.add_argument('--s',
                        dest='sleep_threshold',
                        action='store',
                        type=float,
                        default=10.00,
                        help=('The max time a system should have taken to '
                              'enter a sleep state. (Default: %(default)s)'
                              ))
    parser.add_argument('--r',
                        action='store',
                        dest='resume_threshold',
                        type=float,
                        default=5.00,
                        help=('The max time a system should have taken to '
                              'resume from a sleep state. (Default: '
                              '%(default)s)'))
    args = parser.parse_args()

    try:
        with open(args.filename) as file:
            lines = file.readlines()
    except IOError as e:
        print(e)
        return False

    sleep_time = None
    resume_time = None
    # find our times
    for line in lines:
        if "Average time to sleep" in line:
            sleep_time = float(line.split(':')[1].strip())
        elif "Average time to resume" in line:
            resume_time = float(line.split(':')[1].strip())

    if sleep_time is None or resume_time is None:
        print("ERROR: One or more times was not reported correctly")
        return False

    print("Average time to enter sleep state: %s seconds" % sleep_time)
    print("Average time to resume from sleep state: %s seconds" % resume_time)

    failed = False
    if sleep_time > args.sleep_threshold:
        print("System failed to suspend in less than %s seconds" %
              args.sleep_threshold)
        failed = True
    if resume_time > args.resume_threshold:
        print("System failed to resume in less than %s seconds" %
              args.resume_threshold)
        failed = True
    if sleep_time <= 0.00 or resume_time <= 0.00:
        print("ERROR: One or more times was not reported correctly")
        failed = True

    return failed

if __name__ == "__main__":
    sys.exit(main())
