#!/bin/bash

echo "Hi.  I'm going to attempt to help you configure your Linux system to"
echo "dual-boot both Linux and DOS.  I'll try to guess where your DOS"
echo "partition is, and then I'll ask for confirmation from you before"
echo "modifying any files."
echo ""

if [ ! -f /etc/lilo.conf ]; then
    echo "Your system has no lilo.conf on it! This probably isn't an" >&2
    echo "Intel based system. In any case, I can't set this machine up" >&2
    echo "for dual boot." >&2
    exit 2
fi

if [ ! -f /sbin/lilo ]; then
    echo "Your system has no lilo executable on it and probably isn't an" >&2
    echo "Intel based system. In any case, I can't set this machine up" >&2
    echo "for dual boot." >&2
    exit 2
fi

if grep label=dos /etc/lilo.conf > /dev/null; then
    echo "You already have an entry for booting dos in your lilo.conf. You"
    echo "must remove it before using this script to add a new one."
    exit 1
fi

echo -n "Looking for first DOS partition... "

part=`/sbin/fdisk -l 2>/dev/null | awk '/DOS/ { print $1 }' | head -1`

if [ -z $part ]; then
    echo "none"
    echo
    echo "No DOS partitions exist on this system (according to fdisk)."
    exit 1
fi

echo "found ${part}."
echo ""

if grep "other=$part" /etc/lilo.conf > /dev/null; then
    echo "You already have an entry for ${part} in /etc/lilo.conf. You must"
    echo "remove it before using this script to add a new one."
    exit 1
fi

table=`echo $part | cut -c1-8`

echo "I'm going to add the following entry to your /etc/lilo.conf:"
echo ""
echo "other=$part"
echo "	label=dos"
echo "	table=$table"
echo ""
echo "If this is correct, type 'y' followed by <Return> now. Typing anything"
echo "else will abort the operation"
echo ""
echo -n "Would you like to have your /etc/lilo.conf updated now? "
read answer
echo ""

if [ $answer = y -o $answer = yes -o $answer = Y ]; then
    echo -n "Updating /etc/lilo.conf... "
    echo "other=$part" >> /etc/lilo.conf
    echo "	label=dos" >> /etc/lilo.conf
    echo "	table=$table" >> /etc/lilo.conf
    echo "done."
    echo ""
    echo "Rerunning lilo now."
    echo ""
    /sbin/lilo
else
    echo "Operation aborted -- your /etc/lilo.conf has not been modified."
fi
