#!/bin/bash

upload_cam(){
	clear
	slogo
	FILENAME=$1

	if [ ! -f "$profdir/$FILENAME" ]
	then
		printf "$r_l\n  $txt_error $FILENAME $txt_upload_cam1 $re_\n"
		exit
	else
		printf "$w_l\n  CONFIG  : $g_l$FILENAME $txt_upload_cam2 $re_\n"
	fi

#load data from config
	source "$profdir/$FILENAME"

#find newest build by date for toolchain
	cd "$bdir"
	buildcamname="$(find . -type f \( -iname "*$toolchain*" ! -iname "*list_smargo" \) -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" " |sed 's@./@@g')"
	cd "$workdir"

#output date from cam
	if [ -f "$bdir/$buildcamname" ]
	then
		printf "$w_l  CAMNAME : $y_l$buildcamname\n"
		printf "$w_l  FILEDATE: "$(
			stat -c %y "$bdir/$buildcamname" |awk '{print $1" "; printf substr($2,1,8)}'
		)
		printf "$w_l"
	else
		printf "\n  matching \$CAM not found in $bdir$re_\n"
		exit
	fi

	printf "$p_l\n  UPLOAD  > $w_l$txt_to IP:$b_l$ip$w_l port:$b_l$port\n"
	sshpass -p "$password" scp -P "$port" "$bdir/$buildcamname" "$loginname@$ip":/tmp

#replace target cam
	if [ "$replace_target" == "y" ]
	then
		printf "$p_l  REPLACE > $y_l$targetcam$re_\n"
		sshpass -p "$password" ssh -p "$port" -o StrictHostKeyChecking=no "$loginname@$ip" \
		"cd /tmp;if [ -f \"$buildcamname\" ];then if [ -f \"$targetcam\" ];then if [ \"$backup_target\" == \"y\" ]; then cp -pf \"$targetcam\" \"$targetcam.backup\"; fi;mv -f \"$buildcamname\" \"$targetcam\";fi;fi;exit;"
		printf " $g_l$txt_done!$re_\n"
	fi

#stop target cam
	if [ "$stop_target" == 'y' ]
	then
		stop="killall -9 $(basename "$targetcam")"
	fi

#remote command (example restart cam)
	if [ ! "$remote_command" == "none" ]
	then
		printf "$w_l  SSH COMMANDS $remote_command $y_l$txt_wait\n\n"
		# do remote commands
		sshpass -p "$password" ssh -p "$port" -o StrictHostKeyChecking=no "$loginname@$ip" "$remote_command;"
		_nl
	fi
}