#! /usr/bin/perl 
# -*- Mode: Perl -*-
# This script created automatically from scripts/create_replacements.in
##############################################################################
##
##  $Id: create_replacements.in,v 1.1.1.1 1998/02/21 11:40:04 fawcett Exp $
##
##  CREATE_REPLACEMENTS
##  Copyright (C) 1996-2000  Tom Fawcett (fawcett@members.sourceforge.net)
##
##  This program is free software; you may redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
##############################################################################
use strict qw(subs refs);
use File::Basename;
use File::Path;
use FileHandle;
use Cwd;
use English;
use lib "/etc/yard", "/usr/share/yard";
use yardconfig;

BEGIN { require "yard_utils.pl" } # Defines info() and error() for compiler

require "Config.pl";		# Just for $oldroot, actually

##############################################################################
my($fstab_full) = "/etc/yard/Replacements/etc/fstab";
if (!-e $fstab_full) {
   create_fstab($fstab_full);
} else {
   print "\nYou already have file $fstab_full.  Creating a new file\n";
   print "\t$fstab_full.new\nRename it to $fstab_full if you want to use it.\n";
   create_fstab("$fstab_full.new");
}


my($rc_full) = "/etc/yard/Replacements/etc/rc";
if (!-e $rc_full) {
   create_rc($rc_full);
} else {
   print "\nYou already have file $rc_full.  Creating a new file\n";
   print "\t$rc_full.new\nRename it to $rc_full if you want to use it.\n";
   create_rc("$rc_full.new");
}


my($lilo_conf_full) = "/etc/yard/Replacements/etc/lilo.conf";
if (!-e $lilo_conf_full) {
   create_lilo_conf($lilo_conf_full);
} else {
   print "\nYou already have file $lilo_conf_full.  Creating a new file\n";
   print "\t$lilo_conf_full.new\nRename it to $lilo_conf_full if you want to use it.\n";
   create_rc("$lilo_conf_full.new");
}
exit;

##############################################################################

sub create_fstab {
   my($NEWFSTAB) = @_;
   open(NEWFSTAB, ">$NEWFSTAB") or die "$NEWFSTAB: $!";
    
    print NEWFSTAB <<BLARD;
# DEVICE	MOUNTPOINT	TYPE	OPTIONS	DUMP	FSCKORDER
#----------------------------------------------------------------
/dev/ram0       /               ext2    defaults
/proc           /proc           proc    defaults
# Entries adapted from existing fstab:
BLARD
	
    my($line);
    open(FSTAB, "/etc/fstab") or die "/etc/fstab: $!\n";

    while ($line = <FSTAB>) {
	chomp $line;
	next if $line =~ /^\#/ or $line =~ /^\s*$/;
	
	my($device, $mpt, $type, $options, @rest) = split(' ', $line);
	
	if ($device =~ m!^/(proc|dev/ram)! or $type eq "proc") {
	    ## Don't allow /proc or /dev/ram? definitions
	    next;

	} elsif ($type eq 'swap') {
	    ##  Pass swap through unchanged

	} else {
	    ##  By default:
	    ##  - Add a 'noauto' option if it doesn't already have one
	    ##  - Put mountpoint under oldroot
	    $options .= ',noauto' unless $options =~ /\bnoauto\b/;

	    if ($mpt eq '/') {
		$mpt = $CFG::oldroot; # limitation of mount cmd
	    } else {
		$mpt =  $CFG::oldroot . $mpt;
	    }
	}
	
	print NEWFSTAB join("\t", ($device, $mpt, $type, $options,
				   @rest)), "\n";
    }

    close(FSTAB);
    close(NEWFSTAB);

    print "Created $NEWFSTAB\n";
}

##############################################################################

sub create_rc {

}

sub create_lilo_conf {
   my($LILO_CONF) = @_;
    my($append_line);
    my($lilo_conf) = "/etc/lilo.conf";

    if (-e $lilo_conf) {
	open(LC, "<$lilo_conf") or die "$lilo_conf: $!";
	print "Found your Lilo configuration file $lilo_conf\n";
	while (<LC>) {
	    if (/^\s*append\s*=/) {
		$append_line = $_;
		print "Grabbing lilo APPEND line: $append_line";
	    }
	}
	close(LC);
    } else {
	print "You have no file $lilo_conf.\n";
    }

   open(LILO_CONF, ">$LILO_CONF") or die "$LILO_CONF: $!";

    print LILO_CONF "boot	  =\$floppy\n";
    print LILO_CONF "install   =/boot/boot.b\n";
    print LILO_CONF "map	  =/boot/map\n";
    print LILO_CONF "read-write\n";
    print LILO_CONF "backup    =/dev/null\n";
    print LILO_CONF $append_line                    if defined($append_line);
# I removed 'compact' because of possible BIOS conflicts
#    print LILO_CONF "compact\n";
    print LILO_CONF "image	  =\$kernel_basename\n";
    print LILO_CONF "ramdisk	  = 10240\n"; # 10 MEG ramdisk by default
    print LILO_CONF "label	  =Yardboot\n";
    print LILO_CONF "root	  =\$floppy\n";

    close(LILO_CONF) or die "closing $LILO_CONF: $!";

    print "Created $LILO_CONF\n";
}
