#!/bin/bash --norc

# Prevent possible access to locale files
export LC_ALL=C

FREE_MEM=$1

kB_done=0

# smem outputs '*' every 512 kilobytes
while read -n 1 output_char
do
	if [ "${output_char}" = '*' ]
	then
		kB_done=$(($kB_done + 512))
		kB_next=$(($kB_done + 512))
		if [ ${kB_next} -gt ${FREE_MEM} ]
		then
			/usr/bin/killall smem
			exit 0
		fi
	fi
done

# allow parent process to detect whether smem was actually killed
exit 18
