#!/bin/bash
# Author: sfs <https://puppyrrus.org>
# Author: crims0n <https://minios.dev>

[ ! "$1" ] && echo "Updates cache for all bundles
    Usage:   $0 [bundles mount points location]
    Example: $0 /run/initramfs/memory/bundles" && exit 1

# Ensure the script is run as root
if [ "$(id -u)" -ne 0 ]; then
    echo "This script must be run as root!"
    exit 1
fi

mkdir -p /var/log/minios
exec 19>/var/log/minios/minios-update-cache.trace.log
BASH_XTRACEFD=19
PS4='${LINENO}: '
set -x

# Function to update various system caches from bundles
update_caches() {
    if [ ! "$UPDATE_MIME" ]; then
        [ -d "$1/usr/share/mime/" ] && UPDATE_MIME=true && update-mime-database /usr/share/mime &
    fi
    if [ ! "$UPDATE_BUFFERS" ]; then
        [ -d "$1/usr/lib/gdk-pixbuf-2.0/" ] && UPDATE_BUFFERS=true && gdk-pixbuf-query-loaders --update-cache &
    fi
    if [ ! "$UPDATE_ICONS" ]; then
        [ -d "$1/usr/share/icons/hicolor/" ] && UPDATE_ICONS=true && gtk-update-icon-cache -f -i -q /usr/share/icons/hicolor &
    fi
    if [ ! "$UPDATE_SCHEMAS" ]; then
        [ -d "$1/usr/share/glib-2.0/schemas/" ] && UPDATE_SCHEMAS=true && glib-compile-schemas /usr/share/glib-2.0/schemas/ &
    fi
    if [ ! "$UPDATE_APPLICATIONS" ]; then
        if [ -d "$1/usr/share/applications/" -o -d "$1/usr/local/share/applications/" ]; then
            UPDATE_APPLICATIONS=true
            update-desktop-database -q &
            touch /usr/share/applications/screensavers &
        fi
    fi
    if [ ! "$FCNEED" ]; then
        for FONT_DIR in /usr/share/fonts{,/default}/TTF /usr/X11R6/lib/X11/fonts/TTF; do
            if [ -d "$1/${FONT_DIR}" ]; then
                FCNEED=true
                mkfontscale ${FONT_DIR} &
                mkfontdir ${FONT_DIR} &
            fi
        done
        [ $FCNEED ] && fc-cache -f -s &
    fi
}

# Function to configure kernel modules from bundles
configure_kernel_modules() {
    for MODULE_PATH in $(find $BUNDLES -type d -path "*/lib/modules/*$(uname -r)" -exec find {} -type f \( -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" \) \; 2>/dev/null | egrep -v "$BUNDLES/(01-kernel|$CORE_BUNDLE_PREFIX)"); do
        MODULE_NAME="$(basename $MODULE_PATH | sed 's/.[gx]z$//')"
        [ "$(cat /etc/modprobe.d/*.conf | awk '/^blacklist / && / '$MODULE_NAME'$/ {print $2}')" ] && continue
        [ "$(lsmod | egrep '^'$MODULE_NAME' ')" ] && continue # module already loaded -> next MODULE_PATH
        if [ ! "$DEPMOD" ]; then                              # if DEPMOD is empty, i.e. depmod did not
            insmod "$MODULE_PATH" && continue
            depmod && DEPMOD=true
        fi
        modprobe "$MODULE_NAME"
    done
}

BUNDLES="$1"
CORE_BUNDLE_PREFIX="00-core"
BEXT="sb"


export HOME=/root
export LC_ALL=C

# Update library cache using ldconfig
ldconfig

# Iterate over all bundles except for the core bundle and call update_caches function for each of them
for BUNDLE in $(ls -d $BUNDLES/*.$BEXT | egrep -v "^$BUNDLES/00"); do
    update_caches "$BUNDLE"
done

# Call configure_kernel_modules function to load kernel modules from bundles
configure_kernel_modules
