#!/bin/sh

###################
#
# spare-package init script
#
# chkconfig: 345 80 --
# description: start a check on rpm database to verify that each package name is
# the only one available
#
###################
#
# Changelog:
#
#   - Tue Aug 03 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.fr>
#   * script creation associated to crms00242620
#
#   - Wed Aug 18 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#   * add a lock on the check of RPM database
#
#   - Wed Nov 24 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#   * check whether upgrade process is enabled before analyzing the database
#
#   - Tue Feb 29 2012 - Thanh lam NGUYEN <thanh-lam.nguyen@alcatel-lucent.com>
#   * Fix crms00362595: Set reset in loop after reset flash R250 step1 version SUP1 (01.003.0) on OT 1.1
#
##################

# Source global script to access some common functions
. /usr/lib/upgrade/rpmdbbackup-addon.sh

check_dbl=$(which dblcheck)
statusfile=/config/upgrade/status

# Retrieve current configuration
if [ -f /etc/init.d/rc.config ]; then
    . /etc/init.d/rc.config
    readCurrentConfig
fi

######
# force function
# force the check of the database
######
force() {
    local FUNCTION=force
    ENTER_FUNCTION
    local ret=

    # Set lock file
    save_pid

    $check_dbl -e
    ret=$?

    # unset it
    remove_pid
    
    # Treat the error case here (i.e. returned greater than EXIT_NO_ERROR)
    if [[ $ret -gt 2 ]]; then
	logger -s -t upgrade -p local0.NOTICE "Failed to check for spare packages in rpm database"
    fi

    RETURN ${ret}
}

# +++++ crms00362595 +++++
######
# check_restore_sig function
# Check the rpm signature of retorepool
# Return code:
#	0	Ok, everything is correct
#	1	A rebuild of the catabase should be performed
######
check_restore_sig() {
	local FUNCTION=check_restore_sig
	ENTER_FUNCTION
	local ret=0 no_sig sig_success sig_fail nb_rpm nb_key
	nb_key=$(rpm -qa | grep gpg -c)
	if ! is_unlocked && [[ $nb_key -ne 0 ]]; then
		# Locked version, verify the restore pool
		ret="$(rpm -K $restoredir/*.rpm 2>/dev/null)"
		nb_rpm=$(echo "$ret" | grep . -c)
		no_sig=$(echo "$ret" | grep -vi " rsa " | grep . -c)
		sig_success=$(echo "$ret" | grep " rsa " | grep . -c)
		sig_fail=$(echo "$ret" | grep " RSA " | grep . -c)
		: "nb_rpm=$nb_rpm"
		: "no_sig=$no_sig"
		: "sig_sucess=$sig_success"
		: "sig_fail=$sig_fail"
		# do a restore only if some rpms are signed and they all failed the rsa signature checked
		if [[ $nb_rpm -ne 0 && $no_sig -ne $nb_rpm && $sig_success -eq 0 ]]; then
			ret=1
		else
			ret=0
		fi
	fi
	RETURN $ret
}

######
# start function
######
start() {
	local FUNCTION=start
	local ret=0 STEP=pre0 go=0
	ENTER_FUNCTION

	# read status file
	[[ -f "$statusfile" ]] && source $statusfile

	# do the check only if upgrade is enabled and the step is Pre[1-5]
	if [[ "$ADMCFG_UPDATE_ENABLE" == "true" ]]; then
		# Upgrade is enabled
		if ! check_restore_sig; then
			# Could not check the signature of the RPMs in the restore pool
			logger -t upgrade -p local0.EMERG "${0##*/} will force a restore due to signature checking"
			do_restore
			# the restore was not possible, rebuild the rpm database
			displaymsg "Rebuilding rpm database starting now, it takes ~10mn, please wait..."
			logger -s -t upgrade -p local0.EMERG "${0##*/} Rebuilding rpm database start"
			u=$(udate)
			rpm --rebuilddb
			u=$(udate $u)
			logger -s -t upgrade -p local0.EMERG "${0##*/} Rebuilding rpm database end"
			if [[ $STEP == pre0 ]]; then
				# we are in Pre0, removing the checksum to force build scenario to be run
				rm $profilesum
			fi
		fi
		if [[ "${STEP%[1-5]}" == "Pre" ]]; then
			# We are in Pre1, Pre2, Pre3, Pre4 or Pre 5
			go=1
		fi
	fi

	if [[ $go -eq 1 ]]; then
		force
		ret=$?
	else
		logger -s -t upgrade -p local0.NOTICE "No check on database is needed" && return 0
	fi


	RETURN $ret
}
# ----- crms00362595 -----

######
# stop function
# function not used
######
stop() {
    local FUNCTION=stop
    ENTER_FUNCTION

    echo "Nothing to stop"
    RETVAL=0

    RETURN
}

# See how we were called.
echo entering $0 ...
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    force)
        force
        ;;
    *)
        echo $"Usage: $0 {start|stop|force}"
        ;;
esac

echo leaving $0 ...

exit ${RETVAL}
