#!/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
/usr/bin/dnf --setopt=ignorearch=True --nogpgcheck --assumeyes install basesystem chrony polkit lockdev rng-tools man openssh-server rfkill wireless-tools bluez wpa_supplicant make rsync binutils ldetect usbutils #kernel-desktop-latest # systemd-bootchart libcap-utils keyutils mindi-busybox plymouth-scripts lvm2  #dmsetup

#For dracut :
# /usr/bin/dnf --setopt=ignorearch=True --nogpgcheck --assumeyes install u-boot-tools mindi-busybox plymouth-scripts #systemd-bootchart libcap-utils keyutils  lvm2  #dmsetup

# ############################
# ## For compiling a module ##
# ############################
# BUG : Make does not like to build inside chroot. It doesn't find tools. Impossible to build any module there.
# /usr/bin/dnf --setopt=ignorearch=True --nogpgcheck --assumeyes install kernel-source-latest
# #See if it is a Server or Desktop kernel :
# rpm -q kernel-serveur-latest
# if [ 0 -ne ${?} ]; then
#     K_INST_VERSION=$(rpm -q kernel-desktop-latest | sed -e 's/kernel-\([[:alnum:]]*\)-latest-\([\.[:digit:]]*\)-\([\.[:alnum:]]*\).*$/\2-\1-\3/' )
# else
#     K_INST_VERSION=$(rpm -q kernel-serveur-latest | sed -e 's/kernel-\([[:alnum:]]*\)-latest-\([\.[:digit:]]*\)-\([\.[:alnum:]]*\).*$/\2-\1-\3/' )
# fi
# #Recover the kernel module path :
# K_MOD_PATH=$(urpmf "modules/${K_INST_VERSION}$" | cut -d ':' -f 2)
# #Recover the source version : 
# K_SRC_VERSION=$(rpm -q kernel-source-latest | sed -e 's/kernel-source-latest-\([\.[:digit:]]*\)-\([\.[:alnum:]]*\).*$/\1-\2/' )
# #Recover the source path : 
# K_SRC_PATH=$(urpmf "src/kernel-${K_SRC_VERSION}$" | cut -d ':' -f 2)
# #Set the default Mageia's configuration file into the source path :
# cp /boot/config-${K_INST_VERSION} ${K_SRC_PATH}/.config
# ## Can't go further... Make does not like to build inside chroot. It doesn't find tools.
# ## Impossible to build any module there.


# ####################################################
# ## To embed a module inside the early boot initrd ##
# ####################################################
# #Set the kernel name :
# KERNELVERSION="5.3.7-desktop-4.mga7"
K_INST_VERSION=$(rpm -q kernel-desktop-latest | sed -e 's/kernel-\([[:alnum:]]*\)-latest-\([\.[:digit:]]*\)-\([\.[:alnum:]]*\).*$/\2-\1-\3/')
# #Set the list of added modules and omitted modules :
# DRIVER_ADD_LIST="8250_dw"
# DRIVER_OMI_LIST="drm"
# #Create a configuration file inside the FS, it will be used by dracut for all kernel update
# echo -e "# Omiting drm as it failed for arm :\nomit_dracutmodules+=\" ${DRIVER_OMI_LIST} \"\n\n# Driver to add for banana Pro (serial) :\nadd_drivers+=\" ${DRIVER_ADD_LIST} \"\n" > /etc/dracut.conf.d/30-bananapro.conf
# #Generate the initrd :
# OUTPUT=$(ARCH=arm CROSS_COMPILE=arm-linux-gnu- 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 initrd-${K_INST_VERSION}.img ${K_INST_VERSION} 2>&1 | tee /dev/tty )
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
# #Add the uboot header to the initrd file, first, find the file :
# INITRD_FILE="$(echo "${OUTPUT}" | grep "Creating initramfs image file" | sed -e "s/^[*:[:space:][:alnum:]]*'//" -e "s/'.*$//" )"
# #Add the uboot header, the result file will contain a 'u' as prefix-name.
# mkimage -A arm -T ramdisk -C lzma -n initrd -d ${INITRD_FILE} "$(dirname "${INITRD_FILE}")/u$(basename "${INITRD_FILE}")"
# #generate a symlink relative r and symbolic s :
# rm -f /boot/initrd
# ln -r -s "$(dirname "${INITRD_FILE}")/u$(basename "${INITRD_FILE}")" /boot/initrd

###########################################
## Install a package from testing repo : ##
###########################################
# dnf --refresh --enablerepo=updates_testing-\* --assumeyes install kernel-desktop-latest

############################################
## Installing the kernel (not on testing) ##
############################################
# dnf --refresh --assumeyes install kernel-desktop-latest

###############################################
## Create a symlink for the lastest kernel : ##
###############################################
    ## As long as the first installed kernel remains installed, the boot.scr script will continue to work, even if the extlinux.conf file is not working with uboot anymore.
# #Last rpm package :
# KERNELRPM=$(rpm -qa --last | grep "^kernel-[sd]e[[:alpha:]]*-[[:digit:]]" | head -n 1 | cut -d ' ' -f 1 | sed -e 's/\(kernel-[[:alnum:]]*-[\.[:digit:]]*-[\.[:alnum:]]*\)-.*/\1/' )
# #Get the vmlinuz file :
# VMLINUZ=$(urpmf ${KERNELRPM} | grep "boot/vmlinuz" | cut -d ':' -f 2)
#Get the vmlinuz file :
VMLINUZ=$(ls --sort=time -1 /boot/vmlinuz-[[:digit:]]* | head -n 1)
rm -f /boot/uvmlinuz
ln -r -s "${VMLINUZ}" /boot/uvmlinuz #For the uboot script
# Normally, /boot/vmlinuz is generated with the /sbin/installkernel script, but it is not generated with chroot.
ln -r -s "${VMLINUZ}" /boot/vmlinuz #For extlinux.conf

################################
## Create the extlinux script ##
################################
# BUG : installkernel call bootloader-config which doesn't process inside a chroot.
# echo -e "\nTrying to generate extlinux script\n"
# K_INST_VERSION=$(rpm -q kernel-desktop-latest | sed -e 's/kernel-\([[:alnum:]]*\)-latest-\([\.[:digit:]]*\)-\([\.[:alnum:]]*\).*$/\2-\1-\3/')
# /sbin/installkernel %{kversion}-$kernel_flavour-%{buildrpmrel}
# bash -x /sbin/installkernel ${K_INST_VERSION}
# perl -d /usr/sbin/bootloader-config --kernel-version ${K_INST_VERSION} --initrd-options '' --action add-kernel
# /usr/sbin/bootloader-config --kernel-version ${K_INST_VERSION} --initrd-options '' --action add-kernel
# echo -e "\nEnd trying to generate extlinux script with errcode ${?}\n"


/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


    ## DO NOT EXIT THE SCRIPT. A second part is automatically added in this script during the process in order to create the default user and set root password.
