#!/bin/sh

SDCARD=/dev/mmcblk1p1

start()
{
	# try to check and restore the SD card
	echo "check and repair SD card..."
	fsck.vfat -a $SDCARD
	echo "check and repair SD card: done"

	# mount SD card to /Core
	mount $SDCARD /Core

	# show startup logo on screen
	fbv -s 1 /Core/Startup/*.png > /dev/null 2>&1
	
	# Files recovered by the fsck.vfat can be deleted, since their full recovery
	# in automatic mode does not seem possible.
	printf "delete FSCK*.REC files: "
	rm -f /Core/FSCK*.REC
	echo "done"

	printf "Starting Core: "
	
	# mount the Linux Application Partition to /app
	mount -t ubifs ubi0:application /app

	# start and setup ethernet
	ifconfig eth0 192.168.0.83 netmask 255.255.255.0 up

	# start and set up CAN Bus
	ip link set can0 up type can bitrate 1000000

	# got to top level directory
	cd /

	# start up CORE system
	sh /app/CoreStart/corestart.sh &
	#./app/PBX32LinuxApp &
	
	echo "OK"
}

stop() {
	printf "Stopping Core: "
	umount /app
	umount /Core
	echo "OK"
}

case "$1" in
  start)
 	start
	;;
  stop)
	stop
	;;
  *)
	echo "Usage: $0 {start|stop}"
	exit 1
esac

exit $?
