#!/bin/sh

functions=/lib/rcscripts/sh/fsupgrade-functions.sh
[ -f $functions ] && . $functions || {
	echo "Failed to source helper functions!"
	exit 1
}

mounted() {
	_cmd='_res=$(mount | grep "on[[:blank:]]\+$1[[:blank:]]") || _err=$?'
	eval $_cmd
	if [ $_err -ne 0 ]; then
		return 1
	fi
	return 0
}

IVDIR=/usr/local
IVDEV=/dev/part/iv
FSTYPE=ubifs


printf "Erasing $IVDIR\n"
DOMOUNT=n
if mounted $IVDIR; then
	unmount $IVDIR || error "skipping erase of $IVDIR"
	DOMOUNT=y
fi

if [ -c $IVDEV ]; then
	ubiupdatevol --truncate $IVDEV || warning "failed erasing $IVDIR"
fi

if [ $DOMOUNT = y ]; then
	mmount -t $FSTYPE $IVDEV $IVDIR || warning "failed to mount $IVDIR after erase"
fi