post_install() {
    check_hostid
    run_depmod
}

post_remove() {
    run_depmod
}

post_upgrade() {
    check_hostid
    run_depmod
}

check_hostid() {
    # Check /etc/hostid to see if it set to the sentinel value, see
    # https://wiki.archlinux.org/index.php/ZFS for more information.
    HOSTID=$(hostid)
    if [ "0x$HOSTID" == "0xffffffff" ]; then
        # Generate a new hostid
        : >/etc/hostid
        HOSTID=$(hostid)
        # hostid is 4 byte little endian
        printf $(echo -n $HOSTID | sed 's/\(..\)\(..\)\(..\)\(..\)/\\x\4\\x\3\\x\2\\x\1/') >/etc/hostid
    fi
}

run_depmod() {
    echo ">>> Updating module dependencies. Please wait ..."
    # `uname -r` returns the current running kernel version, we need the
    # currently installed kernel version.
    KERNEL_VERSION=$(pacman -Qi linux | sed -n 2p | cut -d: -f2)
    # For cases when the kernel version is 3.8, 3.9
    LINUX_VER=$(echo $KERNEL_VERSION | cut -d- -f1)
    if [ $(grep -o "\." <<<"$LINUX_VER" | wc -l) -eq 1 ]; then
        KERNEL_VERSION="${LINUX_VER}.0-${KERNEL_VERSION:(-1)}"
    fi
    depmod $(basename $(find /usr/lib/modules -type d -iname $KERNEL_VERSION\*))
}
