config() {
    NEW="$1"
    OLD="$(dirname $NEW)/$(basename $NEW .new)"
    # If there's no config file by that name, mv it over:
    if [ ! -r $OLD ]; then
        mv $NEW $OLD
    elif [ "$(cat $OLD | md5sum)" = "$(cat $NEW | md5sum)" ]; then
        # toss the redundant copy
        rm $NEW
    fi
    # Otherwise, we leave the .new copy for the admin to consider...
	
}

preserve_perms() {
    NEW="$1"
    OLD="$(dirname $NEW)/$(basename $NEW .new)"
    if [ -e $OLD ]; then
        cp -avr $OLD ${NEW}.incoming
        cat $NEW > ${NEW}.incoming
        mv ${NEW}.incoming $NEW
    fi
    config $NEW
	
}

( cd lib/firmware/brcm ; rm -rf brcmfmac43430a0-sdio.ilife-S806.txt )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430a0-sdio.jumper-ezpad-mini3.txt )
( cd lib/firmware/brcm ; rm -rf "brcmfmac43430a0-sdio.ONDA-V80 PLUS.txt" )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.AP6212.txt )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.bin )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.clm_blob )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.Hampoo-D2D3_Vi8A1.txt )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.MUR1DX.txt )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.raspberrypi,3-model-b.txt )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.raspberrypi,model-zero-w.txt )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.sinovoip,bpi-m2-plus.txt )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.sinovoip,bpi-m2-ultra.txt )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.sinovoip,bpi-m2-zero.txt )
( cd lib/firmware/brcm ; rm -rf brcmfmac43430-sdio.sinovoip,bpi-m3.txt )

# Roll the config()
if [ -f lib/firmware/brcm/*.new ]; then 
  config lib/firmware/brcm/brcmfmac43430-sdio.bin.new
fi
# set /dev/ttyS0 serial port list entry to /dev/ttyAMA0 in /etc/inittab and comment out
# this will prevent the 'INIT: Id "s0" respawning too fast' error
if grep -q '^s0:12345' etc/inittab ; then
  sed -Ei 's/^s0:12345:respawn.*/#s0:12345:respawn:\/sbin\/agetty --keep-baud 115200,38400,9600 ttyAMA0 vt100/g' etc/inittab &&   echo " + Serial line #s0:12345:respawn in /etc/inittab - DISABLED"
fi

# enable PermitRootLogin in /etc/ssh/sshd_config
if grep -q '^#PermitRootLogin' etc/ssh/sshd_config ; then
  sed -Ei 's/#PermitRootLogin.*/PermitRootLogin yes/g' etc/ssh/sshd_config &&   echo " + PermitRootLogin in /etc/ssh/sshd_config - ENABLED"
fi

