#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lpp/bos/post_migration.sh 1.5 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2003,2008 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# post_migration script
# @(#)33        1.5  src/bos/usr/lpp/bos/post_migration.sh, migration, bos720 6/2/08 22:44:51

function fileset_consistency
{
lppchk -v >$HOMEDIR/software_consistency 2>&1 &
echo $! >/tmp/lppchk.v.$$
}

function fileset_checksum
{
lppchk -c >$HOMEDIR/software_checksum_verification 2>&1 &
echo $! >/tmp/lppchk.c.$$
}

function fileset_filecheck
{
lppchk -f >$HOMEDIR/software_file_existence_check 2>&1 &
echo $! >/tmp/lppchk.f.$$
}

function fileset_linkcheck
{
lppchk -l >$HOMEDIR/software_link_existence_check 2>&1 &
echo $! >/tmp/lppchk.l.$$
}

function tcbck_check
{
ODMDIR=/etc/objrepos odmget -q attribute=TCB_STATE PdAt | grep -q "tcb_enabled"
if [[ $? = 0 ]]
then
    tcbck -n ALL >$HOMEDIR/tcbck.output 2>&1 &
    echo $! >/tmp/tcbck.pd.$$
fi
}


function vrmfcheck
{
# First parameter is old vrmf
# Second parameter is new vrmf
# Returns 0 if old is a prior release or the same release as new.
# otherwise returns 1

# Cannot do mere alphabetical comparison because
# there are no leading zeroes.  1.2.0.0 is
# alphabetically later than 1.13.0.0, but it is
# a numerically earlier level.
oldvrmf=$1
newvrmf=$2
OIFS=$IFS
    IFS=.
    set -- $oldvrmf
    oldV=$1;oldR=$2;oldM=$3;oldF=$4
    set -- $newvrmf
    newV=$1;newR=$2;newM=$3;newF=$4
    IFS=$OIFS

    if ( [[ $oldV -gt $newV ]] ||
        ( [[ $oldV -eq $newV ]] && ( [[ $oldR -gt $newR ]] ||
        ( [[ $oldR -eq $newR ]] && ( [[ $oldM -gt $newM ]] ||
        ( [[ $oldM -eq $newM ]] && [[ $oldF -ge $newF ]] ))))))
    then
        return 0
    fi

    return 1
}

function compare_software_installed
{
# First find the last pre_migration run and get the list of software
# from before the migration
ls -d /home/pre_migration.[0-9]* > /tmp/pre_migration.dirlist.$$
latest=0
for each_dir in $(cat /tmp/pre_migration.dirlist.$$)
do
    date=$(echo $each_dir | cut -d. -f 2)
    [[ $date > $latest ]] && latest=$date
done
rm /tmp/pre_migration.dirlist.$$

PRELIST=/home/pre_migration.$latest/software_installed_before_migration
LANG=C /usr/bin/lslpp -qlc -Ous > $HOMEDIR/software_installed_after_migration
POSTLIST=$HOMEDIR/software_installed_after_migration

for each_file in $(cut -d: -f2 $PRELIST)
do
    grep -q ":$each_file:" $POSTLIST
    if [[ $? = 0 ]]
    then
        newvrmf=$(grep ":$each_file:" $POSTLIST | cut -d: -f3)
        oldvrmf=$(grep ":$each_file:" $PRELIST | cut -d: -f3)
        vrmfcheck $oldvrmf $newvrmf
        if [[ $? = 0 ]]
        then
            echo $each_file >> $HOMEDIR/filesets_to_be_updated
        else
            echo $each_file >> $HOMEDIR/filesets_updated
        fi
    fi
done

}

function compare_saved_configfiles
{
# pre_migration saved directory
SAVEDDIR=/home/pre_migration.$latest/saved_configuration_files

# the output from all the diffs
>$HOMEDIR/compared_saved_configuration_files

cd $SAVEDDIR
find . -type f | while read filename
do
    realfile=`echo $filename | sed "s/^.//"`
	echo "
Comparing $realfile to $SAVEDDIR$realfile
" >> $HOMEDIR/compared_saved_configuration_files

	diff $realfile $SAVEDDIR$realfile >> $HOMEDIR/compared_saved_configuration_files
	[ "$?" = 0 ] && echo "No difference" >> $HOMEDIR/compared_saved_configuration_files
done
}

# MAIN Main main

# Create directory for output
TIMESTAMP=`/usr/bin/date +"%y %m %d %H %M %S" | sed 's/ //g'`
mkdir /home/post_migration.$TIMESTAMP
if [[ $? = 0 ]]
then
        HOMEDIR=/home/post_migration.$TIMESTAMP
else
        echo "post_migration: failed making directory /home/post_migration.$TIMESTAMP"
        exit 1
fi

# First find the last pre_migration run and get the list of software
# from before the migration
ls -d /home/pre_migration.[0-9]* > /tmp/pre_migration.dirlist.$$ 2>/dev/null
latest=0
for each_dir in $(cat /tmp/pre_migration.dirlist.$$)
do
    date=$(echo $each_dir | cut -d. -f 2)
    [[ $date > $latest ]] && latest=$date
done
rm /tmp/pre_migration.dirlist.$$

# if pre_migration was not run, latest will be 0, and there 
# will be some checks not run.
# Check which software was and was not updated

if [[ $latest != 0 ]]
then
	echo "
Checking for software that was unchanged during the migration."
	compare_software_installed

	# Diff configuration files
	echo "
Comparing saved configuration files."
	compare_saved_configfiles
fi

# fileset consistency check
echo "
Running lppchk commands. This may take awhile."
fileset_consistency
fileset_checksum
fileset_filecheck
fileset_linkcheck
tcbck_check

# verify checks complete before cleanup
while [[ -s /tmp/lppchk.l.$$ ]]
do
    ps -p $(cat /tmp/lppchk.l.$$) | grep -q lppchk
    if [[ $? != 0 ]]
    then
        rm /tmp/lppchk.l.$$
    fi
done
while [[ -s /tmp/lppchk.f.$$ ]]
do
    ps -p $(cat /tmp/lppchk.f.$$) | grep -q lppchk 
    if [[ $? != 0 ]]
    then
        rm /tmp/lppchk.f.$$
    fi
done
while [[ -s /tmp/lppchk.c.$$ ]]
do
    ps -p $(cat /tmp/lppchk.c.$$) | grep -q lppchk 
    if [[ $? != 0 ]]
    then
        rm /tmp/lppchk.c.$$
    fi
done
while [[ -s /tmp/lppchk.v.$$ ]]
do
    ps -p $(cat /tmp/lppchk.v.$$) | grep -q lppchk 
    if [[ $? != 0 ]]
    then
        rm /tmp/lppchk.v.$$
    fi
done
while [[ -s /tmp/tcbck.pd.$$ ]]
do
    ps -p $(cat /tmp/tcbck.pd.$$) | grep -q tcbck 
    if [[ $? != 0 ]]
    then
        rm /tmp/tcbck.pd.$$
    fi
done

# Tell where saved information is
echo "
All saved information can be found in: $HOMEDIR
"
exit 0
