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=737 # MT=754108, qemu: -m 780
			level2=424 # MT=433340, qemu: -m 460
			min=316    # MT=322748, qemu: -m 350
		;;
		arm64)
			level1=215 # MT=219856, qemu: -m 260
			level2=205 # MT=209636, qemu: -m 250
			min=200    # MT=204524, qemu: -m 245
		;;
		arm|armel|armhf)
			level1=170 # MT=173496, qemu: -m 190
			level2=120 # MT=122872, qemu: -m 140
			min=120    # MT=122872, qemu: -m 140
		;;
		i386)
			level1=466 # MT=478056, qemu: -m 485
			level2=348 # MT=356312, qemu: -m 365
			min=269    # MT=275220, qemu: -m 285
		;;
		mips|mipsel)
			level1=155 # MT=158676, qemu: -m 170
			level2=146 # MT=148544, qemu: -m 160
			min=145    # MT=147520, qemu: -m 159
		;;
		mips64el)
			level1=293 # MT=299216, qemu: -m 345
			level2=207 # MT=211408, qemu: -m 225
			min=182    # MT=186256, qemu: -m 200
		;;
		ppc64el)
			level1=440 # MT=450000, qemu: -m 500
			level2=195 # MT=200000, qemu: -m 256
			min=195    # MT=200000, qemu: -m 256
		;;
		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
		;;
		hurd-amd64)
			# TODO
			level1=600 # MT=614208, qemu: -m 600
			level2=480 # MT=491328, qemu: -m 480
			min=475    # MT=486208, qemu: -m 475
		;;
		hurd-i386)
			level1=600 # MT=614208, qemu: -m 600
			level2=480 # MT=491328, qemu: -m 480
			min=475    # MT=486208, qemu: -m 475
		;;
		*)
			level1=155
			level2=120
			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
	# Check for forced level, useful for debugging
	forced_level=$(grep "lowmem=+[012]" /proc/cmdline | \
			sed "s/^.*lowmem=+\([0-9]*\).*$/\1/")
	if [ "$forced_level" ] ; then
		logger -t lowmem "Forcing $forced_level"
		use_level=$forced_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
