#!/bin/bash -x
# http://kaneda.bohater.net
# semi automatic script to build :
#
#kernel-ide: 2.4.32
#kernel-ide: [ POM-ng-20050918|IMQ|ipp2p_v0.8.1_rc1|xfs|shfs-0.35|iANS-3.4.3a ]
#kernel-ide: CLASSIFY CONNMARK SAME addrtype comment ownercmd realm HOPLIMIT
#kernel-ide: IPV4OPTSSTRIP NETLINK NETMAP REJECT TTL connlimit expire fuzzy
#kernel-ide: iprange ipv4options nth psd quota random set time u32 IPMARK ROUTE
#kernel-ide: TARPIT TCPLAG XOR account condition cuseeme-nat directx8-conntrack-nat
#kernel-ide: eggdrop-conntrack geoip h323-conntrack-nat ip_queue_vwmark ipp2p
#kernel-ide: mms-conntrack-nat msnp-conntrack-nat owner-socketlookup
#kernel-ide: pptp-conntrack-nat quake3-conntrack-nat rpc rsh rtsp-conntrack
#kernel-ide: talk-conntrack-nat dstlimit mport

KERNEL_VERSION="2.4.32"
IPTABLES_VERSION="1.3.5"
PATCHOMATIC_DATE="20050918"

#
export TERM=linux

#Przechodzimy do katalogu /usr/src/
cd /usr/src/

#Sprawdzamy czy istnieje katalog/plik linux.
if [ -e linux ] ; then
  echo "Error: istnieje katalog/plik: linux"
  exit
fi

if [ ! -f linux-$KERNEL_VERSION.tar.bz2 ] ; then
  wget http://www.kernel.org/pub/linux/kernel/v2.4/linux-$KERNEL_VERSION.tar.bz2
fi

if [ ! -f iptables-$IPTABLES_VERSION.tar.bz2 ] ; then
  wget http://netfilter.org/projects/iptables/files/iptables-$IPTABLES_VERSION.tar.bz2
fi

if [ ! -f patch-o-matic-ng-$PATCHOMATIC_DATE.tar.bz2  ] ; then
  wget ftp://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/patch-o-matic-ng-$PATCHOMATIC_DATE.tar.bz2
fi

if [ ! -f linux-2.4.28-imq2.diff ] ; then
  wget http://www.linuximq.net/patchs/linux-2.4.28-imq2.diff
fi

if [ ! -f iptables-1.3.0-imq1.diff ] ; then
  wget http://www.linuximq.net/patchs/iptables-1.3.0-imq1.diff
fi

if [ ! -f imq-nat.diff  ] ; then
  wget http://www.linuximq.net/patchs/imq-nat.diff
fi

echo "Unpacking kernel..."
cd /usr/src
tar xjf linux-$KERNEL_VERSION.tar.bz2
ln -s linux-$KERNEL_VERSION linux
cd /usr/src/linux

echo "Patching IMQ..."
cp ../linux-2.4.28-imq2.diff .
patch -p1 < linux-2.4.28-imq2.diff

echo "Kernel $KERNEL_VERSION rozpakowany. IMQ patched. Press Enter..."
read a

echo "Unpacking iptables + POM Patching..."
cd /usr/src
rm -rf iptables-$IPTABLES_VERSION
tar xjf iptables-$IPTABLES_VERSION.tar.bz2
ln -s iptables-$IPTABLES_VERSION iptables
tar xjf patch-o-matic-ng-$PATCHOMATIC_DATE.tar.bz2
cd /usr/src/patch-o-matic-ng-$PATCHOMATIC_DATE
IPTABLES_DIR=/usr/src/iptables KERNEL_DIR=/usr/src/linux ./runme --batch patchlets --exclude=osf --exclude=ACCOUNT --exclude=connbytes --exclude=nf-log --exclude=raw --exclude=tcp-window-tracking --exclude=TRACE --exclude=dropped-table --exclude=layer2-hooks --exclude=nat-reservations --exclude=owner-supgids --exclude=tproxy --exclude=MARK-operations --exclude=netfilter-docbook --exclude=goto --exclude=string

echo "Iptables $IPTABLES_VERSION + POM $PATCHOMATIC_DATE ok... Press Enter... "
read b

echo "Iptables IMQ Patching + Compiling..."

cd /usr/src/iptables
cp ../iptables-1.3.0-imq1.diff .
patch -p1 <iptables-1.3.0-imq1.diff
chmod +x extensions/.IMQ-test*
cat Makefile | sed s/"PREFIX:=\/usr\/local"/"PREFIX:=\/usr"/g > /tmp/$$.kubelek
cat /tmp/$$.kubelek |sed s/"COPT_FLAGS:=-O2"/"COPT_FLAGS:=-O2 -march=i486 -mcpu=i686"/g > /tmp/$$.kubelek2
mv -f /tmp/$$.kubelek2 Makefile
make
make experimental
make install DESTDIR=/tmp/package-iptables-$IPTABLES_VERSION
make install-devel DESTDIR=/tmp/package-iptables-$IPTABLES_VERSION
make install-experimental DESTDIR=/tmp/package-iptables-$IPTABLES_VERSION
chown -R root.bin /tmp/package-iptables-$IPTABLES_VERSION/usr/sbin
( cd /tmp/package-iptables-$IPTABLES_VERSION
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
gzip -9 /tmp/package-iptables-$IPTABLES_VERSION/usr/man/man?/*

echo "Iptables skompilowane ... Press Enter..."
read c

cd /usr/src/linux/drivers/net
cp ../../../imq-nat.diff .
patch <imq-nat.diff

echo "IMQ-nat patch ... Enter..."
read d

echo "Teraz kernela skonfiguruj i skompiluj..."


