#!/bin/bash

set -eux

nproc=`nproc`
if [ -d downloads ];
then
    ls -alh downloads;
else
    mkdir downloads;
fi
cd downloads

DESTDIR=/usr/local

if lsb_release &>/dev/null;
then
        dist=$(lsb_release -a | grep Distributor | awk {'print $3'})
else
        dist="Debian"
fi

case $dist in
        Ubuntu | Debian)
                sudo env NEEDRESTART_SUSPEND=1 \
                     apt install nftables \
                     libnetfilter-queue-dev \
                     libsodium-dev \
                     build-essential \
                     autoconf \
                     python3 \
                     python3-virtualenv \
                     python3-dev \
                     python3-build \
                     wget \
                     curl \
                     git \
                     libnfnetlink-dev \
                     python3-nftables \
                     libssl-dev \
                     -y
                sudo env NEEDRESTART_SUSPEND=1 \
                     apt install python3-capstone -y || :
                sudo env NEEDRESTART_SUSPEND=1 \
                     apt install lib25519-dev -y || :
                sudo env NEEDRESTART_SUSPEND=1 \
                     apt install libmceliece-dev -y || :
                sudo env NEEDRESTART_SUSPEND=1 \
                     apt install libntruprime-dev -y || :
                ;;

        Arch)
                sudo pacman \
                     --noconfirm \
                     -S nftables \
                     wget \
                     curl \
                     git \
                     libnetfilter_queue \
                     libsodium \
                     base-devel \
                     autoconf \
                     python \
                     python-virtualenv \
                     libnfnetlink
                sudo pacman \
                     --noconfirm \
                     -S python-capstone || :
                ;;

        Gentoo)
                sudo emerge -vn net-firewall/nftables \
                     net-libs/libnetfilter_queue \
                     dev-libs/libsodium \
                     dev-lang/python \
                     dev-python/pip \
                     dev-python/virtualenv \
                     dev-build/autoconf \
                     net-libs/libnfnetlink \
                     net-misc/wget \
                     net-misc/curl \
                     dev-libs/openssl \
                     dev-vcs/git


                ;;
esac

install_libcpucycles() {
        # Lifted from https://cpucycles.cr.yp.to/download.html and
        # https://cpucycles.cr.yp.to/install.html
        wget -m https://cpucycles.cr.yp.to/libcpucycles-latest-version.txt
        cpucycles_version=$(cat cpucycles.cr.yp.to/libcpucycles-latest-version.txt)
        wget -m https://cpucycles.cr.yp.to/libcpucycles-$cpucycles_version.tar.gz
        tar -xzf cpucycles.cr.yp.to/libcpucycles-$cpucycles_version.tar.gz


        cd libcpucycles-$cpucycles_version \
                && ./configure --prefix="$DESTDIR" \
                && sudo make -j$nproc install
        cd ..
}

install_librandombytes()
{
        wget -m https://randombytes.cr.yp.to/librandombytes-latest-version.txt
        randombytes_version=$(cat randombytes.cr.yp.to/librandombytes-latest-version.txt)
        wget -m https://randombytes.cr.yp.to/librandombytes-$randombytes_version.tar.gz
        tar -xzf randombytes.cr.yp.to/librandombytes-$randombytes_version.tar.gz

        cd librandombytes-$randombytes_version \
                && ./configure --prefix="$DESTDIR" \
                && sudo make -j$nproc install
        cd ..
}

install_lib25519()
{
        wget -m https://lib25519.cr.yp.to/lib25519-latest-version.txt
        lib25519_version=$(cat lib25519.cr.yp.to/lib25519-latest-version.txt)
        wget -m https://lib25519.cr.yp.to/lib25519-$lib25519_version.tar.gz
        tar -xzf lib25519.cr.yp.to/lib25519-$lib25519_version.tar.gz

        command -v valgrind && VALGRIND="--valgrind" || VALGRIND="--no-valgrind"

        cd lib25519-$lib25519_version \
                && ./configure --prefix="$DESTDIR" $VALGRIND \
                && sudo make -j$nproc install
        cd ..
}

install_libmceliece()
{
        wget -m https://lib.mceliece.org/libmceliece-latest-version.txt
        libmceliece_version=$(cat lib.mceliece.org/libmceliece-latest-version.txt)
        wget -m https://lib.mceliece.org/libmceliece-$libmceliece_version.tar.gz
        tar -xzf lib.mceliece.org/libmceliece-$libmceliece_version.tar.gz

        command -v valgrind && VALGRIND="--valgrind" || VALGRIND="--no-valgrind"

        cd libmceliece-$libmceliece_version \
                && ./configure --prefix="$DESTDIR" $VALGRIND \
                && sudo make -j$nproc install

        cd ..
}

install_libntruprime()
{
        wget -m https://libntruprime.cr.yp.to/libntruprime-latest-version.txt
        libntruprime_version=$(cat libntruprime.cr.yp.to/libntruprime-latest-version.txt)
        wget -m https://libntruprime.cr.yp.to/libntruprime-$libntruprime_version.tar.gz
        tar -xzf libntruprime.cr.yp.to/libntruprime-$libntruprime_version.tar.gz

        command -v valgrind && VALGRIND="--valgrind" || VALGRIND="--no-valgrind"

        cd libntruprime-$libntruprime_version \
                && ./configure --prefix="$DESTDIR" $VALGRIND \
                && sudo make -j$nproc install
        cd ..
}

gcc -lcpucycles 2>&1 | grep main > /dev/null || install_libcpucycles
gcc -lrandombytes 2>&1 | grep main > /dev/null || install_librandombytes
gcc -l25519 2>&1 | grep main > /dev/null || install_lib25519
gcc -lmceliece 2>&1 | grep main > /dev/null || install_libmceliece
gcc -lntruprime 2>&1 | grep main > /dev/null || install_libntruprime

exit 0
