#!/usr/bin/env bash

locale-gen

ln -sf /usr/share/zoneinfo/UTC /etc/localtime

usermod -s /bin/bash root
cp -aT /etc/skel/ /root/

useradd -m -p "" -g 'users' -G 'adm,audio,floppy,log,network,nopasswdlogin,optical,power,rfkill,scanner,storage,sys,tty,users,uucp,wheel' -s /bin/bash liveuser
usermod -m -p "" -g 'users' -G 'adm,audio,autologin,network,nopasswdlogin,optical,power,rfkill,storage,sys,users,uucp,video,wheel' -s /bin/bash liveuser
usermod -m -p "" -g 'users' -G 'adm,audio,autologin,floppy,log,network,nopasswdlogin,optical,power,rfkill,scanner,storage,sys,tty,users,uucp,video,wheel' -s /bin/bash liveuser

chmod 750 /etc/sudoers.d
chmod 440 /etc/sudoers.d/g_wheel
chown -R 1000:1000 /home/liveuser

sed -i "s/#Server/Server/g" /etc/pacman.d/mirrorlist
sed -i 's/#\(Storage=\)auto/\1volatile/' /etc/systemd/journald.conf

echo -e "[Daemon]\nTheme=melawy-lera-sugar\nDeviceScale=1\nDeviceTimeout=8\nShowDelay=0" > "/usr/share/plymouth/plymouthd.defaults"

cat "/usr/lib/melawy-linux-release" >> "/etc/motd"
echo "------------------" >> "/etc/motd"


mkdir -p "/etc/calamares/configs/melawy-linux/files/"
cp "/root/wallpaper.png" "/etc/calamares/configs/melawy-linux/files/melawy-linux-wallpaper.png"
chmod 644 "/etc/calamares/configs/melawy-linux/files/"*".png"

mkdir -p "/usr/share/melawy-linux/backgrounds/"
cp "/root/wallpaper.png" "/usr/share/melawy-linux/backgrounds/melawy-linux-wallpaper.png"
chmod 644 "/usr/share/melawy-linux/backgrounds/"*".png"


mv "/usr/lib/modprobe.d/nvidia-utils.conf" "/etc/calamares/configs/melawy-linux/files/nv-modprobe"
mv "/usr/lib/modules-load.d/nvidia-utils.conf" "/etc/calamares/configs/melawy-linux/files/nv-modules-load"

systemctl enable sddm.service
systemctl enable ntpd.service
systemctl enable NetworkManager.service systemd-timesyncd.service bluetooth.service firewalld.service
systemctl enable vboxservice.service vmtoolsd.service vmware-vmblock-fuse.service
systemctl enable intel.service
systemctl enable pacman-init.service choose-mirror.service
#systemctl set-default multi-user.target
systemctl set-default graphical.target

pacman -Qs | grep "/calamares " | cut -c7- > iso_package_versions
pacman -Qs | grep "/firefox " | cut -c7- >> iso_package_versions
pacman -Qs | grep "/linux-xanmod-anbox " | cut -c7- >> iso_package_versions
pacman -Qs | grep "/mesa " | cut -c7- >> iso_package_versions
pacman -Qs | grep "/xorg-server " | cut -c7- >> iso_package_versions
pacman -Qs | grep "/nvidia-dkms " | cut -c7- >> iso_package_versions
mv "iso_package_versions" "/home/liveuser/"

cd "/root"

haveged -w 1024
pacman-key --init
pkill haveged
pacman-key --populate archlinux melawy-linux arcolinux
pacman-key --updatedb

tar -xJvf /root/archives/skel.tar.xz -C /etc/skel --strip=1 --overwrite
chown -R 0:0 /etc/skel

tar -xJvf /root/archives/liveuser.tar.xz -C /home/liveuser --strip=1 --overwrite
chown -R 1000:1000 /home/liveuser

setfacl -m u:sddm:x /home/liveuser/
setfacl -m u:sddm:r /home/liveuser/.face.icon
setfacl -m u:sddm:r /home/liveuser/.face

cp /home/liveuser/Calamares-etc.desktop /usr/share/applications/
