#!/bin/sh

# This script remove a device from a E450 or E550
# This script was created for Oscar Mauricio Morales (oscar.morales@ezlo.com) from the Ezlo Customer Care Team.

# How to use it?
# Tranfer this file to /tmp in the controller
# Send the command:
# sh remove_device.sh {device_id1} {device_id2} {device_id3} ... {device_idn}
# Make sure to insert the valid device id

echo
echo Initializing... 

# Install dbreader is necessary

echo
echo Checking if dbreader is installed...

var=$(opkg list-installed | grep dbreader)

if [ "$var" = "" ]
then
	echo 
	echo dbreader is not installed.
	echo Installing dbreader...
	opkg update && opkg install firmware-dev-dbreader 2> /dev/null
fi

echo dbreader is installed.
echo

for deviceId in "$@"
do

	echo The device to delete is: $deviceId

	# Check if the id is correct
	/opt/firmware/bin/dbreader -t db.firmware.cored.devices | grep _id | awk '{print $2}' | awk '{print substr($0, 2, length($0)-3)}' > ./device_ids                                                                                        
	num_devices=$(wc -l device_ids | awk '{print $1}')   

	if grep -Fxq "$deviceId" ./device_ids
	then

		# name=$(/opt/firmware/bin/dbreader -id $deviceId | grep name | awk '{print substr($0, 14, length($0)-15)}') 2> /dev/null

		# Remove settings
		/opt/firmware/bin/dbreader -t db.firmware.cored.device_settings | grep -B 5 $deviceId | grep _id | awk '{print $2}' | awk '{print substr($0, 2, length($0)-3)}' > ./device_settings                                                      
		num_settings=$(wc -l device_settings | awk '{print $1}')                                                                                                                                                                                
		                                                                                                                                                                                                                                        
		for i in $(seq 1 1 $num_settings)                                                                                                                                                                                                       
		do                                                                                                                                                                                                                                      
		        setting=$(awk -v i=$i 'NR==i' device_settings)                                                                                                                                                                                  
		        echo Removing setting $setting                                                                                                                                                                                                  
		        /opt/firmware/bin/dbreader -t db.firmware.cored.device_settings -rid $setting                                                                                                                                                   
		done

		echo $num_settings settings were removed                                                                                                                                                                                                                                 



		#Remove items                                                                                                                                                                                                                                     
		/opt/firmware/bin/dbreader -t db.firmware.uid.items | grep -B 1 $deviceId | grep _id | awk '{print $2}' | awk '{print substr($0, 2, length($0)-3)}' > ./device_items                                                                     
		num_items=$(wc -l device_items | awk '{print $1}')                                                                                                                                                                                      
		                                                                                                                                                                                                                                        
		for i in $(seq 1 1 $num_items)                                                                                                                                                                                                          
		do                                                                                                                                                                                                                                      
		        item=$(awk -v i=$i 'NR==i' device_items)                                                                                                                                                                                        
		        echo Removing item $item                                                                                                                                                                                                        
		        /opt/firmware/bin/dbreader -t db.firmware.uid.items -rid $item                                                                                                                                                                  
		done               

		echo $num_items items were removed                                                                                                                                                                                                                        
		                                                                                                                                                                                                                                        
	                                                                                                                                                                                

		#Remove child devices                                                                                                                                                                                                                                 
		for i in $(seq 1 1 $num_devices)                                                                                                                                                                                                        
		do                                                                                                                                                                                                                                      
		        id=$(awk -v i=$i 'NR==i' device_ids)                                                                                                                                                                                            
		        parent=$(/opt/firmware/bin/dbreader -id $id | grep parentDeviceId | awk '{print $2}' | awk '{print substr($0, 2, length($0)-3)}') 2> /dev/null                                                                                  
		        if [ "$parent" = "$deviceId" ]                                                                                                                                                                                                  
		        then                                                                                                                                                                                                                            
		                echo Removing child $id                                                                                                                                                                                                 
		                /opt/firmware/bin/dbreader -t db.firmware.cored.devices -rid $id                                                                                                                                                        
		        fi                                                                                                                                                                                                                              
		done     
 

		#Remove the main device                                                                                                                                                                                                                         
		echo Removing device $deviceId
		/opt/firmware/bin/dbreader -t db.firmware.cored.devices -rid $deviceId

		rm ./device_ids && rm ./device_items && rm ./device_settings                                                                                                                                                      
		echo Done. The device has been removed.

		echo

	else
	    echo ERROR: Enter a valid device id
	fi

done

echo Terminating...
echo
