#!/bin/sh

set -e

#MARKER1
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__
begin 755 elf-ok
M?T5,1@$"`0`````````````"``0````!@```=````#0```"P```````T`"``
M`@`H``8`!0````$`````@````(````````!Z````>@````4``"```````0``
M`'R``"!\@``@?```````````````!@``(`!P`4*!3D`````N<WEM=&%B`"YS
M=')T86(`+G-H<W1R=&%B`"YT97AT`"YD871A`"YS8G-S`"YB<W,`````````
M````````````````````````````````````````````````````&P````$`
M```&@```=````'0````&```````````````$`````````"$````!`````X``
M('P```!\````````````````````!``````````G`````0````&``"!\````
M?`````````````````````$`````````+0````@````#@``@?````'P`````
M```````````````$`````````!$````#``````````````!\````,@``````
+`````````0``````
`
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

set +e
grep -q binfmt_elf /proc/modules 2>/dev/null
rc=$?
set -e

if [ $rc = 1 -o $rc = 2 ]; then
	rm elf-ok
	if [ -f /etc/ld.so.conf ]; then
	    cp /etc/ld.so.conf /etc/ld.so.conf.save
	fi
	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
#MARKER2

if [ -f /etc/ld.so.conf ]; then
    cp /etc/ld.so.conf /etc/ld.so.conf.save
fi
exit 0
