#!/usr/local/bin/perl -w-- # -*- perl -*-
#
# $Id: rth,v 1.3 1995/03/28 20:16:02 ejb Exp $
# $Source: /home/ejb/scripts/RCS/rth,v $
# $Author: ejb $
#

# This code, from the perl manual page, forces this to be run by perl from 
# perl, sh, or csh.  It must be first.
eval '(exit $?0)' && eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
& eval 'exec /usr/local/bin/perl -S $0 $argv:q'
    if 0;

$whoami = ($0 =~ m,([^/]*)$,) ? $1 : $0;

$tape = ($ENV{"TAPE"} || "/dev/rst0");
$tape =~ s:/dev/nrst:/dev/rst:;
$tape =~ s:n$::;

&usage if (@ARGV < 1);
$cmd = shift(@ARGV);

@cmds = ("load", "unload", "status");

$found = 0;
for (@cmds)
{
    $found = 1 if ($cmd eq $_);
}

&usage unless $found;

if (($cmd eq "load") || ($cmd eq "unload"))
{
    &usage unless (@ARGV == 1);
    $slot = shift(@ARGV);
    $str = sprintf("%s%d",
		   ($cmd eq "load" ? "GeT" : "PuT"),
		   $slot);
    @status = &status();
    if ($cmd eq "unload")
    {
	if ($status[0] == 0)
	{
	    print "No tape is loaded\n";
	}
	elsif ($status[$slot])
	{
	    print "There is already a tape in slot $slot.\n";
	}
	else
	{
	    print "Unloading tape into slot $slot.\n";
	    &write_to_tape($str);
	    # Wait for the tape to unload
	    for ($i = 0; $i < 12; $i++)
	    {
		sleep(5);
		@status = &status();
		last if ($status[0] == 0);
	    }
	    if ($status[0])
	    {
		die "$whoami: timed out waiting for tape to unload.\n";
	    }
	    else
	    {
		if ($status[$slot])
		{
		    print "Tape succesfully unloaded into slot $slot.\n";
		}
		else
		{
		    die "$whoami: failure unloading tape into slot $slot.\n";
		}
	    }
	}
    }
    else
    {
	if ($status[0])
	{
	    print "There is already a tape in the drive.\n";
	}
	elsif ($status[$slot] == 0)
	{
	    print "There is no tape in slot $slot.\n";
	}
	else
	{
	    print "Loading tape from slot $slot.\n";
	    &write_to_tape($str);
	    @status = &status();
	    if (($status[0] == 1) && ($status[$slot] == 0))
	    {
		print "Tape succesfully loaded from slot $slot.\n";
	    }
	    else
	    {
		die "$whoami: failure loading tape from slot $slot.\n";
	    }	    
	}
    }
}
elsif ($cmd eq "status")
{
    @status = &status();
    print "There is a tape in the drive.\n" if ($status[0]);
    for ($i = 1; $i <= 6; $i++)
    {
	print "There is a tape in slot $i.\n" if ($status[$i]);
    }
}
else
{
    die "$whoami: INTERNAL ERROR: unhandled command\n";
    exit 1;
}

sub usage {
    print STDERR <<EOF
Usage: $whoami cmd [args]
  cmd is load, unload, or status.  load and unload require a tape number.
EOF
    ;
    exit 1;
}

sub status {
    local(@result) = (0, 0, 0, 0, 0, 0, 0);
    local(@status, $status, $i);
    &write_to_tape("Rd_ElS");
    open(TAPE, "<$tape") || die "$whoami: can't open $tape: $!\n";
    $status = "";
    sysread(TAPE, $status, 512) || die "$whoami: can't read $tape: $!\n";
    close(TAPE);

    @status = split('', $status);

    # Hard-coded numbers come from rth.c sources 
    $result[0] = 1
	if ((ord($status[18]) == 0x1)  || (ord($status[18]) == 0x9));
    for ($slot_no = 1, $i = 42; $i < 123; $i += 16, $slot_no++)
    {
	$result[$slot_no] = 1 if (ord($status[$i]) == 0x9);
    }
    @result;
}

sub write_to_tape {
    local($str) = @_;
    open(TAPE, ">$tape") || die "$whoami: can't open $tape to write\n";
    print TAPE $str, "\n";
    close(TAPE);
}
