#!/bin/sh -x

curl -skRO https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.0.tar.xz
tar xpJf linux-5.0.tar.xz

curl -kRO https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/patch-5.0.21.xz
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/UNICON/vd_unicon-kernel-20190309-5.0.patch
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/UNICON/vd_unicon-kernel-device_set_font_noarg.patch
wget http://vdlinux.sourceforge.jp/dists/UNICON/vd_unicon-kernel-fonts-20140614.patch.bz2
git clone https://github.com/sfjro/aufs5-standalone.git
( cd aufs5-standalone ; git checkout origin/aufs5.0 ; git reset --hard 25f304c )
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/mcp/5.0-panic_beep.patch
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/mcp/5.0-morse_code_panics.patch
git clone https://gitlab.com/nigelcunningham/tuxonice-kernel.git
( cd tuxonice-kernel ; git checkout origin/tuxonice-5.0 ; git reset --hard d800c12
    git diff fd1594e | bzip2 > ../tuxonice-for-linux-5.0.21-2019-06-07.patch.bz2 )
git clone https://github.com/raspberrypi/linux.git
( cd linux ; git checkout origin/rpi-5.0.y ; git reset --hard 0222b82
    git diff fd1594e | xz > ../rpi-5.0.21-20190923-0222b82.patch.xz )
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/ovlfs/overlayfs-allow_unprivileged_mounts.patch

cd linux-5.0
unxz -c ../patch-5.0.21.xz | patch -Np1 -i -
patch -Np1 -i ../vd_unicon-kernel-20190309-5.0.patch
patch -Np1 -i ../vd_unicon-kernel-device_set_font_noarg.patch
bunzip2 -c ../vd_unicon-kernel-fonts-20140614.patch.bz2 | patch -Np1 -i -
patch -Np1 -i ../aufs5-standalone/aufs5-kbuild.patch
patch -Np1 -i ../aufs5-standalone/aufs5-base.patch
patch -Np1 -i ../aufs5-standalone/aufs5-mmap.patch
patch -Np1 -i ../aufs5-standalone/aufs5-standalone.patch
cp -av ../aufs5-standalone/Documentation .
cp -av ../aufs5-standalone/fs .
cp -pv ../aufs5-standalone/include/uapi/linux/aufs_type.h include/uapi/linux
patch -Np1 -i ../5.0-panic_beep.patch
patch -Np1 -i ../5.0-morse_code_panics.patch
bunzip2 -c ../tuxonice-for-linux-5.0.21-2019-06-07.patch.bz2 | patch -Np1 -i -
unxz -c ../rpi-5.0.21-20190923-0222b82.patch.xz | patch -Np1 -i -
patch -Np1 -i ../overlayfs-allow_unprivileged_mounts.patch
find . -name "*.orig" -ls -exec rm {} \;
