#!/bin/bash

appfile=applications

#below is a list of applications and their version, please note that the application name and version are
#seperated by the "~" symbol 
#if you wish to check for other applications simply add the entry you wish belowno further changes will be necessary
#records below use the following structure:
#<RPM Package name>~<Major release>~<Latest security update version>~<Latest application update version>
#<Major release> is used by the script below to differenciate between major release of packages. 
#the system works by matching the value of <Major release> with the first corresponding number of characters of the actually version.

#data file starting below
cat << EOF > applications 
apache2~2.0~2.0.52~2.0.52
apache~1.3~1.3.33~1.3.33
bash~2~2.05c~2.05c
bash~1~1.14.7~1.14.7
kernel-smp~2.6~2.6.9~2.6.9
kernel~2.6~2.6.9~2.6.9
kernel-smp~2.4~2.4.28~2.4.28
kernel~2.4~2.4.28~2.4.28
kernel-smp~2.2~2.2.26~2.2.26
kernel~2.2~2.2.26~2.2.26
kernel-smp~2.0~2.0.40~2.0.40
kernel~2.0~2.0.40~2.0.40
openssh~3~3.9p1~3.9p1
samba~3~3.0.9~3.0.9
openssl~0.9~0.9.7e~0.9.7e
bind~9~9.3.0~9.3.0
bind~8~8.4.5~8.4.5
bind~4~4.9.11~4.9.11
dhcp~3~3.0.2rc3~3.0.2rc3
gcc-c++~3~3.4.3~3.4.3
sudo~1~1.6.8p2~1.6.8.p5
gcc~3~3.4.3~3.4.3
iptables~1.2~1.2.11~1.2.11
sendmail~8~8.12.10~8.13.1
mysql-shared~4.0~4.0.22~4.0.22
mysql-shared~4.1~4.1.7~4.1.7
mysql-shared~5.0~5.0.2~5.0.2
mysql~4.0~4.0.22~4.0.22
mysql~4.1~4.1.7~4.1.7
mysql~5.0~5.0.2~5.0.2
postgresql~7~7.4.6~7.4.6
cvs~1.11~1.11.18~1.11.18
cvs~1.12~1.12.11~1.12.11
net-snmp~5~5.2~5.2
postfix~2.1~2.1.5-4~2.1.5-4
EOF
#data file finished
#anything below here should not require any editing. Unless there's a need to change the script's functionality ofcourse.

declare -a apps
filestore=`date --iso-8601`_apps 

if [ ! -e "$filestore" ]
then
	rpm -qa --qf '%{NAME}~%{VERSION}\n' > "$filestore"
	filetodelete=`cat filestorever`
	rm -f `echo $filetodelete`
	echo "$filestore" > filestorever

