#! /usr/bin/perl
#
# webpdf -- small set of scripts to make PDF files automatically from various
#           formats, via a web interface.
#
# Copyright 2005 Steinar H. Gunderson <sgunderson@bigfoot.com>. 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
# 
use CGI;
use POSIX;
use strict;
use File::Temp;
require './config.pm';

$ENV{"HOME"} = $pdfweb::config::homedir;

my $cgi = CGI->new;
my $filename = $cgi->param('input');
my $file = $cgi->upload('input');
my $url = $cgi->param('url');

# It kind of sucks that we just can't get the temporary file name from
# CGI.pm, but OK, here goes :-)

my $pdf_filename = join('', (0..9, 'A'..'Z', 'a'..'z')[
	rand 62, rand 62, rand 62, rand 62, rand 62, 
	rand 62, rand 62, rand 62, rand 62, rand 62
]) . '.pdf';

my $pdfopts = "";
my $psopts = "/setdistillerparams { } /def";
my $outname;

if ($cgi->param('preset') eq 'screen') {
	$pdfopts = "-dPDFSETTINGS=/screen";
} elsif ($cgi->param('preset') eq 'ebook') {
	$pdfopts = "-dPDFSETTINGS=/ebook";
} elsif ($cgi->param('preset') eq 'printer') {
	$pdfopts = "-dPDFSETTINGS=/printer";
} elsif ($cgi->param('preset') eq 'prepress') {
	$pdfopts = "-dPDFSETTINGS=/prepress";
} elsif ($cgi->param('preset') eq 'default') {
	$psopts = "";
}

if ($url =~ /^http/i) {
	$outname = "web.pdf";

	# Render through Gecko
	$ENV{'DISPLAY'} = $pdfweb::config::xserver;
	system("gnome-web-print", "-f", $url, "$pdfweb::config::outputdir/$pdf_filename.ps");
	system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < $pdfweb::config::outputdir/$pdf_filename.ps >&2");
} elsif ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
	$outname = "$1.pdf";

	# Yay, just a round through GhostScript
	open PIPE, "| gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
		or die "gs: $!";
		
	my ($buf, $ret);
	while ($ret = read($file, $buf, 1024)) {
		print PIPE $buf;
	}
	close PIPE;
} elsif ($filename =~ /(.*)\.(bmp|png|jpe?g|xpm)$/i) {
	$outname = "$1.pdf";

	# Run through ImageMagick first of all, then gs
	open PIPE, "| convert $2:- pdf:- | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
                or die "convert: $!";

	my ($buf, $ret);
	while ($ret = read($file, $buf, 1024)) {
		print PIPE $buf;
	}
	close PIPE;
} elsif ($filename =~ /(.*)\.txt$/i) {
	$outname = "$1.pdf";
	
	# Use mpage
	open PIPE, "| mpage -da -1 - | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
                or die "convert: $!";

	my ($buf, $ret);
	while ($ret = read($file, $buf, 1024)) {
		print PIPE $buf;
	}
	close PIPE;
} elsif ($filename =~ /(.*)\.(doc|xls|ppt)$/i) {
	$outname = "$1.pdf";
	my $ext = $2;

	# Oh my, OpenOffice
	open DOC, ">output/$pdf_filename.$ext"
		or die "output/$pdf_filename.$ext: $!";
	my ($buf, $ret);
	while ($ret = read($file, $buf, 1024)) {
		print DOC $buf;
	}
	close DOC;

	# Create PostScript from OOo :-)
	system("/usr/lib/openoffice/program/soffice -display $pdfweb::config::xserver -headless -pt pdf $pdfweb::config::outputdir/$pdf_filename.$ext");

	# This is quite hideous -- it looks like OO.o calls the file something slightly
	# different depending on the time format, phase of the moon or something... So
	# we try both.
	my $inp_ps = "output/$pdf_filename.$ext.pdf";
	if (! -r $inp_ps) {
		$inp_ps = "output/$pdf_filename.pdf";
	}

	system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < $inp_ps >&2");
} elsif ($filename =~ /(.*)\.(c|cc|cpp|cs|h|py|rb|pl|diff|patch|js|php[1-5]?|hs|f|f90|java|css|sql|l|y|s?ml|sh|awk|m|v)$/i) {
	$outname = "$1.pdf";
	my $ext = $2;

	open DOC, ">output/$pdf_filename.$ext"
		or die "output/$pdf_filename.$ext: $!";
	my ($buf, $ret);
	while ($ret = read($file, $buf, 1024)) {
		print DOC $buf;
	}
	close DOC;

	(my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;

	# This is just perverse :-P 
	system("vim -Z -R +\"set printheader=\%<$sanitized_filename\%h\%m\%=Page\\ \%N\" +\"ha >$pdfweb::config::outputdir/$pdf_filename.ps\" +:q output/$pdf_filename.$ext");

	system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.ps >&2");
} else {
	print <<"EOF";
Content-type: text/html

<html><head><title>Error</title></head>
<body><h1>Error</h1>
<p>You sent an unknown file type.</p>
</body></html>
EOF
	exit;
}

my $size = -s "output/$pdf_filename";

if (defined($size) && $size > 0) {
	# Make a thumbnail from the finished PDF, for later reference. (Output to
	# stdout is so we make sure we get only the first page; it's the simplest
	# hack I can find offhand. :-) )
	system("convert -resize 192x192 output/$pdf_filename png:- | convert png:- png:- > output/$pdf_filename.png");
	open DESC, ">output/$pdf_filename.desc";

	if ($url =~ /^http/i) {
		$url =~ tr/\n//d;
		print DESC "$url\n";
	} else {
		$filename =~ tr/\n//d;
		print DESC "$filename\n";
	}

	close DESC;
}
	
(my $sanitized_outname = $outname) =~ tr/a-zA-Z0-9. -/_/c;

print "Content-type: application/pdf\n";
print "Content-disposition: attachment; filename=\"$sanitized_outname\"\n";
print "Content-length: $size\n\n";

system("cat output/$pdf_filename");  # yuck?
