#!/bin/sh

AMIROOT=`whoami | awk {'print $1'}`
if [ "$AMIROOT" != "root" ] ; then

        echo "  **** Error *** must run script with sudo"
        echo ""
        exit
fi

check_for_sdcards()
{
        # find the avaible SD cards
        ROOTDRIVE=`mount | grep 'on / ' | awk {'print $1'} |  cut -c6-8`
        PARTITION_TEST=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\<sd.\>\|\<mmcblk.\>' | grep -n ''`
        if [ "$PARTITION_TEST" = "" ]; then
                echo -e "Please insert a SD card to continue\n"
                while [ "$PARTITION_TEST" = "" ]; do
                        read -p "Type 'y' to re-detect the SD card or 'n' to exit the script: " REPLY
                        if [ "$REPLY" = 'n' ]; then
                            exit 1
                        fi
                        ROOTDRIVE=`mount | grep 'on / ' | awk {'print $1'} |  cut -c6-8`
                        PARTITION_TEST=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\<sd.\>\|\<mmcblk.\>' | grep -n ''`
                done
        fi
}

check_for_sdcards

echo -e "\nAvailable Drives to write images to: \n"
DEVICEDRIVENAME=`cat /proc/partitions | grep -i boot | awk {'print $4'} | cut -c1-7 | uniq`
IMAGEDEVICE=`cat /proc/partitions|grep -v $DEVICEDRIVENAME|awk {'print $4'}|grep mmc| cut -c1-7 | uniq`

DRIVE=/dev/$DEVICEDRIVENAME
IMAGEDRIVE=/dev/$IMAGEDEVICE

echo $DRIVE
echo $IMAGEDRIVE

if [[ ${DEVICEDRIVENAME} = ^sd. ]]; then
        echo "$DRIVE is an sdx device"
        P=''
	NUM_ALL_DRIVES=`cat /proc/partitions | grep ${DEVICEDRIVENAME} | wc -l`
else
        echo "$DRIVE is an mmcblkx device"
        P='p'
	NUM_ALL_DRIVES=`cat /proc/partitions | grep ${DEVICEDRIVENAME}$P | wc -l`
fi

echo $NUM_ALL_DRIVES
DEL_CMD=""
if [ "$NUM_ALL_DRIVES" == "1" ];then
	DEL_CMD="d\n"
fi

echo "Format EMMC Start..." > /dev/tty1
dd if=/dev/zero of=${DRIVE} bs=1024 count=1

sfdisk --force -uM ${DRIVE} << EOF
,500,0c,*
600,,0c
EOF

sleep 5
echo "Format EMMC End..." > /dev/tty1

echo "Format Disk Start..." > /dev/tty1
mkfs.vfat -F 32 ${DRIVE}p1
until [ "$?" == 0 ]; do
	sleep 8
done

mkfs.vfat -F 32 ${DRIVE}p2
until [ "$?" == 0 ]; do
	sleep 8
done
echo "Format Disk End..." > /dev/tty1
sleep 8

mkdir -p /mnt/boot
mkdir -p /mnt/image

if [ ! -d "/mnt/boot" ]; then
	mkdir -p /mnt/boot
else
	echo "mount ${DRIVE}p1 on /mnt/boot" > /dev/tty1
	mount ${DRIVE}p1 /mnt/boot
fi

if [ ! -d "/mnt/image" ]; then
	mkdir -p /mnt/image
else
	echo "mount ${IMAGEDRIVE}p1 on /mnt/image" > /dev/tty1
	mount ${IMAGEDRIVE}p1 /mnt/image
fi

cd /mnt/image

if [ ! -d "wince" ]; then
	echo "Wince Image Folder not found" > /dev/tty1
	exit 1
else
	echo "Start Copy Wince Image..." > /dev/tty1
	cd wince/
	if [ -f "MLO" ]; then
		echo "Copy MLO..." > /dev/tty1
		cp MLO /mnt/boot
	else
		echo "Can Not Find MLO..." > /dev/tty1
		exit 1
	fi
	if [ -f "logo.bmp" ]; then
		echo "Copy logo.bmp..." > /dev/tty1
		cp logo.bmp /mnt/boot
	else
		echo "Can Not Find logo.bmp..." > /dev/tty1
	fi
	if [ -f "EBOOTSD.nb0" ]; then
		echo "Copy EBOOTSD..." > /dev/tty1
		cp EBOOTSD.nb0 /mnt/boot
	else
		echo "Can Not Find EBOOTSD.nb0..." > /dev/tty1
		exit 1
	fi
	if [ -f "NK.bin" ]; then
		echo "Copy NK.bin..." > /dev/tty1
		cp NK.bin /mnt/boot
	else
		echo "Can Not Find NK.bin..." > /dev/tty1
		exit 1
	fi
fi

echo "Sync files..." > /dev/tty1
sync
sync
sync

cd ~
echo "Image Done..." > /dev/tty1
umount /mnt/boot
umount /mnt/image

#buzzer
echo 0 > /sys/class/pwm/pwmchip0/export
sleep 1
echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/period
echo 500000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
sleep 10
echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable
echo 7 > /sys/class/backlight/backlight/brightness
