#!/usr/bin/perl
# -*- Mode: cperl -*-
# Copyright (C) 2000 by MandrakeSoft, Pixel <pixel@mandrakesoft.com>
# and others MandrakeSoft folks.
# Redistribution of this file is permitted under the terms of the GNU 
# Public License (GPL)
##
# Description: Detect what type of loader you have on your MBR
# If neither LILO nor GRUB is found on MBR, try partitions too.
# $Id: detectloader,v 1.5 2000/09/20 11:32:13 prigaux Exp $

open F, "/proc/partitions";
while (<F>) {
  /[0-9]$/ || ($LOADER= detect(split) || $LOADER ); 
}
if (!$LOADER) {
  warn "no bootloader on MBR, trying partitions!\n";
  open F, "/proc/partitions";
  while (<F>) {
    /[0-9]$/ && ($LOADER= detect(split) || $LOADER ); 
  }
}
print $LOADER, "\n";
  
sub detect {  
  my (undef, undef, $size, $dev) = @_;
  $size == 0x3fffffff and return; # skip cdroms (otherwise stops cd-audios)
  sysopen D, "/dev/$dev", 0 or return;
  sysread D, $_, 2;
  if (/\xEB[GH]/) {
    $magic = "GRUB \0";
    $anothermagic = "stage1 \0";
    sysseek D, 0x17d, 0; sysread D, $a, length($anothermagic) + 5;
    sysseek D, 0x181, 0; sysread D, $b, length $magic;
    return "GRUB" if $a =~ $magic || $b eq $anothermagic;
  } else {
    sysread D, $_, 8;
    return "LILO" if /LILO/; 
  }
}
