#!/usr/bin/env bash

locale-gen

usermod -p '$y$j9T$DCmooGCtnp3i6gg0IuIhG/$n9NDMoszCJzqEPuJvZppPqjDMttYJlNZVDpxQikzHH1' -s /bin/bash root
cp -aT /etc/skel/ /root/

usermod -p '$y$j9T$TNQr0I6vYHoPyYW..UMVS.$REa5y1aHgxjCdYbEeGviSn2oxSSD6AuoyP1ktQptkL6' liveuser
usermod -g 'users' liveuser

groups="adm audio autologin floppy log network nopasswdlogin optical power rfkill scanner storage sys tty users uucp video wheel"
for i in $groups
do
    #usermod -m -d /home/liveuser -p "123" -g 'users' -G $i -s /bin/bash liveuser
    usermod -a -G $i liveuser
done

#usermod -a -G adm,audio,autologin,floppy,log,network,nopasswdlogin,optical,power,rfkill,scanner,storage,sys,tty,users,uucp,video,wheel liveuser

chown -R 1000:1000 /home/liveuser

findstr=$(grep -e 'kdesu' /usr/bin/eos-install-mode-run-calamares)
sed -e "s/$findstr//g" -i /usr/bin/eos-install-mode-run-calamares

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"

chmod 644 "/usr/share/wallpapers/melawy-linux-wallpapers/"*".png"

cp "/usr/lib/modprobe.d/nvidia-utils.conf" "/etc/calamares/configs/melawy-linux/files/nv-modprobe"
cp "/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"

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/
