#!/usr/bin/env bash
mkdir -p /var/log/local
exec > >(tee -i /var/log/local/iutinfo-root-$(date '+%F-%T').log)
exec 2>&1
export PS4='+ \D{%F %T} '
set -x

export DEBIAN_FRONTEND=noninteractive

if ! grep -q IUTINFO /etc/apt/sources.list ; then
    mv -f /etc/apt/sources.list /etc/apt/sources.list.orig
    cat <<EOF > /etc/apt/sources.list
# Modifié par IUTINFO pour Debian Bookworm via https

deb https://deb.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
deb-src https://deb.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware

deb https://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
deb-src https://deb.debian.org/debian bookworm main contrib non-free non-free-firmware

deb https://deb.debian.org/debian bookworm-updates main contrib non-free non-free-firmware
deb-src https://deb.debian.org/debian bookworm-updates main contrib non-free non-free-firmware
EOF
fi
apt-get -qy update

wget -qO - https://gitlab.com/paulcarroty/vscodium-deb-rpm-repo/raw/master/pub.gpg |
    gpg --dearmor |
    dd of=/usr/share/keyrings/vscodium-archive-keyring.gpg

cat <<EOF >/etc/apt/sources.list.d/vscodium.list
deb [ signed-by=/usr/share/keyrings/vscodium-archive-keyring.gpg ] https://paulcarroty.gitlab.io/vscodium-deb-rpm-repo/debs vscodium main
EOF

apt-get -qy update

apt-get -qy dist-upgrade

apt-get -qy install \
           wget curl \
           default-jdk default-jdk-doc ant ant-doc \
           emacs emacs-common-non-dfsg vim vim-doc hexedit tree ncal calendar \
           netcat-traditional evince ed groff info feh x11-apps icu-devtools \
           git git-doc gitk \
           unixodbc mdbtools odbc-mdbtools odbc-postgresql \
           mariadb-client libmariadb-java \
           postgresql-client libpg-java \
           sqlite3 sqlite3-doc \
           jupyter thonny \
           python3-opencv python3-matplotlib python3-numpy python3-pandas python3-seaborn python3-sklearn python3-sklearn-pandas \
           python3-numpydoc python-pandas-doc python-sklearn-doc \
           codium \
           pandoc \
           ffmpeg ffmpeg-doc vlc vlc-l10n mplayer mplayer-doc mencoder \
           obs-studio recordmydesktop screenkey \
           gnome-subtitles subtitleeditor \
           texlive-latex-base texlive-latex-base-doc \
           texlive-latex-recommended texlive-latex-recommended-doc \
           texlive-lang-french \
           dnsutils nmap tcpdump wireshark wireshark-doc tshark \
           snmp snmp-mibs-downloader \
           manpages manpages-posix \
           x11vnc screen sshfs \
           openvpn network-manager-openvpn network-manager-openvpn-gnome \
           rsync etckeeper \
           rdesktop libguestfs-tools net-tools inetutils-tools \
           dconf-cli

apt-get -qy clean
apt-get -qy autopurge

test -d /iut || { mkdir /iut ; chown 1000:1000 /iut ; }

adduser $(id -un 1000) sudo

mandb

{ nmcli connection show | grep -q ULILLE_VPN_ETU_TCP_v3 ; } &&
    nmcli connection delete ULILLE_VPN_ETU_TCP_v3
{ nmcli connection show | grep -q ULILLE_VPN_ETU_TCP_v4_Linux ; } ||
    (
        cd /tmp
        wget -qN https://infotuto.univ-lille.fr/fileadmin/user_upload/infotuto/images/DSI/Fichiers_telechargeables/Clients_VPN/ULILLE_VPN_ETUDIANT_Linux_v4_2.zip
        unzip ULILLE_VPN_ETUDIANT_Linux_v4_2.zip
        nmcli connection import type openvpn file ULILLE_VPN_ETU_TCP_v4_Linux.ovpn
        rm -rf *ETU*
    )

wget -q -O /usr/local/bin/iutinfo https://gitlab.univ-lille.fr/iut/tp-utils/-/raw/master/iutinfo
chmod a+x /usr/local/bin/iutinfo

cat <<EOF >/root/.iutinforc
# Environnement bash pour l'IUTINFO -*-sh-*-

EOF

cat <<'EOF' >>/root/.iutinforc
# Mise à jour de tout l'environnement
alias upgrade-iutinfo="wget -q --show-progress https://env.iutinfo.fr/setup-iutinfo -O /root/setup-iutinfo && chmod +x /root/setup-iutinfo && /root/setup-iutinfo"

# Mise à jour de la commande de gestion de l'environnement
alias download-iutinfo="wget -q --show-progress https://gitlab.univ-lille.fr/iut/tp-utils/-/raw/master/iutinfo -O /usr/local/bin/iutinfo && chmod a+x /usr/local/bin/iutinfo"

EOF

grep -q 'source $HOME/.iutinforc' /root/.bashrc || {
    cat <<'EOF' >> /root/.bashrc

# Environnement de l'IUTINFO
test -r $HOME/.iutinforc && source $HOME/.iutinforc
EOF
}

set +x
