#!/bin/sh
# /etc/init.d/validate_oem



#set -x
set +e

#                       1=Raven        2=AGCO         3=CaseIH       4=New Holland  5=ET           6=Miller       7=Buhler       8=Hagie        9=RavenISO     10=CaseISO     11=Agrotronix  12=Caruelle    13=RBR         14=Kinze
SOFTWARE_PN=("INVALID" "077-0171-375" "077-0171-382" "077-0171-376" "077-0171-380" "077-0171-378" "077-0171-377" "077-0171-381" "077-0171-379" "077-0171-402" "077-0171-403" "077-0171-407" "077-0171-408" "077-0171-424" "077-0171-421") 
MAX_OEM_ID="14"
DATABASE="/usr/bin/raven/databases/my.db.sqlite"
REGISTRATION="/usr/bin/raven/registration.xml"
LOCAL_INSTALL="/var/lib/smart/installer_current.zip"

SetVariable() {
	VARNAME=$1
	VARVALUE=$2
	
	exec 3>&2
	exec 2> /dev/null
	CURVALUE=`/sbin/fw_printenv $1 | awk -F= '{ print $2}'`
	exec 2>&3
	
	if [ "${CURVALUE}" = "${VARVALUE}" ]; then
		return 0;
	else
		/sbin/fw_setenv $1 $2
		return 1;
	fi
}

case "$1" in
  start)
		#check to make sure our RPM database is nice (not corrupt)
		perl -e "alarm 120; exec @ARGV" "rpm -V rpm-libs &> /dev/null" 
		if [ $? -ne 0 ]; then
			echo "Error scanning database, validate-oem"		
			rm -f /var/lib/rpm/__db*      # if we are resuming an install we cant trust any of our database locks, remove and recreate
			exit 1 #dont do anything further, we cant trust anyone... except that drum player in the corner, he's cool
		fi
  
		OEMID=""
		if [ -f ${DATABASE} ]; then
				OEMID=`/usr/bin/sqlite3 ${DATABASE} 'select oem from registrationinfo'`
				if [ $? -ne 0 ]; then
					if [ -f ${REGISTRATION} ]; then
						OEMID=`cat ${REGISTRATION} | grep \<OEM\> | sed 's/<OEM>//' | sed 's/<\/OEM>//'`			  ##TODO:: we could consolidate these two identical lines with a check if OEMID is still empty		
					fi
				fi
		else
			if [ -f ${REGISTRATION} ]; then
				OEMID=`cat ${REGISTRATION} | grep \<OEM\> | sed 's/<OEM>//' | sed 's/<\/OEM>//'`					 ##TODO:: we could consolidate these two identical lines with a check if OEMID is still empty	
			fi
		fi
		
		##clean off any spaces/newlines/garbage
		OEMID=${OEMID%%[[:space:]]}
		
		if [  ${OEMID} -gt ${MAX_OEM_ID} ]; then
		    echo "OEM ID out of range, defaulting to Raven"
			OEMID=1
		fi
		
		if [ ${OEMID} -ge 1 ] && [ ${OEMID} -le ${MAX_OEM_ID} ]; then 
			echo "Found OEMID=$OEMID, PN ${SOFTWARE_PN[${OEMID}]}"
			
			#write part number (without dashes) to file for ROS to consume
			SWPN="${SOFTWARE_PN[${OEMID}]//-}"
			echo -n "${SWPN}" > /usr/bin/raven/fishspear/softwarepartnumber.txt			
			SetVariable "rvn_sw_pn" "${SWPN}"

			#now lets check to see if this OEMID is installed
			INSTALLED=`/usr/bin/rpm -qa | grep "${SOFTWARE_PN[${OEMID}]}"`
			if [ "${INSTALLED}" = "" ]; then #package isn't installed
				echo "Incorrect ID Found"
				if [ -f ${LOCAL_INSTALL} ]; then	#last install was local, extract and apply part number
					echo "Local previous install detected... Installing - ${SOFTWARE_PN[${OEMID}]}"
					/etc/init.d/fwinstall extract
					/usr/sbin/update_firmware local ${SOFTWARE_PN[${OEMID}]} | /usr/sbin/update_firmware_gui
				else #last install was network based
					if [ "$2" = "update_firmware" ]; then	#we were called from the update_firmware script, everything is setup just install the package
						echo "Network install detected... Installing - ${SOFTWARE_PN[${OEMID}]}"
						/usr/bin/smart install ${SOFTWARE_PN[${OEMID}]} -y
					fi
				fi 
			fi
		else
			echo "Invalid OEMID=${OEMID}"
			exit 1
		fi
	;;
  *)
    echo "Usage: /etc/init.d/$NAME {start}"
    exit 1
    ;;
esac

exit 0
