#! /bin/sh

if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    echo "$0 shall be run as root"
    exit 1
fi

. /etc/profile
PATH=/sbin:/bin:/usr/sbin:/usr/bin

if [ ! -e /boot/diya-config.txt ]; then
    # prepare env for recovery boot
    cat << EOF > /boot/diya-config.txt
recovery=true
recovery_image=recovery-${MACHINE}.cpio.gz
EOF
else
    sed -i 's/^.*recovery.*/recovery=true/g' /boot/diya-config.txt || \
                echo "recovery=true" >> /boot/diya-config.txt
fi
echo "Rebooting to recovery mode"
reboot