#!/usr/bin/env python3
import os
import sys

import build
from pyfastogt import system_info
from pyfastogt import utils


def print_usage():
    print("Usage:\n"
          "[required] argv[1] cmake_root_path\n"
          "[optional] argv[2] platform\n"
          "[optional] argv[3] architecture\n"
          "[optional] argv[4] packages for example(\"DEB RPM\")\n")


def print_message(progress, message):
    print('{0:.1f}% {1}'.format(progress, message))
    sys.stdout.flush()


if __name__ == "__main__":
    argc = len(sys.argv)

    if argc > 1:
        cmake_root = sys.argv[1]
    else:
        print_usage()
        sys.exit(1)

    if argc > 2:
        platform_str = sys.argv[2]
    else:
        platform_str = system_info.get_os()

    if argc > 3:
        arch_bit_str = sys.argv[3]
    else:
        arch_bit_str = system_info.get_arch_name()

    if argc > 4:
        packages = sys.argv[4].split()
    else:
        packages = []

    request = build.BuildRequest(platform_str, arch_bit_str)
    saver = build.ProgressSaver(print_message)

    # fastonosql build
    fastonosql_branding_file = os.path.abspath('fastonosql.txt')
    fastonosql_branding_options = utils.read_file_line_by_line_to_list(fastonosql_branding_file)
    # request.build(cmake_root, fastonosql_branding_options, 'build_fastonosql_' + platform_str, None,
    #              packages, saver)

    # pro version
    fastonosql_branding_options.append('-DPRO_VERSION=ON')
    fastonosql_branding_options.append('-DBUILD_STRATEGY:STRING=community')
    request.build(cmake_root, fastonosql_branding_options, 'build_fastonosql_pro_' + platform_str, None,
                  packages, saver)

    # fastoredis build
    fastoredis_branding_file = os.path.abspath('fastoredis.txt')
    fastoredis_branding_options = utils.read_file_line_by_line_to_list(fastoredis_branding_file)
    # request.build(cmake_root, fastoredis_branding_options, 'build_fastoredis_' + platform_str, None,
    #               packages, saver)

    # pro version
    fastoredis_branding_options.append('-DPRO_VERSION=ON')
    fastoredis_branding_options.append('-DBUILD_STRATEGY:STRING=community')
    request.build(cmake_root, fastoredis_branding_options, 'build_fastoredis_pro_' + platform_str, None,
                  packages, saver)
