#!/bin/sh
##################################################################
#
# Copyright (C) 2002 R Bos
#
# File:           $RCSfile: aptrpm-1,v $
# Revision:       $Revision: 1.2 $
# Last change:    $Date: 2002/11/25 04:37:57 $
# Last change by: $Author: corsepiu $
#
# Send suggestions to: apt4rpm-devel@lists.sourceforge.net
# Homepage: http://apt4rpm.sourceforge.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# For a copy of the GNU General Public License, visit
# http://www.gnu.org or write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##################################################################

function my_exit ()
{
	if [ -f /tmp/aptrpm-$$-1 -o -f /tmp/aptrpm-$$-2 ]
	then
		cat /tmp/aptrpm-$$-1 /tmp/aptrpm-$$-2 | mailx -s aptrpm root
	fi
	rm -f /tmp/aptrpm-$$-1 /tmp/aptrpm-$$-2
}

trap my_exit HUP INT QUIT TERM EXIT
PATH=/sbin:/usr/sbin:/bin:/usr/bin

while [ $# -gt 0 ]
do
	case $1 in
	  --auto)		RUN_MODE=auto;;
	  --checksign)		OPT_CHECK_SIGN=true;;
	  --no-checksign)	OPT_CHECK_SIGN=false;;
	  --no-run-config)	OPT_RUNPOSTSCRIPT=false;;
	  --run-config)		OPT_RUNPOSTSCRIPT=true;;
	  --test)		OPT_SIMULATE=true; ARGS="$ARGS $1";;
	  --*)			ARGS="$ARGS $1";;
	   -*i*)		ARGS="$ARGS $1"; OPER_MODE="install";;
	   -*e*)		ARGS="$ARGS $1"; OPER_MODE="remove";;
	   -*U*)		ARGS="$ARGS $1"; OPER_MODE="update";;
	    -h)                  echo "Argument -h ignored";;

	    *)			RPMS="$RPMS $1";;
	esac
	shift
done

if [ "$RUN_MODE" == "auto" ]
then
	# Started from a cronjob...
	exec 1>/tmp/aptrpm-$$-1
	exec 2>/tmp/aptrpm-$$-2
fi

if [ "$OPT_SIMULATE" == "true" ]
then
	SIMULATE=echo
fi

# Check rpm gpg signature?
if [ "$OPER_MODE" == "install" -o "$OPER_MODE" == "update" ]
then
	if [ -n "$OPT_CHECK_SIGN" ]
	then
		CHECK_SIGN=$OPT_CHECK_SIGN
	else
		CHECK_SIGN=true
		eval $(apt-config shell CHECK_SIGN RPM::CheckSign/b)
	fi

	if [ "$CHECK_SIGN" == "true" ]
	then
		if [ -x "$(type -p gpg)" ]
		then
			GPG_USER_DIR=/root
			eval $(apt-config shell GPG_USER_DIR RPM::gpgUserDir)
		else
			echo "Error: gpg not found or not executable" >&2
			exit 1
		fi
	fi

elif [ "$OPER_MODE" == "remove" ]; then
	CHECK_SIGN=false
else
	( echo "Error: incorrect operation mode encountered"
	  echo "Provide 1 of the operation modes -U, -i or -e"
	) >&2
	exit 1
fi

# Is a kernel package going to be updated?
KERNELPKG=k_deflt,k_smp,kernel
eval $(apt-config shell KERNELPKG SystemConfig::KernelPkg)

