#!/bin/bash

. /etc/os-release

if [[ -n $(cat /etc/shadow | grep -ioP 'vscode') ]] || [[ -d /workspaces ]]; then
[[ $(whoami) != "root" ]] && echo "You must be root for this!!!" && exit 1

sudo cat << EOF >> /home/vscode/.bashrc

sudo -i
EOF

sudo cat << EOF >> /root/.bashrc

chown -R vscode:vscode /home/vscode
chown -R vscode:vscode /workspaces
chown -R vscode:vscode /root
cd /workspaces/*
EOF

rm -r /etc/apt/sources.list.d
mkdir /etc/apt/sources.list.d

VERSION_CODENAME=questing
fi

if [[ $ID == "debian" ]]; then
cat << EOF > /etc/apt/sources.list
deb http://ftp.de.debian.org/debian/ $VERSION_CODENAME main contrib non-free
deb-src http://ftp.de.debian.org/debian/ $VERSION_CODENAME main contrib non-free

deb http://ftp.de.debian.org/debian/ $VERSION_CODENAME-updates main contrib non-free
deb-src http://ftp.de.debian.org/debian/ $VERSION_CODENAME-updates main contrib non-free

deb http://security.debian.org/ $VERSION_CODENAME-security main contrib non-free
deb-src http://security.debian.org/ $VERSION_CODENAME-security main contrib non-free
EOF
elif [[ $ID == "ubuntu" ]]; then
cat << EOF > /etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu/ $VERSION_CODENAME main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $VERSION_CODENAME main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ $VERSION_CODENAME-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $VERSION_CODENAME-updates main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ $VERSION_CODENAME-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $VERSION_CODENAME-security main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ $VERSION_CODENAME-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $VERSION_CODENAME-backports main restricted universe multiverse

$(if [[ $VERSION_CODENAME != "questing" ]]; then
deb http://archive.canonical.com/ubuntu $VERSION_CODENAME partner
deb-src http://archive.canonical.com/ubuntu $VERSION_CODENAME partner
fi)
EOF
fi

apt modernize-sources

cat << EOF >> /root/.bashrc

export LANG=de_DE.UTF-8
export HOME=/root
EOF

apt update

apts=(
    *libfbclient*
    autoconf
    bison
    btop
    build-essential
    curl
    dh-autoreconf
    enchant-2
    fail2ban
    flex
    git
    jq
    libapparmor-dev
    libargon2-1
    libasound2t64
    libbrotli-dev
    libbtrfs-dev
    libbz2-dev
    libcap-dev
    libcapstone-dev
    libcrypto++-dev
    libcurl4-openssl-dev
    libdb5.3-dev 
    libdmalloc-dev
    libedit-dev
    libreadline-dev
    libenchant*
    libfbclient2
    libgbm1
    libgpgme-dev
    libiodbc*
    libjpeg-dev
    libldap-dev
    libldap2-dev
    libldb-dev
    liblzma-dev
    libicu-dev
    libmagickwand-dev
    libmemcached-dev
    libpng-dev
    librabbitmq-dev
    libnghttp2-dev
    libnsl-dev
    libonig-dev
    libpci-dev
    libpng-dev
    libpq-dev
    libpsl-dev
    librtmp-dev
    libseccomp-dev
    libsensors-dev
    libsodium-dev
    libsqlite3-dev
    libssh2-1-dev
    libssl*-dev
    libsystemd-dev
    libtidy-dev
    libtool
    libudev-dev
    liburcu-dev
    libuv1-dev
    libwrap0-dev
    libxml2-dev
    libxslt1-dev
    libzip-dev
    libzstd-dev
    lsb-release
    make
    nasm
    perl
    pkg-config
    qemu-guest-agent
    re2c
    snmpd
    sqlite3
    systemtap-sdt-dev
    ufw
    unzip
    unzip
    valgrind
    wget
    xml2
    zlib1g-dev
)

[[ $(lsb_release -cs) == 'trixie' ]] && apts+=(libsnmp40t64)

apt install ${apts[@]} -y

locale-gen --lang de_DE

cat << EOF >> /etc/sysctl.conf
fs.inotify.max_user_watches=999999999
EOF

sysctl -p

bash <(curl -s https://scripts.justman10000.de/scripts.sh)