#!/bin/sh
#
#    Broadcom Proprietary and Confidential.
#    Copyright © 1996-2020 Broadcom.
#    All Rights Reserved.
#    The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
#
#    Restore script to perform FOS configuration restore for firmware downgrade
#    and firmware restore cases.
#
PROGRAM='ccm'

MNT_CCM_PATH="/mnt/etc/fabos/config/scripts/ccm" # This is the new firmware being downloaded
CCM_PATH="/etc/fabos/config/scripts/ccm"  # This is the current / running firmware
# direct invoke ccm.sh
SH_DO_CCM=0
CCM_ECHO='echo -e'
DATE=/bin/date

verbose=1
value=`/fabos/cliexec/config get cfgut.debug 2`
if [ ! -z ${value+x} ] && [ ! -z "$value" ]; then
    verbose=$value
fi

#$CCM_ECHO "path=$0 dirname=$(dirname "$0")"

if [ "$(dirname "$0")" == "/mnt/sbin" ]; then
    cfglog="/mnt/var/log/cfglog"

elif [ "$(dirname "$0")" == "/sbin" ]; then
    cfglog="/var/log/cfglog"

else
    SH_DO_CCM=1
    cfglog="/var/log/cfglog"
fi

#$CCM_ECHO "BASH_SOURCE=$0 CFGLOG=$cfglog"

cfglog_init() {
  if [ -f "$cfglog" ]; then
#      log_size=`wc -c $cfglog | awk '{print $1}'`
#      if [ $log_size -gt 102400 ]; then
	  /bin/cp $cfglog $cfglog.1
	  /bin/date > $cfglog
          /bin/chmod 644 $cfglog
	  /bin/cp $cfglog $cfglog.1
          /bin/chmod 644 $cfglog.1
#      fi
  fi
}
CFGLOG_INIT="cfglog_init"

cfglogf() {
    $DATE >> $cfglog
    $CCM_ECHO $@ >> $cfglog
    if [ $verbose -ge 2 ]; then
	$CCM_ECHO $@
    fi

    /bin/chmod 644 $cfglog
}
CFGLOG="cfglogf"

cfgcmd()
{
    /bin/date -u               >> $cfglog
    $CCM_ECHO "CFGCMD $@:"     >> $cfglog
    $@                         >> $cfglog
    rc=$?                      >> $cfglog
    if [ $rc -ne 0 ]; then
        $CCM_ECHO "CFGCMD ERR:$rc, $@" >> $cfglog
    fi
    /bin/chmod 644 $cfglog
    /bin/cp $cfglog $cfglog.1
    /bin/chmod 644 $cfglog.1
}

CFGCMD="cfgcmd"

#################################################################################################
#####################################################################################
# implemented in postinst otherwise need to load with postinst package
#####################################################################################
$CFGLOG_INIT
$CFGLOG "$PROGRAM $@"
$CFGLOG "do ccm:$0"

if [ $SH_DO_CCM -eq 1 ]; then
    $CFGLOG "Direct execute ccm script"
    TO_MAJOR=$1
    TO_MINOR=$2
    TO_PATCH=$3
fi

    FROM_VER=`rpm -q fabos | cut -f 2 -d '-'`
    FROM_MAJOR=`echo $FROM_VER|cut -d '.' -f 1`
    FROM_MINOR=`echo $FROM_VER|cut -d '.' -f 2`
    FROM_PATCH_STR=`echo $FROM_VER|cut -d '.' -f 3`
    FROM_PATCH=${FROM_PATCH_STR:0:1}
    FROM_RELEASE_STR=`cat /etc/version.conf | grep "Release" | /bin/awk '{print $2}'`
    FROM_RELEASE=${FROM_RELEASE_STR:0:1}

TO_RELEASE_STR=`cat /mnt/etc/version.conf  | grep "Release" | /bin/awk '{print $2}'`
TO_RELEASE=${TO_RELEASE_STR:0:1}

TO_VERSION="$TO_MAJOR$TO_MINOR$TO_PATCH$TO_RELEASE"
FROM_VERSION="$FROM_MAJOR$FROM_MINOR$FROM_PATCH$FROM_RELEASE"

# This is required to run PostPlay and restore scripts
CCM_VERSION="/mnt/etc/fabos/config/scripts/ccm_version.conf"

