doinst () 
{ 
    InstallDir="/boot/efi";
    TargetDir="/EFI/refind";
    Refind="refind_x64.efi";
    Efibootmgr=`which efibootmgr 2> /dev/null`;
    if [[ -n "$Efibootmgr" ]]; then
        InstallDisk=`grep "$InstallDir" /etc/mtab | cut -d " " -f 1 | cut -c 1-8`;
        PartNum=`grep "$InstallDir" /etc/mtab | cut -d " " -f 1 | cut -c 9-10`;
        EntryFilename="$TargetDir/$Refind";
        EfiEntryFilename=`echo ${EntryFilename//\//\\\}`;
        EfiEntryFilename2=`echo ${EfiEntryFilename} | sed s/\\\\\\\\/\\\\\\\\\\\\\\\\/g`;
        ExistingEntry=`"$Efibootmgr" -v | grep -i "$EfiEntryFilename2"`;
        if [[ -n "$ExistingEntry" ]]; then
            ExistingEntryBootNum=`echo "$ExistingEntry" | cut -c 5-8`;
            FirstBoot=`"$Efibootmgr" | grep BootOrder | cut -c 12-15`;
            if [[ "$ExistingEntryBootNum" != "$FirstBoot" ]]; then
                echo "An existing rEFInd boot entry exists, but isn't set as the default boot";
                echo "manager. The boot order is being adjusted to make rEFInd the default boot"loc;
                echo "manager. If this is NOT what you want, you should use guef or efibootmgr to";
                echo "manually adjust your EFI's boot order.";
            fi;
            "$Efibootmgr" -b $ExistingEntryBootNum -B &> /dev/null;
        fi;
        echo "Installing it!";
        if [[ "$KeepName" == 0 ]]; then
            "$Efibootmgr" -c -l "$EfiEntryFilename" -L "rEFInd Boot Manager" -d $InstallDisk -p $PartNum &> /dev/null;
        else
            "$Efibootmgr" -c -l "$EfiEntryFilename" -L "rEFInd Boot Manager" -d $InstallDisk -p $PartNum -u "$TargetShim $TargetX64" &> /dev/null;
        fi;
        if [[ $? != 0 ]]; then
            EfibootmgrProblems=1;
            Problems=1;
        fi;
    else
        EfibootmgrProblems=1;
        Problems=1;
    fi;
    if [[ -n $EfibootmgrProblems ]]; then
        echo;
        echo "ALERT: There were problems running the efibootmgr program! You may need to";
        echo "rename the $Refind binary to the default name (EFI/BOOT/bootx64.efi";
        echo "on x86-64 systems, EFI/BOOT/bootia32.efi on x86 systems, or";
        echo "EFI/BOOT/bootaa64.efi on ARM64 systems) to have it run!";
        echo;
    else
        echo "rEFInd has been set as the default boot manager.";
    fi
}
doinst
