#!/bin/sh
#
#	Copyright (c) 2014 Brocade Communications Systems, Inc.
#	All rights reserved.
#
#	File name: cfgfosclean
#	Module name: utils/firmware
#
#	This script is used to cleanup the back-end configuration files
#	which are not installed by any RPM. This script will get executed
#	by post-install during fwdl if user specifies factory-default.
#	Also defaults the backend config keys.
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin
FABOS_0_CONF_FILE="/etc/fabos/fabos.0.conf"
FABOS_0_CONF_FILE_TMP="/etc/fabos/fabos.0.conf.tmp"
MNT_FABOS_0_CONF_FILE="/mnt/etc/fabos/fabos.0.conf"

#
# Directories that are scanned.
#
DIRS="
/etc
/var
/fabos/config
/fabos/users/*/.ssh
"

#
#Files that don't belong to any package by default
#
LOOSE_FILES=" 
/var/lib/rpm/*
/var/images
/var/images/*
/var/images/*/*
/var/pkg_bitmap
/var/config/vcs
/var/spool
/var/spool/cron/root
/var/log/fwdl.log
/var/rb_reason
/etc/fstab
/etc/mtab
/etc/fwdl.conf
/etc/fabos/NosSmd/*/*
/etc/fabos/emd
/etc/fabos/upgrade_status1
/etc/config
/etc/ld.so.cache
/etc/dhclientv4/dhclient.leases
/etc/dhclientv6/dhclient.leases
/etc/fabos/skm/IngrianNAE.properties
/etc/ssh_host_rsa_key
/etc/ssh_host_dsa_key
/etc/fabos/vcs
/etc/fabos/Dcmd/Dcmd.Linux.powerpc.local.conf
/etc/confd/docroot
/etc/confd/docroot/*
/etc/confd/docroot/*/*
/etc/confd/docroot/*/*/*
/etc/confd/docroot/*/*/*/*
/etc/confd/docroot/*/*/*/*/*
/etc/confd/docroot/*/*/*/*/*/*
"

KEEP_BASE_LOOSE_FILES="
/etc/fabos/lic_*
/etc/fabos/.lic*
/etc/fabos/lservrc*
"

CONFIG_BACKUP_FILES="
/etc/fabos/fabos.chassis.conf_backup
/etc/raslog.conf_backup
/etc/ssh_config_backup
/etc/sshd_config_backup
/etc/group_backup
/etc/idle.conf_backup
/etc/pam.d/netlogin_backup
/etc/pam.d/ttylogin_backup
/etc/pam.d/sshd_backup
/etc/shadow_backup
/etc/passwd_backup
/etc/hosts_backup
/etc/static-routes.options_backup
"

FWCLEAN_TMP_FILE="/tmp/fwclean.tmp"

setcontext()
{
	if [ "$1" != "chassis" ]
	then
		export FABOS_SWITCHNO=$2
		export CURRENT_VF=$1
		export CHASSIS_ROLEID=0
	else
		export FABOS_SWITCHNO=-1
		export CURRENT_VF=255
		export CHASSIS_ROLEID=0
	fi
	export ROLE_ID=root
}

usage()
{
	echo "Usage: cfgcleanup -h -n -o -p -s"
	echo "	-h: Display the usage of this script."
	echo "	-n: Display the obsolete files without removing them."
	echo "	-o: Skip Verbose mode output."
	echo "	-p: Delete the obsolete files of primary partition."
	echo "	-s: Delete the obsolete files of secondary partition."
}

do_cleanup_internal(){
	$VERBOSE "Checking $ROOT$DIR, please wait ..."
	INSTALLED_FILES=`/usr/bin/find $DIR -name "*" -xdev -print`
	/bin/rpm -qf $INSTALLED_FILES > $FWCLEAN_TMP_FILE 2>&1
	/bin/cat $FWCLEAN_TMP_FILE | grep "is not owned by any package" | /usr/bin/cut -d ' ' -f 2 |
	while read FILE; do
	    case "$LOOSE_FILES_PATTERN" in
	    *:"$FILE":*)
			$VERBOSE "--Retain $ROOT$FILE"
			continue
		;;
	    *)
		SUBFILE=${FILE:0:16}
		if [ "$NO_REMOVE" = 0 ]; then
			if [ "$SUBFILE" != "/tftpboot/common" ]; then
				$VERBOSE "--Remove $ROOT$FILE"
				/bin/rm -f "$FILE"  > /dev/null 2>&1
			fi
		elif [ "$NO_REMOVE" = 1 ]; then
			if [ "$SUBFILE" != "/tftpboot/common" ]; then
				$VERBOSE "--Skip removal $ROOT$FILE"
			fi
		fi
		;;
	    esac
	done
}

