#!/bin/bash
lxc-ls --active -1 | grep Assistant || {
echo "`date` This script can be called only from the Primary Node (= Node on which Assistant is running)"
exit -1
}
aline=$(2>/dev/null /opt/inventory/inventory appliance_list --full  | xpath -e '//appliance[(hwTypeNumber="2")]/id|//appliance[(hwTypeNumber="2")]/MemTotal|//appliance[(hwTypeNumber="2")]/soco2Version' 2>/dev/null | sed -e "s/<\//§\n/g" | sed -e "s/.*>//g")
		arrIds="§"
		arrLbls="§"
		arrVers="§"
		index=0
		applcount=0
		while [ 1 ]; do
			anelem=`echo -en ${aline%%§*}`
			anelem=${anelem%+*}
			[ "x" = "x$anelem" ] && break
			aline=${aline#*§}
			case $index in
				0)
					arrIds="$arrIds${anelem}§"
					index=1
					;;
				1)
					arrLbls="$arrLbls${anelem}§"
					index=2
					;;
				2)
					if [[ $anelem =~ ^V ]]; then
						arrVers="$arrVers${anelem}§"
						index=0
					else
						arrIds="${arrIds%§*§}§"
						arrLbls="${arrLbls%§*§}§"
						arrIds="$arrIds${anelem}§"
						index=1
					fi
					;;
			esac
		done

                >/dev/os4k_osa500_4GB.cache
                echo "APPLCOUNT=$applcount" >/dev/os4k_osa500_4GB.cache
                echo "ARR_MEM=(${arrIds//§/ })" >>/dev/os4k_osa500_4GB.cache
                echo "ARR_LBs=(${arrLbls//§/ })" >>/dev/os4k_osa500_4GB.cache
                echo "ARR_VERs=(${arrVers//§/ })" >>/dev/os4k_osa500_4GB.cache
                source /dev/os4k_osa500_4GB.cache

	
	
		applcount=0 

		for i in "${ARR_MEM[@]}"; 
			do 
			if [[ $i -lt 5000000 ]];then
			applcount=$(($applcount+1))
			fi 
		done
		sed -i "s/APPLCOUNT=.*/APPLCOUNT=$applcount/"  /dev/os4k_osa500_4GB.cache


exit $applcount