fi
oldversions=FALSE
treecreate=FALSE
apps=(` cat "$filestore" | tr '\n' ' '`)
count=${#apps[*]}
echo "" > installedapps
for ap in ${apps[@]}
do
	echo $ap >> installedapps
done
#echo ${apps[@]} > installedapps
declare -a applist
applist=( `cat "$appfile" | tr '\n' ' '`)
appcount=${#applist[*]}

for AppToTest in ${applist[@]}
do
	appname=`echo $AppToTest | awk -F~ ' { print $1 } '`
	appmajorrelease=`echo $AppToTest | awk -F~ ' { print $2 } '`
	appfullversec=`echo $AppToTest | awk -F~ ' { print $3 } '`
	appfullverapp=`echo $AppToTest | awk -F~ ' { print $4 } '`
	appmajor1app=`echo $appfullverapp | awk -F. ' { print $1 } '`
        appmajor2app=`echo $appfullverapp | awk -F. ' { print $2 } '`
        appminorapp=`echo $appfullverapp | awk -F. ' { print $3 } '`
	matchapps=`grep $appname installedapps`
	for AppRecord in ${matchapps[@]}
	do
		StAppName=`echo $AppRecord | awk -F~ ' { print $1 } '`
		if [ "$StAppName" == "$appname" ]
		then
			len=`echo $appmajorrelease | awk ' { print length } '`
			tempapp=`echo $AppRecord~$len`
			stmajorrelease=`echo $tempapp | awk -F~ ' { print substr($2,1,$3) } '`
			stappfullver=`echo $tempapp | awk -F~ ' { print $2 } '`
			stappfullversec=`echo $AppRecord | awk -F~ ' { print $3 } '`
			stappfullverapp=`echo $AppRecord | awk -F~ ' { print $4 } '`
		        stappmajor1=`echo $stappfullver | awk -F. ' { print $1 } '`
		        stappmajor2=`echo $stappfullver | awk -F. ' { print $2 } '`
		        stappminor=`echo $stappfullver | awk -F. ' { print $3 } '`
			if [ "$stmajorrelease" == "$appmajorrelease" ]
			then

				#substitution for version which include RC
				stappmajor1=`echo $stappmajor1 |  sed s/rc/./`
				stappmajor2=`echo $stappmajor2 | sed s/rc/./`
				stappminor=`echo $stappminor | sed s/rc/./`
				appmajor1app=`echo $appmajor1app | sed s/rc/./`
                                appmajor2app=`echo $appmajor2app | sed s/rc/./`
                                appminorapp=`echo $appminorapp | sed s/rc/./`
                                #substitution for version which include p
				stappmajor1=`echo $stappmajor1 |  sed s/p/./`
				stappmajor2=`echo $stappmajor2 | sed s/p/./`
				stappminor=`echo $stappminor | sed s/p/./`
				appmajor1app=`echo $appmajor1app | sed s/p/./`
        	                appmajor2app=`echo $appmajor2app | sed s/p/./`
                	        appminorapp=`echo $appminorapp | sed s/p/./`
                                
                                stappmajor1=`echo $stappmajor1 | sed 's/[a-f]/.&/'`
                                stappmajor2=`echo $stappmajor2 | sed 's/[a-f]/.&/'`
                                stappminor=`echo $stappminor | sed 's/[a-f]/.&/'`
                                appmajor1app=`echo $appmajor1app |  sed 's/[a-f]/.&/'`
                                appmajor2app=`echo $appmajor2app |  sed 's/[a-f]/.&/'`
                                appminorapp=`echo $appminorapp |  sed 's/[a-f]/.&/'`

				stappmajor1=`echo $stappmajor1 |  tr [a-f] [1-6]`
	                        stappmajor2=`echo $stappmajor2 | tr [a-f] [1-6]`
        	                stappminor=`echo $stappminor |  tr [a-f] [1-6]`
				appmajor1app=`echo $appmajor1app | tr [a-f] [1-6]`
                                appmajor2app=`echo $appmajor2app | tr [a-f] [1-6]`
                                appminorapp=`echo $appminorapp | tr [a-f] [1-6]`

        	                if [ "$stappmajor2" == "" ]
                	        then
                        	        stappmajor2=0
	                        fi
	
				 if [ "$appmajor2app" == "" ]
                                then
                                        appmajor2app=0
                                fi

                                if [ "$appminorapp" == "" ]
                                then
                                        appminorapp=0
                                fi

				if [ "$stappminor" == "" ]
                	        then
                        	        stappminor=0
		                        fi

	
                                temp=`echo "$appmajor1app $appmajor2app $appminorapp"`
                                procappverapp=`echo $temp | awk '{ print $1 * 10000 + $2 * 100 + $3 }'`
                                temp=`echo "$stappmajor1 $stappmajor2 $stappminor"`
                                procstappver=`echo $temp | awk ' { print $1 * 10000 + $2 * 100 + $3 }'`



				if [ $procstappver \< $procappverapp ]
                                then
					if [ "$oldversions" == FALSE ]
					then
						echo "TRUE:"
						oldversions=TRUE
					fi
		                               
					app=`echo $AppToTest | awk -F~ ' { print $1 } '`
                                        echo "AddListItem([[[]]],[[[Application: $app current version is: $stappfullver. The latest release version for this application is: $appfullverapp]]])"
                                        
                                fi


			fi
		fi
	done
done
if [ "$oldversions" == FALSE ]
then
	echo "FALSE:"
fi
rm -f applications
rm -f installedapps
echo "!!SCRIPT_FINISHED!!"
