#!/usr/bin/env bash

ENABLED=yes
[ "$ENABLED" != "yes" ] && exit 0

. /usr/lib/ublinux/os-config

SOURCE=${SYSCONF}/config; [ -f ${SOURCE} ] && . ${SOURCE} 2>/dev/null

PATH_CHANGES="/memory/changes"
PATH_ROOTCOPY=$(find /memory/layer-base/*/ -maxdepth 1 -type d -name "rootcopy" | head -1)
[[ -n ${PATH_ROOTCOPY} ]] || PATH_ROOTCOPY="$(find /memory/layer-base/*/ -maxdepth 1 -type f -name "ublinux-data*.sgn" | head -1 | xargs dirname)/rootcopy"

    install -dm0400 -o root -g root "${PATH_ROOTCOPY}"
    if [[ ${SAVE_ALL_CACHE} == "rootcopy" && -w ${PATH_ROOTCOPY} ]]; then
	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 usr/share/mime/ -type f -name *.xml -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	find var/cache/fontconfig/ -type f -exec install -CDm644 {} ${PATH_ROOTCOPY}/{} \;
	install -dm700 ${PATH_ROOTCOPY}/var/cache/ldconfig
	find var/cache/ldconfig/ -type f -name aux-cache -exec install -CDm600 {} ${PATH_ROOTCOPY}/{} \;
	cp -au etc/pacman.d ${PATH_ROOTCOPY}/etc/
	cp -au etc/ssh ${PATH_ROOTCOPY}/etc/
    fi
