#!/usr/bin/env bash
#
# Author: Dmitry Razumov <asmeron@ublinux.com>
# Copyright (c) 2021-2025 UBLinux <support@ublinux.com>
#

ENABLED=yes
[[ ${ENABLED} == "yes" ]] || exit 0
DEBUGMODE=no

[[ -d /usr/lib/ublinux ]] && { unset ROOTFS; unset CMD_CHROOT; } || { ROOTFS="/sysroot"; CMD_CHROOT="chroot ${ROOTFS}"; }
SOURCE=${ROOTFS}/usr/lib/ublinux/functions; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
SOURCE=${ROOTFS}/usr/lib/ublinux/default; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
debug_mode "$0" "$@"

SYSCONF="${ROOTFS}${SYSCONF}"

SOURCE=/usr/lib/ublinux/default; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
SOURCE=${SYSCONF}/config; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null
SOURCE=${SYSCONF}/save; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null
SOURCE=${SYSCONF}/network; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null


exec_save_all_cache(){
    if [[ -n ${SAVE_ALL_CACHE} ]]; then
	PATH_CHANGES="/memory/changes"
	NAME_ROOTCOPY="rootcopy"
	NAME_MODULES="modules"
        if [[ ${SAVE_ALL_CACHE} == "rootcopy" ]]; then
	    PATH_ROOTCOPY=$(find /memory/layer-base/*/ -maxdepth 1 -type d -name "${NAME_ROOTCOPY}" | head -1)
	    [[ -n ${PATH_ROOTCOPY} ]] || PATH_ROOTCOPY="$(find /memory/layer-base/*/ -maxdepth 1 -type f -name "ublinux-data*.sgn" | head -1)"
	    [[ -n ${PATH_ROOTCOPY} ]] && PATH_ROOTCOPY="${PATH_ROOTCOPY%/*}/${NAME_ROOTCOPY}" || return 0
    	    [[ -n ${PATH_ROOTCOPY} ]] || install -dm0755 -o root -g root "${PATH_ROOTCOPY}"
    	    [[ -w ${PATH_ROOTCOPY} ]] || return 1
        
	    cd ${PATH_CHANGES}
#	    find etc/ -type f -name ld.so.cache -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find etc/udev/ -type f -name hwdb.bin -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find usr/lib/gdk-pixbuf-*/*/ -type f -name loaders.cache -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find usr/lib/locale/ -type f -name locale-archive -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find usr/lib/modules/*/ -type f -name modules. -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find usr/share/applications/ -type f -name mimeinfo.cache -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find usr/share/fonts/ -type f \( -name "fonts.dir" -o  -name "fonts.scale" \) -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find usr/share/glib-*/schemas/ -type f -name gschemas.compiled -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find usr/share/icons/ -type f -name icon-theme.cache -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find usr/share/mime/ -maxdepth 1 -type f -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	    find var/cache/fontconfig/ -type f -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
#	    find var/cache/ldconfig/ -type f -name aux-cache -exec install -CDm600 {} ${PATH_ROOTCOPY}/{} \;
#	    install -dm700 ${PATH_ROOTCOPY}/var/cache/ldconfig
	    cp -af etc/pacman.d ${PATH_ROOTCOPY}/etc/
	    cp -af etc/ssh ${PATH_ROOTCOPY}/etc/
	elif [[ ${SAVE_ALL_CACHE} == "modules" ]]; then
	    PATH_MODULES=$(find /memory/layer-base/*/ -maxdepth 1 -type d -name "${NAME_MODULES}" | head -1)
	    [[ -n ${PATH_MODULES} ]] || PATH_MODULES="$(find /memory/layer-base/*/ -maxdepth 1 -type f -name "ublinux-data*.sgn" | head -1)"
	    [[ -n ${PATH_MODULES} ]] && PATH_MODULES="${PATH_MODULES%/*}/${NAME_MODULES}" || return 0
	    [[ -n ${PATH_MODULES} ]] || install -dm0755 -o root -g root "${PATH_MODULES}"
	    [[ -w ${PATH_MODULES} ]] || return 1
# TODO    	    
	fi
    fi
}

################
##### MAIN #####
################

    [[ ${SYSTEMBOOT_STATEMODE} == "changes" ]] && exit 0
    exec_save_all_cache $@
