#!/bin/sh
#
# UIRD Init script
# Author: Anton Goroshkin <http://magos-linux.ru>
#

. /livekitlib

#. /lib/magos-lib.sh

#transfer_initramfs

UNION=$NEWROOT

MEMORY=/memory
BUNDLES=$MEMORY/bundles
CHANGES=$MEMORY/changes
MACHINES=$MEMORY/machines
DATAMNT=$MEMORY/data
COPY2RAM=$MEMORY/copy2ram
LAYER_BASE=$MEMORY/layer-base
LAYER_CACHE=$MEMORY/layer-cache
LAYER_HOMES=$MEMORY/layer-homes
#MOUNTDIR=$DATAMNT
MOUNTDIR=$LAYER_BASE

LIVEKITNAME="MagOS"

echo "0" >/proc/sys/kernel/printk

#loadfont < /usr/lib/consolefonts/UniCyr_8x14.psf
loadfont < /usr/lib/consolefonts/ter-u16n.psf >/dev/null
loadkeys /usr/lib/kbd/ru.map >/dev/null

header "Запускается UIRD init <http://magos-linux.ru/>"

debug_start
debug_shell

init_devs
debug_shell

# start quickshell
start_quickshell
debug_shell

# Resume from swsuspend
resume_from_suspend
debug_shell

mkdir -p $MEMORY

getcmdline >/memory/cmdline
cat /basecfg.ini >/memory/basecfg.ini 2>/dev/null

RAMSIZE=$(cmdline_value uird.ramsize)
if [ "$RAMSIZE" = "" ]; then RAMSIZE="70%"; fi
mount -t tmpfs -o "size=$RAMSIZE" tmpfs $MEMORY

mkdir -p $MOUNTDIR $CHANGES

getcmdline >/memory/cmdline
cat /basecfg.ini >/memory/basecfg.ini 2>/dev/null


# find data dir with filesystem bundles
#DATA="$(find_data 60 "$DATAMNT")"

#init BASE layer
[ "$(cmdline_value uird.from)" ] && init_layer $DATAMNT/from $LAYER_BASE uird.from "check_SGN"
debug_shell
# init CACHE layer
[ "$(cmdline_value uird.cache)" ] && init_layer $DATAMNT/cache $LAYER_CACHE uird.cache
debug_shell
# init HOMES layer
[ "$(cmdline_value uird.homes)" ] && init_layer $DATAMNT/homes $LAYER_HOMES uird.homes
debug_shell

#setting up config
setup_config
debug_shell

#setting up source for changes
setup_changes $DATAMNT/changes $CHANGES
debug_shell
#setting up machines
setup_machines $MACHINES $DATAMNT/machines $CHANGES $DATAMNT/changes
debug_shell

# sync data from BASE layer to CACHE layer
#for d_dirs in "$LAYER_BASE" ;do
    # copy to CACHE if needed (uird.copy2cache=)
#    copy_to_cache $d_dirs "$LAYER_CACHE"
#done
[ "$(cmdline_value uird.from)" ] && copy_to_cache $LAYER_BASE "$LAYER_CACHE"
debug_shell
# sync data from BASE,CACHE layers to RAM
#for d_dirs in "$LAYER_BASE" "$LAYER_CACHE" ;do
    # copy to RAM if needed (uird.copy2ram=)
#    copy_to_ram $d_dirs "$COPY2RAM"
#done
[ "$(cmdline_value uird.from)" ] && copy_to_ram $LAYER_BASE "$COPY2RAM"
debug_shell
[ "$(cmdline_value uird.cache)" ] && copy_to_ram $LAYER_CACHE "$COPY2RAM"
debug_shell
# setup persistent changes, if possible
#persistent_changes "$DATA" "$CHANGES"
#                                                                                                                                                      debug_shell

# init aufs filesystem
init_aufs

# init aufs union
init_union "$CHANGES" "$UNION"

debug_shell
# add data to union
#for d_dirs in "$COPY2RAM" "$LAYER_CACHE" "$LAYER_BASE"  ;do
#    union_append_bundles $d_dirs "$BUNDLES" "$UNION"
#done

[ "$(cmdline_value uird.copy2ram)$(cmdline_parameter uird.copy2ram)" ] && union_append_bundles $COPY2RAM "$BUNDLES" "$UNION"
debug_shell
[ "$(cmdline_value uird.cache)" ] && union_append_bundles $LAYER_CACHE "$BUNDLES" "$UNION"
debug_shell
[ "$(cmdline_value uird.from)" ] && union_append_bundles $LAYER_BASE "$BUNDLES" "$UNION"
debug_shell

#setup homes by using aufs
setup_homes "$LAYER_HOMES" "$UNION/home"                                                                                                                                                      debug_shell
debug_shell

# rootcopy
#copy_rootcopy_content "$DATA" "$UNION"

echo_green_star
echolog "Обновление файла /etc/fstab и директории /mnt "
touch $UNION/etc/fstab >/dev/null
rmdir $UNION/mnt/* 2>/dev/null
fstab_update $UNION
debug_shell

# old compatibility to linuxlive init
mkdir -p $UNION/mnt/live/memory $UNION/mnt/live/etc
cp liblinuxlive $UNION/mnt/live/liblinuxlive
cp /etc/resolv.conf $UNION/mnt/live/etc/ 2>/dev/null
[ -d "/memory/data/from/0" ] && ln -sf /memory/data/from/0 $UNION/mnt/livemedia
[ -d "/memory/data/from/1" ] && ln -sf /memory/data/from/1 $UNION/mnt/livedata
[ -d "/memory/bundles" ] && ln -sf /memory/bundles $UNION/mnt/live/memory/images
debug_shell

cd $UNION
echo_green_star
echolog "Выполняются  rc.preinit скрипты"
[ -x "etc/rc.d/rc.preinit" ] && /bin/bash etc/rc.d/rc.preinit
debug_shell
mv /var/log/magos $UNION/var/log/ 2>/dev/null

#need for usable_root of dracut
mkdir -p $UNION/proc $UNION/sys $UNION/dev $UNION/$MEMORY

#need TODO
mount --move $MEMORY $UNION/$MEMORY

header "UIRD init завершил работу , запускается $LIVEKITNAME"
debug_shell

shell_cmd
