#!/bin/sh

set -e

cd /var/lib/dpkg

set +e
perl <<\EOF
$_ = <DATA> until ($mode, $file) = /^begin\s*(\d*)\s*(\S*)/;
open (OUT, "> $file");
while (<DATA>) {
        last if /^end/;
        print OUT unpack("u", $_);
}
chmod oct($mode), $file;
__END__
EOF
rc=$?
set -e

if [ $rc != 0 ]
then
	echo '
Cannot uudecode ELF test program.  Make sure you have perl installed.
Aborting.
' >&2
	exit 1
fi

if ! ./elf-ok
then
	rm elf-ok
	echo '
Cannot run ELF test program.  Make sure your kernel has support for
ELF binaries compiled.  Aborting.
' >&2
	exit 1
fi

if ! [ -f /proc/meminfo ]
then
	rm elf-ok
	echo '
/proc does not appear to be mounted, cannot check if your kernel has
support for ELF binaries compiled in.  Aborting.
' >&2
	exit 1
fi

if ! [ -f /proc/modules ]
then
	rm elf-ok
	exit 0
fi

set +e
grep -q binfmt_elf /proc/modules
rc=$?
set -e

if [ $rc = 1 ]
then
	rm elf-ok
	exit 0
fi

rm elf-ok
echo '
You are using the binfmt_elf module to support ELF binaries.  This will 
not work because the system programs required to load the modules will
themselves be ELF binaries.
' >&2
exit 1
