#!/bin/sh -x

wget -q https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.13.tar.xz
tar xpJf linux-4.13.tar.xz

wget https://www.kernel.org/pub/linux/kernel/v4.x/patch-4.13.16.xz
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/UNICON/vd_unicon-kernel-20170909-4.13.patch
wget http://vdlinux.sourceforge.jp/dists/UNICON/vd_unicon-kernel-fonts-20140614.patch.bz2
git clone https://github.com/sfjro/aufs4-standalone.git
( cd aufs4-standalone ; git checkout origin/aufs4.13 ; git reset --hard 78cbc7f )
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/mcp/4.13-panic_beep.patch
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/mcp/4.13-morse_code_panics.patch
git clone https://github.com/NigelCunningham/tuxonice-kernel.git
( cd tuxonice-kernel ; git checkout origin/tuxonice-4.13 ; git reset --hard d09eb39
    git diff e87c139 | bzip2 > ../tuxonice-for-linux-4.13.16-2017-11-24.patch.bz2 )
git clone https://github.com/raspberrypi/linux.git
( cd linux ; git checkout origin/rpi-4.13.y ; git reset --hard c9b4367
    git diff e87c139 | xz > ../rpi-4.13.16-20171117-c9b4367.patch.xz )
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/ovlfs/overlayfs-allow_unprivileged_mounts.patch

cd linux-4.13
unxz -c ../patch-4.13.16.xz | patch -Np1 -i -
patch -Np1 -i ../vd_unicon-kernel-20170909-4.13.patch
bunzip2 -c ../vd_unicon-kernel-fonts-20140614.patch.bz2 | patch -Np1 -i -
patch -Np1 -i ../aufs4-standalone/aufs4-kbuild.patch
patch -Np1 -i ../aufs4-standalone/aufs4-base.patch
patch -Np1 -i ../aufs4-standalone/aufs4-mmap.patch
patch -Np1 -i ../aufs4-standalone/aufs4-standalone.patch
cp -av ../aufs4-standalone/Documentation .
cp -av ../aufs4-standalone/fs .
cp -pv ../aufs4-standalone/include/uapi/linux/aufs_type.h include/uapi/linux
patch -Np1 -i ../4.13-panic_beep.patch
patch -Np1 -i ../4.13-morse_code_panics.patch
bunzip2 -c ../tuxonice-for-linux-4.13.16-2017-11-24.patch.bz2 | patch -Np1 -i -
unxz -c ../rpi-4.13.16-20171117-c9b4367.patch.xz | patch -Np1 -i -
patch -Np1 -i ../overlayfs-allow_unprivileged_mounts.patch
find . -name "*.orig" -ls -exec rm {} \;
