#!/bin/sh
SYSCFG_UBIFS_MNT=/tmp/syscfg
. /lib/functions.sh

# mtdpart: the ubifs syscfg partition
# rom: the read-only rootfs mount point
# overlay: the overlay upper directory
pre_check() {
	# return 1 on failed
	grep -qs ubifs /proc/filesystems ||  return 1
	grep -qs overlay /proc/filesystems ||  return 1
	[ ! -e $SYSCFG_UBIFS_MNT ] && mkdir -p $SYSCFG_UBIFS_MNT

	mtdpart="$(find_mtd_part rootfs_data)"
	[ -z "$mtdpart" ] && return 1
	mtdpart_idx="$(echo $mtdpart | tr -d "/dev/mtdblock")"

	rom=$(awk '/jffs2 ro/ {print $2}' /proc/mounts)
	overlays=$(awk '/ubifs/ {print $2}' /proc/mounts | grep overlay)
	return 0
}

clean_mounted_overlayfs(){
	if [ ! -z "$overlays" ]
	then
		echo "first stop respawn app..."
		/etc/init.d/odhcpd stop
		/etc/init.d/log stop
		/etc/init.d/dnsmasq stop
		/etc/init.d/network stop
		/etc/init.d/services.init stop
		/etc/init.d/cm.init stop
		/etc/init.d/sdcard_mount stop

		echo "then stop other left app..."
		ps | sed '/ash\|PID\|firstboot\|sed/d;/\[.*\]$/d;s/^ \+//;s/ .*//;/^'$$'$/d;/^'$PPID'$/d' | xargs kill -9
		sleep 2

		echo "start to umount overlay-fs..."
		if [ -d /NVM/*data ]; then
			/bin/umount /NVM/*data
		fi
		if [ -d /usr/*web ]; then
			/bin/umount /usr/*web
		fi
		rm -rf /data/*
		/bin/umount /data /mnt /log
		/bin/umount /etc
		/bin/umount /NVM
		for overlay in $overlays; do
			echo -n "delete all file under $overlay/ ... "
			rm -rf $overlay/*
			/bin/umount $overlay
		done
		echo "done"
		return 0
	fi
}

__try_ubifs_syscfg_mount() {
	overlay_mountpoint=$1
	if [ -z $overlay_mountpoint ]
	then
		overlay_mountpoint=/overlay
	fi
	recover_ubifs=0
	[ ! -e /dev/ubi0 ] && ubiattach /dev/ubi_ctrl -m $mtdpart_idx -d 0 || recover_ubifs=1
	if [ $recover_ubifs -eq 0 ]
	then
		ubi0_nod_id=`cat /sys/class/ubi/ubi0/dev | tr -s ":" " "`
		[ ! -e /dev/ubi0 ] && mknod /dev/ubi0 c ${ubi0_nod_id}
		if [ ! -e /sys/class/ubi/ubi0_0/dev ]
		then
			# no volume
			recover_ubifs=1
		else
			# check for "data" volume
			ubi0_0_nod_id=`cat /sys/class/ubi/ubi0_0/dev | tr -s ":" " "`
			[ ! -e /dev/ubi0_0 ] && mknod /dev/ubi0_0 c ${ubi0_0_nod_id}
			{ ubinfo /dev/ubi0_0 | grep Name  | grep -qs "data" ; } || \
			recover_ubifs=1
		fi
	fi
	if [ $recover_ubifs -eq 1 ]
	then
		echo "ubifs syscfg partition is damaged"
		echo "try to recover by formatting $mtdpart..."
		[ -e /dev/ubi0 ] && ubidetach -m $mtdpart_idx
		ubiformat -y -q /dev/mtd$mtdpart_idx
		ubiattach -m $mtdpart_idx /dev/ubi_ctrl
		ubi0_nod_id=`cat /sys/class/ubi/ubi0/dev | tr -s ":" " "`
		[ ! -e /dev/ubi0 ] && mknod /dev/ubi0 c ${ubi0_nod_id}
		ubimkvol /dev/ubi0 -n 1 -N etc -t dynamic -s 2MiB
		ubimkvol /dev/ubi0 -n 0 -N data -t dynamic --maxavsize
	fi

	# finally mount the ubifs
	mount -t ubifs -o noatime ubi0:data /data || return 1
	mount -t ubifs -o noatime ubi0:data /mnt || return 1
	mount -t ubifs -o noatime ubi0:data /log || return 1
	mount -t ubifs -o noatime ubi0:etc $overlay_mountpoint/etc || return 1
	return 0
}

try_ubifs_syscfg_mount() {
	__try_ubifs_syscfg_mount || {
		echo "roofs_data mount fail, try to recover by erase..."
		mtd erase rootfs_data
		__try_ubifs_syscfg_mount
	}

	return 0
}

if [ "$1" != "-y" ]
then
read -p "This will erase all settings and remove any installed packages. Are you sure? [N/y]" answer
case $answer in
	[Yy]* ) break;;
	[Nn]* ) exit 0;;
	* )     exit 0;;
esac
fi

pre_check
if [ ! -z "$overlays" ]
then
	echo "overlayfs is mounted"
	clean_mounted_overlayfs
else
	echo "try to mount overlayfs"
	try_ubifs_syscfg_mount || exit 1
	overlays=$(awk '/ubifs/ {print $2}' /proc/mounts | grep overlay)
	clean_mounted_overlayfs
fi

