#!/bin/bash -x

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

case $what in
    all)
        $script gcc-all
        $script clang-all
        $script embedded
        ;;
    gcc-all)
        $script gcc-4.8
        $script gcc-4.9
        $script gcc-5
        $script gcc-6
        #$script gcc-7
        #$script gcc-8
        $script gcc-9
        $script gcc-10
        $script gcc-11
        $script gcc-12
        $script gcc-13
        ;;
    clang-all)
        $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)
        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
        ;;
    g++-4.9|gcc-4.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
        ;;
    g++-5|gcc-5)
        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
        ;;
    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-7)
    #    ;;
    #gcc-8)
    #    ;;
    g++-9|gcc-9)
        apt-get update
        apt-get install -y \
                cpp-9 gcc-9 g++-9 g++-9-multilib libstdc++-9-dev lib32stdc++-9-dev
        ;;
    g++-10|gcc-10)
        apt-get update
        apt-get install -y \
                cpp-10 gcc-10 g++-10 g++-10-multilib libstdc++-10-dev lib32stdc++-10-dev
        ;;
    g++-11|gcc-11)
        apt-get update
        apt-get install -y \
                cpp-11 gcc-11 g++-11 g++-11-multilib libstdc++-11-dev lib32stdc++-11-dev
        ;;
    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
        ;;
    g++-13|gcc-13)
        apt-get update
        apt-get install -y \
                wget \
                software-properties-common
        apt-add-repository --yes --no-update ppa:ubuntu-toolchain-r/test
        apt-get update
        apt-get install -y \
                cpp-13 gcc-13 g++-13 g++-13-multilib libstdc++-13-dev lib32stdc++-13-dev
        ;;
    #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++-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++-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++-6.0|clang-6.0)
    #    apt-get update
    #    apt-get install -y \
    #            clang-6.0 clang-tidy-6.0
    #    ;;
    #clang++-7|clang-7)
    #    apt-get update
    #    apt-get install -y \
    #            clang-7 clang-tidy-7
    #    ;;
    #clang++-8|clang-8)
    #    apt-get update
    #    apt-get install -y \
    #            clang-8 clang-tidy-8
    #    ;;
    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++-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++-11|clang-11)
        apt-get update
        apt-get install -y \
                clang-11 clang-tidy-11
        ;;
    clang++-12|clang-12)
        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 \
                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/jammy/ llvm-toolchain-jammy-15 main"
        apt-get update
        apt-get install -y \
                clang-15 clang-tidy-15
        ;;
    clang++-16|clang-16)
        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 ppa:ubuntu-toolchain-r/test
        apt-add-repository --yes --no-update "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main"
        apt-get update
        apt-get install -y \
                clang-16 clang-tidy-16
        ;;
    arm-none-eabi)
        # taken from: https://askubuntu.com/questions/1243252/how-to-install-arm-none-eabi-gdb-on-ubuntu-20-04-lts-focal-fossa/1371525#1371525
        apt-get update
        apt-get install -y \
                wget
        echo "Creating gcc-arm-none-eabi debian package"
        set -xe
        ver=10.3-2021.10
        longver=15:10.3-2021.10-9
        name=/tmp/gcc-arm-none-eabi-$ver
        rm -rf /tmp/gcc-arm-none-eabi-$ver*
        echo "Downloading..."
        wget --no-check-certificate -nv -O $name.tar "https://developer.arm.com/-/media/Files/downloads/gnu-rm/$ver/gcc-arm-none-eabi-$ver-x86_64-linux.tar.bz2"
        echo "Extracting..."
        ( cd /tmp ; tar -xf $name.tar )
        rm -f $name.tar
        echo "Generating debian package..."
        mkdir -p $name-dpkg/DEBIAN
        mkdir -p $name-dpkg/usr
        echo "Package: gcc-arm-none-eabi" >> $name-dpkg/DEBIAN/control
        echo "Version: $longver" >> $name-dpkg/DEBIAN/control
        echo "Architecture: amd64" >> $name-dpkg/DEBIAN/control
        echo "Maintainer: maintainer" >> $name-dpkg/DEBIAN/control
        echo "Description: Arm Embedded toolchain" >> $name-dpkg/DEBIAN/control
        cat $name-dpkg/DEBIAN/control
        mv -f $name/* $name-dpkg/usr/
        echo "Building debian package..."
        dpkg-deb --build --root-owner-group $name-dpkg
        mv -v $name-dpkg.deb $name.deb
        dpkg -i $name.deb
        echo "Testing install..."
        arm-none-eabi-gcc --version
        arm-none-eabi-g++ --version
        ;;
    arm-linux-gnueabihf)
        apt-get update
        apt-get install -y \
                gcc-arm-linux-gnueabihf \
                g++-arm-linux-gnueabihf
        ;;
    qemu)
        apt-get update
        apt-get install -y \
                qemu
        ;;
    *)
        echo "unknown package: $what"
        exit 1
        ;;
esac
