#! /local/bin/perl 

use strict;
use Getopt::Long;
use Date::Format;
use Tk;
use Tk::ROText;
use Tk::BrowseEntry;
use Tk::Dialog;
#
# $Log: datlabel.pl,v $
# Revision 1.5  2006/09/28 12:00:08  jive_cc
# xrxict printer added
#
#
# Create a cover with file catalogue for fitstape.
#

my $boldFont  = "-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1";
# my $descrFont = '-*-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*';
my $descrFont = '-adobe-Helvetica-Medium-R-Normal--12-120-75-75-p-77-iso8859-1';
#my $descrFont = '-adobe-courier-Medium-R-Normal--12-120-75-75-p-77-iso8859-1';
my ($experiment, $tape, $date);
GetOptions("e=s" => \$experiment, "t=s" => \$tape);
$tape = 1 if (! $tape);

if (! $experiment) {
    print "\n datlabel -e experiment [-t tapenr]\n",
          "where:\n",
          "experiment = name of experiment in file ./exp.fitslog\n",
          "tapenr     = tapenr (default = 1)\n\n";
    exit;
}
my ($shortExp,$d) = split(/_/,$experiment);
my $logdir  = "./";
my $logfile = "$logdir/$experiment.datlog";
open(FL,$logfile) || die "Could not open file $logfile";

print "$logfile\n";
my @logList = <FL>;
close FL;

my @lines;
my $maxTape;
# 2005-03-11 10:40:36  eg030_1_1.IDI1             559549440   1 of 1

foreach (@logList) {
    my @fields = split(/\s+/,$_);
#    print "@fields\n";

    if ($fields[0] =~ /(\d\d\d\d-\d\d-\d\d)/ && $fields[4] == $tape) {
#        print "$fields[2]   $fields[3]\n";
        $date = $1;
#        print "date = $date\n";
        push(@lines,"$fields[2]   $fields[3]");
        $maxTape = $fields[6];
    }
}

my $width   = "75m";
my $heigth  = "122m";

my $bgColor = "white";
my $title   = "datlabel.pl (" . $ENV{HOST} . ")";
my $top     = MainWindow->new(-bg => $bgColor);
$top->title($title);
$top->geometry();
my $jive    = $top->Photo(-file=>"/export/jive/kramer/fits2dat/jive01.gif");

my $frame   = $top->Frame()->pack(-side => "top");


my $orientation = "portrait";

my $canvas = $top->Canvas(-width =>  $width,
			  -height => $heigth,
			  -border => 1,
			  -relief => 'ridge',
			  -background=> 'grey');

$frame->Button(-text => 'Exit',
	     -width => 12,
	     -font  => $boldFont,
	     -bg=> $bgColor,
	     -fg => 'black',
	     -command => sub{exit 0})->pack(-side => 'left');

$frame->Button(-text => 'Print',
	     -width => 12,
	     -font  => $boldFont,
	     -fg => 'black',
	     -bg=> $bgColor,
	     -command => sub{toPrinter($top,$canvas,$orientation)})
    ->pack(-side => 'right');

$canvas->pack(-side => 'top');

$canvas->create('rectangle',0, 0,$width,$heigth,
		-outline => 'black',
		-fill => $bgColor,
		-tags => 'rect');

my $col1 = "5m";
my $col2 = "12m";
my $col3 = "45m";

$canvas->createImage(130,45,-image=>$jive);
$canvas->createLine("0m","55m","75m","55m");
$canvas->create('text',$col1,"61m",-font => $boldFont,
                -text=>"$shortExp   Tape $tape of $maxTape",-anchor =>'w');
$canvas->createLine("0m","67m","75m","67m");

my $y = 28;

#my $date = time2str("%Y-%m-%d",time());
my $line = sprintf("Fits data exp: %-13s %10s", uc $shortExp,$date);
$canvas->create('text',$col1,$y."m",-font   => $boldFont,-text=>$line,-anchor=>'w');
$y += 8;
$canvas->create('text',$col1,$y."m",-font => $descrFont,-text=>"No",-anchor =>'w');
$canvas->create('text',$col2,$y."m",-font => $descrFont,-text=>"Filename",-anchor =>'w');
$canvas->create('text',$col3,$y."m",-font => $descrFont,-text=>"Length (bytes)",-anchor =>'w');
$y += 4;
my $nr = 1;

foreach (@lines) {

    $canvas->create('text',$col1,$y."m",-font => $descrFont,-text=>sprintf("%2s",$nr),-anchor =>'w');
    my ($file,$length) = split(/\s+/,$_);
    $canvas->create('text',$col2,$y."m",-font => $descrFont,-text=>$file,-anchor =>'w');
    $canvas->create('text',$col3,$y."m",-font => $descrFont,-text=>$length,-anchor =>'w');    

    $y += 16 if ($nr == 4);
    $y += 4;
    $nr++;
}
MainLoop();

sub getPrinters
{
#
# Read the names of available printer.
#

   my @printers = ("xrxcorrelator","xrxjive","xrxict");
   return @printers;
}

sub toPrinter
{
#
#   Print information to printer or save information to file.
#
    my $main        = shift;
    my $canvas      = shift;
    my $orientation = shift;

    my @printerList = ();
    my $defPrinter = "xrxcorrelator";

    @printerList= getPrinters();

    my $top    = $main->Toplevel;

    my $header = $top->Label(-text => "Print")->pack();
    my $printFrame  = $top->Frame()->pack(-side => 'top',-fill=>'both');
    my $printButton = $printFrame->Button(-text=>'Print to',-width => 10,
        -command => sub{printScreen($top,$canvas,$orientation,$defPrinter)});
    $printButton->pack(-side => 'left');

    my $prBrowse = $printFrame->BrowseEntry( -variable => \$defPrinter,
					     -choices  => \@printerList,
					     -state    => 'normal',
                                             -width    =>10 )->pack(-side => 'left');

    my $quitFrame  = $top->Frame()->pack(-side => 'bottom',-fill=>'both');

    my $quitButton  = $quitFrame->Button(-text => 'Quit',-width => 10,
                       -command => sub{$top->withdraw()})->pack(-side => 'left');
    MainLoop();
}

sub printScreen
{
    my $top         = shift;
    my $canvas      = shift;
    my $orientation = shift;
    my $printer     = shift;

    my $a;

    if ($orientation eq "landscape") {
        $a = $canvas->postscript(-rotate => '1',
                            -pagewidth => '250m',
                            -x => '0',
                            -y => '0',
                            -pageanchor => 'center');
    } else {

        $a = $canvas->postscript(-rotate => '0',
                            -pagewidth => '75m', # '180m',
                            -x => '0',
                            -y => '0',
                            -pageanchor => 's');
    }

    open(LPR, "| lp -d $printer");
    print LPR $a;
    close(LPR);
    $top->withdraw;

}






