#!/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

# Set gcc as $CC if otherwise unset
: "${CC:=gcc}";
ARCH=`uname -m`;
USERSPACE=`getconf LONG_BIT`;
if [ $ARCH = i386 ] || [ $ARCH = x86_64 ] && [ $USERSPACE = 32 ]; then
  export ARCH=i386;
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-venv \
                     python3-dev \
                     python3-build \
                     wget \
                     curl \
                     git \
                     libnfnetlink-dev \
                     python3-nftables \
                     libssl-dev \
                     libcap2-bin \
                     --no-install-recommends \
                     -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 || :
                ;;

        Alpine)
                sudo apk add \
                     alpine-sdk \
                     autoconf \
                     cargo \
                     curl \
                     clang \
                     git \
                     iproute2 \
                     iputils \
                     libcap-setcap \
                     libnetfilter_queue \
                     libnetfilter_queue-dev \
                     libnfnetlink \
                     libnfnetlink-dev \
                     linux-headers \
                     libsodium \
                     musl-dev \
                     nftables \
                     openssl-dev \
                     py3-pip \
                     py3-virtualenv \
                     python3 \
                     python3-dev \
                     rust \
                     wget;
                ;;

        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" --host="$ARCH" \
                && 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" --host="$ARCH" $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" --host="$ARCH" $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" --host="$ARCH" $VALGRIND \
                && sudo make -j$nproc install
        cd ..
}


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

sudo ldconfig $DESTDIR/lib;

exit 0
