#!/bin/sh

storage_enabled() {
    return 0
}

function get_slot
{
    for i in `cat /proc/cmdline`; do
        if [[ $i == bootslot=* ]]; then
            CURRENT_SLOT="${i: -1}"
        fi;
    done
    [ "$CURRENT_SLOT" = "1" ] || CURRENT_SLOT="0";
}

# run module post-rootfs to load the disks.
storage_overlay_hook() {
	status=$1
	module=$2

    [ "$module" = "rootfs" ] || return 0
    debug "storage_overlay_hook running $1 $module"

    get_slot
    #install modules.squashfs in rootfs
    mounts=""
    for f in /storage/bsp/$CURRENT_SLOT/mounts/*.squashfs; do
        [ -f "$f" ] || continue
        f=$(basename -s .squashfs $f)
        mkdir -p /volumes/$f
        mount -t squashfs -o loop /storage/bsp/$CURRENT_SLOT/mounts/$f.squashfs /volumes/$f && mounts="/volumes/$f:$mounts"
    done
    overlaymod=$(find /lib/modules -name overlay.ko | head -n 1)
    insmod $overlaymod || info "Failed to load overlay module"
    # clear the overlay if the clear_overlay file exists
    if [ -f /storage/clear_overlay ]; then
        info "Clearing overlay"
        rm -rf /storage/overlay/upper/*
        rm -rf /storage/overlay/work/*
        rm -rf /storage/containers/*
        rm -rf /storage/config/pre-clear-persist || debug "No pre-clear-persist to remove"
        mv -f /storage/config/persist /storage/config/pre-clear-persist
        rm -rf /storage/clear_overlay
    fi
    mkdir -p /storage/overlay/upper
    mkdir -p /storage/overlay/work
    mkdir -p /rofs && mount --move $ROOTFS_DIR /rofs
    mount -t overlay -o index=off,metacopy=off,lowerdir=${mounts}/rofs,upperdir=/storage/overlay/upper,workdir=/storage/overlay/work overlay "$ROOTFS_DIR"
    if [ $? -ne 0 ]; then
        info "Failed to mount overlay for /. Clearing overlay."
        mv -f /storage/overlay/upper /storage/overlay/failed_upper && mkdir -p /storage/overlay/upper
        rm -rf /storage/overlay/work/*
        mount -t overlay -o index=off,metacopy=off,lowerdir=/rofs,upperdir=/storage/overlay/upper,workdir=/storage/overlay/work overlay "$ROOTFS_DIR" || mount --move /rofs $ROOTFS_DIR
    fi
    mkdir -p ${ROOTFS_DIR}/rofs && mount -o bind /rofs ${ROOTFS_DIR}/rofs || info "Failed to bind /rofs to ${ROOTFS_DIR}/rofs"
    if [ -f "/boot/updated${CURRENT_SLOT}" ]; then
        for f in /storage/config/persist/* ; do
            cp -fr -t "/$ROOTFS_DIR" "$f"
        done
        mv -f "/boot/updated${CURRENT_SLOT}" "/boot/update_booted${CURRENT_SLOT}"
    fi
    # move the storage mount to the rootfs
    [ -d /$ROOTFS_DIR/storage ] && mount -n --move /storage "$ROOTFS_DIR/storage"
}

storage_run() {
    # add function to add overlay after rootfs
    add_module_post_hook "storage_overlay_hook"

    # wait for disks to become available
	C=0
	delay=${bootparam_rootdelay:-1}
	timeout=${bootparam_roottimeout:-5}
	while true; do
		[ $(( $C * $delay )) -gt $timeout ] && fatal "root '$bootparam_root' doesn't exist or does not contain a /dev."

        (findfs LABEL=storage || findfs PARTLABEL=storage) && break

		debug "Sleeping for $delay second(s) to wait for disk to settle..."
		sleep $delay
		C=$(( $C + 1 ))
	done

    # mount boot partition
    get_slot
    mkdir -p /boot
    fsck.ext4 -p /dev/disk/by-label/boot
    mount /dev/disk/by-label/boot    /boot    || mount /dev/disk/by-partlabel/boot    /boot

    mkdir -p /storage
    fsck.ext4 -p /dev/disk/by-label/storage
    mount /dev/disk/by-label/storage /storage || mount /dev/disk/by-partlabel/storage /storage
}
