#!/bin/bash
    
# get arguments 
kernelversion="$1"
kernelimagefile="$2"
    
# backup original /boot for optional recovery
/bin/cp -a /boot /boot_backup_$(date +%s)
    
# install kernel image file
/bin/mv $kernelimagefile /boot/kernel7.img
    
# install device tree files for all rpi 2 and 3
# by copying the device tree files for raspberry pi into /boot
# for rpi kernel use the raspbian specific bcm27 device tree files
#/bin/cp /usr/lib/linux-image-${kernelversion}/bcm27*.dtb /boot

# install device tree files for all rpi 2 and 3
# by copying the device tree files for raspberry pi into /boot
cd /usr/lib/linux-image-${kernelversion}/
if [[ "$kernelversion" == *rpi* ]]
then 
    # for rpi kernel use the raspbian specific bcm27 device tree files
    /bin/cp bcm27*.dtb /boot
    # for rpi also copy the overlay files into /boot/overlays
    /bin/cp -a overlays /boot
else
    # for mainline kernel use the bcm28 files, however give
    # them the names the raspbian bootloader expect (that is
    # the  raspbian specific bcm27 names)
    /bin/cp bcm2836-rpi-2-b.dtb   /boot/bcm2709-rpi-2-b.dtb
    /bin/cp bcm2837-rpi-3-b.dtb   /boot/bcm2710-rpi-3-b.dtb
    /bin/cp bcm2837-rpi-3-b-plus.dtb   /boot/bcm2710-rpi-3-b-plus.dtb
    # note: kernel.org doesn't yet have dts file for rpi-4; just released
fi    

