#!/opt/bin/sh

check_usb_drive() {
while true
do
	if [ "$(ls /tmp/mnt)" == "" ]
	then echo "Please, plug a USB drive to your Keenetic device. When done, press Enter"; read userInput
	else break
	fi
done
}

get_part_label() {
echo "All the partitions of the external storage are specified below:";echo
ls /tmp/mnt -F | grep "@" | sed "s/@//g";echo
while true
do
	echo; echo -n "Input the label of the partition to backup Asterisk: "
	read partLabel
	if [ "$(ls /tmp/mnt/ | grep -x ${partLabel})" == "$partLabel" ] 
	then break
	else echo "ERROR! Partition '$partLabel' doesn't exist!"
	fi
done
echo "Partition '${partLabel}' was selected."
}

search_backup_file() {
	echo "Searching backup files.."
	backupFileList=$(ls /tmp/mnt/${partLabel} -1 | grep "backup-int")
	if [ "$backupFileList" == "" ]; then echo "There are no backup files found. Place backup file on this partition of your USB drive then start this script again."; exit; fi
	echo "Following backup files are found:"; echo; ls /tmp/mnt/${partLabel} -1 | grep "backup-int"; echo
	while true
	do
		echo "Input the name of the file to restore Asterisk from:"; read backupFileName
		if [ "$(ls /tmp/mnt/${partLabel} -1 | grep -x ${backupFileName})" == "$backupFileName" ]
		then echo "File '${backupFileName}' was selected."; break
		else echo "ERROR! File '${backupFileName}' doesn't exist!"
		fi
	done
}

clean_storage() {
echo "Checking the internal storage.."
if [ "$(ls /storage)" == "" ] 
then 
	echo "The storage is empty."; return
else 
	echo "Deleting data, please, wait.."; rm /storage/* -r; 
	while true
	do
	sleep 5
	if [ "$(ls /storage)" == "" ]; then break; fi
	done		
	echo "All data was deleted.";return
fi			
}

copy_to_storage() {
echo "Restoring your Asterisk installation, please, wait.."
if [ "$(cd /opt; cp ./bin ./etc ./home ./lib ./root ./sbin ./share ./tmp ./usr ./var /storage -R
)"=="" ]
then sleep 10; echo "Asterisk has been restored."
else exit
fi
}

extract_to_storage() {
echo "Restoring your Asterisk installation from the file '${backupFileName}', please wait.."
tar -zxvf /tmp/mnt/${partLabel}/${backupFileName} -C /storage
sleep 10; echo; echo "Asterisk has been restored."
}

#============================ MAIN ============================
echo; echo "This script will restore your Entware Asterisk installation from a backup file to the internal storage of your Keenetic device."
echo -n "Continue? <y/n> "; read userInput; if [ "$userInput" != "y" ]; then exit; fi
check_usb_drive; get_part_label
search_backup_file
echo; echo -n "Now everything is ready to start restoration of Entware Asterisk. All the data on the internal storage will be deleted. Continue? <y/n>"; read userInput; if [ "$userInput" != "y" ]; then exit; fi
clean_storage
extract_to_storage
#### Start Asterisk on the internal storage
echo
echo "Now everything is ready to start Entware Asterisk installation on the internal storage."
echo; echo "Open web configurator of your Keenetic device in web browser, go to Management>OPKG>OPKG package manager, choose 'Internal storage' and click 'Save' in the bottom of the page. After that Entware Asterisk installation will be started on the internal storage."
echo; echo "Press any key to exit the script"; read userInput
exit