PATTERN=$(echo $KERNELPKG | sed -e 's+^+\^+' -e 's+,+|\^+g')
for FILE in $RPMS
do
	# The grep is only for the name, while the variabele
	# pkg includes the name and the version (so don't do a
	# word grep).
	basename $FILE | sed -e 's+%5f+_+g' | grep -qE $PATTERN
	if [ $? == 0 ]
	then
		kernel_update=true
	fi

	if [ "$CHECK_SIGN" == "true" ]
	then
		echo "Checking the signature."
		SIGN_CHECK=$(HOME=$GPG_USER_DIR /bin/rpm -K $FILE 2>&1)
		echo $SIGN_CHECK

		case ${SIGN_CHECK##*:} in
		*gpg* )
			# Correctly signed package
			GPG_OK="$GPG_OK $FILE"
			;;
		*GPG* )
			( echo -n "Error: ${FILE##*/} has invalid or "
			  echo "unknown GPG key"
			  QUAL="  File     : $FILE\n"
			  QUAL="$QUAL  Name     : %{NAME}\n"
			  QUAL="$QUAL  Feedback : %{PACKAGER}\n"
			  QUAL="$QUAL  Buildhost: %{BUILDHOST}\n"
			  /bin/rpm -qp --queryformat "$QUAL" $FILE
			  echo
			) >&2
			GPG_FAIL_INCORRECT="${FILE##*/} $GPG_FAIL_INCORRECT"
			;;
		*"rpmReadSignature failed"*|*"readLead failed" )
			( echo "Error: read signature failed (Corrupt file?)"
			  echo " File: $FILE"
			) >&2
			GPG_FAIL_CORRUPT="${FILE##*/} $GPG_FAIL_CORRUPT"
			;;
		*)
			( echo "Error: ${FILE##*/} is not GPG signed"
			  QUAL="  File     : $FILE\n"
			  QUAL="$QUAL  Name     : %{NAME}\n"
			  QUAL="$QUAL  Feedback : %{PACKAGER}\n"
			  QUAL="$QUAL  Buildhost: %{BUILDHOST}\n"
			  /bin/rpm -qp --queryformat "$QUAL" $FILE
			  echo
			) >&2
			GPG_FAIL_NOT_SIGNED="${FILE##*/} $GPG_FAIL_NOT_SIGNED"
			;;
		esac
	fi
done

if [ -n "$GPG_FAIL_INCORRECT" -o -n "$GPG_FAIL_CORRUPT" -o -n "$GPG_FAIL_NOT_SIGNED" ]
then
	( echo
	  echo "Error: Some packages have gpg problems."
	  echo "       Take corrective action and rerun apt."
	) >&2
	exit 1
fi

# FIXME: catch errors and report to the maintainer
$SIMULATE /bin/rpm $ARGS $RPMS

# Actions to be performed in case a kernel pkg has been updated
if [ "$kernel_update" = "true" ]
then
	MK_INITRD=/sbin/mk_initrd
	eval $(apt-config shell MK_INITRD SystemConfig::MkInitRd)
	if [ -x $MK_INITRD   ]
	then
		$SIMULATE $MK_INITRD
	else
		( echo -n "Warning: mk_initrd script \"$MK_INITRD\" "
		  echo "is not executable"
		) >&2
	fi

	BOOTMGR=/sbin/lilo
	eval $(apt-config shell BOOTMGR	SystemConfig::BootMgr)
	if [ -x $BOOTMGR   ]
	then
		$SIMULATE $BOOTMGR
	else
		echo "Warning: bootmanager \"$BOOTMGR\" is not executable" >&2
	fi
fi

# Is a post rpm install configure script required to run?
if [ -n "$OPT_RUNPOSTSCRIPT" ]
then
	POSTCONFIG=$OPT_RUNPOSTSCRIPT
else
	POSTCONFIG=true
	eval $(apt-config shell POSTCONFIG RPM::PostConfigureScript/b)
fi

if [ "$POSTCONFIG" == "true" ]
then
	CONFIGURESCRIPT=/sbin/SuSEconfig
	eval $(apt-config shell CONFIGURESCRIPT SystemConfig::ConfigureScript)
	if [ -x $CONFIGURESCRIPT ]
	then
		$SIMULATE $CONFIGURESCRIPT
	else
		( echo -n "Warning: configure script \"$CONFIGURESCRIPT\" "
		  echo "is not executable"
		) >&2
	fi
fi

CHECK_ORPHANED_FILES=true
eval $(apt-config shell CHECK_ORPHANED_FILES RPM::checkOrphanedFiles/b)
if [ "$CHECK_ORPHANED_FILES" == "true" ]
then
	PROCS=$(lsof -n 2>/dev/null | grep R[P]MDELETE | cut -d" " -f1 | sort -u)
	if [ -n "$PROCS" ]
	then
		echo "WARNING"
		echo "======="
		echo
		echo "The following processes are accessing deleted files:"
		echo
		echo "$PROCS"
		echo
		echo "Please restart these processes to finish the update."
		echo
		echo "You can check for used files using the command"
		echo "lsof -n | grep R[P]MDELETE"
	fi
fi