#!/bin/sh
# Copyright (c) 2007-2012 Meraki, Inc
# Copyright (c) 2017 Cisco Systems, Inc
# All rights reserved
#

. /usr/bin/pubfunc.sh
. /usr/cisco/bin/ciscosetup.sh

export PATH=/bin:/sbin:/usr/bin:/var/wireless:/usr/sbin:/opt/cisco/bin
MERAKI_BOARD=`cat /MERAKI_BOARD`

# do these things a bit later, to give the cpu to brain/click as everything gets started
# sleep 10

# don't complain too loudly about a missing config
[ -f /storage/config ] || echo valid_config true > /storage/config

# if [ "$MERAKI_BOARD" != "apvirtual" ]; then
# just in case testfastcgi runs us out of memory, wait until we've gotten a new config
# ( while ! [ -f /tmp/config_fetch_successful ]; do sleep 10; done; /usr/bin/wget -q -O - http://127.0.0.1/testfastcgi.cgi > /dev/null ) &
# fi

# TBD: need to understand the logic here, when to do one vs the other
if [ "$MERAKI_BOARD" != "corsica" -a "$MERAKI_BOARD" != "mallorca" -a \
     "$MERAKI_BOARD" != "apvirtual" -a "$MERAKI_BOARD" != "axel-qca" -a \
     "$MERAKI_BOARD" != "owna-cav" -a "$MERAKI_BOARD" != "axel-bcm" -a \
     "$MERAKI_BOARD" != "entr17" ]; then
  cert_base load
  if [ ! -d "/tmp/certs" ]; then
    mkdir -p /tmp/certs
    if [ -f /storage/certs/cisco_signed_cert.cert ]; then
      cp /storage/certs/cisco_signed_cert.cert /tmp/certs/cisco_signed_cert.cert
      cp /storage/certs/priv_key /tmp/certs/priv_key
    elif [ -f /storage/cert.pem ]; then
      cp /storage/cert.pem /tmp/certs/cisco_signed_cert.cert
      cp /storage/key.pem /tmp/certs/priv_key
    fi
  else
      cp /tmp/certs/sha1/cisco_signed_cert.cert /tmp/certs/cisco_signed_cert_sha1.cert
      cp /tmp/certs/sha1/priv_key /tmp/certs/priv_key_sha1
  fi
  cp /etc/ssl/*.cert /tmp/certs/
else
    mkdir -p /tmp/certs
    cp /etc/ssl/*.cert /tmp/certs/
    cert_tool sha2_installed
    if [ $? -eq 0 ]; then
      echo "Loading sha2..."
      cert_load sha1
      cert_load sha2
    else
      cert_tool sha1_installed
      if [ $? -eq 0 ]; then
        echo "Loading sha1..."
        cert_load sha1
      elif [ -f /storage/certs/cisco_signed_cert.cert ]; then
        cp /storage/certs/cisco_signed_cert.cert /tmp/certs/cisco_signed_cert.cert
        cp /storage/certs/priv_key /tmp/certs/priv_key
      elif [ -f /storage/cert.pem ]; then
        cp /storage/cert.pem /tmp/certs/cisco_signed_cert.cert
        cp /storage/key.pem /tmp/certs/priv_key
      fi
    fi
fi


if [ "$MERAKI_BOARD" == "mallorca" -o "$MERAKI_BOARD" == "entr17" ]; then
    cookie_model=`cat /COOKIE_PID |cut -d '-' -f 2`
    case "$cookie_model" in
        AP1815W|AP1815T|OEAP1815)
            # Wallplate and Teleworker/OEAP SKU have wired0-3 ports
            IFACES="wired0 wired1 wired2 wired3"
            ;;
        *)
            # for all other models, only wired0 is present
            IFACES="wired0"
            ;;
    esac

    for iface in $IFACES; do
        ethtool_disable_offloads $iface
    done
fi
if [ "$MERAKI_BOARD" == "axel-qca" ]; then
    ethtool_disable_offloads "wired0"
fi

# set 160Mhz BW per platform
#  default on(1), turn off(0) if necessary by platform
FILE_160="/var/platform/is160_capable"
if [ "$BOARD_NAME"  == "milos" ] || [ "$BOARD_NAME"  == "duplo" ] || [ "$MERAKI_BOARD" == "axel-bcm" ]; then
    echo 0 > $FILE_160
else
    mkdir -p /tmp/var/platform/
    echo 1 > $FILE_160
fi

if [ "$MERAKI_BOARD" == "barbados" ] || [ "$MERAKI_BOARD" == "axel-mvl" ]; then
    # build power table
    /usr/bin/cli_scripts/loadpwrallrates.sh xx 17
fi
