#! /usr/bin/perl

#ident "$Id: clean-efibootmgr,v 1.1 2015/08/31 11:16:52 carsten.kuhl Exp $ Copyright (c) 2013 Bruker BioSpin GmbH";

use strict;
use warnings;

my @bmenu = `efibootmgr -D`;
printf join "", @bmenu;
printf "\nSearching for duplicates ...\n";

my %hash = ();
foreach my $l (@bmenu) {
  next if !($l =~ /^Boot\d{1,}/);
  chomp $l;
  my ($num, $val) = ($l =~ /(^\S+)\s+(.+$)/g);
  (my $hnum = $num) =~ s/^Boot([0-9a-fA-F]{1,}).*$/$1/g;
  $hash{$num} = $val."\#\#\#".$hnum;
}

my $old_val = '';
foreach my $value (sort values %hash) {
  my @splt = split /\#\#\#/, $value;
  if ($splt[0] eq $old_val) {
    printf "Removing boot entry ".$splt[1]." ...\n";
    `efibootmgr -b $splt[1] -B`;
  }
  $old_val = $splt[0];
}
printf "\nCleanup result: \n".`efibootmgr`."\n";
