#!/usr/bin/perl

# This script is GPLed
# Copyright 2002 Wookey, Aleph One Ltd
# Derived from a script by Wenkel Jacob

#file locations
#this is where the cf device appears on your system
#e.g. /dev/sdn if using usb or /dev/hdc or hda if using an IDE converter 
my $cfdev="/dev/sda";
my $cfdev1=$cfdev."1";
my $cfdev2=$cfdev."2";
#dirs to be used to mount the filesystems on the card. 
my $mntpath="/mnt/flash";
my $mntpath1=$mntpath."1";
my $mntpath2=$mntpath."2";

sub ask {
    while (1) {
        print("$_[0] [$_[1]]: ");
        my $response = uc <STDIN>;
        chomp $response;
        if ($response eq "") {
            $response = uc $_[1];
        }
        if ($response eq "Y" or $response eq "YES") {
            return 1;
        } elsif ($response eq "N" or $response eq "NO") {
            return 0;
        }
    }
}

sub askpath {
    print("$_[0] [$_[1]]: ");
    my $response = <STDIN>;
    chomp $response;
    if ($response eq "") {
        $response = $_[1];
    }
    return $response;
}

sub main {
    system("clear");
    print("           ***********   PsiLinux 0.9 formatter  ***********         \n");
    print("---------------------------------------------------------------------\n");
    print(" Beware - this will reformat flash cards or other drives.            \n");
    print(" Be very careful!!                                                   \n");
    print(" Note that you need to be root to run this utility.                                                   \n");
    print("Checking drive $cfdev\n");
    $output = qx/sfdisk -d $cfdev/;
    #need a check here for plausible-looking output
    print("$cfdev currently looks like this: \n");
    print($output);
    print("(If there is no partition list above then something is wrong!)\n");
    print("If you say 'y' here the specified drive will be repartitioned \n");
    print("and reformatted.\n");
    print(" **************************************************************\n");
    print(" ** Doing this to the wrong drive will destroy your system. ***\n");
    print(" **************************************************************\n");
    print("Are you quite sure that $cfdev is the CF card you wish to use for PsiLinux?\n");
    print("Note that this program currently assumes a 64MB card - smaller cards\n");
    print("will not work -larger cards will have unallocated space beyond 64MB.\n");
    $goforit = &ask("Re-partition this drive ?", "n");
    # ideally we should calc formats to fit CF size
    # for now assume 64MB and use pre-done table 
    if (!$goforit) {
	print "Exiting - nothing changed\n";
	exit(0);
    }
    #unmount these if currently mounted
    system("umount $cfdev1");
    system("umount $cfdev2");
    #and check mount points exist - create them if not
    system("mkdir $mntpath1 $mntpath2");
    print "Partitioning $cfdrv with 2.5Mb EPOC and 61MB Linux partitions\n";
    if ( $result = system("sfdisk $cfdev < 64MBsftable.dsk >/dev/null")) {
	die "Couldn't reformat CF card.";
    }
    print("OK\nformating first partition ($cfdev1) for EPOC..."); 
    if ($result = system("mkfs.msdos -n psilinux $cfdev1 >/dev/null")) {
	die "EPOC formatting failed.";
    }
    print("OK\nformating second partition ($cfdev2) for Linux..."); 
    if ($result = system("mkfs.ext2 $cfdev2 >/dev/null")) {
	die "Linux formatting failed.";
    }
    print( "OK\nMounting EPOC partition...");    
    #check it exists first and try to create it if not
    if (! -e $mntpath1) {
	if ($result = system("mkdir $mntpath1 >/dev/null")) {
	    die "Unable to create mount point $mntpath1.";
	}    
    } 
    if ($result = system ("mount -t vfat $cfdev1 $mntpath1 >/dev/null")) {
	die "mounting $cfdev1 on $mntpath1 failed\n";
    } 
    print( "OK\nCopying epoc files...");    
    if ($result= system ("cp -R epoc/* $mntpath1 >/dev/null")) { 
        system ("umount $cfdev1 >/dev/null");
        die "copying EPOC files failed\n"; 
    }
    print( "OK\nMounting Linux partition...");
    #check it exists first and try to create it if not
    if (! -e $mntpath2) {
	if ($result = system("mkdir $mntpath2 >/dev/null")) {
	    die "Unable to create mount point $mntpath2\n";
	}    
    } 
    if ($result = system ("mount -t ext2 $cfdev2 $mntpath2 >/dev/null")) {
    	die "mounting $cfdev2 on $mntpath2 failed\n";
    } 
    print( "OK\nCopying Linux files...");    
    #run the packaging configurator to copy image in
    # specify device, mounted path and to do 'default packages with no questions'
    if ($result = system ("./install.pl $cfdev2 $mntpath2 auto")) {
        die "problem configuring Linux system\n";
	}
    print( "OK\nUnmounting partitions...");
    if ($result = system ("umount $cfdev1 $cfdev2 >/dev/null")) {
    	die "unmounting partitions $cfdev1 and $cfdev2 failed\n";
    } 
    print( "OK\nPsiLinux is installed on this CF card.\n");

}
&main();
