#!/bin/ksh

# $Id: cleanup4humans.sh 211 2008-12-31 00:52:06Z elkner $
# (C) 2008 by Jens Elkner jel+lu@cs.uni-magdeburg.de
# Licence: CDDL - see http://opensource.org/licenses/cddl1.php

T1="/tmp/ck1.$$"
T2="/tmp/ck2.$$"
export PATH="/usr/bin:/usr/sbin"

usage() {
	printf 'Usage: %s [-h] [-t dir] basedir

Analyzes $basedir/var/sadm/system/data/upgrade_cleanup, whereby $basedir is
assumed to be the directory used as directory parameter for lumount(1M).
It creates files with basic command lines, which one probably needs to use to
decide, whether to copy back replaced files or replace files with the version
suggested by the upgrade package as well as making recommendations, what files
should be removed for cleanup.

It is assumed, that all zones are running or are at least bootable!

  -h       .. print this help text and exit
  -t dir   .. directory, where to store the result files.
              Default: basedir/var/tmp/
  basedir  .. the directory, where the BE in question is lumounted
' $0
}

TDIR=""

while getopts "ht:" option ; do
	case "$option" in
		"h") usage; exit 1;;
		"t") TDIR="$OPTARG";;
	esac
done
shift $((OPTIND-1))

if [ -z "$1" ]; then
	usage; exit 1
fi
if [ ! -r "$1/var/sadm/system/data/upgrade_cleanup" ]; then
	echo "Unable to read file $1/var/sadm/system/data/upgrade_cleanup"
	exit 1
fi

if [ `/usr/ucb/whoami` != "root" ]; then
	echo "run as 'root' to get relyable results"
	exit 1;
fi
if [ -z "$TDIR" ]; then
	TDIR="$1/var/tmp"
fi
if [ ! -d "$TDIR" ]; then
	echo "$TDIR is not a directory. Unable to store results."
	exit 2
fi
cd "$1/var/sadm/system/data"
echo "Analyzing $1/var/sadm/system/data/upgrade_cleanup. Results go into:"

MNTDIR="$1"
if [ "$MNTDIR" = "/" ]; then
	print -u2 "WARNING: You should halt all running zones to get meaningful data!"
fi

setZname() {
	if [ -z "$1" ]; then
		ZNAME="global"
		return
	fi
	ZNAME="$1"
	ZNAME=${ZNAME%.}
	ZNAME=${ZNAME%\>}
	ZNAME=${ZNAME#\<}
}

RN="$TDIR/renamed.txt"
PR="$TDIR/preserved.txt"
RS="$TDIR/restored.txt"
TC="$TDIR/typechange.txt"
SC="$TDIR/symlinkchange.txt"
LC="$TDIR/hardlinkchange.txt"
RM="$TDIR/removeSafe.sh"
IG="$TDIR/ignored.txt"

for f in $RN $PR $RS $TC $SC $LC $RM $IG; do
	echo $f
	cp /dev/null $f
done
echo '# One may need to apply changes to EVERY zone
# If changes in *.new are ok, just replace "diff -u" with "mv"' >$PR
echo '# The first file was the one used before the upgrade
# The second file is the one, installed by the upgrade
# So if you want to retain your previous settings, replace "diff -u" with "mv"
# Otherwise consider to remove the first file to cleanup your system' >$RN
echo "# Files that can be ignored (comming from a mounted fs)" >$IG
/usr/xpg4/bin/egrep '^/[^ ]*:' upgrade_cleanup | while read afile atail; do
	afile=`echo $afile | sed -e 's,/lu/a/,/root/,'`
	afile=${afile%:}
	afile=${afile#/a}
	if [ ! -a "$MNTDIR/$afile" ]; then
		echo "$afile" >>$IG
		continue
	fi
	set -- $atail
	if [ "$1" = "had" ]; then
		# remove *.la files - cause of libtool brain damages
		setZname "${10}"
		if [ "${afile%.la}" != "$afile" ]; then
			echo "rm -f $MNTDIR/${afile}" >>$RM
		else
			echo "${ZNAME}: ${afile}" >>$RS
		fi
	elif [ "$1" = "existing" ]; then
		new=${afile}
		if [ "$3" = "renamed" ]; then
			old="$5"
			setZname "$6"
			f="$RN"
		else
			# preserved
			old="${10}"
			setZname "${11}"
			f="$PR"
		fi
		old=`echo $old | sed -e 's,/lu/a/,/root/,'`
		old=${old#/a}
		if [ ! -a "$MNTDIR/$old" ]; then
			# probably blindly generated (deduced from global zone) by lu
			echo "$old" >>$IG
			continue
		fi
		x=`echo $new | fgrep '/etc/gconf/'`
		if [ -n "$x" ]; then
			sed -e 's, mtime="[^"]*",,' "$MNTDIR/$new" >$T1
			sed -e 's, mtime="[^"]*",,' "$MNTDIR/$old" >$T2
			cmp -s $T1 $T2
		else
			cmp -s "$MNTDIR/$new" "$MNTDIR/$old"
		fi
		if [ $? -ne 0 ]; then
			echo "${ZNAME}: diff -u $MNTDIR/$old $MNTDIR/$new" >>$f
		else
			echo "rm -f $MNTDIR/$old" >>$RM
		fi
	elif [ "$1" = "target" ]; then
		# no zoned example seen, so prefix with ': ' for now
		if [ "$3" = "hard" ]; then
			echo ": ${afile} $8" >>$LC
		else
			echo ": ${afile} $8 $10" >>$SC
		fi
	elif [ "$1" = "file" -a "$2" = "type" ]; then
		echo ": ${afile} $6 $8" >>$TC
	else
		echo "# ${afile}: $@" >>$IG
	fi
done
rm $T1 $T2
echo "Done."
