# Tiny Cloud - Early Phase Functions
# vim:set ts=4 et ft=sh:

: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR"/tiny-cloud/init-common

expand_root() {
    skip_action expand_root && return

    local dev=$(awk '$2 == "/" {print $1}' "$ROOT"/proc/mounts)
    local partition=$(cat "$ROOT/sys/class/block/${dev#/dev/}/partition" 2>/dev/null)

    if [ -n "$partition" ]; then
        # it's a partition, resize it
        local volume=$(readlink -f "$ROOT/sys/class/block/${dev#/dev/}/..")
        volume="/dev/${volume##*/}"
        echo ", +" | $MOCK sfdisk -q --no-reread -N "$partition" "$volume"
        $MOCK partx -u "$volume"
    fi
    # resize filesystem
    $MOCK mount -orw,remount /
    $MOCK resize2fs "$dev"
}

has_cloud_hotplugs() { [ -n "$HOTPLUG_MODULES" ]; }

install_hotplugs() {
    skip_action install_hotplugs && return

    local result rc=0

    for module in $HOTPLUG_MODULES; do
        result='-'
        printf " $module"
        if type "mod__$module" | grep -q -w "function"; then
            "mod__$module" && result='+' || { result='!'; rc=1; }
        fi
        printf "($result)"
    done
    return $rc
}

: "${HOTPLUG_TYPE:=mdev}"
if [ -f "$LIBDIR"/tiny-cloud/"$HOTPLUG_TYPE" ]; then
	. "$LIBDIR"/tiny-cloud/"$HOTPLUG_TYPE"
fi
