#!/bin/bash
#
# cs_list_clone_files
#
# (c) 2016-2017 SUSE Linux GmbH, Germany.
# (c) 2018-2019 SUSE LLC
# Author: L.Pinne.
# GNU General Public License 2. No warranty.
# http://www.gnu.org/licenses/gpl.html
#
# Version: 2019-11-28 SLES12, SLES15
#
# shellcheck disable=SC1090

EXE="$0"

CFG="/etc/ClusterTools2/cs_list_clone_files"
test -s $CFG && source $CFG

test -z "${CLONE_FILES}" &&\
	CLONE_FILES="
/etc/HOSTNAME
/boot/grub/menu.lst
/boot/grub2/grub.cfg
/etc/udev/rules.d/70-persistent-net.rules
/etc/sysconfig/network/ifcfg-*
/etc/fstab
/etc/iscsi/initiatorname.iscsi
/etc/machine-id
/etc/rhn/rhn.conf
/etc/sysconfig/rhn/systemid
/etc/sysconfig/rhn/osad-auth.conf
/etc/sshd/ssh_host_*
/etc/SUSEConnect
/etc/zmd/deviceid
/etc/zmd/secret
/etc/zypp/credentials.d/*credentials
/etc/zypp/services.d/*
/root/.ssh/*
/var/cache/SuseRegister/lastzmdconfig.cache
/var/lib/dbus/machine-id
"
# TODO double check list above
# TODO application files, f.e. /etc/oraInst.loc or even /etc/smt.conf

CFGVAR="
CLONE_FILES
"

function help(){
	echo "usage:	$(basename "$EXE") [OPTION]"
	echo
	echo " --help		show help"
	echo " --version	show version"
	echo " --writecfg	show some internal variables"
	echo " --content	show content of files"
	echo " --md5sum 	show md5sum of files"
	echo
}


function writecfg(){
	echo -e "# $CFG \n# For SLES11,12.\n#"
	for c in $CFGVAR; do
		echo "${c}=\""
		# shellcheck disable=SC2086
		echo ${!c} | tr " " "\n"
		echo "\""
		echo "#"
	done
}


# TODO call cs_sum_base_config instead of having own function
function sum_config(){
	S="NA"
	test -r "$f" && S=$(md5sum "$f" | awk '{print $1}')
	echo "$f = $S"
}


# TODO call cs_sum_base_config instead of having own function
function sum_files(){
	nf=1
	# TODO: better loop
	for f in ${CONF_FILES}; do
		(( nf++ ))
		S="NA"
		test -r "$f" && S=$(md5sum "$f" | awk '{print $1}')
		echo "$f = $S"
	done
	echo "INFO: $nf files" >>/dev/stderr
}


function show_files(){
	nf=1
	f=$CFG
	for f in ${CONF_FILES}; do
		(( nf++ ))
		S="NA"
		echo "#########################################################"
		test -r "$f" && S=$(grep -v "^[[:blank:]]*#" "$f" | tr -s "\n")
		echo "# $f :"
		echo "$S"
		echo
	done
	echo "INFO: $nf files" >>/dev/stderr
}


# main()
case $1 in
	-v|--version)
		echo -n "$(basename "$EXE") "
		head -11 "$EXE" | grep "^# Version: "
		exit
	;;
	-w|--writecfg)
		writecfg
		exit
	;;
	-c|--content)
		test $UID -gt 0 && echo "please call as root." && exit
		CONF_FILES="${CLONE_FILES}"
		show_files
		exit
	;;
	-m|--md5sum)
		test $UID -gt 0 && echo "please call as root." && exit
		CONF_FILES="${CLONE_FILES}"
		sum_files
		exit
	;;
	*)
		help
		exit
	;;
esac
#
