#!/bin/sh
# Pt-web/src/app-root/scripts/Factory/Write_EEPROM_mk4.sh
# 
#  Copyright: ©2012–2015, Güralp Systems Ltd.
#  Author: Laurence Withers <lwithers@guralp.com>
#  License: GPLv3
#

set -ex

BASE_SERIAL="$1"
GPIO_SET="$2"
INA209_SENSE_R="0.1"
INA209_SCALE="$3"
FLASH_MODULE_SIZE="$4"
SPYRUS_USB_PRESENT="$5"
SPYRUS_USB_LINE="$6"
FORCE_SPEED_ETH0="$7"

CONF_DIR="/etc/conf.local"
MANUF_LOG="${CONF_DIR}/hw_manuf_log"
MANUF_LOG_NEW="${CONF_DIR}/.hw_manuf_log.new"
HW_CONFIG="${CONF_DIR}/hw_config"
HW_CONFIG_NEW="${CONF_DIR}/.hw_config.new"
FLASH_CF="${CONF_DIR}/flash_module"
FLASH_CF_NEW="${CONF_DIR}/.flash_module.new"
ETH_CF="${CONF_DIR}/ethernet"
ETH_CF_NEW="${CONF_DIR}/.ethernet.new"

LOG_ENTRY="Pt-web `isodate -se`"



#
# Create new log file
#

# make sure 'cat' doesn't fail if log doesn't exist
[ -e "${MANUF_LOG}" ] || touch "${MANUF_LOG}"
cat - "${MANUF_LOG}" > "${MANUF_LOG_NEW}" <<EOF
${LOG_ENTRY}
EOF



#
# Create new hardware config
#

cat > "${HW_CONFIG_NEW}" <<EOF
# ${LOG_ENTRY}
BASE_SERIAL="${BASE_SERIAL}"
HW_GPIO_SET="${GPIO_SET}"
EOF

if [ "${SPYRUS_USB_PRESENT}" -ne 0 ]
then
	echo "SPYRUS_USB_PRESENT=\"true\"" >> "${HW_CONFIG_NEW}"
	echo "SPYRUS_USB_POWER_LINE=\"${SPYRUS_USB_LINE}\"" >> "${HW_CONFIG_NEW}"
fi

# save settings for a single power line
power_line() {
	echo "PM_PORT$1=\"$2,${INA209_SENSE_R},${INA209_SCALE}\"" >> "${HW_CONFIG_NEW}"
}

case "${GPIO_SET}" in
sXam)
	power_line 0 "Data Out,INA209,bus0,0x40"
	power_line 1 "Port A,INA209,bus0,0x41"
	power_line 2 "Port C,INA209,bus0,0x43"
	power_line 3 "Sensor-power,INA209,bus0,0x4A"
	power_line 4 "SensorB-power,INA209,bus0,0x4B"
	power_line 5 "Ext0-power,INA209,bus0,0x4D"
	power_line 6 "Ext1-power,INA209,bus0,0x4F"
	;;

eamu3 | eamu7 | EAM-0007RM0VEL000)
	power_line 0 "Data Out,INA209,0,0x40"
	;;

peli3 | peli7)
	power_line 0 "Data Out,INA209,0,0x40"
	power_line 1 "Disk-heater,INA209,0,0x4C"
	power_line 2 "Disk-power,INA209,0,0x49"
	;;

e2am)
	power_line 0 "Data Out,INA209,bus0,0x40"
	power_line 1 "Port A,INA209,bus0,0x41"
	power_line 2 "Port C,INA209,bus0,0x43"
	power_line 3 "Sensor-power,INA209,bus0,0x4A"
	power_line 4 "Ext0-power,INA209,bus0,0x4D"
	power_line 5 "Ext1-power,INA209,bus0,0x4F"
	;;

e2am_3ex)
	power_line 0 "Data Out,INA209,bus0,0x40"
	power_line 1 "Port A,INA209,bus0,0x41"
	power_line 2 "Port C,INA209,bus0,0x43"
	power_line 3 "Ext0-power,INA209,bus0,0x4D"
	power_line 4 "Ext1-power,INA209,bus0,0x4F"
	;;

neptune | dm24s12eam_string)
	power_line 0 "Data Out,INA209,bus0,0x40"
	power_line 1 "Port A,INA209,bus0,0x41"
	power_line 2 "Port C,INA209,bus0,0x43"
	power_line 3 "Port D,INA209,bus1,0x40"
	power_line 4 "Port E,INA209,bus1,0x41"
	power_line 5 "Port F,INA209,bus1,0x42"
	power_line 6 "Port G,INA209,bus1,0x43"
	power_line 7 "Sensor-power,INA209,bus0,0x4A"
	power_line 8 "Ethernet-power,INA209,bus0,0x4D"
	power_line 9 "Aux-power,INA209,bus0,0x4F"
	power_line 10 "Ext0-power,INA209,bus1,0x4A"
	power_line 11 "Ext1-power,INA209,bus1,0x4B"
	power_line 12 "Ext2-power,INA209,bus1,0x4C"
	power_line 13 "Ext3-power,INA209,bus1,0x4D"
	power_line 14 "Ext4-power,INA209,bus1,0x4E"
	;;

bholea)
	power_line 0 "Data Out,INA209,0,0x40"
	power_line 1 "Port A,INA209,0,0x41"
	;;
esac



#
# Create new flash config file
#
if [ -z "${FLASH_MODULE_SIZE}" ]
then
	FLASH_MODULE_SIZE="0"
fi

echo "FLASHSIZE=${FLASH_MODULE_SIZE}" > "${FLASH_CF_NEW}"



#
# Create new Ethernet override file
#
echo "# Ethernet overrides" > "${ETH_CF_NEW}"
if [ -n "${FORCE_SPEED_ETH0}" ]
then
	echo "FORCE_SPEED_eth0=\"${FORCE_SPEED_ETH0}\"" >> "${ETH_CF_NEW}"
fi



#
# Save to EEPROM
#  (we already wrote our own log entry, so inhibit a new one)
#
mv "${MANUF_LOG_NEW}" "${MANUF_LOG}"
mv "${HW_CONFIG_NEW}" "${HW_CONFIG}"
mv "${FLASH_CF_NEW}" "${FLASH_CF}"
mv "${ETH_CF_NEW}" "${ETH_CF}"
exec /bin/sh /usr/lib/platinum/write_eeprom.sh --inhibit-log-entry
