#!/bin/bash


#Preupgrade Assistant performs system upgradability assessment
#and gathers information required for successful operating system upgrade.
#Copyright (C) 2013 Red Hat Inc.
#Ondrej Vasik <ovasik@redhat.com>
#
#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 3 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.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
. /usr/share/preupgrade/common.sh
check_rpm_to "" "diff"
#END GENERATED SECTION

[ ! -f "$VALUE_RPMTRACKEDFILES" ] && exit $RESULT_ERROR
[ ! -f "$VALUE_ALLMYFILES" ] && exit $RESULT_ERROR

localuntracked=$(mktemp /tmp/.localuntrackedXXX)
diff "$VALUE_ALLMYFILES" "$VALUE_RPMTRACKEDFILES" | grep "^<" | cut -d' ' -f2- > $localuntracked

#store temporary files
rm -f "$KICKSTART_DIR/untrackedtemporary"
grep ^/tmp $localuntracked >"$KICKSTART_DIR/untrackedtemporary"
grep ^/var $localuntracked >>"$KICKSTART_DIR/untrackedtemporary"
grep ^/cgroup $localuntracked >>"$KICKSTART_DIR/untrackedtemporary"

#store homedir files
rm -f "$KICKSTART_DIR/untrackeduser"
grep ^/home $localuntracked >"$KICKSTART_DIR/untrackeduser"
grep ^/root $localuntracked >>"$KICKSTART_DIR/untrackeduser"

#and rest is ... system
rm -f "$KICKSTART_DIR/untrackedsystem"
grep -v ^/tmp $localuntracked | grep -v ^/var | grep -v ^/cgroup | \
 grep -v ^/home | grep -v ^/root >"$KICKSTART_DIR/untrackedsystem"

#but some files are expectable... let's filter them out!
rm -f "$KICKSTART_DIR/untrackedexpected"
grep ^/etc/alternatives "$KICKSTART_DIR/untrackedsystem" >"$KICKSTART_DIR/untrackedexpected"
grep ^/etc/rc.d/rc "$KICKSTART_DIR/untrackedsystem" >>"$KICKSTART_DIR/untrackedexpected"
grep ^/etc/selinux/ "$KICKSTART_DIR/untrackedsystem" | grep modules/active >>"$KICKSTART_DIR/untrackedexpected"

diff "$KICKSTART_DIR/untrackedsystem" "$KICKSTART_DIR/untrackedexpected" | grep "<" | cut -d' ' -f2- > "$KICKSTART_DIR/untrackedtmp"
mv "$KICKSTART_DIR/untrackedtmp" "$KICKSTART_DIR/untrackedsystem"

echo " * untrackedsystem - a file that contains all files and directories (untracked by RPM packages) that are not used for common system operations and are located in system directories. Some of them might be user data. If you plan to move the system to a different machine, you need to deal with these files and directories." >>"$KICKSTART_README"
echo " * untrackedexpected - a file that contains files (untracked by RPM packages) that are expected on the system and that are used for common system runtime operations (for example runlevels, alternatives, SELinux active modules and so on). You probably do not need to handle them." >>"$KICKSTART_README"
echo " * untrackeduser - a file that contains all files and directories (untracked by RPM packages) in user directories. If you are planning to move the system to a different machine, you need to deal with these files or directories." >>"$KICKSTART_README"
echo " * untrackedtemporary - an informational file that contains the files in temporary directories. You probably do not need to handle these files." >>"$KICKSTART_README"

rm -f $localuntracked

#we likely have some untracked file, but for the rare case we have
#no such file on the system, give RESULT_PASS
grep -v "/" "$KICKSTART_DIR/untrackedsystem" && \
 grep -v "/" "$KICKSTART_DIR/untrackeduser" && \
 grep -v "/" "$KICKSTART_DIR/untrackedtemporary" && \
 grep -v "/" "$KICKSTART_DIR/untrackedexpected" && \
 exit $RESULT_PASS

#We detected some files untracked by rpm
log_slight_risk "We detected some files untracked by RPM packages. See Remediation instructions for more information."
exit $RESULT_FAIL
