#!/bin/bash
# Usage
#	transfer_device_config <direction> <server> <mac>
#		where:	direction = ToServer -> transfers to the server, FromServer -> transfers from the server
ULBIN=/usr/local/bin
envfile="/run/appliance-env"
directions=(ToServer FromServer FromUser)
direction=$1
config_dir=/etc/nyquist
temp_path="/run"
server_address=${2:-$($ULBIN/find_tftp_server)}
server_path="backup"
mac=${3:-$(sudo ifconfig eth0 | grep 'HWaddr' | awk '{print $5}' | sed -e 's/://g')}

post_presets() {
	tar -dPp -f $1 &> /dev/null
	[ $? -ne 0 ] && tar -xaPp -f $1
	return $?
}

action_ToServer() {
	post_update=
	params=(${1/:/ })
	if [ -s ${config_dir}/${params[0]} ]; then
		ffname=${mac:-000000000000}-${params[0]}
		read results < <(tftp <<< "put ${config_dir}/${params[0]} ${server_address}:${server_path}/$ffname")
		rc=$?
		if [ $rc -ne 0 ]; then
			return 2
		elif [ "${results/*Sent*/sent}" != "sent" ]; then
			return 1
		fi
	fi
	return 0
}

action_FromServer() {
	pushd ${temp_path} > /dev/null
	params=(${1/:/ })
	ffname=${mac:-000000000000}-${params[0]}
	rc=0
	read results < <(sudo tftp $server_address <<< "get ${server_path}/$ffname")

	if [ "${results/*Received*/received}" = "received" ]; then
		diff -q $ffname ${config_dir}/${params[0]} &> /dev/null
		if [ $? -ne 0 ]; then
			sudo chown root:www-data $ffname
			sudo chmod 664 $ffname
			sudo mv -f $ffname ${config_dir}/${params[0]}
			[ -n "${params[1]}" ] && eval post_${params[1]} ${config_dir}/${params[0]}
		fi
	else
		unlink $ffname
		# Do not hard fail on missing presets
		[ "${params[1]}" != "presets" ] && rc=1
	fi

	popd > /dev/null
	return $rc
}

[ -f ${envfile} ] && . ${envfile}

if [ $# -lt 1 -o "${directions[*]/${direction}/}" = "${directions[*]}" ]; then
	logger -t $0 "Invalid parameters"
	exit 1
elif [ "${APPLIANCE_PART}" == "nq-asb" ]; then
	post_update="systemctl reload-or-restart appliance"
	config_files=(canconfig.xml)
else
	post_update="systemctl reload-or-restart dsp-websocket"
	if [ "${APPLIANCE_PART}" == "nq-mix" ]; then
		config_files=(mixer.json)
	elif [ "${APPLIANCE_PART/NQ-A2*G2}" != "${APPLIANCE_PART}" ]; then
		config_files=(ampg2_2c.json presets.tgz:presets)
	elif [ "${APPLIANCE_PART/NQ-A2}" != "${APPLIANCE_PART}" ]; then
		config_files=(amp2c.json presets.tgz:presets)
	elif [ "${APPLIANCE_PART/NQ-A4*G2}" != "${APPLIANCE_PART}" ]; then
		config_files=(ampg2_4c.json presets.tgz:presets)
	elif [ "${APPLIANCE_PART/NQ-A4}" != "${APPLIANCE_PART}" ]; then
		config_files=(amp4c.json presets.tgz:presets)
	elif [ "${APPLIANCE_PART/nq-ga20p}" != "${APPLIANCE_PART}" ]; then
		config_files=(ga20p.json)
	elif [ "${APPLIANCE_PART/nq-ga40p}" != "${APPLIANCE_PART}" ]; then
		config_files=(ga40p.json)
	elif [ "${APPLIANCE_PART/nq-ga10pv}" != "${APPLIANCE_PART}" ]; then
		[ -e /run/message ] || touch /run/message
		[ -e  /run/event ] || touch /run/event
		/usr/local/bin/get_display_update msgupdate 2>&1 | logger -p user.info -t "$0[$$]:get_display_update message"
		exit 0
	elif [ "${APPLIANCE_PART/nq-s810v2}" != "${APPLIANCE_PART}" ]; then
		config_files=(s810v2.json)
	elif [ "${APPLIANCE_PART/nq-s810v3}" != "${APPLIANCE_PART}" ]; then
		config_files=(s810v3.json)
	elif [ "${APPLIANCE_PART/NQ-PA}" != "${APPLIANCE_PART}" ]; then
		config_files=(pama.json)
	elif [ "${APPLIANCE_PART/nq-edp01}" != "${APPLIANCE_PART}" ]; then
		config_files=(edp01.json)
	elif [ "${APPLIANCE_PART/nq-ga400p}" != "${APPLIANCE_PART}" ]; then
		config_files=(ga400p.json)
	else
		exit 0
	fi
fi

if [ "${direction}" != "FromUser" ]; then
	for fname in ${config_files[*]}
	do
		eval action_${direction} $fname
		rc=$?
		if [ $rc -ne 0 ]; then
			post_update="logger -p user.warning -t \"$0[$$]\" \"$fname configuration transfer ${direction} failed\""
			break
		fi
	done
fi

[ -n "$post_update" ] && eval $post_update

exit $rc
