#!python

import platform
import subprocess
import sys
from distutils.version import LooseVersion
from shutil import which

PYTHON_VERSION = LooseVersion(platform.python_version())
if PYTHON_VERSION < "3.6":
    raise RuntimeError("PY Version required >= 3.6")

DEPS_TERRAFORM = {
    "linux": LooseVersion("1.1.0"),
    "osx": LooseVersion("1.1"),
}  # TODO update to 1.0.0
DEPS_KUBECTL = LooseVersion("1.21.3")
DEPS_EKSCTL = LooseVersion("0.60.0")
DEPS_KUSTOMIZE = LooseVersion("4.0.5")


def deps_exist(name: str):
    return which(name) is not None


def linux():
    def get_distribution():
        import csv

        dist = {}
        with open("/etc/os-release") as f:
            reader = csv.reader(f, delimiter="=")
            for row in reader:
                if row:
                    dist[row[0]] = row[1]

        print("Distribution: %s" % dist.get("NAME", "").lower())
        print("Release: %s" % dist.get("VERSION_ID", "").lower())
        print("Version: %s" % dist.get("VERSION", "").lower())
        return dist["NAME"].lower()

    def ubuntu():
        scripts = {}

        scripts[
            "aws-iam-authenticator"
        ] = """
curl -O https://amazon-eks.s3.us-west-2.amazonaws.com/1.19.6/2021-01-05/bin/linux/amd64/aws-iam-authenticator && \
chmod +x ./aws-iam-authenticator && \
mv ./aws-iam-authenticator /usr/local/bin
        """

        scripts["jq"] = "apt-get install -y jq"

        scripts[
            "terraform"
        ] = f"""
echo "terraform"
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
apt install -y terraform={DEPS_TERRAFORM["linux"]}
"""
        scripts[
            "kubectl"
        ] = f"""
curl -LO https://storage.googleapis.com/kubernetes-release/release/v{DEPS_KUBECTL}/bin/linux/amd64/kubectl
chmod +x ./kubectl
mv ./kubectl /usr/local/bin/kubectl
"""
        scripts[
            "eksctl"
        ] = f"""
curl --location https://github.com/weaveworks/eksctl/releases/download/{DEPS_EKSCTL}/eksctl_Linux_amd64.tar.gz | tar xz -C /tmp
mv /tmp/eksctl /usr/local/bin
"""
        scripts[
            "kustomize"
        ] = """
curl -s https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh  | bash
mv ./kustomize /usr/local/bin
"""
        return scripts

    def alpine():
        scripts = {}

        scripts[
            "aws-iam-authenticator"
        ] = """
curl -O https://amazon-eks.s3.us-west-2.amazonaws.com/1.19.6/2021-01-05/bin/linux/amd64/aws-iam-authenticator && \
chmod +x ./aws-iam-authenticator && \
mv ./aws-iam-authenticator /usr/local/bin
        """

        scripts["jq"] = "apk add jq"

        scripts[
            "terraform"
        ] = f"""
curl -LO https://releases.hashicorp.com/terraform/{DEPS_TERRAFORM["linux"]}/terraform_{DEPS_TERRAFORM["linux"]}_linux_amd64.zip
unzip terraform_{DEPS_TERRAFORM["linux"]}_linux_amd64.zip && rm terraform_{DEPS_TERRAFORM["linux"]}_linux_amd64.zip
mv terraform /usr/local/bin
"""
        scripts[
            "kubectl"
        ] = f"""
curl -LO https://storage.googleapis.com/kubernetes-release/release/v{DEPS_KUBECTL}/bin/linux/amd64/kubectl
chmod +x ./kubectl
mv ./kubectl /usr/local/bin/kubectl
"""
        scripts[
            "eksctl"
        ] = f"""
curl --location https://github.com/weaveworks/eksctl/releases/download/{DEPS_EKSCTL}/eksctl_Linux_amd64.tar.gz | tar xz -C /tmp
mv /tmp/eksctl /usr/local/bin
"""
        scripts[
            "kustomize"
        ] = """
curl -s https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh  | bash
mv ./kustomize /usr/local/bin
"""
        return scripts

    if platform.system() != "Linux":
        return

    dist = get_distribution()

    if "ubuntu" in dist.lower():
        exec(scripts=ubuntu())

    if "alpine" in dist.lower():
        exec(scripts=alpine())


def darwin():
    if platform.system() != "Darwin":
        return

    scripts = {}

    scripts["aws-iam-authenticator"] = "brew install aws-iam-authenticator"

    scripts["jq"] = "brew install jq"

    scripts[
        "terraform"
    ] = f"""
brew install terraform@{DEPS_TERRAFORM["osx"]}
brew link terraform@{DEPS_TERRAFORM["osx"]}
"""
    scripts[
        "kubectl"
    ] = f"""
brew install kubectl@{DEPS_KUBECTL}
"""
    scripts[
        "eksctl"
    ] = f"""
brew tap weaveworks/tap
brew install weaveworks/tap/eksctl@{DEPS_EKSCTL}
"""
    scripts[
        "kustomize"
    ] = """
brew install kustomize
"""

    exec(scripts=scripts)


def windows():
    if platform.system() != "Windows":
        return
    raise RuntimeError("Install Unix o Linux os")


def exec(scripts: dict):
    if not scripts:
        raise RuntimeError("Not found scripts for yor os distribution")

    if deps_exist("jq") is False:
        subprocess.check_call(scripts["jq"], shell=True)
    else:
        print("\nJq installed.")

    if deps_exist("terraform") is False:
        subprocess.check_call(scripts["terraform"], shell=True)
    else:
        print(
            f"\nTerraform installed.\nSuggestion version {DEPS_TERRAFORM}"
        )  # TODO check version and print warning if is not compatible

    if deps_exist("kubectl") is False:
        subprocess.check_call(scripts["kubectl"], shell=True)
    else:
        print(
            f"\nKubectl installed.\nSuggestion version {DEPS_KUBECTL}"
        )  # TODO check version and print warning if is not compatible

    if deps_exist("eksctl") is False:
        subprocess.check_call(scripts["eksctl"], shell=True)
    else:
        print(
            f"\nEksctl installed.\nSuggestion version {DEPS_EKSCTL}"
        )  # TODO check version and print warning if is not compatible

    if deps_exist("kustomize") is False:
        subprocess.check_call(scripts["kustomize"], shell=True)
    else:
        print(
            f"\nKustomize installed.\nSuggestion version {DEPS_KUSTOMIZE}"
        )  # TODO check version and print warning if is not compatible

    if deps_exist("aws-iam-authenticator") is False:
        subprocess.check_call(scripts["aws-iam-authenticator"], shell=True)
    else:
        print(
            f"\nAws-iam-authenticator installed.\nSuggestion version {DEPS_KUSTOMIZE}"
        )  # TODO check version and print warning if is not compatible


def main():
    linux()
    darwin()
    windows()


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