#!/bin/sh
# Copyright (C) 2016-2017 Algo Communication Products Ltd.

mkdir -p /apps
if [ -b /dev/mmcblk0p2 ]; then
	if ! mount | grep /apps > /dev/null; then
		e2fsck -y /dev/mmcblk0p2
		mount -t ext2 -o noatime /dev/mmcblk0p2 /apps
	fi
	if [ -d /apps/lost+found ]; then
		rm -rf /apps/lost+found/*
	fi
	if [ ! -d /apps/uiapp ]; then
		cp -r /opt/algo/uiapp /apps/
	fi
	if [ -d /opt/algo/uiapp/user ]; then
		rm -rf /opt/algo/ui/app/user
	fi
else
	if [ ! -L /apps/uiapp ]; then
		ln -sfn /opt/algo/uiapp /apps/uiapp
	fi
fi

if [ ! -L /apps/curapp ]; then
	ln -sfn /apps/uiapp/ /apps/curapp
fi
# remove link created by old firmware
rm -f /apps/uiapp/rc/images/curtheme

if [ ! -d /apps/curapp/user ]; then
	cp -r /apps/curapp/user-default /apps/curapp/user
fi

sync
