import ops
import iopc

TARBALL_FILE="drbd-utils-8.9.10.tar.gz"
TARBALL_DIR="drbd-utils-8.9.10"
INSTALL_DIR="drbd-utils-bin"
pkg_path = ""
output_dir = ""
tarball_pkg = ""
tarball_dir = ""
install_dir = ""
install_tmp_dir = ""
cc_host = ""

def set_global(args):
    global pkg_path
    global output_dir
    global tarball_pkg
    global install_dir
    global install_tmp_dir
    global tarball_dir
    global cc_host
    pkg_path = args["pkg_path"]
    output_dir = args["output_path"]
    tarball_pkg = ops.path_join(pkg_path, TARBALL_FILE)
    install_dir = ops.path_join(output_dir, INSTALL_DIR)
    install_tmp_dir = ops.path_join(output_dir, INSTALL_DIR + "-tmp")
    tarball_dir = ops.path_join(output_dir, TARBALL_DIR)
    cc_host_str = ops.getEnv("CROSS_COMPILE")
    cc_host = cc_host_str[:len(cc_host_str) - 1]

def MAIN_ENV(args):
    set_global(args)

    ops.exportEnv(ops.setEnv("CC", ops.getEnv("CROSS_COMPILE") + "gcc"))
    ops.exportEnv(ops.setEnv("CXX", ops.getEnv("CROSS_COMPILE") + "g++"))
    ops.exportEnv(ops.setEnv("CROSS", ops.getEnv("CROSS_COMPILE")))
    ops.exportEnv(ops.setEnv("DESTDIR", install_tmp_dir))

    return False

def MAIN_EXTRACT(args):
    set_global(args)

    ops.unTarGz(tarball_pkg, output_dir)
    #ops.copyto(ops.path_join(pkg_path, "finit.conf"), output_dir)

    return True

def MAIN_PATCH(args, patch_group_name):
    set_global(args)
    for patch in iopc.get_patch_list(pkg_path, patch_group_name):
        if iopc.apply_patch(tarball_dir, patch):
            continue
        else:
            sys.exit(1)

    return True

def MAIN_CONFIGURE(args):
    set_global(args)

    extra_conf = []
    extra_conf.append("--with-distro=generic")
    extra_conf.append("--without-manual")
    extra_conf.append("--without-udev")
    extra_conf.append("--without-83support")
    extra_conf.append("--with-initscripttype=sysv")
    #extra_conf.append("--prefix=" + install_dir)
    extra_conf.append("--host=" + cc_host)
    iopc.configure(tarball_dir, extra_conf)

    return True

def MAIN_BUILD(args):
    set_global(args)

    ops.mkdir(install_dir)
    ops.mkdir(install_tmp_dir)
    iopc.make(tarball_dir)
    iopc.make_install(tarball_dir)
    return False

def MAIN_INSTALL(args):
    set_global(args)

    ops.mkdir(install_dir)
    ops.copyto(ops.path_join(install_tmp_dir, "lib"), install_dir)

    ops.mkdir(ops.path_join(install_dir, "sbin"))
    ops.copyto(ops.path_join(install_tmp_dir, "usr/local/sbin/drbdadm"), ops.path_join(install_dir, "/sbin"))
    ops.copyto(ops.path_join(install_tmp_dir, "usr/local/sbin/drbdsetup"), ops.path_join(install_dir, "/sbin"))
    ops.copyto(ops.path_join(install_tmp_dir, "usr/local/sbin/drbdmeta"), ops.path_join(install_dir, "/sbin"))

    ops.mkdir(ops.path_join(install_dir, "usr/"))
    ops.rm_file(ops.path_join(install_dir, "var"))
    ops.ln(install_dir, "/var", ops.path_join(install_dir, "var"))
    iopc.installBin(args["pkg_name"], ops.path_join(install_dir, "lib/."), "lib")
    iopc.installBin(args["pkg_name"], ops.path_join(install_dir, "sbin/."), "sbin")
    iopc.installBin(args["pkg_name"], ops.path_join(install_dir, "var"), "usr/local")
    #iopc.installBin(args["pkg_name"], ops.path_join(output_dir, "finit.conf"), "etc")

    return False

def MAIN_SDKENV(args):
    set_global(args)
    return False

def MAIN_CLEAN_BUILD(args):
    set_global(args)

    return False

def MAIN(args):
    set_global(args)

