#!/bin/bash

# define variables
script_dir=`dirname $0`
serial_port="/dev/ttyS0"
log_file="tc3_updater.log"

# export variables
export script_dir
export serial_port
export log_file

if [ -t 0 -a `id -u` -eq 0 ]; then
	# call main script if log file variables is set
	if [ -n "$log_file" ]; 
	then
		# check if all necessary scripts have the correct file permissions
		if [ -d "$script_dir/scripts" -a ! -x "$script_dir/scripts/Update_linux" ]; then
			chmod -R 764 $script_dir/scripts
		fi
		if [ -d "$script_dir/make_FIT" -a ! -x "$script_dir/make_FIT/make_udimg" ]; then
			chmod -R 764 $script_dir/make_FIT
		fi
		# call main updater script and log traffic in file
		zenity --progress --title "tc3_updater_for_linux" \
		--text "Upate process is running ...                        " \
		--no-cancel --pulsate --auto-close &
		ZENITYPID=$!
		script -f -c "$script_dir/scripts/Update_linux" "$script_dir/$log_file"
		killall cat
	    kill $ZENITYPID
		exit 0
	else
		echo -e "\033[31mError: Select log file!\n\033[37m"
		exit 1
	fi
else
	zenity --error --title "tc3_updater_for_linux" --text \
	"Please execute the tc3_updater_for_linux within a terminal as sudo"
fi
