#
# this handler will save various reports of vital system information.
# by default, all the reports are enabled and are saved in /var/backups.
#
# (1) a list of all the packages installed and removed.
#     this file can be used to restore the state of installed packages
#     by running "dpkg --set-selections < dpkg-selections.txt
# 
# (2) the partition table of all disks. 
#     this partition table can be used to format another disk of
#     the same size. this can be handy if using software raid and 
#     you have a disk go bad. just replace the disk and partition it
#     by running "sfdisk /dev/sdb < partitions.sdb.txt"
#     (MAKE SURE YOU PARTITION THE CORRECT DISK!!!)
#
# (3) hardware information. 
#     write to a text file the important things which hwinfo can gleen.
#

getconf packages yes
getconf packagesfile /var/backups/dpkg-selections.txt

getconf partitions yes
getconf partitionsfile /var/backups/partitions.__star__.txt

getconf hardware yes
getconf hardwarefile /var/backups/hardware.txt

getconf SFDISK `which sfdisk`
getconf HWINFO `which hwinfo`
getconf sfdisk_options ""
getconf hwinfo_options ""

# See if vservers are configured
if [ "$vservers" = "yes" ]
then
	if [ ! -d $VROOTDIR ]
	then
		fatal "vservers enabled, but $VROOTDIR does not exist!"
	else
		info "vserver method enabled"
		usevserver=1
	fi
fi

if [ "$packages" == "yes" ]; then
	if [ $usevserver ]
	then
		nodpkg="lost+found|ARCHIVES"
		info "vserver root directory set to: $VROOTDIR"
		for vserver in `ls $VROOTDIR | grep -E -v $nodpkg`
		do
			info "examining vserver: $vserver"
			running=`$VSERVERINFO $vserver RUNNING`
			if [ $running = 1 ]; then
			    if [ ! -x "$VROOTDIR/$vserver`$VSERVER $vserver exec which dpkg`" ]; then
				warning "can't find dpkg in vserver $vserver, skipping installed packages report."
				nodpkg="$nodpkg|$vserver"
			    fi
			else
			    warning "vserver $vserver is not running, skipping installed packages report."
			    nodpkg="$nodpkg|$vserver"
			fi

		done
	else
		if [ ! -x "`which dpkg`" ]; then
			warning "can't find dpkg, skipping installed packages report."
			packages="no"
		fi
	fi
fi

if [ "$partitions" == "yes" ]; then
	if [ ! -x "$SFDISK" ]; then
		warning "can't find sfdisk, skipping partition report."
		partitions="no"
	fi
	if [ ! -x "$HWINFO" ]; then
		warning "can't find hwinfo, skipping partition report."
		partitions="no"
	fi
fi

if [ "$hardware" == "yes" ]; then
	if [ ! -x "$HWINFO" ]; then
		warning "can't find hwinfo, skipping hardware report."
		hardware="no"
	fi
fi

## PACKAGES ##############################

#
# here we grab a list of the packages installed and removed.
#

if [ "$packages" == "yes" ]; then
   if [ $usevserver ]
   then
      for vserver in `ls $VROOTDIR | grep -E -v $nodpkg`
      do
	 debug "$VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile"
	 $VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile
      done
   fi
   
# We want to perform this on the host as well
   if [ "$packages" == "yes" ]; then
      debug "dpkg --get-selections > $packagesfile"
      dpkg --get-selections > $packagesfile
   fi
fi

## PARTITIONS #############################

# here we use sfdisk to dump a listing of all the partitions. 
# these files can be used to directly partition a disk of the same size.

if [ "$partitions" == "yes" ]; then
	devices=`$HWINFO --disk | grep "Device File" | cut -d\  -f5`
	for dev in $devices; do
		[ -b $dev ] || continue
		label=${dev#/dev/}
		label=${label//\//-}
		outputfile=${partitionsfile//__star__/$label}
		debug "$SFDISK $sfdisk_options -d $dev > $outputfile"
		$SFDISK $sfdisk_options -d $dev > $outputfile
	done
fi

## HARDWARE #############################

#
# here we use hwinfo to dump a table listing all the
# information we can find on the hardware of this machine
# 

if [ "$hardware" == "yes" ]; then
	if [ -f $hardwarefile ]; then
		rm $hardwarefile
	fi
	touch $hardwarefile
	echo -e "\n\n====================== summary ======================\n" >>  $hardwarefile
	debug "$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile"
	$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile
	for flag in cpu network disk bios pci; do
		echo -e "\n\n====================== $flag ======================\n" >>  $hardwarefile
		$HWINFO --$flag >> $hardwarefile
	done
fi