#!/bin/bash

brcfg_dir=$(realpath -s $(dirname "$0"))

path=$1
br_path=${path%/}

help()
{
    echo "patch_buildroot2023 <path_to_buildroot>"
    echo "    i.e. vcoctavobr/board/cv/patch_buildroot2023.sh buildroot-2023.02.8/"
    exit
}

while getopts h flag
do
    case "${flag}" in
        h) help;;
    esac
done

rm -rf "${br_path}-svn"
rm -rf "${br_path}/.svn"
svnadmin create "${br_path}-svn"
repos_path=$(realpath -s "${br_path}-svn")

echo "$repos_path"
cd "${br_path}"
svn checkout "file:///$repos_path" .

svn add . --force > /dev/null
svn commit . -m "Import existing codebase"

svn up
patch -p0 < $brcfg_dir/buildroot_patches/uboot-tools-addr.patch
svn commit . --force-log -m "uboot-tools-addr"

svn up
patch -p0 < $brcfg_dir/buildroot_patches/busybox-hostname-dhcp.br2023.patch
svn commit . --force-log -m "busybox-hostname-dhcp"

svn up
patch -p0 < $brcfg_dir/buildroot_patches/pkg-generic.br2023.patch
svn commit . --force-log -m "pkg-generic"

svn up
patch -p0 < $brcfg_dir/buildroot_patches/svn-no-signature-needed.br2023.patch
svn commit . --force-log -m "svn-no-signature-needed"

svn up
patch -p0 < $brcfg_dir/buildroot_patches/svnrepo-uboot-linux.patch
svn commit . --force-log -m "svnrepo-uboot-linux"
