#!/bin/sh

# preserve-user-changes:  script to preserve user changes for use with patches
#                         in patch manager

# $1 = patch name
# $2 = root mount point (new)
# $3 = some scratch space

# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

makefilesonly=false
if [ ${#} = 2 ] && [ "$1" = "NULL" ]; then
    makefilesonly=true
elif [ ${#} != 3 ] ; then
    echo "Incorrect invocation of patch script.  Help!"
    exit 1
fi

nargs () {
    echo ${#}
}

cat > $2/etc/ifs/patchmgr/files_to_preserve <<-EOF
	etc/ifs/hardware.xml 
	etc/ifs/drives.xml 
	etc/ifs/array.xml 
	etc/ifs/local.xml 
	etc/ifs/gmp.xml 
	etc/ifs/ad0_is_data_drive
	etc/mcp/override
	etc/mail/sendmail.cf 
	etc/mail/aliases 
	etc/mail/aliases.db 
	etc/periodic 
	etc/fstab
	etc/ssh/ssh_host_key 
	etc/ssh/ssh_host_key.pub 
	etc/ssh/ssh_host_dsa_key 
	etc/ssh/ssh_host_dsa_key.pub 
	etc/ftpusers 
	etc/group 
	etc/hosts 
	etc/hosts.equiv 
	etc/hosts.allow 
	etc/hosts.lpd 
	etc/mcp/override 
	etc/resolv.conf
	etc/ntp.conf
	etc/syslog.conf
	etc/master.passwd
	etc/localtime
	etc/isilon_serial_number
	etc/passwd
	etc/spwd.db
	etc/pwd.db
	etc/local
	usr/local/etc/smb.conf
	usr/local/private
	etc/policyconf.xml
EOF
    
cat > $2/etc/ifs/patchmgr/files_to_discard <<-EOF
	etc/periodic/daily/100.clean-disks
	etc/periodic/daily/110.clean-tmps
	etc/periodic/daily/120.clean-preserve
	etc/periodic/daily/130.clean-msgs
	etc/periodic/daily/140.clean-rwho
	etc/periodic/daily/150.clean-hoststat
	etc/periodic/daily/200.backup-passwd
	etc/periodic/daily/210.backup-aliases
	etc/periodic/daily/220.backup-distfile
	etc/periodic/daily/300.calendar
	etc/periodic/daily/310.accounting
	etc/periodic/daily/320.rdist
	etc/periodic/daily/330.news
	etc/periodic/daily/400.status-disks
	etc/periodic/daily/420.status-network
	etc/periodic/daily/430.status-rwho
	etc/periodic/daily/440.status-mailq
	etc/periodic/daily/450.status-security
	etc/periodic/daily/500.queuerun
	etc/periodic/daily/460.status-mail-rejects
	etc/periodic/daily/999.local
	etc/periodic/daily/340.uucp
	etc/periodic/daily/410.status-uucp
	etc/periodic/monthly/200.accounting
	etc/periodic/monthly/999.local
	etc/periodic/weekly/120.clean-kvmdb
	etc/periodic/weekly/310.locate
	etc/periodic/weekly/320.whatis
	etc/periodic/weekly/330.catman
	etc/periodic/weekly/340.noid
	etc/periodic/weekly/400.status-pkg
	etc/periodic/weekly/999.local
	etc/periodic/weekly/300.uucp
EOF

if $makefilesonly; then
    return 0
fi

if [ ! -f $2/etc/ifs/patchmgr/files_to_preserve ] ; then
    echo "Failed to create $2/etc/ifs/patchmgr/files_to_preserve.  All files will be updated"
fi

# always preserve etc/system_update_time.
# always preserve etc/fstab
filestopreserve=`cat $2/etc/ifs/patchmgr/files_to_preserve`
filestopreserve=$filestopreserve' etc/fstab etc/system_update_time'
cat > $3/patternstoavoid <<EOM
	^dev.*
	.*files_saved_for_system_update
	.*files_marked_mutable
	.*usermodified.tar
	^var.*
EOM
cat $2/etc/ifs/patchmgr/files_to_discard >> $3/patternstoavoid
rm -f $3/file_list

for file in `(cd $2 && find $filestopreserve -fstype ufs -type f -or -type l |\
    grep -x -v -f $3/patternstoavoid ) 2> /dev/null` ; do
        echo $file >> $3/file_list
done

rm $3/patternstoavoid

if [ -f $3/file_list ]; then
    echo -n "Saving `wc -l $3/file_list | awk '{print $1}'` "
    echo "user-modified files for later restore..."
    rm  -f $3/saved_files_$1.tar 
    tar -C $2 -vWcf $3/saved_files_$1.tar -I $3/file_list > $3/tar-results 2>&1
    retval=${?}
    if [ $retval = 0  ]; then
        rm $3/file_list $3/tar-results
    else
	echo "tar returned an error '${retval}'"
	echo Output from tar can be found at $3/tar-results
	
    fi
else
    echo "No files to restore"
    retval=true
fi 

strings $2/boot/kernel/kernel | grep "(#)Isilon OneFS" |\
    awk '{print $3}' > $3/previousversion

return $retval
