#!/bin/sh

source /usr/local/lib/utils

# Get a key
getkey() {
    local key=$(dd if=/dev/input/event0 bs=32 count=1 status=none | xxd -p -c 32 | cut -b 21-26)
    echo ${key}
}

# Display meny item
menu_display () {
    local entry=${1}

    #clear_notif
    case ${entry} in
	0)

	    # Information
	    message=" INFO"
	    ;;

	1)
	    # USB mount/unmount
	    mount | grep -q /dev/mmcblk0p4
	    if [ $? -eq 0 ]; then
		message=" USB MOUNT"
	    else
		message=" USB UNMOUNT"
	    fi
	    ;;

	2)

	    # USB check
	    message=" USB CHECK"
	    ;;

	3)

	    # USB format
	    message=" USB FORMAT"
	    ;;

	4)

	    # Network enable/disable
	    if [ -e /mnt/usbnet ]; then
		message=" NETWORK DISABLE"
	    else
		message=" NETWORK ENABLE"
	    fi
	    ;;

	5)

	    # QR code
	    message=" QR CODE"
	    ;;

	6)

	    # Factory tests enable/disable
	    RUN_ENV_VAR=$(fw_printenv -n assembly_tests 2>/dev/null)
	    if [ "x${RUN_ENV_VAR}" == "x1" ]; then
		message=" FACTORY TESTS DISABLE"
	    else
		message=" FACTORY TESTS ENABLE"
	    fi
	    ;;

	7)

	    # First boot enable/disable
	    FIRST_BOOT_OK_ENV_VAR=$(fw_printenv -n first_boot_ok 2>/dev/null)
	    if [ "x${FIRST_BOOT_OK_ENV_VAR}" == "x1" ]; then
		message=" FIRST BOOT DISABLE"
	    else
		message=" FACTORY TESTS ENABLE"
	    fi
	    ;;

	8)

	    # Exit Recovery
	    message=" EXIT RECOVERY"
	    ;;

	9)

	    # Shutdown
	    message=" SHUTDOWN"
	    ;;
    esac
    notif "${message}"
}

