#!/bin/sh

# Reset the Board using the grey-code CPLD reset
# If that failed for some reasion, try using the LSC_REFRESH method
# Else, reboot the MC

ec=$(cat /proc/moose/cpld/post_ec)
sc=$(cat /proc/moose/cpld/post_sc)
mc=$(cat /proc/moose/cpld/post_mc)

printf "Resetting the Controller, ec: 0x%X, sc: 0x%X, mc: 0x%X\n" $ec $sc $mc | logger

# First shut the SC down (just making sure)
capi.app --shutdownsc --live --timeout=30

# Prepare the MC apps for rebooting
preprestart.sh

# Delay x seconds if requested
if [ -n "$1" -a "$1" -gt 0 ]
then
    sleep $1
fi

# Reset the Controller
i2c-codeload --target cpld --reset

# Should never get here
sleep 30

# Try using the LSC Refresh method if the --reset option failed.
i2c-codeload --target cpld --reboot

# Again, should never get here
sleep 30

# Reboot the MC to communicate with the user
reboot
