#!/bin/sh

. kuro_lib

# $1 : Error type {mounted | formatted}
ExitWithError(){
	miconapl -a led_set_cpu_mcon diag
	miconapl -a led_set_on_off diag
	miconapl -a led_set_brink diag
	miconapl -a bz_on on3off3
	case $1 in
	mounted)
		echo "disk is already mounted!" > /dev/console
		;;
	formatted)
		echo "disk is already formatted!" > /dev/console
		;;
	esac
	sleep 5
	miconapl -a bz_on
	miconapl -a led_set_brink off
	miconapl -a led_set_on_off off
	miconapl -a led_set_cpu_mcon off
	exit 1
}

# Check point 1. disk is already mounted or not.
IsMounted ${DISK1_DEV} ; [ $? = 1 ] && ExitWithError mounted
IsMounted ${DISK1_DEV}1 ; [ $? = 1 ] && ExitWithError mounted
IsMounted ${DISK1_DEV}4 ; [ $? = 1 ] && EixtWithError mounted

# Check point 2. disk is already formatted or not.
XFS_FORMATTED=`dd if=${DISK1_DEV} bs=1 count=3` ; [ "${XFS_FORMATTED}" = XFS ] && ExitWithError formatted
XFS_FORMATTED=`dd if=${DISK1_DEV}1 bs=1 count=3` ; [ "${XFS_FORMATTED}" = XFS ] && ExitWithError formatted
XFS_FORMATTED=`dd if=${DISK1_DEV}4 bs=1 count=3` ; [ "${XFS_FORMATTED}" = XFS ] && ExitWithError formatted

# OK! Disk is not mounted and not formatted.

miconapl -a led_set_code_information 28
miconapl -a bz_on button

fdisk ${DISK1_DEV} < /usr/local/bin/PartitionDefinition
Format_EXT3 ${DISK1_DEV}1 
Format_EXT3 ${DISK1_DEV}2
Format_EXT3 ${DISK1_DEV}4
mkswap ${DISK1_DEV}3

./SetupDevEnv.sh

miconapl -a led_set_code_information clear
