#! /bin/sh

get_slot() {
    for i in $(cat /proc/cmdline); do
        case $i in bootslot=*) CURRENT_SLOT="${i#*=}" ;; esac
    done
    # if recovery flag is set, use the other slot
    if [ "$CURRENT_SLOT" = "1" ]; then
        [ -f /boot/recovery ] && CURRENT_SLOT="0";
    else
        CURRENT_SLOT="0";
        [ -f /boot/recovery ] && CURRENT_SLOT="1";
    fi
}

get_slot

#check if 'recovery' in args
select='stable'
case "$@" in *recovery*) select='recovery' ;; esac

[ "$CURRENT_SLOT" = "1" ] && selection="-e ${select},slot0"
[ "$CURRENT_SLOT" = "0" ] && selection="-e ${select},slot1"

echo SWUPDATE_EXTRA_ARGS="${selection}" > /tmp/swupdate.env

# check if 'post' in args
case "$@" in *post*) 
    mv "/boot/update_booted$CURRENT_SLOT" "/boot/running$CURRENT_SLOT" || echo "no boot flag file" ;; 
esac