#!/usr/bin/env bash
#
# Initial script for UBLinux
# This script are launching before starting init from initrd script
# Current dir allways must be set to root (/)
# All system path must be relative, except initrd dirs

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

PATH=.:/:/usr/bin:/usr/local/bin:/usr/local/sbin

[[ -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=${SYSCONF}/config; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null

    find /memory/layer-base/ -maxdepth 2 -path "*" -iname "ublinux-data*.sgn" | while read FIND_FILESGN; do 
	mkdir -p ${FIND_FILESGN%/*}/backgrounds/ublinux
    done
    [[ -d ${ROOTFS}/usr/share/backgrounds ]] || mkdir -p ${ROOTFS}/usr/share/backgrounds
    if [[ $(cmdline_parameter ub.mask) == "" || $(cmdline_parameter ub.hide) == "" ]]; then 
    # With unmasked /memory/* , without parametre kernel ub.mask ub.hide
        find /memory/layer-base/ -maxdepth 3 -path "*/backgrounds/*" -exec ln -fs {} ${ROOTFS}/usr/share/backgrounds/ \;
    else
    # With masked /memory/* , with parametre kernel ub.mask ub.hide
        find /memory/layer-base/ -maxdepth 3 -path "*/backgrounds/*" -exec cp -fru {} ${ROOTFS}/usr/share/backgrounds/ \;
    fi
