#!/bin/bash

PACKAGES=(
    "linux-lts" "linux-firmware" "base" "base-devel" "grub" "vi" "emacs" "vim" "nano"
    "micro" "neovim" "bash-completion" "btop" "gdu" "git" "curl" "wget" "rsync"
    "unzip" "zip" "gdb" "jdk-openjdk" "openjdk-doc" "tcpdump" "openssh" "bind" "inetutils"
    "nmap" "firefox" "firefox-i18n-fr" "libreoffice" "libreoffice-fr" "rpcbind" "nfs-utils"
    "xfce4" "xfce4-goodies" "yakuake" "gimp" "xorg" "xf86-video-vesa" "xf86-video-dummy"
    "ttf-dejavu" "ttf-liberation" "ttf-bitstream-vera" "acpid" "sdl12-compat" "sdl_ttf"
    "sdl_gfx" "sdl_image" "sdl_mixer" "sdl_net" "sdl_sound" "sdl2_gfx" "sdl2_image"
    "sdl2_mixer" "sdl2_net" "sdl2_ttf" "dosfstools" "ntfs-3g" "subversion" "cvs" "cifs-utils"
    "breezy" "python" "wireshark-qt" "wireshark-cli" "evince" "dbus" "mlocate" "gvfs"
    "strace" "net-tools" "postfix" "sudo" "gtksourceview3" "mplayer" "cronie" "optipng"
    "lib32-glibc" "libvirt" "qt5" "xterm" "graphviz" "vlc" "pulseaudio" "pulseaudio-alsa"
    "tmux" "sleuthkit" "tk" "tcl" "junit" "lightdm" "samba" "bind" "krb5" "tree" "pam_mount"
    "time" "noto-fonts" "noto-fonts-cjk" "noto-fonts-emoji" "noto-fonts-extra" "numlockx"
    "lightdm-gtk-greeter" "tigervnc" "xfce4-pulseaudio-plugin" "docker" "docker-compose"
    "android-tools" "opam" "jdk17-openjdk" "vde2" "shellcheck" "man-db" "man-pages"
    "man-pages-fr" "duf" "eza" "bat" "clang" "socat" "iputils" "termshark" "cups" "cups-pdf"
    "bluefish"
)

PACKAGES_AUR=(
    "saxon6" "staruml" "xfce4-pulseaudio-plugin" "pa-applet-git" "sublime-text"
    "android-studio" "android-sdk" "android-sdk-platform-tools" "android-sdk-build-tools"
    "android-platform" "android-platform-21" "oracle-instantclient-basic"
    "oracle-instantclient-jdbc" "oracle-instantclient-odbc"
    "oracle-instantclient-sdk" "oracle-instantclient-sqlplus" "oracle-instantclient-tools"
    "ncurses5-compat-libs" "visual-studio-code-bin" "google-chrome" "brave-bin"
)

SERVICES=(
    "systemd-networkd.service" "systemd-resolved.service" "systemd-timesyncd.service"
    "docker.service" "ovs-vswitchd.service" "cups.service" "smb" "nmb" "winbind"
)

# Enable time synchronization
timedatectl set-ntp true

# Increase parallel downloads in pacman
sed -i "s/#ParallelDownloads = 5/ParallelDownloads = 16/" /etc/pacman.conf

# Update the keyring
pacman -Sy --noconfirm archlinux-keyring

pacman -S "${PACKAGES[@]}"

# Set the timezone
ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime

# Set the hardware clock time standard
hwclock --systohc

# Generate the locales
sed -i "s/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/" /etc/locale.gen
sed -i "s/#fr_FR.UTF-8 UTF-8/fr_FR.UTF-8 UTF-8/" /etc/locale.gen
locale-gen

# Set the default locale
echo "LANG=en_US.UTF-8" > /etc/locale.conf

# Set the keyboard layout
echo "KEYMAP=fr-latin1" > /etc/vconsole.conf

echo "template" > /etc/hostname

mkdir /mnt/hd
echo "
iluvatar.iut-fbleau.fr:/export /export nfs rw,hard,intr,nodev,nosuid,proto=tcp,retrans=2 0 0

/dev/loop0 /mnt/hd ext3 user,noauto,loop 0 0
/tmp/image.ext3 /mnt/hd ext3 user,noauto,loop 0 0
" >> /etc/fstab

# Droits sur tcpdump
chmod 4755 /usr/sbin/tcpdump

# Droits d'accès à kpageflags et startx
chmod 444 /proc/kpageflags
chmod 700 /usr/bin/startx

#on suid dumpcap pour wireshark
chmod 4755 /usr/bin/dumpcap

chmod 700 /usr/bin/wall

# Disable multicast DNS and LLMNR
sed -i 's/#LLMNR=no/LLMNR=no/g' /etc/systemd/resolved.conf
sed -i 's/#MulticastDNS=no/MulticastDNS=no/g' /etc/systemd/resolved.conf

# Configure the network for DHCP
echo "[Match]
Name=en*

[Network]
DHCP=yes
" > /etc/systemd/network/20-wired.network

# Enable case-insensitive completion
echo 'set completion-ignore-case On' >> /etc/inputrc

# Create local users
useradd -d /home/user1 -g users -m -s /bin/bash -c "Utilisateur local" user1
echo "user1:user1" | chpasswd

useradd -d /home/util -g users -m -s /bin/bash -c "Utilisateur local 2" util
echo "mot de passe de util"
passwd util

# Installation de la librairie graphique de Denis
cd /tmp
wget -O bibliotheque-graphique-iut.tar.gz https://grond.iut-fbleau.fr/IUT/bibliotheque-graphique/archive/main.tar.gz
tar xzvf bibliotheque-graphique-iut.tar.gz
cd bibliotheque-graphique
./configure
make
make install

su - util <<'EOF'
cd /tmp
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg --syncdeps --install --needed --noconfirm

yay -Syu --noconfirm "${PACKAGES_AUR[@]}"
EOF

# Fix Oracle SQL Developer
sed -i '/^#!\/bin\/bash/a export JAVA_HOME=\/usr\/lib\/jvm\/java-17-openjdk\nexport LD_LIBRARY_PATH=\/usr\/lib\/jvm\/java-17-openjdk\/lib\/server:\/usr\/lib\/jvm\/java-17-openjdk\/lib' /usr/bin/oracle-sqldeveloper

# Android Studio config
chmod 1755 /opt/android-sdk/platform-tools/adb
chmod 1755 /opt/android-sdk/tools/emulator

# Patch Imunes
sed -i '/readConfigFile/s/^/#/' /usr/lib/imunes/imunes.tcl

# Enable the services
systemctl enable "${SERVICES[@]}"
cp -R configfiles/* /

# DNS configuration
ln -sf ../run/systemd/resolve/stub-resolv.conf /etc/resolv.conf