has_package() {
    pacman -Qq "${1}" > /dev/null 2>&1
}

update_fstab() {
    # Needs to be in sync with the mount options in post_install()
    local BOOT_OPTIONS="defaults,noexec,nodev,showexec"

    # Ensure updated mount options for /boot in /etc/fstab, if the /boot
    # partition entry is present and it is using vfat filesystem
    if [ -f /etc/fstab ] && \
       grep -q -E "[[:blank:]]+/boot[[:blank:]]+vfat[[:blank:]]+" /etc/fstab && \
       ! grep -q -E "[[:blank:]]+/boot[[:blank:]]+vfat[[:blank:]]+${BOOT_OPTIONS}[[:blank:]]+" /etc/fstab; then
        echo "Updating mount options for /boot in /etc/fstab..."
        sed -i -e "s#\([ \t]\+\)\(/boot\)\([ \t]\+\)\([^ \t]\+\)\([ \t]\+\)\([^ \t]\+\)\(.*\)\$#\1\2\3\4\5${BOOT_OPTIONS}\7#g" \
            /etc/fstab
    fi
}

post_install() {
    # Needs to be in sync with the mount options in update_fstab()
    local BOOT_OPTIONS="defaults,noexec,nodev,showexec"

    # Configure /boot in /etc/fstab
    # Make sure not to add the new /boot partition entry to /etc/fstab
    # in the following cases:
    # 1) If the /boot partition entry in /etc/fstab is already present,
    #    but defined in some other way, such as by using PARTUUID that
    #    may be the case on some installations
    # 2) If there's no /boot partition entry present in /etc/fstab, but
    #    there's also no filesystem with the expected label, which may
    #    be the case on some installations;  if this is an image build,
    #    add the new /boot partition entry anyway
    if [ -f /etc/fstab ] && \
       ! grep -q -E '^LABEL=BOOT_MNJRO[[:blank:]]+' /etc/fstab && \
       ! grep -q -E '[[:blank:]]+/boot[[:blank:]]+' /etc/fstab && \
       (lsblk -f -r | grep -q -E '[[:blank:]]+BOOT_MNJRO[[:blank:]]+' || \
        [ -f /MANJARO-ARM-IMAGE-BUILD ]); then
        echo "Configuring /boot in /etc/fstab..."
        echo "LABEL=BOOT_MNJRO  /boot   vfat    ${BOOT_OPTIONS}     0   0" >> /etc/fstab
    fi

    # Configure Plymouth hook in /etc/mkinitcpio.conf
    # Do not change anything if the user has no Plymouth package installed,
    # which may be the case on some installations
    if has_package "plymouth" && \
       has_package "mkinitcpio" && \
       [ -f /etc/mkinitcpio.conf ] && \
       grep -q '^HOOKS=(base udev autodetect modconf block filesystems keyboard fsck)' /etc/mkinitcpio.conf; then
        echo "Configuring hooks in /etc/mkinitcpio.conf..."
        sed -i s/"^HOOKS=(base udev autodetect modconf block filesystems keyboard fsck)"/"HOOKS=(base udev plymouth autodetect modconf block filesystems keyboard fsck)"/g \
            /etc/mkinitcpio.conf
    fi

    # Set the Manjaro theme in Plymouth
    # Do not change anything if the user has no Plymouth package installed,
    # which may be the case on some installations
    if has_package "plymouth" && \
       [ -f /usr/bin/plymouth-set-default-theme ] && \
       [ -d /usr/share/plymouth/themes/materia-manjaro ]; then
        local CURRENT_THEME=$(plymouth-set-default-theme 2> /dev/null)

        # Check is the right theme already selected
        if [[ "${CURRENT_THEME}" != "materia-manjaro" ]]; then
            echo "Configuring Plymouth theme..."
            plymouth-set-default-theme -R materia-manjaro
        fi
    fi

    # Perform the system cleanups
    # Needs to be performed here as well, i.e. not only on updates, because
    # older installations will have this package installed as a dependency
    update_fstab
}

post_upgrade() {
    # Perform the system cleanups
    update_fstab
}
