#!/usr/bin/python3 -s
#
# auto-cpufreq - Automatic CPU speed & power optimizer for Linux
#
# Blog post: http://foolcontrol.org/?p=3124

# core import
import sys
import time
import click
from subprocess import call, run

sys.path.append('../')
from auto_cpufreq.core import *

# cli
@click.command()
@click.option("--daemon", is_flag=True, hidden=True)
@click.option("--debug", is_flag=True, help="Show debug info (include when submitting bugs)")
@click.option("--install/--remove", default=True, help="Install/remove daemon for (permanent) automatic CPU optimizations")
@click.option("--live", is_flag=True, help="Monitor and make (temp.) suggested CPU optimizations")
@click.option("--log", is_flag=True, help="Deprecated flag replaced by --stats")
@click.option("--monitor", is_flag=True, help="Monitor and see suggestions for CPU optimizations")
@click.option("--stats", is_flag=True, help="View live stats of CPU optimizations made by daemon")
@click.option("--version", is_flag=True, help="Show currently installed version")
def main(daemon, debug, install, live, log, monitor, stats, version):
    if len(sys.argv) == 1:
        print("\n" + "-" * 32 + " auto-cpufreq " + "-" * 33 + "\n")
        print("Automatic CPU speed & power optimizer for Linux")
        print("\nExample usage:\nauto-cpufreq --monitor")
        print("\n-----\n")

        run(["auto-cpufreq", "--help"])
        footer()
    else:
        # Important: order does matter
        if daemon:
            file_stats()
            if os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled":
                while True:
                    root_check()
                    footer()
                    gov_check()
                    cpufreqctl()
                    distro_info()
                    sysinfo()
                    set_autofreq()
                    countdown(5)
            elif os.getenv("PKG_MARKER") != "SNAP":
                while True:
                    root_check()
                    footer()
                    gov_check()
                    cpufreqctl()
                    distro_info()
                    sysinfo()
                    set_autofreq()
                    countdown(5)
            else:
                daemon_not_found()
        elif monitor:
            while True:
                print("\nNote: You can quit monitor mode by pressing \"ctrl+c\"")
                time.sleep(1)
                root_check()
                footer()
                running_daemon()
                gov_check()
                cpufreqctl()
                distro_info()
                sysinfo()
                mon_autofreq()
                countdown(5)
        elif live:
            print("\nNote: You can quit live mode by pressing \"ctrl+c\"")
            time.sleep(1)
            while True:
                root_check()
                footer()
                running_daemon()
                gov_check()
                cpufreqctl()
                distro_info()
                sysinfo()
                set_autofreq()
                countdown(5)
        elif stats:
            print("\nNote: You can quit stats mode by pressing \"ctrl+c\"")
            time.sleep(1)
            read_stats()
        elif log:
            deprecated_log_msg()
        elif debug:
            root_check()
            footer()
            distro_info()
            sysinfo()
            print("")
            app_version()
            print("")
            python_info()
            print("")
            device_info()
            if charging():
                print("Battery is: charging")
            else:
                print("Battery is: discharging")
            print("")
            app_res_use()
            display_load()
            get_current_gov()
            get_turbo()
            footer()
        elif version:
            footer()
            distro_info()
            app_version()
            footer()
        elif install:
            if os.getenv('PKG_MARKER') == "SNAP":
                root_check()
                running_daemon()
                gov_check()
                run("snapctl set daemon=enabled", shell=True)
                run("snapctl start --enable auto-cpufreq", shell=True)
                deploy_complete_msg()
            else:
                root_check()
                running_daemon()
                gov_check()
                deploy_daemon()
                deploy_complete_msg()
        elif remove:
            if os.getenv('PKG_MARKER') == "SNAP":
                root_check()
                run("snapctl set daemon=disabled", shell=True)
                run("snapctl stop --disable auto-cpufreq", shell=True)
                if auto_cpufreq_stats_path.exists():
                    if auto_cpufreq_stats_file is not None:
                        auto_cpufreq_stats_file.close()

                    auto_cpufreq_stats_path.unlink()
                remove_complete_msg()
            else:
                root_check()
                remove()
                remove_complete_msg()


if __name__ == '__main__':
    main()
