#!/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"
elif [ "$NUM_ALL_DRIVES" == "2" ];then
	DEL_CMD="d\n2\nd\n"
elif [ "$NUM_ALL_DRIVES" == "3" ];then
	DEL_CMD="d\n3\nd\n2\nd\n"
elif [ "$NUM_ALL_DRIVES" == "4" ];then
	DEL_CMD="d\n4\nd\n3\nd\n2\nd\n"
else
	DEL_CMD=""
fi

echo "Format EMMC Start..."  > /dev/tty1
echo -e "${DEL_CMD}n\np\n1\n\n+100M\nn\np\n2\n\n+2048M\nn\np\n3\n\n\nt\n1\nc\na\n1\np\nw\n"| fdisk ${DRIVE}
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.ext4 -F ${DRIVE}p2
until [ "$?" == 0 ]; do
	sleep 8
done
mkfs.ext4 -F ${DRIVE}p3
until [ "$?" == 0 ]; do
	sleep 8
done
echo "Format Disk End..." > /dev/tty1
sleep 8

mkdir -p /mnt/boot
mkdir -p /mnt/root
mkdir -p /mnt/home
mkdir -p /mnt/image

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

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

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

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

cd /mnt/image

if [ ! -d "linux" ]; then
	echo "Linux Image Folder not found"  > /dev/tty1
	exit 1
else
	echo "Start Copy Linux Image..." > /dev/tty1
	cd linux/
	if [ -d "boot" ]; then
		echo "Copy Boot..." > /dev/tty1
		cp boot/* /mnt/boot
	else
		echo "Can Not Find Boot..." > /dev/tty1
		exit 1
	fi

	if [ -d "rootfs" ]; then
		echo "Copy Rootfs..." > /dev/tty1
		cd rootfs
		tar -xf wt2416-rootfs.tar --strip-components 1 -C /mnt/root
		tar -xf wt2416-usrfs.tar --strip-components 1 -C /mnt/home
	else
		echo "Copt Rootfs End..." > /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/root
umount /mnt/home
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