# Run menu item
menu_run () {
    local entry=${1}

    case ${entry} in
	0)

	    # Information
	    rootfs_mount=/tmp/rootfs
	    mkdir -p ${rootfs_mount}
	    mount -t ext4 -o ro /dev/mmcblk0p2 ${rootfs_mount}
	    if [ $? -ne 0 ]; then
		version_rootfs="???"
	    else
		version_rootfs=$(grep rootfs ${rootfs_mount}/etc/sw-versions | cut -f 2)
	    fi
	    umount ${rootfs_mount}
	    version_recovery=$(grep Recovery /etc/sw-versions | cut -f 2)
	    ip_addr=$(ifconfig usb0 | grep "inet " | awk -F'[: ]+' '{ print $4 }')
	    notif "${message}^^    Recovery: ${version_recovery}^    rootfs  : ${version_rootfs}^    IP addr : ${ip_addr}"
	    ;;

	1)

	    # USB mount/unmount
	    mount | grep -q /dev/mmcblk0p4
	    if [ $? -eq 0 ]; then
		notif "${message}..."
		share start
		if [ $? -ne 0 ]; then
		    notif "${message}...^CANNOT MOUNT USB!"
		else
		    message=" USB UNMOUNT"
		    notif "${message}"
		fi
	    else
		notif "${message}..."
		share stop
		if [ $? -ne 0 ]; then
		    notif "${message}...^CANNOT UNMOUNT USB!"
		else
		    for file in $(ls /mnt/FunKey-*.fwu); do
			swupdate -i "${file}"
			if [ $? -ne 0 ]; then
			    notif "${message}...^CANNOT UPDATE!"
			    break
			fi
		    done
		    message=" USB MOUNT"
		    notif "${message}"
		fi
	    fi
	    ;;

	2)

	    # USB check
	    mount | grep -q /dev/mmcblk0p4
	    if [ $? -eq 0 ]; then
		notif "${message}..."
		umount /mnt >/dev/null 2>&1
		if [ $? -ne 0 ]; then
		    notif "${message}...^CANNOT CHECK USB!"
		fi
		fsck.fat -a -v -w /dev/mmcblk0p4
		if [ $? -ne 0 ]; then
		    notif "${message}...^CANNOT CHECK USB!"
		fi
		mount /mnt
		if [ $? -ne 0 ]; then
		    notif "${message}...^CANNOT CHECK USB!"
		else
		    notif "${message} DONE"
		fi
	    else
		notif "${message}...^CANNOT CHECK USB WHEN MOUNTED!"
	    fi
	    ;;

	3)

	    # USB format
	    mount | grep -q /dev/mmcblk0p4
	    if [ $? -eq 0 ]; then
		notif "${message}...^PRESS A TO CONFIRM"
		while true; do
		    case $(getkey) in
			1e0001|1e0002)
			    umount /mnt &&
				mkfs.vfat /dev/mmcblk0p4 &&
				mount /mnt
			    if [ $? -ne 0 ]; then
				notif "${message}...^CANNOT FORMAT USB!"
			    else
				notif "${message} DONE"
			    fi
			    break
			    ;;

			1e0000)
			    ;;

			*)
			    notif "${message}...^ABORTED!"
			    break
			    ;;
		    esac
		done
	    else
		notif "${message}...^CANNOT FORMAT USB WHEN MOUNTED!"
	    fi
	    ;;

	4)

	    # Network enable/disable
	    mount | grep -q /dev/mmcblk0p4
	    if [ $? -eq 0 ]; then
		if [ -e /mnt/usbnet ]; then
		    rm -f /mnt/usbnet
		    message=" NETWORK ENABLE"
		else
		    touch /mnt/usbnet
		    message=" NETWORK DISABLE"
		fi
		notif "${message}"
	    else
		notif "${message}...^CANNOT CHANGE NET IF MOUNTED!"
	    fi
	    ;;

	5)

	    # QR code
	    qrencode -o /tmp/qrcode.png "https://www.funkey-project.com/_functions/qrcode/$(get_sid)"
	    fbv -e /tmp/qrcode.png -d 1
	    ;;

	6)

	    # Factory tests enable/disable
	    RUN_ENV_VAR=$(fw_printenv -n assembly_tests 2>/dev/null)
	    if [ "x${RUN_ENV_VAR}" == "x1" ]; then
		fw_setenv assembly_tests 0
		message=" FACTORY TESTS ENABLE"
	    else
		fw_setenv assembly_tests 1
		message=" FACTORY TESTS DISABLE"
	    fi
	    notif "${message}"
	    ;;

	7)

	    # First boot enable/disable
	    FIRST_BOOT_OK_ENV_VAR=$(fw_printenv -n first_boot_ok 2>/dev/null)
	    if [ "x${FIRST_BOOT_OK_ENV_VAR}" == "x1" ]; then
		fw_setenv first_boot_ok
		message=" FIRST BOOT ENABLE"
	    else
		fw_setenv first_boot_ok 1
		message=" FIRST BOOT DISABLE"
	    fi
	    notif "${message}"
	    ;;

	8)
	    notif "^^^^^^^^         RESTARTING...^^^^^^^^"
	    normal_mode
	    ;;

	9)
	    notif "^^^^^^^^          SHUTDOWN...^^^^^^^^"
	    poweroff
	    ;;
    esac
}

# Display first menu entry
entry=0
menu_display ${entry}

# Recovery menu loop
while true; do

    # Get key events
    case $(getkey) in
	160001|160002)

	    # Key UP pressed or repeat
	    let entry=${entry}-1
	    let entry=${entry}%9
	    if [ ${entry} -eq -1 ]; then
		entry=8
	    fi
	    menu_display ${entry}
	    ;;

	200001|200002)

	    # Key DOWN pressed or repeat
	    let entry=${entry}+1
	    let entry=${entry}%9
	    menu_display ${entry}
	    ;;

	1e0001|1e0002)

	    # Key A pressed or repeat
	    menu_run ${entry}
	    ;;
    esac
done