$CFGLOG "check ccm FROM:$FROM_VERSION TO:$TO_VERSION"

STATIC_POSTPLAY="/mnt/etc/fabos/config/scripts/S90postplay"
STATIC_RESTORE="/etc/fabos/config/scripts/S90restore"

CFGPOSTPLAY_PATH="/mnt/etc/fabos/config/scripts/cfgpostplay"
CFGREST_PATH="/mnt/etc/fabos/config/scripts/cfgrest"
CFGSYNC_PATH="/mnt/etc/fabos/config/scripts/cfgsync"

CFG_PATH=$CFGREST_PATH

set_hdr ()
{
    sfile=$1
    #$CFGLOG "set_hdr Create $1"
    echo "#!/bin/sh"                                                     > $sfile
    echo "#"                                                            >> $sfile
    echo "#    Broadcom Proprietary and Confidential."                  >> $sfile
    echo "#    $sfile"                                                  >> $sfile
    echo "#"                                                            >> $sfile
    chmod 755 $sfile
}


build_postplay ()
{
  CFG_PATH=$CFGPOSTPLAY_PATH
  
  if [ ! -d "$CFG_PATH" ]; then
    $CFGLOG "Fail, ${FUNCNAME[0]} $CFG_PATH not exist"      
    return 1;
  fi

  # copy static cfg scripts to runtime path
  if [ -f $STATIC_POSTPLAY ]; then
    $CFGCMD cp "$STATIC_POSTPLAY" "$CFGPOSTPLAY_PATH"
  else
    $CFGLOG "$STATIC_POSTPLAY not found"
  fi
  return 0
}

build_restore_910_901a ()
{
  CFG_PATH=$CFGREST_PATH

  if [ ! -d "$CFG_PATH" ]; then
    $CFGLOG "Fail, ${FUNCNAME[0]} $CFG_PATH not exist"      
    return 1;
  fi

  if [ -f $STATIC_RESTORE ]; then
    $CFGCMD cp "$STATIC_RESTORE" "$CFG_PATH"
  fi

  $CFGLOG "Create 9.1.0 dynamic restore scripts"

  # if unit test is enabled
  # revert postplay.sh.in test cases set by cfgut.enable
  cfgut_enable=`/fabos/cliexec/config get cfgut.enable 2`
  if [ ! -z ${cfgut_enable+x} ]; then

    #config remove cfgut.CH.9.1key.1 = 1
    cfgkey="cfgut.CH.9.1key.1"
    rfile="$CFG_PATH/S85$cfgkey"
    $CFGLOG "Create 9.1.0 restore scripts:$rfile"
    set_hdr "$rfile"

    echo "#"                                                                >> $rfile
    echo "# include cfg utility lib:cfgutil"                                >> $rfile
    echo "cfgutil=\"/etc/fabos/config/scripts/cfgutil\""                    >> $rfile
    echo ". \$cfgutil"                                                      >> $rfile

    echo "\$CHASSISCMD /fabos/cliexec/config remove $cfgkey"                >> $rfile

    #config remove cfgut.SW.9.1key.4
    cfgkey="cfgut.SW.9.1key.4"
    echo "\$SWITCHCMD -1 /fabos/cliexec/config remove $cfgkey"              >> $rfile
 
    #config set ccfgut.CH.9.0key.4 = 4
    cfgkey="cfgut.CH.9.0key.4"
    rfile="$CFG_PATH/S89$cfgkey"
    $CFGLOG "Create 9.1.0 restore scripts:$rfile"
    set_hdr "$rfile"
    cfgkey_type=2
    evalue=4
    echo "#"                                                                >> $rfile
    echo "# include cfg utility lib:cfgutil"                                >> $rfile
    echo "cfgutil=\"/etc/fabos/config/scripts/cfgutil\""                    >> $rfile
    echo ". \$cfgutil"                                                      >> $rfile
    echo " "                                                                >> $rfile
    echo "setcontext chassis"                                             >> $rfile
    echo "value=\`/fabos/cliexec/config get $cfgkey $cfgkey_type\`"       >> $rfile
    echo "if [ ! -z \${value+x} ]; then"                                  >> $rfile
    echo "  if [ \$value -gt $evalue ]; then"                             >> $rfile
    echo "    \$CFGLOG \"set $cfgkey to $evalue\""                        >> $rfile
    echo "    /fabos/cliexec/config set $cfgkey $cfgkey_type $evalue"     >> $rfile
    echo "  fi"                                                           >> $rfile
    echo "else"                                                           >> $rfile
    echo "    \$CFGLOG \"$rfile fail to get $cfgkey to $evalue\""         >> $rfile
    echo "    \$ECHO \"$rfile fail to get $cfgkey to $evalue\""           >> $rfile
    echo "fi"                                                             >> $rfile
  fi
  return 0
}

