#!/bin/sh

# We're passed the version of the kernel being installed
inst_kern=$1

# if kernel update && no kernel-devel installed && redhat or centos case
# the synosnap will be installed as weak modules, we need to prevent this case

# Function to find synosnap module
find_synosnap() {
    if [ -d "/lib/modules/${inst_kern}/weak-updates" ]; then
        find "/lib/modules/${inst_kern}/weak-updates" -name "synosnap.ko*" -print -quit
    else
        echo ""
    fi
}
# Check if there is a synosnap module in weak-updates directory
synosnap_path=$(find_synosnap)

# only the centos/rhel may trigger weak-updates
if [ -n "$synosnap_path" ]; then
    echo "Found synosnap has been installed as weak modules, force removed it"
    echo "Please reinstall agent after reboot into new kernel."
    
    for initrd in "initrd-$inst_kern.img" "initramfs-$inst_kern.img" "initrd.img-$inst_kern" "initrd-$inst_kern" ''; do
        [[ $initrd && -f /boot/$initrd ]] && break
    done
    if ! [[ $initrd ]]; then
			# Return if we cannot find an initrd.
			echo -n "Warning: "
			echo "Unable to find an initial ram disk that we know how to handle." \
				"Will not try to make an initrd, plesae do it manually."
			return 1
		fi
    rm -fv $synosnap_path
    depmod -a ${inst_kern}
    dracut -f /boot/$initrd $inst_kern

fi


exit 0