#!/usr/local/bin/perl -w
use strict;

# Based on the example in the HOWTO using dd.  Does not work!
# add_file("cat mtboot mtboot boot |", 512);

# Based on the maketape.c program and the maketape.data data file.
add_file("bootstrap", 512);
end_file();
add_file("fixed_root.img", 5120);
end_file();
add_file("cpio", 512);
end_file();
add_file("root.cpio", 5120);
end_file();
add_file("usr.cpio", 5120);
end_file();
add_file("src1.cpio", 5120);
end_file();
add_file("src2.cpio", 5120);
end_file();
add_file("man.cpio", 5120);
end_file();
end_file();

sub end_file {
  print "\x00\x00\x00\x00";
}

sub add_file {
  my($filename, $blocksize) = @_;
  my($block, $bytes_read, $length);

  open(FILE, $filename) || die("Can't open $filename: $!");
  while($bytes_read = read(FILE, $block, $blocksize)) {
    if($bytes_read < $blocksize) {
      $block .= "\x00" x ($blocksize - $bytes_read);
      $bytes_read = $blocksize;
    }
    $length = pack("V", $bytes_read);
    print $length, $block, $length;
  }
  close(FILE);
}
