#! /bin/sh

cache_path() {
    path="$1"

    if [ -d "$path" ]; then
        for f in $(find "$path" -type f); do
            cache_path "$f"
        done
    elif [ -f "$path" ] && [ ! -L "$path" ]; then
        if [ -x "$path" ]; then
            if file -L "$path" | grep -q 'dynamically linked'; then
                for lib in $(ldd "$path" | awk '{ print $3 }'); do
                    cache_path "$lib"
                done
            fi
        fi
        echo -n >> "$path"
    fi
}

preload() {
	for path in /etc /lib/udev /lib/systemd /sbin/agetty /tmp /usr/libexec/accounts-daemon  /usr/bin/ostree-maintain /usr/share/dbus-1 /usr/bin/dbus-daemon /usr/bin/dbus-send /usr/bin/dbus-run-session /usr/bin/dbus-update-activation-environment /usr/sbin/lightdm /usr/bin/kylin-process-manager-daemon /usr/bin/kylin-process-manager /usr/bin/kylin-process-resource-manager-daemon /usr/share/kysdk/kysdk-base/ /usr/libexec/udisks2/udisksd /usr/sbin/ksaf-devctl-sync-daemon;
    do
        echo "cache $path"
        cache_path "$path"
	done
}

casper=$(cat /proc/cmdline | grep casper)

if [ "X$casper" = "X" ];then
	echo "Normal exit"
else
	echo "Casper preload bin"
	preload
	echo "preload done"
fi