#!/bin/bash
{
	#////////////////////////////////////
	# DietPi LED control Script
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	#
	# Info:
	# - Whiptail menu to select LED trigger types for all available /sys/class/leds entries
	#
	# Usage:
	# - /DietPi/dietpi/func/dietpi-led_control		Main menu
	# - /DietPi/dietpi/func/dietpi-led_control 1	Apply from savefile
	#////////////////////////////////////

	#Grab Input
	INPUT=0
	if [[ $1 =~ ^-?[0-9]+$ ]]; then

		INPUT=$1

	fi

	#Import DietPi-Globals ---------------------------------------------------------------
	. /DietPi/dietpi/func/dietpi-globals
	G_CHECK_ROOT_USER
	export G_PROGRAM_NAME='DietPi-LED_control'
	#Import DietPi-Globals ---------------------------------------------------------------

	FP_SETTINGS='/DietPi/dietpi/.dietpi-led_control'

	aLED_NAME=0
	aLED_SETTING_AVAILABLE=0
	aLED_SETTING_CURRENT=0

	Init_LED_Info(){

		aLED_NAME=( $(ls /sys/class/leds) )

		for ((i=0; i<${#aLED_NAME[@]}; i++))
		do

			#	Obtain available/cur settings
			aLED_SETTING_AVAILABLE[$i]=$(cat /sys/class/leds/${aLED_NAME[$i]}/trigger | tr -d '[]')
			aLED_SETTING_CURRENT[$i]=$(cat /sys/class/leds/${aLED_NAME[$i]}/trigger | grep -o '\[.*\]' | tr -d '[]')

		done

		if (( ${#aLED_NAME[@]} == 0 )); then

			G_DIETPI-NOTIFY 1 'Unable to find any supported LEDS on this device.'
			exit

		fi

	}

	Settings_Write_Apply(){

		rm "$FP_SETTINGS" &> /dev/null

		G_DIETPI-NOTIFY 3 DietPi-LED_Control "Applying LED triggers"

		for ((i=0; i<${#aLED_NAME[@]}; i++))
		do


			echo -e "${aLED_SETTING_CURRENT[$i]}" > "/sys/class/leds/${aLED_NAME[$i]}/trigger"
			if (( $? == 0 )); then

				G_DIETPI-NOTIFY 0 "${aLED_NAME[$i]}: ${aLED_SETTING_CURRENT[$i]}"

			else

				G_DIETPI-NOTIFY 1 "${aLED_NAME[$i]}: ${aLED_SETTING_CURRENT[$i]}"

			fi

			cat << _EOF_ >> "$FP_SETTINGS"
${aLED_NAME[$i]} ${aLED_SETTING_CURRENT[$i]}
_EOF_

		done

	}

	Settings_Read(){

		if [ -f "$FP_SETTINGS" ]; then

			local index=0
			while read -r line
			do

				if (( $index < ${#aLED_NAME[@]} )); then #Only process available LED's at time of script

					if [ "${aLED_NAME[$index]}" = "$(echo -e $line | awk '{print $1}' )" ]; then

						aLED_SETTING_CURRENT[$index]=$(echo -e "$line" | awk '{print $2}' )
						((index++))

					fi

				else

					break

				fi

			done < "$FP_SETTINGS"

		fi

	}

	#Menu's
	WHIP_TITLE='DietPi - LED Control'
	TARGETMENUID=0
	SELECTED_LED_NAME=0

	DEFAULT_ITEM=''

	Menu_Main(){

		Init_LED_Info

		TARGETMENUID=-1

		local whiptail_menu_array=()

		for ((i=0; i<${#aLED_NAME[@]}; i++))
		do

			whiptail_menu_array+=("${aLED_NAME[$i]}" ": ${aLED_SETTING_CURRENT[$i]}")

		done

		OPTION=$(whiptail --title "$WHIP_TITLE" --menu "Please select an LED to configure its options:" --cancel-button "Back" 16 60 8 "${whiptail_menu_array[@]}" 3>&1 1>&2 2>&3)

		CHOICE=$?

		unset whiptail_menu_array

		if (( $CHOICE == 0 )); then

			SELECTED_LED_NAME=$OPTION
			TARGETMENUID=1

		fi

	}

	Menu_LED_Options(){

		TARGETMENUID=0

		local whiptail_menu_array=()
		local current_index=0

		for ((i=0; i<${#aLED_NAME[@]}; i++))
		do

			if [ "$SELECTED_LED_NAME" = "${aLED_NAME[$i]}" ]; then

				echo -e "${aLED_SETTING_AVAILABLE[$i]}" | tr ' ' '\n' > /tmp/aLED_SETTING_AVAILABLE

				while read -r line
				do

					whiptail_menu_array+=("$line" '')

				done < /tmp/aLED_SETTING_AVAILABLE

				DEFAULT_ITEM=${aLED_SETTING_CURRENT[$i]}
				current_index=$i

				break

			fi

		done

		OPTION=$(whiptail --title "$WHIP_TITLE" --menu "Please select an option for $SELECTED_LED_NAME LED:" --default-item "$DEFAULT_ITEM" --cancel-button "Back" 18 60 10 "${whiptail_menu_array[@]}" 3>&1 1>&2 2>&3)

		CHOICE=$?

		unset whiptail_menu_array

		if (( $CHOICE == 0 )); then

			aLED_SETTING_CURRENT[$current_index]=$OPTION
			Settings_Write_Apply

		fi

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Main Loop
	#/////////////////////////////////////////////////////////////////////////////////////
	#-----------------------------------------------------------------------------------
	Init_LED_Info

	Settings_Read
	Settings_Write_Apply

	if (( $INPUT == 0 )); then

		while (( $TARGETMENUID > -1 ))
		do

			if (( $TARGETMENUID == 0 )); then

				Menu_Main

			elif (( $TARGETMENUID == 1 )); then

				Menu_LED_Options

			fi

		done

	fi
	#-----------------------------------------------------------------------------------
	#Delete[] array
	unset aLED_NAME
	unset aLED_SETTING_AVAILABLE
	unset aLED_SETTING_CURRENT
	#-----------------------------------------------------------------------------------
	exit
	#-----------------------------------------------------------------------------------
}