#!/bin/bash -xe

script=$0
what=${1:-all}

case $what in
    all)
        $script gcc-all
        $script clang-all
        $script embedded
        ;;
    gcc-all)
        $script gcc-14
        $script gcc-13
        $script gcc-12
        $script gcc-11
        $script gcc-10
        $script gcc-9
        $script gcc-8
        $script gcc-7
        $script gcc-6
        $script gcc-5
        $script gcc-4.9
        $script gcc-4.8
        ;;
    clang-all)
        $script clang-19
        $script clang-18
        $script clang-17
        $script clang-16
        $script clang-15
        $script clang-14
        #$script clang-13
        #$script clang-12
        #$script clang-11
        #$script clang-10
        #$script clang-9
        #$script clang-8
        #$script clang-7
        #$script clang-6.0
        #$script clang-5.0
        $script clang-4.0
        $script clang-3.9
        ;;
    embedded)
        $script arm-none-eabi
        $script arm-linux-gnueabihf
        $script qemu
        ;;
    g++-4.8|gcc-4.8|gcc4.8)
        apt-get update
        apt-get install -y \
                software-properties-common
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ bionic main"
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ bionic universe"
        apt-get update
        apt-get install -y \
                cpp-4.8 gcc-4.8 g++-4.8 libstdc++-4.8-dev
        gcc-4.8 --version
        g++-4.8 --version
        ;;
    g++-4.9|gcc-4.9|gcc4.9)
        apt-get update
        apt-get install -y \
                wget \
                software-properties-common
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6AF7F09730B3F0A4
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ xenial main"
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe"
        apt-get update
        apt-get install -y \
                cpp-4.9 gcc-4.9 g++-4.9 libstdc++-4.9-dev
        gcc-4.9 --version
        g++-4.9 --version
        ;;
    g++-5|gcc-5|gcc5)
        apt-get update
        apt-get install -y \
                wget \
                software-properties-common
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6AF7F09730B3F0A4
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ xenial main"
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe"
        apt-get update
        apt-get install -y \
                cpp-5 gcc-5 g++-5 libstdc++-5-dev
        gcc-5 --version
        g++-5 --version
        ;;
    g++-6|gcc-6)
        apt-get update
        apt-get install -y \
                wget \
                software-properties-common
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6AF7F09730B3F0A4
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ bionic main"
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ bionic universe"
        apt-get update
        apt-get install -y \
                cpp-6 gcc-6 g++-6 libstdc++-6-dev
        gcc-6 --version
        g++-6 --version
        ;;
    gcc-7)
        apt-get update
        apt-get install -y \
                wget \
                software-properties-common
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6AF7F09730B3F0A4
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ bionic main"
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ bionic universe"
        apt-get update
        apt-get install -y \
                cpp-7 gcc-7 g++-7 libstdc++-7-dev
        gcc-7 --version
        g++-7 --version
        ;;
    gcc-8)
        apt-get update
        apt-get install -y \
                wget \
                software-properties-common
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6AF7F09730B3F0A4
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ bionic main"
        apt-add-repository --yes --no-update "deb http://dk.archive.ubuntu.com/ubuntu/ bionic universe"
        apt-get update
        apt-get install -y \
                cpp-8 gcc-8 g++-8 libstdc++-8-dev
        gcc-8 --version
        g++-8 --version
        ;;
    g++-9|gcc-9)
        apt-get update
        apt-get install -y \
                wget \
                software-properties-common
        add-apt-repository --yes --no-update ppa:ubuntu-toolchain-r/ppa
        apt-get update
        apt-get install -y \
                cpp-9 gcc-9 g++-9 g++-9-multilib libstdc++-9-dev lib32stdc++-9-dev
        gcc-9 --version
        g++-9 --version
        ;;
    g++-10|gcc-10)
        apt-get update
        apt-get install -y \
                wget \
                software-properties-common
        add-apt-repository --yes --no-update ppa:ubuntu-toolchain-r/ppa
        apt-get update
        apt-get install -y \
                cpp-10 gcc-10 g++-10 g++-10-multilib libstdc++-10-dev lib32stdc++-10-dev
        gcc-10 --version
        g++-10 --version
        ;;
    g++-11|gcc-11)
        apt-get update
        apt-get install -y \
                wget \
                software-properties-common
        add-apt-repository --yes --no-update ppa:ubuntu-toolchain-r/ppa
        apt-get update
        apt-get install -y \
                cpp-11 gcc-11 g++-11 g++-11-multilib libstdc++-11-dev lib32stdc++-11-dev
        gcc-11 --version
        g++-11 --version
        ;;
    g++-12|gcc-12)
        apt-get update
        apt-get install -y \
                cpp-12 gcc-12 g++-12 g++-12-multilib libstdc++-12-dev lib32stdc++-12-dev
        gcc-12 --version
        g++-12 --version
        ;;
    g++-13|gcc-13)
        apt-get update
        apt-get install -y \
                cpp-13 gcc-13 g++-13 g++-13-multilib libstdc++-13-dev lib32stdc++-13-dev
        gcc-13 --version
        g++-13 --version
        ;;
    g++-14|gcc-14)
        apt-get update
        apt-get install -y \
                cpp-14 gcc-14 g++-14 g++-14-multilib libstdc++-14-dev lib32stdc++-14-dev
        gcc-14 --version
        g++-14 --version
        ;;
    clang++-3.9|clang-3.9)
        apt-get update
        apt-get install -y \
                software-properties-common
        apt-add-repository --yes --no-update ppa:ubuntu-toolchain-r/test
        apt-get update
        apt-get install -y \
                clang-3.9 clang-tidy-3.9
        clang-3.9 --version
        clang++-3.9 --version
        ;;
    clang++-4.0|clang-4.0)
        apt-get update
        apt-get install -y \
                software-properties-common
        apt-add-repository --yes --no-update ppa:ubuntu-toolchain-r/test
        apt-get update
        apt-get install -y \
                clang-4.0 clang-tidy-4.0
        clang-4.0 --version
        clang++-4.0 --version
        ;;
    #clang++-5.0|clang-5.0)
    #    apt-get update
    #    apt-get install -y \
    #            software-properties-common
    #    apt-add-repository --yes --no-update ppa:ubuntu-toolchain-r/test
    #    apt-get update
    #    apt-get install -y \
    #            clang-5.0 clang-tidy-5.0
    #    clang-5.0 --version
    #    clang++-5.0 --version
    #    ;;
    #clang++-6.0|clang-6.0)
    #    apt-get update
    #    apt-get install -y \
    #            wget \
    #            software-properties-common
    #    wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key 2>/dev/null | apt-key add -
    #    apt-add-repository --yes --no-update "deb http://apt.llvm.org/focal/ llvm-toolchain-focal main"
    #    apt-get update
    #    apt-get install -y \
    #            clang-6.0 clang-tidy-6.0
    #    clang-6.0 --version
    #    clang++-6.0 --version
    #    ;;
    #clang++-7|clang-7)
    #    apt-get update
    #    apt-get install -y \
    #            wget \
    #            software-properties-common
    #    wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key 2>/dev/null | apt-key add -
    #    apt-add-repository --yes --no-update "deb http://apt.llvm.org/focal/ llvm-toolchain-focal main"
    #    apt-get update
    #    apt-get install -y \
    #            clang-7 clang-tidy-7
    #    clang-7 --version
    #    clang++-7 --version
    #    ;;
    #clang++-8|clang-8)
    #    apt-get update
    #    apt-get install -y \
    #            wget \
    #            software-properties-common
    #    wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key 2>/dev/null | apt-key add -
    #    apt-add-repository --yes --no-update "deb http://apt.llvm.org/focal/ llvm-toolchain-focal main"
    #    apt-get update
    #    apt-get install -y \
    #            clang-8 clang-tidy-8
    #    clang-8 --version
    #    clang++-8 --version
    #    ;;
    #clang++-9|clang-9)
    #    apt-get update
    #    apt-get install -y \
    #            wget \
    #            software-properties-common
    #    wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key 2>/dev/null | apt-key add -
    #    apt-add-repository --yes --no-update "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-9 main"
    #    apt-get update
    #    apt-get install -y \
    #            clang-9 clang-tidy-9
    #    clang-9 --version
    #    clang++-9 --version
    #    ;;
    #clang++-10|clang-10)
    #    apt-get update
    #    apt-get install -y \
    #            wget \
    #            software-properties-common
    #    wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key 2>/dev/null | apt-key add -
    #    apt-add-repository --yes --no-update "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-10 main"
    #    apt-get update
    #    apt-get install -y \
    #            clang-10 clang-tidy-10
    #    clang-10 --version
    #    clang++-10 --version
    #    ;;
    #clang++-11|clang-11)
    #    apt-get update
    #    apt-get install -y \
    #            clang-11 clang-tidy-11
    #    clang-11 --version
    #    clang++-11 --version
    #    ;;
    #clang++-12|clang-12)
    #    apt-get update
    #    apt-add-repository --yes --no-update "deb http://apt.llvm.org/noble/ llvm-toolchain-noble main"
    #    apt-get update
    #    apt-get install -y \
    #            clang-12 clang-tidy-12
    #    ;;
    #clang++-13|clang-13)
    #    apt-get update
    #    apt-get install -y \
    #            clang-13 clang-tidy-13
    #    ;;
    clang++-14|clang-14)
        apt-get update
        apt-get install -y \
                clang-14 clang-tidy-14
        ;;
    clang++-15|clang-15)
        apt-get update
        apt-get install -y \
                clang-15 clang-tidy-15
        ;;
    clang++-16|clang-16)
        apt-get update
        apt-get install -y \
                clang-16 clang-tidy-16
        ;;
    clang++-17|clang-17)
        apt-get update
        apt-get install -y \
                clang-17 clang-tidy-17
        ;;
    clang++-18|clang-18)
        apt-get update
        apt-get install -y \
                clang-18 clang-tidy-18
        ;;
    clang++-19|clang-19)
        apt-get update
        apt-get install -y \
                clang-19 clang-tidy-19
        ;;
    arm-none-eabi)
        apt-get update
        apt-get install -y \
                gcc-arm-none-eabi
        arm-none-eabi-gcc --version
        arm-none-eabi-g++ --version
        ;;
    armv7|arm-linux-gnueabihf)
        apt-get update
        apt-get install -y \
                gcc-arm-linux-gnueabihf \
                g++-arm-linux-gnueabihf
        arm-linux-gnueabihf-gcc --version
        arm-linux-gnueabihf-g++ --version
        ;;
    armv6|arm-linux-gnueabi)
        apt-get update
        apt-get install -y \
                gcc-arm-linux-gnueabi \
                g++-arm-linux-gnueabi
        arm-linux-gnueabi-gcc --version
        arm-linux-gnueabi-g++ --version
        ;;
    aarch64|aarch64-linux-gnu)
        apt-get update
        apt-get install -y \
                gcc-aarch64-linux-gnu \
                g++-aarch64-linux-gnu
        aarch64-linux-gnu-gcc --version
        aarch64-linux-gnu-g++ --version
        ;;
    s390|s390x|s390x-linux-gnu)
        apt-get update
        apt-get install -y \
                gcc-s390x-linux-gnu \
                g++-s390x-linux-gnu
        s390x-linux-gnu-gcc --version
        s390x-linux-gnu-g++ --version
        ;;
    ppc64le|powerpc64le|ppc64le-linux-gnu)
        apt-get update
        apt-get install -y \
                gcc-powerpc64le-linux-gnu \
                g++-powerpc64le-linux-gnu
        powerpc64le-linux-gnu-gcc --version
        powerpc64le-linux-gnu-g++ --version
        ;;
    ppc64|powerpc64|ppc64-linux-gnu)
        apt-get update
        apt-get install -y \
                gcc-powerpc64-linux-gnu \
                g++-powerpc64-linux-gnu
        powerpc64-linux-gnu-gcc --version
        powerpc64-linux-gnu-g++ --version
        ;;
    riscv64|riscv64-linux-gnu)
        apt-get update
        apt-get install -y \
                gcc-riscv64-linux-gnu \
                g++-riscv64-linux-gnu
        riscv64-linux-gnu-gcc --version
        riscv64-linux-gnu-g++ --version
        ;;
    qemu)
        apt-get update
        apt-get install -y \
                qemu-user \
                qemu-user-static
        ;;
    mingw)
        apt-get update
        apt-get install -y \
                gcc-mingw-w64-x86-64 \
                g++-mingw-w64-x86-64 \
                mingw-w64 \
                mingw-w64-x86-64-dev \
                mingw-w64-tools
        update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix
        update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
        ;;
    wine)
        apt-get update
        apt-get install -y wine
        # run exe with env WINEPATH="/usr/lib/gcc/x86_64-w64-mingw32/7.3-win32;/usr/x86_64-w64-mingw32/lib" wine exe
        ;;
    *)
        echo "unknown package: $what"
        exit 1
        ;;
esac
