#!/bin/bash
echo 'Project Name:' $1 
echo 'Debug Directory:' $2'/Debug'
echo 'Device Name:' $3
echo 'Location of the .hex file:' $2
echo '.hex file Name:' $1'.hex'
echo 'Softdevice Path:' $2/$4
echo 'Jlink.exe Path:' $5

touch jlinkfile.jlink
echo "Would you like to install the Softdevice ? (y/n)"
read RESP
if [ "$RESP" = "y" ]; then
	if [ -f $2/$4 ];then
		echo  -e "exec device = $3\nr\nw4 4001e504 2\nw4 4001e50c 1\nw4 4001e514 1\nw4 4001e504 1\nr\nsleep 100\nloadbin $2/$4 0x0 \nloadbin $2/Debug/$1.hex 0x14000 \nr\nw4 4001e504 2\nw4 4000e544 1\nsleep 100\ng\nexit" > $2/jlinkfile.jlink
	cd $5
	./JLinkExe -Device nrf51822_xxaa -if SWD $2/jlinkfile.jlink 
	else
		echo "File $FILE does not exists"
	fi
else 
	echo  -e "exec device = $3\nr\nw4 4001e504 1\nr\nsleep 100\nloadbin $2/Debug/$1.hex 0x14000 \nr\nw4 4001e504 2\nw4 4000e544 1\nsleep 100\ng\nexit" > $2/jlinkfile.jlink
	cd $5
	./JLinkExe -Device nrf51822_xxaa -if SWD $2/jlinkfile.jlink 
fi


