# This script file contains actual plugins which are responsible for
# clearing configuration.
# For any new configurations owner need to add it's resetting procedure
# in this script.
# Below is procedure to add new plugins.
# Operations are divided into two categories:
# If the reset operations need to be performed in both active and
# stand-by CP, it should be added in array active_standby_reset_conf as below.
#,
# function active_standby_reset_operation() {
#      /* Perform operation */
# }
# active_standby_reset_conf[Next_Available_Index]=”active_standby_reset_operation”
#
#
# If the reset  operation needs to be executed only in active CP, it should be added
# in array active_reset_conf.
# Example:
# function active_reset_operation() {
#       /* Perform operation */
# }
# active_reset_conf[Next_Available_Index]="active_reset_operation"
#


################################################################
# Plugins to reset configuration on both active and stand-by CPs.
###############################################################

function delete_fabos_conf() {
	line=$1
        cd /
        filter=$(/bin/ls $line 2>>/var/log/factoryreset.log | /usr/bin/wc -w 2>>/var/log/factoryreset.log)
        if [ $filter -gt 0 ]; then
                /bin/rm -rf $line 2>>/var/log/factoryreset.log
                if [ -f "/mnt/etc/fabos/config/switch-conf" ]; then
                        if [ -f "/mnt/etc/fabos/config/vf-conf" ]; then
                                cd /mnt 2>>/var/log/factoryreset.log
                                /bin/rm $line 2>>/var/log/factoryreset.log
                        fi
                fi
       	fi 
}