# Make this function available to child processes, in particular ones
# started inside chroot
export -f do_cleanup_internal

do_cleanup()
{
	ROOT=$1
	NO_REMOVE=$2
	VERBOSE=$3
	LOOSE_FILES_PATTERN_TEMP="$(echo $LOOSE_FILES | tr -s ' \n\t' :)"
	KEEP_BASE_LOOSE_FILES_PATTERN_TEMP="$(echo $KEEP_BASE_LOOSE_FILES | tr -s ' \n\t' :)"

	if [ $RETAIN_BASE -eq 1 ];then
		LOOSE_FILES_PATTERN=":$LOOSE_FILES_PATTERN_TEMP$KEEP_BASE_LOOSE_FILES_PATTERN_TEMP:"
	else
		LOOSE_FILES_PATTERN=":$LOOSE_FILES_PATTERN_TEMP:"
	fi

	export ROOT NO_REMOVE VERBOSE DIR LOOSE_FILES_PATTERN FWCLEAN_TMP_FILE
	echo Performing configuration cleanup. Please wait ...
	for DIR in $DIRS; do
	    # loop 3 times on each, breaking out if we don't core
	    # This is suppressing the problem in defect 79694
	    # it is not fixing it, just hiding it.
	    for i in 1 2 3 ; do
		( chroot $ROOT bash -c "do_cleanup_internal" )
		[ $? -lt 127 ] && break
		echo "There was a problem cleaning $DIR, retrying"
	    done
	done
	echo "Finished cleanup. "
	echo
	if [ -f $FWCLEAN_TMP_FILE ]; then
		rm $FWCLEAN_TMP_FILE
	fi
}

copy_backup_config()
{
	ROOT=$1	
	for file in $CONFIG_BACKUP_FILES; do
		cp -f $ROOT/$file $ROOT/${file%_backup}
	done
}

#
# main routine
#
PRI=0
SEC=0
NO_REMOVE=0
VERBOSE=echo
UPG_PART=/
RETAIN_BASE=0

while getopts hnopsv:b: opt; do
	case $opt in
		h) usage
	 	   exit 1	
		   ;;
		n) NO_REMOVE=1 
		   ;;
		o) VERBOSE=:
		   ;;
		p) PRI=1
		   ;;
		s) SEC=1
		   ;;
		v)
			# Specifies the upgraded parition
			UPG_PART=$OPTARG
		   ;;
		b)
			RETAIN_BASE=$OPTARG
		   ;;
		\?) usage;
		    exit 1
		   ;;
	esac
done

case "$PRI$SEC" in
  00|11)
	do_cleanup / $NO_REMOVE $VERBOSE
	do_cleanup /mnt $NO_REMOVE $VERBOSE
	;;
  01) do_cleanup /mnt $NO_REMOVE  $VERBOSE ;;
  10) do_cleanup / $NO_REMOVE  $VERBOSE ;;
esac

# Cleanup the FOS-Config.
setcontext 128 0

# Preserve the existing config
rbridge_id=`/fabos/cliexec/config get fabric.domain 2`

if [ $SEC -eq 1 ]; then
	# Delete the persistent config file in secondary partition.
	# This will not reset the config Mem and file in primary but need to make
	# sure that no one commits the config from primary to secondary.
	$VERBOSE "Clearing FOS Config in Secondary."
	/bin/rm -f $MNT_FABOS_0_CONF_FILE

	# Add the necessary configurations.
	echo "fabric.domain:$rbridge_id" >> $MNT_FABOS_0_CONF_FILE
	copy_backup_config /mnt
fi

if [ $PRI -eq 1 ]; then
	# Reset the config Mem and persistent file in primary partition.
	#
	# "Config import" will clear the existing config Mem, then imports
	# the contents of file and then adds the default entries to config Mem.
	#
	# "Config export" will flush the contents of config mem to a specified file.
	# Use the persistent config file for this case.
	$VERBOSE "Clearing FOS Config in Primary."
	echo "fabric.domain:$rbridge_id" > $FABOS_0_CONF_FILE_TMP
	/fabos/cliexec/config import $FABOS_0_CONF_FILE_TMP
	/fabos/cliexec/config export $FABOS_0_CONF_FILE
	/bin/rm -f $FABOS_0_CONF_FILE_TMP
	copy_backup_config
fi

/bin/sync

exit 0
