#!/bin/bash

# shellcheck shell=sh
# shellcheck disable=SC2034 
# shellcheck disable=SC2059
# shellcheck disable=SC1091
# shellcheck disable=SC1090

set -e
#set -x

if [ "$(id -u)" -ne 0 ]; then
	echo "$0 needs root: please use sudo."
	exit 1
fi

# Source Auxilium (args parser)
. /usr/share/auxilium/auxilium.sh

auxilium_init "$(basename "$0")" -m 'Update Firmware of a device'

auxilium_add nopause -s n -t opt -m 'No pause after firmware was succefully updated'
auxilium_add device -t positional -m 'Device to update (/dev/sdX)'


if [ -f "/usr/share/ferreus-orbit/common.sh" ]; then
	. "/usr/share/ferreus-orbit/common.sh"
elif [ -f "share/common.sh" ] ; then
	. "share/common.sh"
elif [ -e "../share/common.sh" ] ; then
	. "../share/common.sh"
else
	echo "Cannot load common.sh: exiting."
	exit 1
fi

auxilium_parse "$@"

fo_lock

if [ -z "$AUXILIUM_ARG_sh_completion" ]; then
	fo_init
fi

if ! [ -r "${FW_CONF_FILE}" ] ; then
	echo "Cannot find configuration file."
	exit 1
fi

# shellcheck disable=SC2154
echo "---> Checking $AUXILIUM_ARG_device"
eval "$(udevadm info -q property --export "${AUXILIUM_ARG_device}")"
SMARTCTL_OUTPUT=$(mktemp -t "${FO_SCRIPT}.smartctl.XXXXXX")
readonly SMARTCTL_OUTPUT
if fo_check_raid; then
	did=$(fo_get_did)
	smartctl -i -d megaraid,"${did}" "$AUXILIUM_ARG_device" > "${SMARTCTL_OUTPUT}" || true
else
	smartctl -i "$AUXILIUM_ARG_device" > "${SMARTCTL_OUTPUT}" || true
fi
SMARTCTL_VENDOR=$(grep -E '^Vendor:' "${SMARTCTL_OUTPUT}" | awk '{print $2}')
readonly SMARTCTL_VENDOR
SMARTCTL_PRODUCT=$(grep -E '^Product:' "${SMARTCTL_OUTPUT}" | awk '{print $2}')
readonly SMARTCTL_PRODUCT
SMARTCTL_REVISION=$(grep -E '^Revision:' "${SMARTCTL_OUTPUT}" | awk '{print $2}')
readonly SMARTCTL_REVISION
rm -f "${SMARTCTL_OUTPUT}"

if [ "${SMARTCTL_VENDOR}" != "SEAGATE" ] ; then
	echo "-> Vendor Unknown"
	exit 0
fi

if [ -z "${SMARTCTL_PRODUCT}" ] ; then
	echo "Cannot find product name."
	exit 0
fi

if [ -z "${SMARTCTL_REVISION}" ] ; then
	echo "Cannot find revision."
	exit 0
fi

NAME=$(grep "^${SMARTCTL_PRODUCT}," "$FW_CONF_FILE"| awk -F',' '{print $2}')
readonly NAME
MINVER=$(grep "^${SMARTCTL_PRODUCT}," "$FW_CONF_FILE"| awk -F',' '{print $3}')
readonly MINVER
FW=$(grep "^${SMARTCTL_PRODUCT}," "$FW_CONF_FILE"| awk -F',' '{print $4}')
readonly FW

if [ -z "${NAME}" ] || [ -z "${MINVER}" ] || [ -z "${FW}" ] ; then
	echo "Disk Model not found"
	exit 0
else
	echo "-> Detected ${NAME}"
fi


if dpkg --compare-versions "${SMARTCTL_REVISION}" lt "${MINVER}" 2>/dev/null ; then
	echo "-> Detected revision ${SMARTCTL_REVISION}, min version is ${MINVER}: upgrading firmware..."
	openSeaChest_Firmware -d "${AUXILIUM_ARG_device}" --downloadFW "${FW_DIR}/${FW}"
	Err=$?
	if [ "$Err" -gt 0 ] && [ "$Err" -lt 6 ]; then
	  if fo_check_raid ;then
			did=$(fo_get_did)
			phy=$(jq -r '.["PD LIST"][]|select(.DID=='"${did}"')["EID:Slt"]' "${FO_CACHE_FILE_STORCLI}")
			es="/e$(echo "${phy}"|awk -F':' '{print $1"/s"$2}')"
			${STORCLI} "/call${es}" download file="${FW_DIR}/${FW}" force ResetNow || true
		fi
	fi
	if [ -z "$AUXILIUM_ARG_nopause" ]; then
  	echo Pausing for 2 secondes
	  sleep 5
	fi
else
	echo "-> Firmware is up-to-date (${SMARTCTL_REVISION}, min version is ${MINVER})."
fi

exit 0
