ram=$(grep ^MemTotal: /proc/meminfo | { read x y z; echo $y; }) || true # in kilobytes

if [ "$(udpkg --print-os)" = linux ]
then
	# Linux' default is now 50%
	mount / -o remount,size=100%
fi

if [ -z "$ram" ]; then
	echo "Cannot determine system memory, skipping lowmem probe" >&2
else
	ram=$(expr $ram / 1024) # convert to megabytes

	# See README in lowmem source on how to update these levels
	# Also update the values in the installation manual, in
	# build/arch-options/
	# Also update the gtk values in rootskel, S60frontend
	# Also update the values in debian-installer/build/boot/x86/f2.txt
	ARCH=$(udpkg --print-architecture)
	case $ARCH in
		alpha)
		# Update needed
			level1=82
			level2=55
			min=39
		;;
		amd64)
			level1=163 # MT=166348, qemu: -m 178
			level2=163 # MT=166348, qemu: -m 178
			min=163    # MT=166348, qemu: -m 178
		;;
		arm|armel|armhf)
		# Update needed
			level1=76 # MT=77036, qemu: -m 80
			level2=76 # MT=77036, qemu: -m 80
			min=27    # MT=27316, qemu: -m 31
		;;
		armeb)
		# Update needed
			level1=32 # MT=32792, qemu: -m 36
			level2=21
			min=18
		;;
		i386)
			level1=135 # MT=137688, qemu: -m 145
			level2=135 # MT=137688, qemu: -m 145
			min=135    # MT=137688, qemu: -m 145
		;;
		mips)
		# Update needed
			level1=78 # MT=79788, qemu: -m 85
			level2=78 # MT=79788, qemu: -m 85
			min=27    # MT=27004, qemu: -m 33
		;;
		mipsel)
		# Update needed
			level1=78 # MT=79788, qemu: -m 85
			level2=74 # MT=74760, qemu: -m 80
			min=27    # MT=27004, qemu: -m 33
		;;
		m68k)
		# Update needed
			level1=64
			level2=32
			min=0 #FIXME
		;;
		s390|s390x)
		# Update needed
			level1=44 # needs MAINSIZE=48 in Hercules
			level2=28
			min=20
		;;
		sh4)
		# Update needed
			level1=41
			level2=36
			min=28
		;;
		kfreebsd-amd64)
		# Please see #696786 for how these were chosen
			level1=135 # MT=137744, qemu: -m 232
			level2=135 # MT=137744, qemu: -m 232
			min=73     # MT=74256,  qemu: -m 170
		;;
		kfreebsd-i386)
		# Please see #696786 for how these were chosen
			level1=44 # MT=44376, qemu: -m 104
			level2=44 # MT=44376, qemu: -m 104
			min=30    # MT=30040, qemu: -m 90
		;;
		# Update needed
		hurd-i386)
			level1=499 # qemu: -m 500
			level2=409 # qemu: -m 410
			min=364    # qemu: -m 365
		;;
		*)
			level1=64
			level2=32
			min=0 #FIXME
		;;
	esac

	use_level=0
	if  [ $ram -lt $min ]; then
		use_level=9
	elif [ $ram -lt $level2 ]; then
		use_level=2
	elif [ $ram -lt $level1 ]; then
		use_level=1
	fi

	# Check for lowmem level set by user; only 1 and 2 supported
	# We cannot use preseeding yet, so parse /proc/cmdline directly
	user_level=$(grep "lowmem=[12]" /proc/cmdline | \
			sed "s/^.*lowmem=\([0-9]*\).*$/\1/")
	if [ "$user_level" ] && [ $user_level -gt $use_level ]; then
		logger -t lowmem "Using $user_level instead of default level $use_level"
		use_level=$user_level
	fi

	if [ $use_level -gt 0 ]; then
		logger -t lowmem "Entering low memory mode"

		if [ $use_level -le 2 ]; then
			echo $use_level > /var/lib/lowmem
		else
			echo 2 > /var/lib/lowmem
			# 4 mb fuzz for kernel
			echo "$(($min + 4))" > /var/lib/lowmem_insufficient
		fi
		
	       	trimtemplates /var/lib/dpkg/info || true
	fi
fi
