#!/bin/bash

## Patch source to make system compile
sed -i -e'/#if (__GNU_LIBRARY__/i\
#undef __GNU_LIBRARY__' proc/escape.c

sed -i -e's/index/strchr/' sysctl.c

## Fix Makefile: do not compile all tools
sed -i -e'/SKIP[ \t]*:=/a\
SKIP\t:= $(usr/bin)top $(man1)top.1 $(usr/bin)watch $(man1)watch.1 $(usr/bin)slabtop $(man1)slabtop.1 $(bin)ps $(man1)ps.1 $(usr/bin)uptime $(man1)uptime.1 $(usr/bin)free $(man1)free.1 $(bin)kill $(man1)kill.1 $(sbin)sysctl $(man5)sysctl.conf.5 $(man8)sysctl.8 $(usr/bin)skill $(man1)skill.1 $(usr/bin)snice $(man1)snice.1 ' Makefile

sed -i -e's/^\t\(.*skill snice\)/\t#\1/' Makefile

sed -i -e's/ALL[ \t]*:= $(notdir $(BINFILES))/ALL\t:= $(notdir $(filter-out $(SKIP), $(BINFILES)))/' Makefile

## Use cross compiler
sed -i -e'/^VERSION[ \t]*:=/i\
CC :=\tarm-linux-gcc' Makefile

## Do not install as root
sed -i -e's/--owner 0 --group 0//' Makefile

## Do not run ldconfig
sed -i -e's/\(ldconfig[ \t]*:=\).*/\1/' Makefile

## Install to /usr/lib/, not to /lib
sed -i -e's|\(lib[ \t]*:= $(DESTDIR)/\)\($(lib64)/\)|\1usr/\2|' Makefile

## change file mode
sed -i -e's/a=rx/0755/' Makefile proc/module.mk
sed -i -e's/a=r/0644/' Makefile proc/module.mk

##
cat >ECO_INSTALL <<EOF
#!/bin/sh

DESTDIR=\$(pwd)/../../rootfs
EOF
chmod 755 ECO_INSTALL

