#!/bin/bash

# Replaced apt installation with dpkg (offline) by pavroo
# Last update 2020/04/09

DEVCHROOT=$(mount | grep proc | grep calamares | awk '{print $3}' | sed -e "s#/proc##g")

if [ -d /sys/firmware/efi/efivars ]; then
    echo " * Replacing grub-pc with grub-efi..."
    # grub-probe doesn't work in a CHROOT...
    mv $DEVCHROOT/usr/sbin/grub-probe $DEVCHROOT/usr/sbin/grub-probe.bak
    echo "exit 0" > $DEVCHROOT/usr/sbin/grub-probe
    chmod +x $DEVCHROOT/usr/sbin/grub-probe
    # chroot $DEVCHROOT apt-get --allow-unauthenticated -y install grub-efi
    mkdir -p $DEVCHROOT/boot/efi/EFI/sparky
    mkdir -p $DEVCHROOT/debs
    if [ -f /lib/live/mount/medium/offline/*.deb ]; then
    	cp /lib/live/mount/medium/offline/* $DEVCHROOT/debs/
    else
    	cp /run/live/medium/offline/* $DEVCHROOT/debs/
    fi
    #cp /lib/live/mount/medium/offline/* $DEVCHROOT/debs/
    #chroot $DEVCHROOT dpkg -i /debs/*.deb

cat > $DEVCHROOT/tmp/efi-install << FOO
#!/bin/bash
cd debs
dpkg -i *.deb
cd ..
FOO

    chmod +x $DEVCHROOT/tmp/efi-install
    chroot $DEVCHROOT /tmp/efi-install
    rm -f $DEVCHROOT/tmp/efi-install
    rm -rf $DEVCHROOT/debs
    rm -f $DEVCHROOT/usr/sbin/grub-probe
    mv $DEVCHROOT/usr/sbin/grub-probe.bak $DEVCHROOT/usr/sbin/grub-probe
fi
