#! /usr/local/bin/perl
#
# Copyright (c) 1992 The Ohio State University.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that: (1) source distributions retain this entire copyright
# notice and comment, and (2) distributions including binaries display
# the following acknowledgement:  ``This product includes software
# developed by The Ohio State University and its contributors''
# in the documentation or other materials provided with the distribution
# and in all advertising materials mentioning features or use of this
# software. Neither the name of the University nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Based on ideas from the pmt shell script written by S. Michael Roug, 
# A/S Datacentralen, Denmark.
#

$usage = "usage: pmt -f device (rew | fsf N | offl | status)";

require 'global.defs';
require 'local.defs';

$tapecntl_prog = &find("tapecntl");
$mt_prog = &find("mt");

$arg = shift;
die "$usage: expected -f\n" if ($arg ne "-f");
die "$usage: expected device after -f\n" if (! ($device = shift));

$arg = shift;
if ($arg eq "rew" || $arg eq "rewind") {
    $command = ($tapecntl_prog) ? "-w" : "rew";
} elsif ($arg eq "offl" || $arg eq "offline") {
    $command = ($tapecntl_prog) ? "-w" : "offl";
} elsif ($arg eq "status") {
    if ($tapecntl_prog) {
	die 
"error (pmt): tapecntl doesn't support a 'status' command - check the 'status ok' setting for this tape drive in your backup.config.\n";
    }
    $command = "status";
} elsif ($arg eq "fsf") {
    $command = ($tapecntl_prog) ? "-p" : "fsf";
    die "$usage: expected count after fsf\n" if (! ($count = shift));
}

die "$usage: extra args on command line\n" if ($#ARGV != -1);

if ($tapecntl_prog) {
    $command = "$tapecntl_prog -d $device $command $count";
} elsif ($mt_prog) {
    if ($hpux || $irix) {
	$command = "$mt_prog -t $device $command $count";
    } else {
	$command = "$mt_prog -f $device $command $count";
    }
}

$return = system($command)/256;

exit($return);
