#!/usr/bin/perl -w
# update the partition layout on IPF

$rootfs = "";
$oldboot = "$rootfs/boot";
$newboot = "$rootfs/boot/efi";
$tmpboot = "$rootfs/mnt";
$fstab = "$rootfs/etc/fstab";
$fstmp = "$rootfs/etc/fstab.YaST2-uEFImp-save";
$mtabback = "$rootfs/etc/mtab~";

sub System {
  print STDERR "@_\n";
  #return 0;
  return system(@_);
}

# determine device holding /boot file-system from /etc/fstab
# '/boot/efi' is ignored (as are comments!) => nothing to do
sub GetBootDev {
    open (IN, "$fstab") || die "failed to open $fstab for reading";
    my $ret = undef;
    while ($line = <IN>) {
	if ($line =~ /^([^\# \t]+)[ \t]+\/boot[ \t]+.*$/) {
	    $ret = $1;
	    last;
	} elsif ($line =~ /^([^\# \t]+)[ \t]+\/boot\/efi[ \t]+.*$/) {
	    # everthing is fine!
	    exit 0;
	}
    }
    close (IN);
    return $ret;
}

# unmount /boot partition
sub MoveBootPartition2Tmp {
    System ("/bin/rm -f $mtabback") if ( -e $mtabback);
    System ("/bin/mount --move $oldboot $tmpboot") == 0 || die "Moving $oldboot to $tmpboot failed";
}

#update mountpoints in /etc/fstab (/boot -> /boot/efi)
sub UpdateFstab {
    System ("/bin/cp $fstab $fstmp") == 0
	|| die "cannot copy $fstab to $fstmp";
    return 0 unless ( -r $fstmp );
    open (IN,  "< $fstmp") || die "failed to open $fstmp for reading";
    open (OUT, "> $fstab") || die "failed to open $fstab for writing";
    while ($line = <IN>) {
	if ($line =~ /^[^\# \t]+[ \t]+\/boot[ \t]+.*$/) {
	    $line =~ s/^([^\# \t]+[ \t]+\/boot)([ \t]+.*)$/$1\/efi$2/;
	}
	print OUT "$line";
    }
    close (IN);
    close (OUT);
    #System ("/bin/rm $fstmp");
}

# mount the /boot/efi partition
sub MoveBootPartition2New {
    System ("mkdir -p $newboot") == 0|| die "Failed to create EFI mount point";
    System ("/bin/mount --move $tmpboot $newboot") == 0|| die "Failed to move EFI partition";
}

# move files from /boot/efi/* back to /boot (everything except efi)
sub MoveFiles {
    my $files = "{System.map,initrd,vmlinuz,modversions-,config-}*";
    foreach $f ( <$newboot/{$files}*> ) {
      System ("mv '$f' $oldboot") == 0 ||
        die "Moving files from old '/boot' file-system to new '/boot' directory failed";
    }
}

$bootdev = GetBootDev () || die "Failed to determine the device with the EFI partition";
MoveBootPartition2Tmp ();
UpdateFstab ();
MoveBootPartition2New ();
MoveFiles ();

