#!/usr/bin/perl
# 
# Write specified files on AKAI S3000 disk.
# Copyright (c) 1997, Hiroyuki Ohsaki.
# All rights reserved.
# 
# $Id: akaiwrite,v 1.2 1998/06/30 01:35:31 oosaki Exp ohsaki $
# 

# This program is free software; you can 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.

use Synth::AkaiDisk qw(qualify_path unqualify_path join_path);
use Getopt::Std;
use File::Basename;
use integer;
use strict;

my %EXTS = (adr => 100,
	    a1s => 115,
	    aql => 113,
	    a1p => 112,
	    afx => 120,
	    aml => 237,
	    a3p => 240,
	    a3s => 243);

sub write_file {
    my($file, $disk, $path, $type) = @_;

    print STDERR "writing $file\n  -> $path\n" if $::opt_v;
    $disk->write_file($file, $path, $type) || die "write: $path: $!\n";
}

sub usage {
    my $prog = basename($0);
    die <<EOF;
usage: $prog [-v] [-f file] [-p partition] [-d dir] path...
 -v       verbose mode
 -f file  specify Akai disk image (default \$AKAI_DISK)
 -p part  read from part-th partition (default 1)
EOF
}

getopts('vf:p:d:') || usage;
@ARGV || usage;

my $file = $::opt_f || $ENV{AKAI_DISK} || '/dev/mo';
my $part = $::opt_p || 1;

my $disk = new Synth::AkaiDisk($file, $part, 1) || die "new: $file: $!\n";

while (@ARGV) {
    my $path = shift;

    # if $path is a directory, write all files in it
    if (-d $path) {
	for (glob "$path/*") {
	    push(@ARGV, $_);
	}
    } else {
	# if $path has one-level directory, use it as destination
	my($dir, $base, $ext);
	($dir, $base) = (dirname($path), basename($path));
	my $vol;
	if (!$::opt_d) {
	    die "$path: destination directory (-d) must be specified"
		if ($dir eq '.' or $dir =~ m:/:);
	    $vol = $dir;
	} else {
	    $vol = $::opt_d;
	}

	# guess file type from its suffix
	($base, $ext) = ($base =~ m/^(.+)\.([^.]+)$/);
	if (!exists $EXTS{$ext}) {
	    warn "$path: unknown file type\n";
	    next;
	}

	# if $vol does not exist, create it
	my %stat = $disk->stat($vol);
	if (!exists $stat{type}) {
	    $disk->mkdir($vol) || die "mkdir: $vol: $!\n";
	}

	write_file($path, $disk,
		   qualify_path(join_path($vol, $base)), $EXTS{$ext});
    }
}

__END__

=head1 NAME

akaiwrite - Write files to Akai S3000 HDD

=head1 SYNOPSIS

  akaiwrite [-v] [-f file] [-p partition] [-d dir] path...

=head1 DESCRIPTION

This manual page documents B<akaiwrite>.  This program saves
S1000/S3000 files to the HDD in Akai S3000 format.  By default,
B<akaiwrite> writes I<path> in local file system to the directory (or
volume in Akai terminology) specified by C<-d> option in the Akai HDD.
If I<path> is a directory, B<akaiwrite> creates a directory in the
Akai HDD of the same name with I<path>, and saves all files in I<path>
to the created directory.

The notation of the pathname, I<path>, in B<akaiwrite> is almost
equivalent to that of conventional UNIX systems.  See L<akailist(1)>
for more detail.

=head1 OPTIONS

=over 4

=item C<-v>

Verbose mode.  Print much information for debugging.

=item C<-f> file

Specify the regular file of Akai disk image or the block device file
of Akai HDD/CD-ROM.  Usually, this should be the block device of HDD
or CD-ROM such as F</dev/sda>.  If this option is omitted, the
environment variable I<AKAI_DISK> is used as the default device.  If
I<AKAI_DISK> is not defined, F</dev/mo> is assumed.

=item C<-p> partition

Specify the partition number for which B<akaiwrite> works.  The first
partition is 1.  By default, the first partition is used.

=item C<-d> directory

Specify the directory in the Akai HDD, in which all files will be
saved.  This option must be specified when I<path> is a regular file.

=back

=head1 EXAMPLES

This section shows several example usage of B<akaiwrite>.

=over 4

=item - Write F<grand_piano.s3s> to the directory F</ACOUSTIC_PNO> in
the Akai HDD

  akaiwrite -d /acoustic_pno grand_piano.s3s

=item - Write all files in the directory F<acoustic_pno>.

  akaiwrite /acoustic_pno

=item - Restore all files in all directories to the second partition.

  akaiwrite *

=back

=head1 SEE ALSO

akaitools(1), akaiformat(1), akailist(1), akaimkdir(1), akairead(1),
akaiwrite(1), epslist(1), epsread(1), eps2akai(1), wav2akai(1),
any2akai(1), akaiconv(1) 

=head1 AUTHOR

Hiroyuki Ohsaki <ohsaki@lsnl.jp>

=cut

