#!/bin/sh
#
# Removes firmware completely from NAND
#

echo "---- Removing firmware -----------------"

echo "stopping anthill services"
/etc/init.d/S71monitor stop
/etc/init.d/S70miner stop

if grep config /proc/mounts; then
	echo "unmounting config partition"
	umount /config
fi

if [ -d /nvdata/anthillos ]; then
	echo "cleaning filesystem"

	if [ "${REMOVE_STOCK_LOGS}" = "true" ]; then
    	for f in /nvdata/*; do
    		if [ "$(basename "$f")" != "mbin.data" ]; then
    		    rm -rf "$f"
    		fi
		done
	else
		rm -rf /nvdata/anthillos
	fi
fi

# Remove logs
[ -d /tmp/log/miner ] && rm -rf /tmp/log/miner
[ -d /tmp/log/dashd ] && rm -rf /tmp/log/dashd

if ls "/tmp/log/hwscan*" 1> /dev/null 2>&1; then
    rm "/tmp/log/hwscan*"
fi

sync

echo "Firmware successfully removed"
reboot