do_ccm_cleanup ( ) {

    $CFGLOG "${FUNCNAME[0]}"

    if [ ! -z "$(ls -A $CFGPOSTPLAY_PATH)" ]; then
       $CFGLOG "clean up $CFGPOSTPLAY_PATH"
       $CFGCMD rm $CFGPOSTPLAY_PATH/*
    fi
    
    if [ ! -z "$(ls -A $CFGSYNC_PATH)" ]; then
      $CFGLOG "clean up $CFGSYNC_PATH"
      $CFGCMD rm $CFGSYNC_PATH/*
    fi
    
    if [ ! -z "$(ls -A $CFGREST_PATH)" ]; then
      $CFGLOG "clean up $CFGREST_PATH"
      $CFGCMD rm $CFGREST_PATH/*
    fi
}

display_banner () {
echo "FROM: $FROM_MAJOR $FROM_MINOR $FROM_PATCH $FROM_RELEASE" > "$CCM_VERSION"
echo "TO: $TO_MAJOR $TO_MINOR $TO_PATCH $TO_RELEASE" >> "$CCM_VERSION"
/bin/chmod 644 $CCM_VERSION

$CFGLOG "set ccm FROM:$FROM_VERSION TO:$TO_VERSION"

echo "Configuration Compatibility Management"
}

do_ccm () {
  do_ccm_rc=0
  $CFGLOG "${FUNCNAME[0]} start"      

  # FWDL upgrade cases, TO_VERSION >= 9.1.0
  # buid postplay, move S90postplay to /cfgpostplay
  if [[ "$TO_VERSION" > "$FROM_VERSION" ]]; then
      if [[ "$TO_VERSION" > "910" ]] || [[ "$TO_VERSION" == "910" ]]; then 
	  display_banner
	  build_postplay
	  do_ccm_rc=$?
      fi
  else
    # FWDL dowgrade cases,
    # FROM_VERSION >= 9.1.0 and 9.0.1 < TO_VERSION < 9.1.0 
    # build restore for 9.1.x downgrade to 9.0.1a and after
    if [[ "$FROM_VERSION" > "910" ]] || [[ "$FROM_VERSION" == "910" ]]; then 
      if [[ "$TO_VERSION" < "910" ]] && [[ "$TO_VERSION" > "901" ]]; then
	  display_banner
	  build_restore_910_901a
	  do_ccm_rc=$?
      else
	if [ $VF_ENABLED -ne $STS_OK ]; then
		fid=`echo $CONTEXTS | cut -d ' ' -f1`
		$CFGLOG "set to FID $fid"
		eval $(/fabos/cliexec/lscfg_util --switch $fid)
		$CFGLOG "Not CCM supported version $FROM_VERSION to $TO_VERSION, remove ccm.complete, FABOS_SWITCHNO=$FABOS_SWITCHNO CURRENT_VF=$CURRENT_VF"
	else
		$CFGLOG "Not CCM supported version $FROM_VERSION to $TO_VERSION, remove ccm.complete"
	fi
	/fabos/cliexec/config remove ccm.complete
	/fabos/cliexec/config commit
      fi
    fi
  fi
  # cfgut return err
  cfgut_ccm_err=`/fabos/cliexec/config get cfgut.ccm_err 2`
  if [ ! -z ${cfgut_ccm_err+x} ] && [ ! -z "$cfgut_ccm_err" ]; then
   $CFGLOG "${FUNCNAME[0]} cfgut.ccm_err=$cfgut_ccm_err"
   echo "cfgut.ccm_err=$cfgut_ccm_err"
   return $cfgut_ccm_err
  fi

  $CFGLOG "${FUNCNAME[0]} retrun=$do_ccm_rc"     
  return $do_ccm_rc
}

if [ $SH_DO_CCM -eq 1 ]; then
    # echo "Configuration Compatibility Management call do_ccm"
    do_ccm
    exit
fi

return 0
