--- toast-linux	2004/04/11 19:56:34	1.12
+++ toast-linux	2004/04/12 23:24:43	1.13
@@ -46,12 +46,38 @@
 {
   s/\n\n$/\n/;
 }
-$text{"Makefile"} =~ s/^  /\t/mg;
-$text{"Makefile"} =~ s/!GENBY!/$genby/g;
-$text{"Makefile"} =~ s/!VERSION!/$version/g;
-$text{"Makefile"} =~ s/!CONFIGURE!/$configure/g;
-$text{"Makefile"} =~ s/!PREFIX!/$prefix/g;
 
+for($text{"Makefile"})
+{
+  s/^  /\t/mg;
+  s/!GENBY!/$genby/g;
+  s/!VERSION!/$version/g;
+  s/!CONFIGURE!/$configure/g;
+  s/!PREFIX!/$prefix/g;
+}
+
+for($text{"skel/floppy/logo"})
+{
+  s!^(( {8})+)!"\t" x (length($1)/8)!mge;
+}
+
+my($oldcfg);
+for($text{"skel/floppy/syslinux.cfg"})
+{
+  $_ .= "# pad to fill one 512-byte sector\n" while length($_) < 512;
+  s/\n/\r\n/g;
+  substr($_, 510) = "\r\n";
+  s/\r(\r\n$)/#$1/;
+  length($_) == 512 or die;
+  $oldcfg = $_;
+  $oldcfg =~ s/\r\n/\n/g;
+}
+
+for($text{"toast-linux.stub"})
+{
+  s/^!OLDCFG!\n/$oldcfg/m;
+}
+
 my($ok);
 for(sort(keys(%text)))
 {
@@ -403,16 +429,15 @@
 --- FILE toast-linux.stub ---
 #!/usr/bin/perl
 
-use warnings;
-use strict;
-
 =head1 NAME
 
 toast-linux - create toast linux install floppy or CD
 
 =head1 SYNOPSIS
 
-B<toast-linux> I<TODO: insert options here>
+  toast-linux [--floppy|--cd] [--append=CMD] [[--outfile=]FILE]
+  toast-linux --help
+  toast-linux --man
 
 =head1 DESCRIPTION
 
@@ -452,25 +477,81 @@
 
 =cut
 
-my($floppy); # TODO: make true if writing floppy image, false for cd
+use warnings;
+use strict;
+use Getopt::Long;
+use Pod::Usage;
 
-if($floppy)
+sub usage() { pod2usage(2); die }
+
+my($floppy, $outfile, $append);
 {
+  my($help, $man, $cd);
+  usage unless GetOptions
+  (
+    "help|?" => \$help,
+    "man" => \$man,
+    "floppy|fd" => \$floppy,
+    "cd|iso" => \$cd,
+    "outfile=s" => \$outfile,
+    "append=s" => \$append,
+  );
+
+  pod2usage(1) if $help;
+  pod2usage(-exitstatus => 0, -verbose => 2) if $man;
+
+  usage if $floppy && $cd;
+  $outfile = shift unless defined($outfile);
+  usage if @ARGV;
+  $outfile = "/dev/fd0" if !defined($outfile) && $floppy;
+  $cd = 1 if !defined($floppy) && !defined($cd) && defined($outfile) &&
+      $outfile =~ /\.iso$/i;
+  $floppy = 1 if !defined($floppy) && !defined($cd);
+
+  usage unless defined($outfile);
+  $outfile = undef if $outfile eq "-";
+}
+
+open(STDOUT, ">", $outfile) || die("open $outfile: $!") if defined($outfile);
+
+my($oldcfg) = << 'EOF';
+!OLDCFG!
+EOF
+$oldcfg =~ s/\n/\r\n/g;
+my($len) = length($oldcfg);
+die("len=$len") unless $len == 512;
+
+my($newcfg);
+if(defined($append))
+{
+  $newcfg = $oldcfg;
+  $newcfg =~ s/^#[^\r\n]*\r?\n//mg;
+  die unless $newcfg =~ s/^(append [^\r\n]+)(\r\n)/$1 $append$2/mi;
+  die unless length($newcfg) <= 512;
+  $newcfg .= "# pad to fill one 512-byte sector\n" while length($newcfg) < 512;
+  substr($newcfg, 510) = "\r\n";
+  $newcfg =~s/\r(\r\n$)/#$1/;
+  die unless length($newcfg) == 512;
+}
+
+if($floppy || defined($newcfg))
+{
   my($pid) = open(STDOUT, "|-");
   die("fork: $!") unless defined($pid);
   binmode(STDOUT);
   if(!$pid)
   {
     binmode(STDIN);
-    my($i, $rc, $buf, $on) = 0;
+    my($on, $i, $rc, $buf) = (!$floppy, 0);
     while($rc = read(STDIN, $buf = "", 512, 0))
     {
-      next unless $on ||= /^...SYSLINUX.{32}TOAST LINUXFAT12/;
+      next unless $on ||= $buf =~ /^...SYSLINUX.{32}TOAST LINUXFAT12/;
+      $buf = $newcfg if defined($newcfg) && $buf eq $oldcfg;
       print($buf) || die("write: $!");
-      $on &&= ++$i < 2880;
+      $on &&= ++$i < 2880 if $floppy;
     }
     die("read: $!") unless defined($rc);
-    die("error: wrote $i sectors") unless $i == 2880;
+    die("error: wrote $i sectors") unless $i == 2880 || !$floppy;
     close(STDOUT) || die("close: $!");
     exit(0);
   }