#!/bin/sh -x

wget -q --no-check-certificate https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.19.tar.xz
tar xpJf linux-4.19.tar.xz

wget --no-check-certificate https://www.kernel.org/pub/linux/kernel/v4.x/patch-4.19.127.xz
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/UNICON/vd_unicon-kernel-20181027-4.19.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/aufs4-standalone.git
( cd aufs4-standalone ; git checkout origin/aufs4.19.63+ ; git reset --hard 1bb4caf )
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/mcp/4.19-panic_beep.patch
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/mcp/4.19-morse_code_panics.patch
git clone https://gitlab.com/nigelcunningham/tuxonice-kernel.git
( cd tuxonice-kernel ; git checkout origin/tuxonice-4.19 ; git reset --hard 2c940ea
    git diff 84920cc | bzip2 > ../tuxonice-for-linux-4.19.121-2020-05-09.patch.bz2 )
git clone https://github.com/raspberrypi/linux.git
( cd linux ; git checkout origin/rpi-4.19.y ; git reset --hard cc39f1c
    git diff 106fa14 | xz > ../rpi-4.19.127-20200717-cc39f1c.patch.xz )
wget ftp://plamo.linet.gr.jp/pub/Plamo-test/kernelsrc_patches/ovlfs/overlayfs-allow_unprivileged_mounts.patch

cd linux-4.19
unxz -c ../patch-4.19.127.xz | patch -Np1 -i -
patch -Np1 -i ../vd_unicon-kernel-20181027-4.19.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 ../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.19-panic_beep.patch
patch -Np1 -i ../4.19-morse_code_panics.patch
bunzip2 -c ../tuxonice-for-linux-4.19.121-2020-05-09.patch.bz2 | patch -Np1 -i -
unxz -c ../rpi-4.19.127-20200717-cc39f1c.patch.xz | patch -Np1 -i -
patch -Np1 -i ../overlayfs-allow_unprivileged_mounts.patch
find . -name "*.orig" -ls -exec rm {} \;
