The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#! /usr/bin/perl
#---------------------------------------------------------------------
# pro_fmt
# Copyright 1996-2006 Christopher J. Madsen
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# 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 either the
# GNU General Public License or the Artistic License for more details.
#
# Create a blank ProDOS disk image
#---------------------------------------------------------------------

use strict;
use warnings;

use AppleII::Disk 0.04;
use AppleII::ProDOS 0.04;
use Getopt::Long 2.10;

our $VERSION = '0.09';

#---------------------------------------------------------------------
my $blocks = 280;
my $volume = 'NEWDISK';
my $order  = '';
my ($bootFrom, $extendToFullSize, $force);

Getopt::Long::config(qw(bundling no_getopt_compat));
GetOptions(
    'boot-from|b=s'   => \$bootFrom,
    'size|s=i'        => \$blocks,
    'dos-order|d'     => sub { $order = 'd' },
    'extend|e'        => \$extendToFullSize,
    'force|f'         => \$force,
    'prodos-order|p'  => sub { $order = 'p' },
    'volume|v=s'      => \$volume,
    'help'            => \&usage,
    'version'         => \&usage
) or usage();

my $filename = shift @ARGV;
defined($filename) or usage();

sub usage {
    print "pro_fmt $VERSION\n";
    exit if $_[0] and $_[0] eq 'version';
    print "\n" . <<'';
Usage:  pro_fmt [options] FILE
  -b, --boot-from=FILE  Copy boot blocks from disk image FILE
  -e, --extend          Extend FILE to its maximum size
  -f, --force           Overwrite an existing file
  -d, --dos-order       Make a DOS 3.3-order image file
  -p, --prodos-order    Make a ProDOS-order image file
  -s, --size=BLOCKS     Make the image BLOCKS blocks in size (default 280)
  -v, --volume=NAME     Use NAME for the ProDOS volume name (default NEWDISK)
      --help            Display this help message
      --version         Display version information

    exit;
} # end usage

#=====================================================================
if (not $force and -e $filename) {
  print STDERR "pro_fmt: $filename already exists (use --force to overwrite)\n";
  exit 1;
}

my $vol = AppleII::ProDOS->new($volume, $blocks, $filename, $order);

$vol->disk->fully_allocate if $extendToFullSize;

if ($bootFrom) {
  my $boot = eval { AppleII::Disk->new($bootFrom, 'r') };
  if ($@) {
    my $err = $@;
    $err =~ s! at \Q$0\E line \d+\n!!;
    die "$err\nDisk image $filename is not bootable, but is otherwise ok.\n";
  }

  $vol->disk->write_block(0, $boot->read_block(0));
  $vol->disk->write_block(1, $boot->read_block(1));
} # end if $bootFrom

__END__

=head1 NAME

pro_fmt - Create a blank Apple II ProDOS disk image file

=head1 SYNOPSIS

B<pro_fmt> [options] I<FILE>

=head1 DESCRIPTION

B<pro_fmt> creates blank ProDOS disk images.  The images are NOT
bootable, because they lack the necessary code in the boot blocks,
unless you use the "--boot-from" option to copy the boot blocks from a
bootable disk image.

Note that some emulators cannot deal with image files that are smaller
than their nominal size.  That is, they expect a 280 block disk image
file to be 143,360 bytes long.  Always use the "--extend" option when
creating image files for such emulators.

=head1 OPTIONS

 -b, --boot-from=FILE  Copy boot blocks from disk image FILE
 -e, --extend          Extend FILE to its maximum size
 -f, --force           Overwrite an existing file
 -d, --dos-order       Make a DOS 3.3-order image file
 -p, --prodos-order    Make a ProDOS-order image file
 -s, --size=BLOCKS     Make the image BLOCKS blocks in size
                         (default 280)
 -v, --volume=NAME     Use NAME for the ProDOS volume name
                         (default NEWDISK)
     --help            Display this help message
     --version         Display version information

=head1 DEPENDENCIES

B<pro_fmt> requires the modules AppleII::ProDOS and AppleII::Disk,
which are included with LibA2.

=head1 AUTHOR

Christopher J. Madsen C<< <perl AT cjmweb.net> >>

Please report any bugs or feature requests to
C<< <bug-LibA2 AT rt.cpan.org> >>, or through the web interface
at L<http://rt.cpan.org/Public/Bug/Report.html?Queue=LibA2>


=head1 LICENSE

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.


=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.

=cut