#!/bin/bash
#Install Patches without installing the kernel 

RUGBIN=`which rug`
TMPPatches=`mktemp -t patches.XXXXXXXXXX` || exit 1

#Begin Patch Section
if [ -e $RUGBIN ]; then
$RUGBIN pl oes | sed -e '/^i/ d' -e '/--/ d' -e '/Name/ d' | awk '{ print $2 }' >> $TMPPatches

PATCH_ARRAY=( `cat $TMPPatches | tr '\n' ' '` )
KERN_Patches=`mktemp -t kern_patches.XXXXXXXXXX` || exit 1

#Check for Kernel Patches and remove them from the list of Patches to be installed
echo "Checking for Kernel patches..."
for i in ${PATCH_ARRAY[@]}; do
$RUGBIN pi $i | grep -iq "update for Linux kernel"
	if [ $? -eq 0 ]; then
		echo "                                    $i is a kernel patch"
		echo $i >> $KERN_Patches
		cat $TMPPatches | sed "/$i/ d" > $TMPPatches
	else 
		echo "$i is NOT a kernel patch"
	fi
done	 

#Format Patches in a line to install with rug pin
cat $TMPPatches | tr '\n' ' ' > $TMPPatches 

#Install Patches
update=`<$TMPPatches`
if [ "$update" != "" ]; then
echo "Executing rug to patch your system..."

$RUGBIN pin $update

#Summary
echo " "
echo "Summary:"
echo "Patches have been installed"
echo "The following Kernel Patches have not been installed."
echo "Install these patches individually with the following command."
echo "rug pin patch_name"
echo " "
echo "Kernel patch list:"
LEFTOVER=`<$KERN_Patches`
echo "$LEFTOVER"
echo " "
echo "Enjoy!!"
echo " "

else
echo "No Updates Available."
fi
fi

#Remove temp files
rm $TMPPatches
rm $KERN_Patches
