#!/bin/bash
# Version 2
# This script version is for 202006
generate_from_config_db() {
        echo "Get HwSKU and Platfrom from the database"
        show platform summary &>/dev/null
        [ "$?" -ne 0 ] && echo "Failed to get HwSKU and Platfrom from the datebase." && return -1
        HwSKU=`show platform summary | awk '/HwSKU/{print $2}'`
        Platform=`show platform summary | awk '/Platform/{print $2}'`
}
generate_from_host_machine_config(){
        echo "Retry: Get HwSKU and Platfrom from /host/machine.conf"
        number=`cat /host/machine.conf|grep -n "onie_machine="|awk -F ':' '{print $1}'`
        number=${number}"p"
        HwSKU=`cat /host/machine.conf|sed -n $number|awk -F '=' '{print $2}'`
        temp=`echo $HwSKU|awk -F '_' '{print $2"_"$3}'|tr '[:lower:]' '[:upper:]'`
        HwSKU="Accton_"${temp}
        HwSKU=`echo $HwSKU|sed -e 's/_/-/g'`
        number=''
        number=`cat /host/machine.conf|grep -n "onie_platform="|awk -F ':' '{print $1}'`
        number=${number}"p"
        Platform=`cat /host/machine.conf|sed -n $number|awk -F '=' '{print $2}'`
}
generate_from_config_db
[ "$?" -ne 0 ] && generate_from_host_machine_config
sudo sonic-cfggen -H -p /usr/share/sonic/device/$Platform/platform.json --preset t1 -k $HwSKU > ~/config_db.json
sudo cp /etc/sonic/config_db.json /etc/sonic/this_is_backup_of_config_db_from_restore_script.json
sudo mv ~/config_db.json /etc/sonic/config_db.json
echo "/etc/sonic/config_db.json is restored to default"

#echo "Rebooting the switch"
#sudo reboot

