#!/bin/bash

# Copyright (C) 2017-2018 Daniel Tartavel-jeannot   <contact@librepc.com>
# Copyright (C) 2019-2020 Jean-Baptiste Biernacki   <j.biernacki@free.fr>
#

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

export ARCH=arm

/usr/bin/chown :shadow /etc/shadow /etc/gshadow /etc/login.defs /etc/pam.d/chage-chfn-chsh /etc/pam.d/chpasswd-newusers /etc/pam.d/system-auth /etc/pam.d/user-group-mod /bin/passwd /bin/chage

/usr/bin/chown :systemd-journal -R /var/log/journal

#Install all packages you need
# No verifying rpm due to a bug in packages, some are not signed

/usr/bin/dnf --nogpgcheck --assumeyes install basesystem chrony rng-tools man openssh-server rfkill wireless-tools bluez wpa_supplicant make binutils dracut u-boot rsync kernel-desktop-latest mlocate drakx-kbd-mouse-x11 glibc-i18ndata


/usr/bin/chown :chrony -R /etc/chrony.keys /var/log/chrony
/usr/bin/chown :daemon /etc/wpa_supplicant.conf
/usr/bin/chown :tty /bin/write /bin/wall
/usr/bin/chown :lock /sbin/lockdev
/usr/bin/chown :bin /sbin/traceroute
/usr/bin/chown polkitd /etc/polkit-1/rules.d

# KV=$(basename /boot/vmlinuz-*)
# KV=${KV#vmlinuz-}
# dracut --kver $KV

VMLINUZ=$(ls --sort=time -1 /boot/vmlinuz-[[:digit:]]* | head -n 1)
# Normally, /boot/vmlinuz is generated with the /sbin/installkernel script, but it is not generated with chroot.
rm -f /boot/vmlinuz
ln -r -s "${VMLINUZ}" /boot/vmlinuz #For extlinux.conf

rm -f ${0}

K_INST_VERSION=$(rpm -q kernel-desktop-latest | sed -e 's/kernel-\([[:alnum:]]*\)-latest-\([\.[:digit:]]*\)-\([\.[:alnum:]]*\).*$/\2-\1-\3/')
dracut --no-compress --force --no-hostonly --local --no-early-microcode --confdir "/etc/dracut.conf.d" --kmoddir="/lib/modules/${K_INST_VERSION}" --xz --printsize --kernel-image /boot/vmlinuz --fstab /boot/initrd-${K_INST_VERSION}.img ${K_INST_VERSION}
ln -r -s /boot/initrd-${K_INST_VERSION}.img /boot/initrd.img
echo "Dracut done."

alternatives --set soundprofile /etc/sound/profiles/alsa
