#!/bin/sh
#
#    Broadcom Proprietary and Confidential.
#    Copyright © 1996-2020 Broadcom.
#    All Rights Reserved.
#    The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
#
#    CfgD script is created in 9.0.1a to take on the partial 9.1.0 CfgD daemon
#    role to manage the 9.0.1a CCM scripts. It includes executing CCM restore
#    scripts, copy restore scripts to /cfgsync directory, and later cleaning
#    up the CCM directories.

PROGRAM='CfgD'
cfgutil="/etc/fabos/config/scripts/cfgutil"
umask 0

#
#include cfg utility
#
if [ -e "$cfgutil" ]
then
    . $cfgutil
    rc=$?
    if [ $rc -ne 0 ]
    then
      echo "Error -- $cfgutil err=$rc"
      exit 3
    fi
else
    echo "Error -- could not locate configuration common library $cfgutil"
    exit 3
fi

CFGPOSTPLAY_PATH="/etc/fabos/config/scripts/cfgpostplay"
CFGREST_PATH="/etc/fabos/config/scripts/cfgrest"
CFGSYNC_PATH="/etc/fabos/config/scripts/cfgsync"
CFG_TYPE=""
CFG_PATH=$CFGPOSTPLAY_PATH
cfg_commit=0
RESTORE_BANNER="Configuration Restore In Progress..."
POSTPLAY_BANNER="Configuration PostPlay In Progress..."

display_usage () {
    $ECHO "Usage: $PROGNAME -[d:s:r:p:c:t:vh]";
    $ECHO "     -d cfgpath  # cfg script path";
    $ECHO "     -s 0/1      # do_sync 0/1";
    $ECHO "     -r 0/1      # do_restore 0/1";
    $ECHO "     -p 0/1      # do_postplay 0/1";
    $ECHO "     -c 0/1      # cleanup cfgrest or cfgsync";
    $ECHO "     -t cfgtype  # dynamic CCM type";
    $ECHO "     -v          # verbose mode";
    $ECHO "     -h          # this help";
    exit 1;
}

#MAIN
# Get the command-line options
# default move reset to sync 
CFG_CLEANUP=1
CFG_SYNC=1
DO_SYNC=0
DO_REST=0
DO_POSTPLAY=0

while getopts "d:s:r:p:c:t:vh" Option
do
    case $Option in
	d ) CFG_PATH=$OPTARG;;
	s ) DO_SYNC=$OPTARG;;
	r ) DO_REST=$OPTARG;;
	p ) DO_POSTPLAY=$OPTARG;;
	c ) CFG_CLEANUP=$OPTARG;;
	t ) CFG_TYPE=$OPTARG;;
	v ) verbose=1 ;;
        h ) display_usage ;;
	\? ) display_usage ;;
	* ) display_usage ;;
    esac
done

$CFGLOG_INIT

$CFGLOGD "$PROGRAM $@"

#
# $1: configuration script path
# $2: configuration exeuction banner
#
do_scripts ()
{
  SPATH=$1

  if [ ! -d "$SPATH" ]
  then
    $CFGLOG "Fail:$SPATH path not found"
    return 0;
  fi

  cfg_list=`ls $SPATH | wc -l`
  if [ $cfg_list == 0 ]; then
    $CFGLOG "Empty $SPATH"
    return 0;
  fi
  
  $ECHO "$2"

  if [ $# -gt 1 ]; then
      $CFGLOG "$2"
  fi

  cfg_list=`ls -1 $SPATH | sort -nr`
  $CFGLOG "$SPATH Scripts:$cfg_list"

  for cfg in $cfg_list
  do
      $CFGCMD $SPATH/$cfg
  done

  cfg_commit=1

  return 1;
}

do_postplay ()
{ 
  if [ $DO_POSTPLAY -eq 1 ]; then
    $CFGLOG "do_postplay on $CFGPOSTPLAY_PATH"
    do_scripts $CFGPOSTPLAY_PATH "$POSTPLAY_BANNER"
  fi

  if [ $CFG_CLEANUP -eq 1 ]   # may not want to clenaup in unit test
  then
      if [ ! -z "$(ls -A $CFGPOSTPLAY_PATH)" ]; then
	  $CFGLOG "clean up $CFGPOSTPLAY_PATH"
	  $CFGCMD rm $CFGPOSTPLAY_PATH/*
      fi
  fi
}

do_sync ()
{
  if [ $DO_SYNC -eq 1 ]; then
    SYNC_BANNER="Configuration Restore In Progress..."
    do_scripts $CFGSYNC_PATH "$SYNC_BANNER"
  fi

  # clean up /cfgsync
  if [ $CFG_CLEANUP -eq 1 ]   # may not want to clenaup in unit test
  then
    if [ ! -z "$(ls -A $CFGSYNC_PATH)" ]; then
      $CFGLOG "clean up $CFGSYNC_PATH"
      $CFGCMD rm $CFGSYNC_PATH/*
    fi
  fi
}

do_rest ()
{
  CFG_PATH=$CFGREST_PATH
  $CFGLOG "do_rest on $CFG_PATH"
  
  if [ $DO_REST -eq 1 ]; then
      do_scripts $CFG_PATH "$RESTORE_BANNER"

      # move cfgrest to cfgsync
      # move cfgrest to cfgsync, will be done by CfgD in 9.1
      $CFGLOG "move $CFG_PATH to $CFGSYNC_PATH"
      if [ -d "$CFGSYNC_PATH" ]; then
	if [ ! -z "$(ls -A $CFG_PATH)" ]; then
	  cfg_list=`ls -1 $CFG_PATH | sort -nr`
	  for cfg in $cfg_list
	  do
	    $CFGCMD cp "$CFG_PATH/$cfg" "$CFGSYNC_PATH"
	  done
	else
	  $CFGLOG "Empty $CFG_PATH"
	fi
      else
	$CFGLOG "Fail:$CFGSYNC_PATH path not found"
      fi
  fi

  if [ $CFG_CLEANUP -eq 1 ]
  then
    if [ ! -z "$(ls -A $CFG_PATH)" ]; then
	$CFGLOG "clean up $CFG_PATH"
	$CFGCMD rm $CFG_PATH/*
    fi
  fi
}

if [ "$CFG_TYPE" == "cfgpostplay" ]; then
    do_sync
    do_postplay

elif  [ "$CFG_TYPE" == "cfgrest" ]; then
    do_rest
fi

if [ $cfg_commit -eq 1 ]; then
  $CHASSISCMD /fabos/cliexec/config commit
  $SWITCHCMD -1 /fabos/cliexec/config commit
fi

$CFGLOGD "$PROGRAM Exit"
exit 0