function delete_vf_conf() {
	echo -e "Deleting VF configurations and switch configurations....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	/bin/rm  -f /etc/fabos/config/switch-conf.$swbdnum 2>>/var/log/factoryreset.log
	/bin/rm  -f /etc/fabos/config/switch-conf.$swbdnum.save 2>>/var/log/factoryreset.log
	/bin/rm  -f /mnt/etc/fabos/config/switch-conf.$swbdnum 2>>/var/log/factoryreset.log
	/bin/rm  -f /mnt/etc/fabos/config/switch-conf.$swbdnum.save 2>>/var/log/factoryreset.log
	/bin/rm  -f /etc/fabos/config/vf-conf.$swbdnum 2>>/var/log/factoryreset.log
	/bin/rm  -f /etc/fabos/config/vf-conf.$swbdnum.save 2>>/var/log/factoryreset.log
	/bin/rm  -f /mnt/etc/fabos/config/vf-conf.$swbdnum 2>>/var/log/factoryreset.log
	/bin/rm  -f /mnt/etc/fabos/config/vf-conf.$swbdnum.save 2>>/var/log/factoryreset.log
	/bin/rm  -f /etc/fabos/config/vf-conf-new.$swbdnum 2>>/var/log/factoryreset.log
	/bin/rm  -f /etc/fabos/config/vf-conf-new.$swbdnum.save 2>>/var/log/factoryreset.log
	/bin/rm  -f /mnt/etc/fabos/config/vf-conf-new.$swbdnum 2>>/var/log/factoryreset.log
	/bin/rm  -f /mnt/etc/fabos/config/vf-conf-new.$swbdnum.save 2>>/var/log/factoryreset.log
	/bin/ls -l /etc/fabos/config/switch-conf.$swbdnum /mnt/etc/fabos/config/switch-conf.$swbdnum 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	/bin/ls -l /etc/fabos/config/vf-conf.$swbdnum /mnt/etc/fabos/config/vf-conf.$swbdnum 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	/bin/ls -l /etc/fabos/config/vf-conf-new.$swbdnum /mnt/etc/fabos/config/vf-conf-new.$swbdnum 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	/bin/ls -l /etc/fabos/config/*.save 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_standby_reset_cli_firmware_hist() {
	echo -e  "Deleting cli and firmware history....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	delete_fabos_conf 'var/log/glog*'
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}

function active_standby_reset_licenses() {
	echo -e "Deleting all license configurations....\c" | /usr/bin/tee -a /var/log/factoryreset.log

	# Deleting all LCs in the license_certificates directory
	/bin/rm  /etc/fabos/license_certificates/* 2>>/var/log/factoryreset.log
	/bin/rm  /mnt/etc/fabos/license_certificates/* 2>>/var/log/factoryreset.log

	# Deleting licensedb & licensedbext files
	/bin/rm  /etc/fabos/licensedb 2>>/var/log/factoryreset.log
	/bin/rm  /mnt/etc/fabos/licensedb 2>>/var/log/factoryreset.log
	/bin/rm  /etc/fabos/licensedbext 2>>/var/log/factoryreset.log
	/bin/rm  /mnt/etc/fabos/licensedbext 2>>/var/log/factoryreset.log

	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}

#############################################################################
# Array to plugins which reset configuration on both active and stand-by CPs.
#############################################################################
declare -a active_standby_reset_conf
active_standby_reset_conf[0]="delete_vf_conf"
active_standby_reset_conf[1]="active_standby_reset_cli_firmware_hist"
active_standby_reset_conf[2]="active_standby_reset_licenses"


################################################################
# Plugins to reset configuration on active CP.
###############################################################
function active_reset_conf_fips() {
	echo -e "Deleting fips configurations....\c" |/usr/bin/tee -a /var/log/factoryreset.log
    /fabos/cliexec/secfactoryreset --rfd 2>>/var/log/factoryreset.log
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_conf_traceBuffer() {
	echo -e "Clearing trace buffer and core files....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	tracedump -c 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	tracedump -R 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	/fabos/link_rbin/coreshow -R 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_conf_raslogBuffer() {

	echo "errordumpall before clearing raslogs"  1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	/fabos/factory/errdumpall | /usr/bin/tail -n 500 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	sleep 4;
	echo -e "Clearing  raslog ....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	errclear 2>>/var/log/factoryreset.log
	auditdump -c 2>>/var/log/factoryreset.log
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_maps() {
	echo -e "Deleting maps configurations....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	/fabos/rbin/mapspurge 2>>/var/log/factoryreset.log
	sleep 2;
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_dnsInfo() {
	echo -e "Deleting dns configurations....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	dnsconfig --delete 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_switch_host_chassis_name() {
	echo -e "Resetting switch name, host name and chassis name....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	myFABOS_SWITCHNO=$FABOS_SWITCHNO
	FABOS_SWITCHNO=0;
	switchname sw0 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	chassisname sw0 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	FABOS_SWITCHNO=${myFABOS_SWITCHNO};
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_diagConf() {
	echo -e "Resetting diagonstic configurations....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	diagenablepost 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	/fabos/rbin/burninerrclear 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_BSN() {
	echo -e "Resetting BSN configurations....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	supportlink --default 2>>/var/log/factoryreset.log
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_Banner() {
	echo -e "Resetting Banner configurations....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	myFABOS_SWITCHNO=$FABOS_SWITCHNO
	FABOS_SWITCHNO=0;
	bannerset "" 1>>/var/log/factoryreset.log 2>>/var/log/factoryreset.log
	FABOS_SWITCHNO=${myFABOS_SWITCHNO};
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_motd() {
	echo -e "Resetting motd configurations....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	motd --set "" 2>>/var/log/factoryreset.log
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}
function active_reset_sec() {
	/fabos/link_bin/ag --modeshow | grep "NOT enabled" > /dev/null 2>&1
	if [ $? == 0 ]; then
		echo -e "Resetting security fdd configurations....\c" |/usr/bin/tee -a /var/log/factoryreset.log
		myFABOS_SWITCHNO=$FABOS_SWITCHNO
		FABOS_SWITCHNO=0;
		/fabos/sbin/authutil --set -a all 2>>/var/log/factoryreset.log
		/fabos/sbin/authutil --set -g '*' 2>>/var/log/factoryreset.log
		/fabos/sbin/authutil --set -h all 2>>/var/log/factoryreset.log
		/fabos/link_abin/fddcfg --fabwideset "" >> /var/log/factoryreset.log 2>&1 
		/fabos/link_abin/fddcfg --localaccept "SCC;DCC;PWD;FCS;AUTH;IPFILTER" >> /var/log/factoryreset.log 2>&1
	fi
	echo -e "Resetting security fdd config keys....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.fabwidecfg" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hash.dcc" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hash.fbw" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hash.scc" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hash.fcs" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hash.pwd" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hashpresent.dcc" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hashpresent.fbw" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hashpresent.fcs" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hashpresent.pwd" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "fdd.hashpresent.scc" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config remove "acl.clear" 2>>/var/log/factoryreset.log
	/fabos/cliexec/config set sec.mode 1 0
	FABOS_SWITCHNO=${myFABOS_SWITCHNO};
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}

function active_reset_ag() {
	echo -e "Resetting to Native mode if AG mode is enabled\c" |/usr/bin/tee -a /var/log/factoryreset.log
	/fabos/link_bin/ag --modeshow | grep "NOT enabled" > /dev/null 2>&1
	if [ $? != 0 ]; then
		if [ $swbdnum == 175 ];then
			echo y | /fabos/rbin/agdbg --reset | /usr/bin/tee -a /var/log/factoryreset.log
		else
			echo y | /fabos/link_bin/ag --modedisable | /usr/bin/tee -a /var/log/factoryreset.log
		fi
	else
		if [ $swbdnum == 175 ];then
			echo y | /fabos/link_bin/ag --modeenable | /usr/bin/tee -a /var/log/factoryreset.log
		fi
	fi
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}

function active_reset_embedded() {
if [ $swbdnum -eq 176 ]; then
		echo -e "Resetting Factory Configs for Graphite32\c" |/usr/bin/tee -a /var/log/factoryreset.log
		/fabos/cliexec/setfactorydefault | /usr/bin/tee -a /var/log/factoryreset.log
 fi   
	if [ $swbdnum -eq 175 ]; then
		echo -e "Resetting for the starlynx \c" | /usr/bin/tee -a /var/log/factoryreset.log
		/fabos/cliexec/config set fabric.rdp_poll_cycle 2 0 | /usr/bin/tee -a /var/log/factoryreset.log
		/fabos/cliexec/config set fc4.fcp.vendorId 5 Dell | /usr/bin/tee -a /var/log/factoryreset.log
		/fabos/cliexec/configcommit | /usr/bin/tee -a /var/log/factoryreset.log
		/fabos/sbin/license --reserve -port 0 | /usr/bin/tee -a /var/log/factoryreset.log
		/fabos/sbin/license --reserve -port 17 | /usr/bin/tee -a /var/log/factoryreset.log
		/fabos/link_sbin/ipaddrset  -ipv4 -add -dhcp on | /usr/bin/tee -a /var/log/factoryreset.log
		/fabos/link_sbin/ipaddrset -ipv6 -dhcpv6 | /usr/bin/tee -a /var/log/factoryreset.log
		myFABOS_SWITCHNO=$FABOS_SWITCHNO
		FABOS_SWITCHNO=0;
		switchname MXG610s | /usr/bin/tee -a /var/log/factoryreset.log
		chassisname MXG610s | /usr/bin/tee -a /var/log/factoryreset.log
		FABOS_SWITCHNO=${myFABOS_SWITCHNO};
	fi
}
function active_reset_firmware_check() {
	echo -e "Resetting firmware check configuration....\c" |/usr/bin/tee -a /var/log/factoryreset.log
	/fabos/link_bin/firmwarecheck --disable -boot 2>>/var/log/factoryreset.log
	echo "done" |/usr/bin/tee -a /var/log/factoryreset.log
}

#############################################################################
# Array to plugins which reset configuration only on active CP.
#############################################################################
declare -a active_reset_conf
active_reset_conf[0]="active_reset_conf_fips"
active_reset_conf[1]="active_reset_conf_traceBuffer"
active_reset_conf[2]="active_reset_conf_raslogBuffer"
active_reset_conf[3]="active_reset_maps"
active_reset_conf[4]="active_reset_dnsInfo"
active_reset_conf[5]="active_reset_switch_host_chassis_name"
active_reset_conf[6]="active_reset_diagConf"
active_reset_conf[7]="active_reset_BSN"
active_reset_conf[8]="active_reset_Banner"
active_reset_conf[9]="active_reset_motd"
active_reset_conf[10]="active_reset_sec"
active_reset_conf[11]="active_reset_embedded"
active_reset_conf[12]="active_reset_firmware_check"
active_reset_conf[13]="active_reset_ag"  # keep this as the last

