#!/bin/sh -x

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

curl -kRO https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/patch-5.1.21.xz
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/UNICON/vd_unicon-kernel-20190511-5.1.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.1 ; git reset --hard d051ff3 )
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/mcp/5.1-panic_beep.patch
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/mcp/5.1-morse_code_panics.patch
git clone https://gitlab.com/nigelcunningham/tuxonice-kernel.git
( cd tuxonice-kernel ; git checkout origin/tuxonice-5.1 ; git reset --hard f626880
    git diff 4a9b1eb | bzip2 > ../tuxonice-for-linux-5.1.21-2019-08-19.patch.bz2 )
git clone https://github.com/raspberrypi/linux.git
( cd linux ; git checkout origin/rpi-5.1.y ; git reset --hard 29ee73a
    git diff 4a9b1eb | xz > ../rpi-5.1.21-20190923-29ee73a.patch.xz )
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/ovlfs/overlayfs-allow_unprivileged_mounts.patch

cd linux-5.1
unxz -c ../patch-5.1.21.xz | patch -Np1 -i -
patch -Np1 -i ../vd_unicon-kernel-20190511-5.1.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.1-panic_beep.patch
patch -Np1 -i ../5.1-morse_code_panics.patch
bunzip2 -c ../tuxonice-for-linux-5.1.21-2019-08-19.patch.bz2 | patch -Np1 -i -
unxz -c ../rpi-5.1.21-20190923-29ee73a.patch.xz | patch -Np1 -i -
patch -Np1 -i ../overlayfs-allow_unprivileged_mounts.patch
find . -name "*.orig" -ls -exec rm {} \;
