#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

resize_enabled() {
    return 0
}

resize_run() {
    ln -s /proc/mounts /etc/mtab

    if [ -n "$ROOTFS_DIR" ]; then
        if [ ! -e $ROOTFS_DIR/etc/.resized ]
        then
            # check command line to now storage device used
            if [ -n "$bootparam_root" ]; then
                debug "No e2fs compatible filesystem has been mounted, mounting $bootparam_root..."

                if [ "`echo ${bootparam_root} | cut -c1-5`" = "UUID=" ]; then
                    root_uuid=`echo $bootparam_root | cut -c6-`
                    bootparam_root="/dev/disk/by-uuid/$root_uuid"
                elif [ "`echo ${bootparam_root} | cut -c1-9`" = "PARTUUID=" ]; then
                    root_partuuid=`echo $bootparam_root | cut -c10-`
                    bootparam_root="/dev/disk/by-partuuid/$root_partuuid"
                elif [ "`echo ${bootparam_root} | cut -c1-10`" = "PARTLABEL=" ]; then
                    root_partlabel=`echo $bootparam_root | cut -c11-`
                    bootparam_root="/dev/disk/by-partlabel/$root_partlabel"
                elif [ "`echo ${bootparam_root} | cut -c1-6`" = "LABEL=" ]; then
                    root_label=`echo $bootparam_root | cut -c7-`
                    bootparam_root="/dev/disk/by-label/$root_label"
                fi
                if [ -e "$bootparam_root" ]; then
                    bootparam_root_device=$(busybox readlink $bootparam_root -f)
                    j=$(echo $bootparam_root_device | sed "s|/dev/mmcblk\([0-2]\)p.*|\1|")
                    for i in 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20;
                    do
                        DEVICE="/dev/mmcblk"$j"p"$i
                        if [ -e $DEVICE ]; then
                            label=$(/sbin/e2label $DEVICE 2> /dev/null)
                            if [ $? -eq 0 ]; then
                                case $label in
                                user*)
                                    # userfs is shared across both RAUC A/B
                                    # rootfs banks, but the $ROOTFS_DIR/etc/
                                    # .resized marker gating this whole loop
                                    # lives inside whichever rootfs is
                                    # currently booting - a freshly OTA-
                                    # installed bank has never set it, so
                                    # without its own check userfs's full
                                    # e2fsck -c bad-blocks scan re-runs on
                                    # every OTA update's first boot into the
                                    # new bank, even though userfs was
                                    # already grown on the very first boot
                                    # long before that bank existed. Check
                                    # the same marker resize-helper.service
                                    # uses post-boot (a file resident on
                                    # userfs itself, so it survives an OTA
                                    # update to either bank) before doing
                                    # any of this.
                                    userfs_already_resized=0
                                    mkdir -p /tmp/userfs-check
                                    if mount -o ro $DEVICE /tmp/userfs-check 2> /dev/null; then
                                        [ -f /tmp/userfs-check/.resize-helper-done ] && userfs_already_resized=1
                                        umount /tmp/userfs-check
                                    fi
                                    rmdir /tmp/userfs-check 2> /dev/null
                                    if [ "$userfs_already_resized" = "1" ]; then
                                        echo "SKIP USERFS [$DEVICE] (already resized)"
                                    else
                                        echo "RESIZE USERFS [$DEVICE]"
                                        # No -c (bad-blocks) pass: it does a full
                                        # raw read of every sector, which on a
                                        # truly virgin, full-size userfs (~22-26GB)
                                        # takes minutes - long enough that OP-TEE's
                                        # own watchdog (stm32_iwdg) panics and
                                        # resets the SoC before it finishes, since
                                        # nothing feeds that watchdog this early in
                                        # boot (confirmed on hardware: a real reset
                                        # loop on first boot into a freshly-flashed
                                        # board, stuck at ~2.68% after 8s - a full
                                        # pass would take ~5 minutes). eMMC/SD flash
                                        # remaps bad physical blocks internally via
                                        # its own FTL and doesn't expose them to the
                                        # OS the way a rotational disk would, so this
                                        # check has little practical value here
                                        # anyway. A single fast consistency-only
                                        # pass isn't enough on a never-before-checked
                                        # filesystem: when it actually fixes something
                                        # (common on a fresh image's very first real
                                        # check, reported as "FILE SYSTEM WAS
                                        # MODIFIED"), resize2fs refuses to run against
                                        # it ("Please run 'e2fsck -f' first") even
                                        # though e2fsck just ran - it wants a
                                        # subsequent clean pass before trusting the
                                        # filesystem enough to resize it. Run it twice.
                                        /sbin/e2fsck -f -y -C 0 $DEVICE
                                        /sbin/e2fsck -f -y -C 0 $DEVICE
                                        /sbin/resize2fs $DEVICE
                                        # Deliberately NOT touching the marker here.
                                        # On the SD card build, this resize2fs call
                                        # cannot actually grow userfs - the GPT
                                        # partition itself is fixed-size here, and
                                        # only resize-helper.sh (which runs later,
                                        # in the real rootfs, with sgdisk/resizepart
                                        # available) can extend that partition table
                                        # entry before resizing the filesystem into
                                        # it. This initrd environment doesn't have
                                        # those tools, and this stage has no way to
                                        # tell "grew successfully" apart from "already
                                        # at its (still wrong) size, nothing to do."
                                        # Marking done here regardless of which one
                                        # actually happened let this script win the
                                        # race against resize-helper.sh - it always
                                        # runs first - permanently skipping the one
                                        # place that could actually fix userfs on SD
                                        # card. Leave setting the marker entirely to
                                        # resize-helper.sh, the only script that can
                                        # verify (and, on SD card, ensure) it's true.
                                    fi
                                    ;;
                                root*)
                                    echo "SKIP ROOTFS [$DEVICE] (fixed-size A/B slot)"
                                    ;;
                                vendor*)
                                    echo "SKIP VENDORFS [$DEVICE] (fixed-size A/B slot)"
                                    ;;
                                boot*)
                                    echo "SKIP BOOTFS [$DEVICE] (fixed-size A/B slot)"
                                    ;;
                                *)
                                    ;;
                                esac
                            fi
                        fi
                    done
                    touch $ROOTFS_DIR/etc/.resized
                fi
            fi
        fi
    fi
}
