#!/bin/bash

# Buduje pakiety dla Slackware - iptables, kernel, kernel-modules
# Kernel 2.6.24.2 + IMQ + Layer7 + ipp2p
# 2008.02.29

KERNEL_VERSION="2.6.24.2"
IPTABLES_VERSION="1.4.0"
PATCHOMATIC_DATE="20080221"
LAYER7_VERSION="2.17"
LAYER7_DATE="2008-02-20"
VERSION="huge"
VERSIONMINI="6kan"

#####################
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

# Sprawdzamy czy istnieje wymagany plik konfiguracy
if [ ! -f config-$KERNEL_VERSION-imq_ipp2p_layer7 ] ; then
  wget http://kaneda.bohater.net/files/config-$KERNEL_VERSION-imq_ipp2p_layer7
fi

# Pobieramy zrodla kernela
if [ ! -f linux-$KERNEL_VERSION.tar.bz2 ] ; then
  wget http://www.eu.kernel.org/pub/linux/kernel/v2.6/linux-$KERNEL_VERSION.tar.bz2
fi

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

# Pobieramy zrodla patch-o-matic-ng [ipp2p]
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

# Pobieramy zrodla layer7
if [ ! -f http://kent.dl.sourceforge.net/sourceforge/l7-filter/netfilter-layer7-v$LAYER7_VERSION.tar.gz ] ; then
  wget http://kent.dl.sourceforge.net/sourceforge/l7-filter/netfilter-layer7-v$LAYER7_VERSION.tar.gz
fi

# Pobieramy patch IMQ dla kernela
if [ ! -f linux-2.6.24-imq.diff ] ; then
  wget http://www.actusa.net/~linuximq/linux-2.6.24-imq.diff
fi

# Pobieramy patch IMQ dla iptables
if [ ! -f iptables-1.4.0-imq.diff ] ; then
  wget http://www.actusa.net/~linuximq/iptables-1.4.0-imq.diff
fi

# Pobieramy rc.modules dla Slackware
# Uwaga w Slackware 12.0 w source - rc.modules.new jest stare [z kernela 2.4]
# W pkg jest to, ktore pobieramy
if [ ! -f rc.modules.new ] ; then
  wget ftp://ftp.slackware.at/slackware-11.0/extra/source/linux-2.6.17.13/kernel-modules/rc.modules.new
fi

# Pobieramy zrodla definicji protokolow layer7
if [ ! -f l7-protocols-$LAYER7_DATE.tar.gz ] ; then
  wget http://garr.dl.sourceforge.net/sourceforge/l7-filter/l7-protocols-$LAYER7_DATE.tar.gz
fi

# Przechodzimy do katalogu
cd /usr/src

# Usuwamy katalog z rozpakowywanymi zrodlami [dla bezpieczenstwa jesli byl stary]
rm -rf netfilter-layer7-v$LAYER7_VERSION

# Rozpakowywujemy zrodla layer7
tar -xvf netfilter-layer7-v$LAYER7_VERSION.tar.gz

# Rozpakowywujemy zrodla linux kernel
tar xjf linux-$KERNEL_VERSION.tar.bz2

# Tworzymy dowiazanie symboliczne katalogu z kernelem do /usr/src/linux [pomocne pozniej]
ln -s linux-$KERNEL_VERSION linux

# Przechodzimy do katalogu
cd /usr/src/linux

# Nakladamy patch IMQ na jadro kernela
patch -p1 < ../linux-2.6.24-imq.diff

# Nakladamy patch layer7 na jadro kernela
patch -p1 < ../netfilter-layer7-v$LAYER7_VERSION/kernel-2.6.22-2.6.24-layer7-$LAYER7_VERSION.patch

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

# Usuwamy katalog z rozpakowywanymi zrodlami [dla bezpieczenstwa jesli byl stary]
rm -rf iptables-$IPTABLES_VERSION

# Rozpakowywujemy iptables [netfilter]
tar xjf iptables-$IPTABLES_VERSION.tar.bz2

# Tworzymy dowiazanie symboliczne katalogu z iptables [pomocne pozniej]
ln -s iptables-$IPTABLES_VERSION iptables

# Rozpakowywujemy patch-o-matic-ng
tar xjf patch-o-matic-ng-$PATCHOMATIC_DATE.tar.bz2

# Przechodzimy do katalogu patch-o-matic
cd /usr/src/patch-o-matic-ng-$PATCHOMATIC_DATE

# Ustawiamy zmienne odpowiedzialne za katalog z iptables oraz zrodlami kernela
export IPTABLES_DIR=/usr/src/iptables
export KERNEL_DIR=/usr/src/linux

# Pobieramy patch ipp2p oraz nakladamy go na zrodla kernela oraz zrodla iptables
./runme --download
./runme --batch ipp2p

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

# Nakladamy patch layer7 na iptables
patch -p1 < ../netfilter-layer7-v$LAYER7_VERSION/iptables-1.4-for-kernel-2.6.20forward-layer7-$LAYER7_VERSION.patch 

# Nakladamy patch IMQ na iptables
patch -p1 < ../iptables-1.4.0-imq.diff

# Ustawiamy zmienne odpowiedzialne za katalog z iptables oraz zrodlami kernela
export IPTABLES_DIR=/usr/src/iptables
export KERNEL_DIR=/usr/src/linux

# Aktywujemy kompilacje dodatkowych rozszerzen [ IMQ, layer7 ]
chmod +x extensions/.*

# Kompilujemy iptables tak aby naglowki pobrane zostaly z katalogu zrodlowego kernela
make PREFIX=/usr/ KERNEL_DIR=/usr/src/linux KBUILD_OUTPUT=/usr/src/linux

# Usuwamy katalog z rozpakowywanymi zrodlami [dla bezpieczenstwa jesli byl stary]
rm -rf /tmp/package-iptables-$IPTABLES_VERSION

# Kopiujemy skompilowane pliki iptables w docelowe miejsce
make install PREFIX=/usr/ DESTDIR=/tmp/package-iptables-$IPTABLES_VERSION

# Przygotowania do pakietu iptables - prawa, strip, pakowanie manow, doc
chown -R root.root /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?/*
mkdir -p /tmp/package-iptables-$IPTABLES_VERSION/usr/doc/iptables-$IPTABLES_VERSION
cp -f /usr/src/iptables/COMMIT_NOTES /tmp/package-iptables-$IPTABLES_VERSION/usr/doc/iptables-$IPTABLES_VERSION
cp -f /usr/src/iptables/COPYING /tmp/package-iptables-$IPTABLES_VERSION/usr/doc/iptables-$IPTABLES_VERSION
cp -f /usr/src/iptables/INCOMPATIBILITIES /tmp/package-iptables-$IPTABLES_VERSION/usr/doc/iptables-$IPTABLES_VERSION
cp -f /usr/src/iptables/INSTALL /tmp/package-iptables-$IPTABLES_VERSION/usr/doc/iptables-$IPTABLES_VERSION

cd /tmp/package-iptables-$IPTABLES_VERSION
mkdir install
cat > ./install/slack-desc << _EOF_
iptables: iptables (1.4.0 + imq + layer7 + ipp2p)
iptables:
iptables: iptables is a packet filter administration tool. 
iptables:
iptables: Iptables can be used to build internet firewalls based on stateless
iptables: and stateful packet filtering, use NAT and masquerading for sharing
iptables: internet access if you don't have enough public IP addresses, use NAT
iptables: to implement transparent proxies, aid the tc and iproute2 systems
iptables: used to build sophisticated QoS and policy routers, do further packet
iptables: manipulation (mangling) like altering the TOS/DSCP/ECN bits of the IP
iptables: header, and much more.  See:  http://www.netfilter.org
_EOF_

# Tworzymy pakiet iptables
makepkg -l y -c n iptables-$IPTABLES_VERSION-i686-$VERSIONMINI.tgz

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

# Kopiujemy przygotowany plik konfiguracyjny kernela
cp -f ../config-$KERNEL_VERSION-imq_ipp2p_layer7 .config

# Kompilujemy kernela
make

# Usuwamy katalog z rozpakowywanymi zrodlami [dla bezpieczenstwa jesli byl stary]
rm -rf /tmp/package-kernel-$KERNEL_VERSION

# Przygotowywujemy docelowy katalog dla pakietu kernela
mkdir -p /tmp/package-kernel-$KERNEL_VERSION/boot/
mkdir -p /tmp/package-kernel-$KERNEL_VERSION/install

# Kopiujemy pliki kernela do katalogu dla pakietu kernela
cp -f /usr/src/linux-$KERNEL_VERSION/.config /tmp/package-kernel-$KERNEL_VERSION/boot/config-$VERSION-$VERSIONMINI
cp -f /usr/src/linux-$KERNEL_VERSION/arch/i386/boot/bzImage /tmp/package-kernel-$KERNEL_VERSION/boot/vmlinuz-$VERSION-$VERSIONMINI
cp -f /usr/src/linux-$KERNEL_VERSION/System.map /tmp/package-kernel-$KERNEL_VERSION/boot/System.map-$VERSION-$VERSIONMINI

# Przechodzimy do katalogu
cd /tmp/package-kernel-$KERNEL_VERSION/boot/

# Tworzymy dowiazania symboliczne do plikow kernela
ln -s config-$VERSION-$VERSIONMINI config
ln -s vmlinuz-$VERSION-$VERSIONMINI vmlinuz
ln -s System.map-$VERSION-$VERSIONMINI System.map

# Przygotowujemy plik opisujacy pakiet kernela
cat > ../install/slack-desc << _EOF_
kernel-$VERSION: kernel-$VERSION (a general purpose single processor Linux kernel)
kernel-$VERSION:
kernel-$VERSION: This is a Linux kernel with built-in support for most IDE controllers.
kernel-$VERSION: For filesystem support, or if you need to load support for a SCSI or
kernel-$VERSION: other controller, then you'll need to load one or more kernel modules
kernel-$VERSION: using an initial ramdisk, or initrd.  For more information about
kernel-$VERSION: creating an initrd, see the README.initrd file in the /boot directory.
kernel-$VERSION:
kernel-$VERSION: + imq + layer7 + ipp2p
kernel-$VERSION:
kernel-$VERSION:
_EOF_

cd ..

# Tworzymy pakiet kernela
makepkg -l y -c n kernel-$VERSION-$KERNEL_VERSION-i486-$VERSIONMINI.tgz

# Przechodzimy do katalogu
cd /usr/src/linux

# Usuwamy katalog z rozpakowywanymi zrodlami [dla bezpieczenstwa jesli byl stary]
rm -rf /tmp/package-kernel-modules-$KERNEL_VERSION

# Przygotowywujemy docelowy katalog dla pakietu modulow kernela
mkdir -p /tmp/package-kernel-modules-$KERNEL_VERSION
make modules_install INSTALL_MOD_PATH=/tmp/package-kernel-modules-$KERNEL_VERSION

mkdir -p /tmp/package-kernel-modules-$KERNEL_VERSION/etc/rc.d

# Przenosimy pobrany plik rc.modules
mv -f /usr/src/rc.modules.new /tmp/package-kernel-modules-$KERNEL_VERSION/etc/rc.d/rc.modules-$KERNEL_VERSION-$VERSIONMINI.new

# Ustawiamy uprawnienia do pliku rc.modules na rwxr-xr-x
chmod 755 /tmp/package-kernel-modules-$KERNEL_VERSION/etc/rc.d/rc.modules-$KERNEL_VERSION-$VERSIONMINI.new

# Przygotowujemy plik opisujacy pakiet modulow kernela
mkdir -p /tmp/package-kernel-modules-$KERNEL_VERSION/install
cat << EOF > /tmp/package-kernel-modules-$KERNEL_VERSION/install/slack-desc
kernel-modules: kernel-modules (single CPU Linux kernel modules)
kernel-modules:
kernel-modules: A kernel module is a piece of object code that can be dynamically
kernel-modules: loaded into the Linux kernel to provide new kernel functions.  Most of
kernel-modules: these modules provide support for devices such as CD-ROM drives, tape
kernel-modules: drives, and ethernet cards.  You can choose which modules to load by
kernel-modules: editing /etc/rc.d/rc.modules.
kernel-modules:
kernel-modules: +imq + layer7 + ipp2p
kernel-modules:
kernel-modules: For kernel-$VERSION-$KERNEL_VERSION-i486-$VERSIONMINI.tgz
EOF


# Write out the doinst.sh:
cat << EOF > /tmp/package-kernel-modules-$KERNEL_VERSION/install/doinst.sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/rc.d/rc.modules-$KERNEL_VERSION-$VERSIONMINI.new

# If rc.modules is a real file, back it up:
if [ -r etc/rc.d/rc.modules -a ! -L etc/rc.d/rc.modules ]; then
  cp -a etc/rc.d/rc.modules etc/rc.d/rc.modules.bak 
fi

# A good idea whenever kernel modules are added or changed:
if [ -x sbin/depmod ]; then
  chroot . /sbin/depmod -a ${KERNEL_VERSION} 1> /dev/null 2> /dev/null
fi

EOF

# Przechodzimy do katalogu
cd /tmp/package-kernel-modules-$KERNEL_VERSION

# Tworzymy pakiet modulow kernela
makepkg -l y -c n kernel-modules-$KERNEL_VERSION-i486-$VERSIONMINI.tgz

# Przechodzimy do katalogu
cd /usr/src

# Usuwamy katalog z rozpakowywanymi zrodlami [dla bezpieczenstwa jesli byl stary]
rm -rf /tmp/package-layer7_protocols /usr/src/l7-protocols-$LAYER7_DATE

# Przygotowywujemy docelowy katalog dla pakietu protokolow layer7
mkdir /tmp/package-layer7_protocols

# Rozpakowywujemy zrodla protokolow layer7
tar -zxvf l7-protocols-$LAYER7_DATE.tar.gz

# Przechodzimy do katalogu
cd l7-protocols-$LAYER7_DATE

# Kopiujemy pliki protokolow layer7
make install PREFIX=/tmp/package-layer7_protocols

# Przechodzimy do katalogu
cd /tmp/package-layer7_protocols

# Przygotowujemy plik opisujacy pakiet protokolow layer7
mkdir install
cat > ./install/slack-desc << _EOF_
layer7_protocols: layer7_protocols (Protocols definitions /etc/l7-protocols)
layer7_protocols:
layer7_protocols: $LAYER7_DATE
layer7_protocols:
layer7_protocols:
layer7_protocols:
layer7_protocols:
layer7_protocols:
layer7_protocols:
layer7_protocols:
layer7_protocols:
_EOF_

# Tworzymy pakiet protokolow layer7
makepkg -l y -c n layer7_protocols-`echo $LAYER7_DATE | sed s/"-"//g`-noarch-$VERSIONMINI.tgz


