#!/usr/bin/perl
#
# Copyright 2000, 2001 Tivano Software GmbH. This software is
# distributed under the terms of the GNU General Public License. See
# COPYING for additional information.
#
# This is a hacked-up replacement for "amlabel" to label CD-RWs
# intended for use with cdrw-taper

# Path to taperlib.pm
push @INC, "/usr/lib/amanda";

require "taperlib.pm";

##
## No user editable settings below here
##

$WRITE_TAPELIST=1;
while ($ARGV[0] =~ /^-.*/) {
    if ($ARGV[0] eq "-f") {
        $FORCE_WRITE = 1;
    } elsif ($ARGV[0] eq "--no-tapelist-entry") {
        $WRITE_TAPELIST = 0;
    } else {
        print "Illegal argument: ".$ARGV[0]."\n";
        usage();
    }
    shift @ARGV;
}

if ($#ARGV < 1) {
    usage();
}

$CONFIG    = shift @ARGV;
$NEW_LABEL = shift @ARGV;

&readAmandaConfig($CONFIG);

my $writeDev = &loadSlot("current");
my $mountDev;
if ($writeDev =~ /^(.*?):/) {
    $mountDev = $1;
    $writeDev = $';
} else {
    $mountDev = $CDRW_MOUNT_DIR;
}

if ($NEW_LABEL !~ $labelstr) {
    error("Label $NEW_LABEL does not match labelstring \"$labelstr\".");
}

# If $FORCE_WRITE is not specified, check first if an empty or unused
# disk is in the drive and if the given label is not on an active
# amanda disk
if (!$FORCE_WRITE) {
    # Don't use a label that's on an active disc
    if ((! is_usable($NEW_LABEL)) || is_active($NEW_LABEL))
    {
	error("Cannot overwrite active disk!")
    }
}

# Write the label file to a temporary directory
mkdir("/tmp/amlabel-cdrw.$$", 0755) || error("Cannot make directory /tmp/amlabelcd.$$: $!");
open LABEL, ">/tmp/amlabel-cdrw.$$/AMANDA_LABEL" or error("Cannot create label: $!");
print LABEL "$NEW_LABEL\n";
close LABEL;

my $type = getMediaType($mountDev, $writeDev);
if ($type eq 'CD-RW') {
    $result = system("$MKISOFS -J -R -pad -quiet /tmp/amlabel-cdrw.$$ | $CDRECORD dev=$writeDev -data blank=fast -");
	error("Error writing CD-RW") if $result / 256 != 0;
} elsif ($type eq 'DVD+RW') {
	$result = system("$GROWISOFS -Z $mountDev -J -R -pad -quiet /tmp/amlabel-cdrw.$$");
	error("Error writing DVD+RW") if $result / 256 != 0;
} elsif ($type eq 'CD-R' or $type eq 'DVD+R') {
	error("Don't label a $type");
} elsif ($type eq 'none') {
	error("No media in drive");
} elsif (defined($type)) {
	error("Unknown media type: $type");

} else {
    error("Failed to determine media type");
}

# Clean up temporary files
if (-e "/tmp/amlabel-cdrw.$$/AMANDA_LABEL") {
    unlink "/tmp/amlabel-cdrw.$$/AMANDA_LABEL";
}
if (-d "/tmp/amlabel-cdrw.$$") {
    rmdir "/tmp/amlabel-cdrw.$$";
}

if ($WRITE_TAPELIST) {
    # Finally, append the new entry to the media list
    open ML, ">>$tapelist" || error("Cannot write to $tapelist: $!");
    print ML "00000000 $NEW_LABEL reuse\n";
    close ML;
}

exit 0;

# print an error message and exit
sub error {
  # Clean up temporary files
  unlink "/tmp/amlabel-cdrw.$$/AMANDA_LABEL" if -e "/tmp/amlabel-cdrw.$$/AMANDA_LABEL";
  rmdir "/tmp/amlabel-cdrw.$$" if -d "/tmp/amlabel-cdrw.$$";
  print STDERR "amlabel-cdrw: $_[0]\n" if $_[0];
  exit 1;
}

sub usage {
    print STDERR "Usage: amlabel-cdrw [-f] [--no-tapelist-entry] <conf> <label>\n";
    exit 1;
}

