#!/bin/sh

set -e

grub_file=/mnt/grub/grub.cfg

copy_file() {
    for file in $(ls $1)
    do
        if [ -s $1"/"${file}"/grub.cfg" ]; then
            [ -s ${grub_file} ] && cp -a ${grub_file} $1"/"${file}/
        fi
    done
}

efiDev=$1
bootDev=$2

[ -z ${efiDev} ] && exit -1
[ -z ${bootDev} ] && exit -1

mount ${bootDev} /mnt
mount ${efiDev} /mnt/efi

copy_file /mnt/efi/EFI

if [ $(uname -m) = "loongarch64" ]; then
    copy_file /mnt/efi/EFI/BOOT
fi

umount /mnt/efi
umount /mnt
exit 0