#!/usr/bin/perl

###########################################################################
##
##  expect_uml
##  Copyright (C) 2000, 2001, 2002, 2003   by Jonathan Rosenbaum 
##                              <freesource@users.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.
##
##############################################################################


BEGIN {

    my $fhs = grep(/\/usr\/share\/perl5/,@INC);

    if ($fhs == 0) {
	unshift(@INC,"/usr/share/perl5");
	unshift(@INC,".");  # better make sure we mean . when we mean .
    }


    my $sbin = grep(/\/sbin/,$ENV{'PATH'});
    if ($sbin == 0) {
	$ENV{'PATH'} = "/sbin:" . $ENV{'PATH'};
    }

    $sbin = grep(/\/usr\/sbin/,$ENV{'PATH'});
    if ($sbin == 0) {
	$ENV{'PATH'} = "/usr/sbin:" . $ENV{'PATH'};
    }


    my $pwd = `pwd`; chomp $pwd;

    my $tools = grep(/$pwd\/user-mode-linux\/usr\/bin/,$ENV{'PATH'});
    if ($tools == 0) {
	$ENV{'PATH'} = "$pwd/user-mode-linux/usr/bin:" . $ENV{'PATH'};
    }

    $tools = grep(/$pwd\/user-mode-linux\/usr\/lib\/uml/,$ENV{'PATH'});
    if ($tools == 0) {
	$ENV{'PATH'} = "$pwd/user-mode-linux/usr/lib\/uml:" . $ENV{'PATH'};
    }

}

use BootRoot::UML;
use File::Basename;

if ( !$ARGV[0] ) {
    
    die "expect_uml ubd0 ubd1 other-options mount-point preserve_ownership filesystem_command\n";

}

my $arguments = "$ARGV[0] $ARGV[1] $ARGV[2]"; 
$ARGV[2] =~ /umid=([\w_]+)\s*/;
my $umid = $1;
my $mount_point = "$ARGV[3]";
my $preserve_ownership = "$ARGV[4]";
my $uml_kernel = "$ARGV[5]";

my $end = 7;
my $filesystem;
for ( 6 .. $#ARGV ) {
    if ( $_ == 6 ) {
	$filesystem = "$ARGV[6]";
	# What to do with the rieserfs command
	if ( $filesystem eq "mkreiserfs" ) {
	    $filesystem = $filesystem . " -f -f";
	}
    }
    else {
	if ( $ARGV[$end] ) {
	    $filesystem = $filesystem . " $ARGV[$end]";
	    $end++;
	}
	else {
	    last;
	}
    }
}


my $uml  = 
  BootRoot::UML->new( login_prompt    =>  "",
		      kernel          =>  $uml_kernel,
		      arguments       =>  $arguments,
		      login           =>  "root",
		      password_prompt =>  "Password: ",
		      password        =>  "",
		      prompt          =>  ".*# ",
		      halt            =>  "exit"
		      );

$uml->boot();

# Establish PATH, some compilations of bash ignore some important paths
$uml->command("PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:.");

# Sometimes this is required

$uml->command("mount -n /proc");
$uml->command("mount -o remount /");
$uml->command("mount -a");
$uml->command("/bin/hostname bootroot");

# Make the GID/UID 0 FS
if ( $filesystem =~ /^mkcramfs/ || $filesystem =~ /^genromfs/ || 
     $filesystem =~ /^mkfs\.jffs/ ) {
    $uml->command("mke2fs -m0 /dev/ubd/1");
}
else {
    $uml->command("$filesystem /dev/ubd/1");
}

$uml->command("mount /dev/ubd/1 /mnt1");
$uml->command("mount -t hostfs none -o $mount_point /mnt2");
$uml->command("cp -a /mnt2/* /mnt1");

if ( $preserve_ownership == 0 ) {
    $uml->command("chown -R 0:0 /mnt1");
}

if ( $filesystem =~ /^mkcramfs/ ) {
    my $cram_dir = dirname($mount_point);
    my $root_fs_name = basename($ARGV[1]);
    my $cramfs_name = "$root_fs_name" . "_cramfs";
    $uml->command("umount /mnt2");
    $uml->command("mount -t hostfs none -o $cram_dir /mnt2");
    $uml->command("mkcramfs /mnt1 /mnt2/$cramfs_name");
}
elsif ( $filesystem =~ /^genromfs/ ) {
    my $romfs_dir = dirname($mount_point);
    my $root_fs_name = basename($ARGV[1]);
    my $romfs_name = "$root_fs_name" . "_romfs";
    $uml->command("umount /mnt2");
    $uml->command("mount -t hostfs none -o $romfs_dir /mnt2");
    $uml->command("genromfs -d /mnt1 -f /mnt2/$romfs_name");
}
elsif ( $filesystem =~ /^mkfs\.jffs2/ ) {
    my $jffs2_dir = dirname($mount_point);
    my $root_fs_name = basename($ARGV[1]);
    my $jffs2_name = "$root_fs_name" . "_jffs2";
    $uml->command("umount /mnt2");
    $uml->command("mount -t hostfs none -o $jffs2_dir /mnt2");
    if ( $filesystem =~ /^mkfs\.jffs2\s*$/ ) {
	$uml->command("mkfs.jffs2 -r /mnt1 -o /mnt2/$jffs2_name  -e 0x20000 -p");
    }
    else {
	$uml->command("$filesystem -r /mnt1 -o /mnt2/$jffs2_name");
    }

}
elsif ( $filesystem =~ /^mkfs\.jffs/ ) {
    my $jffs_dir = dirname($mount_point);
    my $root_fs_name = basename($ARGV[1]);
    my $jffs_name = "$root_fs_name" . "_jffs";
    $uml->command("umount /mnt2");
    $uml->command("mount -t hostfs none -o $jffs_dir /mnt2");
    if ( $filesystem =~ /^mkfs\.jffs\s*$/ ) {
	$uml->command("mkfs.jffs -d /mnt1 -o /mnt2/$jffs2_name  -e 0x20000");
    }
    else {
	$uml->command("$filesystem -d /mnt1 -o /mnt2/$jffs2_name");
    }
}

$uml->command("umount /mnt1; umount /mnt2");   

# Fall through
my $close = $uml->close_no_init();

# Wait until the last line
if ( $close ) {
    system "uml_mconsole $umid halt";
}
else {

    print "ERROR: problem shutting down, contact freesource@users.sourceforge.net if problem persists\n";

}