#!/usr/bin/perl -w

#use strict;

#use Expect;

# sshlpd - LPD over SSH for CUPS
# http://twofoos.org/content/labprinting/
#
# Copyright (C) 2006 Christopher Povirk <beigetangerine@gmail.com>
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of version 2 of the GNU General Public License as
#  published by the Free Software Foundation.
#
#  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#  USA.
#
#  (Or visit http://www.gnu.org/copyleft/gpl.html for the HTML version.)

# Large portions taken from beh (Backend Error Handler) by Till Kamppeter,
# also available under the GPL.
#
# http://www.linuxprinting.org/beh.html

# Note that this particular copy of sshlpd has been patched to use key-based authentication.
# Thanks to Stephen DeGabriele for the changes.

# TODO would be nice if we didn't have to specify the location of lpr
my $REMOTE_LPR = '/usr/local/bin/lpr';

my $tempFile;

sub cleanUp();
sub dieCleanly(@);

sub cleanUp()
{
  unlink $tempFile if $tempFile;
}

sub dieCleanly(@)
{
  cleanUp();
  die @_;
}

$0 =~ m!^(.*)/([^/]+)\s*$!;
my $progname = ($2 || $0);

if(!$ARGV[0])
{
  print qq#network $progname "Unknown" "LPD over SSH Login"\n#;
  exit 0;
}

if(scalar(@ARGV) < 5 || scalar(@ARGV) > 6)
{
  print STDERR "ERROR: Usage: $progname job-id user title copies options [file]\n";
  exit 1;
}

my ($jobID, $userName, $jobTitle, $copies, $printOptions, $printFile) = @ARGV;

$jobTitle =~ s#\\#\\\\#g;
$jobTitle =~ s#"#\\"#g;
$jobTitle =~ s#\$#\\\$#g;

if(!$printFile)
{ 
  my $jid = $jobID;
  my $uid = $userName;
  $jid =~ s/\W//g; #sanity check
  $uid =~ s/\W//g; #sanity check
  $tempFile = "$ENV{HOME}/$jid-$uid-cupsjob";

  open(OUT, ">$tempFile") or die "ERROR: Cannot write $tempFile: $!\n";

  while(<STDIN>)
  {
    print OUT "$_";
  }

  close OUT;

  $printFile = $tempFile;

  # Backends should only produce multiple copies if a file name is 
  # supplied (see CUPS Software Programmers Manual)
  $copies = 1;
}

my $uri = $ENV{DEVICE_URI};
$uri =~ m#$progname://([^/:]+)\@([^/:]+)/([^/]+)# or dieCleanly(qq#URI must be "$progname://user\@host/destination"\n#);
my ( $user, $host, $destination ) = ( $1, $2, $3 );
my $connectToString = "$user\@$host";

my $whoami = `whoami`; chomp $whoami;

for(my $i = 0; $i < $copies; $i++)
{
  my $command = qq#ssh '$connectToString' '$REMOTE_LPR -P "$destination" -T "$jobTitle"' < '$printFile'#;
  system($command) == 0 or dieCleanly(qq#ssh returned $?: $!\nMake sure that key-based authentication is set up for ssh.\nYou might try running "sudo -u $whoami ssh $connectToString" as root.\nIt should not ask you to accept a host key or enter a password.\nNote that ssh will be run as $whoami.\nMake sure that your ssh configuration exists in that user's home directory.\nAlso be sure that your ssh server is configured properly.\n#);
}

cleanUp();
