#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBD::SQLite;
use DBI;
use Data::Dumper;
use Encode 'decode';
use File::Slurp;
use YAML::XS;

#
#   Libre10
#
#   Copyright 2013 yukikaze, long.inus
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#

my $cgi      = new CGI;
my $mode     = $cgi->param('mode');
my $confpath = -e "libre10.conf" ? "libre10.conf" : "/etc/libre10.conf";
my $confdata = YAML::XS::LoadFile($confpath);
my $temppath = "/tmp/libre10/tmp" . generate_unique_key();
my $dbh      = DBI->connect( "dbi:SQLite:dbname=$confdata->{dburl}", "", "", { RaiseError => 1, AutoCommit => 1 } );
my $pdflist  = $dbh->selectall_hashref(
	"SELECT id, path, page, title, part, genre, startpage, endpage, showpagediff, indexpage
	FROM pdffile"
	, 'id'
);
mkdir '/tmp/libre10' unless ( -e '/tmp/libre10' );

if ( $mode eq 'partpdf' ) {
	my $pdfid = $cgi->param('pdfid');
	my $pdftk = $cgi->param('pdftk');

	# sanitize
	$pdftk =~ s/[^0-9\-]//;

	my $pdfpath = $pdflist->{$pdfid}->{path};
	exit unless ( -e $pdfpath );
	system "pdftk '$pdfpath' cat '$pdftk' output '$temppath.pdf'";
	my $length = -s "$temppath.pdf";
	print "Content-Type: application/pdf\n";
	print "Content-Length: $length\n";
	print "\n";
	print read_file("$temppath.pdf");
	exit;
}

print $cgi->header( -charset => 'utf-8' );
list_all_pdf();

sub list_all_pdf {
	print qq {Partial PDF = ./pdfmanager.pl?mode=partpdf&pdfid=68fc6a6a1cdd79227d0f2fe2f03dd510&pdftk=765-765\n};
	my $ary_ref = $dbh->selectall_arrayref(
		"SELECT id, path, page, title, part, genre, startpage, endpage, showpagediff, indexpage
		FROM pdffile"
		, { Slice => {} }
	);
	print qq {<table>\n};
	print qq {<tr>\n};
	print qq {<th>id</th>\n};
	print qq {<th>path</th>\n};
	print qq {<th>page</th>\n};
	print qq {<th>title</th>\n};
	print qq {<th>part</th>\n};
	print qq {<th>genre</th>\n};
	print qq {<th>start</th>\n};
	print qq {<th>end</th>\n};
	#print qq {<th>showpagediff</th>\n};
	#print qq {<th>indexpage</th>\n};
	print qq {</tr>\n};
	foreach my $line ( @{$ary_ref} ) {
		print qq {<tr>\n};
		print qq {<td><a href="./htmlserver.pl?pdfpath=$line->{id}&amp;pagenum=1">$line->{id}</a></td>\n};
		print qq {<td>$line->{path}</td>\n};
		print qq {<td>$line->{page}</td>\n};
		print qq {<td>$line->{title}</td>\n};
		print qq {<td>$line->{part}</td>\n};
		print qq {<td>$line->{genre}</td>\n};
		print qq {<td>$line->{startpage}</td>\n};
		print qq {<td>$line->{endpage}</td>\n};
		#print qq {<td>$line->{showpagediff}</td>\n};
		#print qq {<td>$line->{indexpage}</td>\n};
		print qq {</tr>\n};
	}
	print qq {</table>\n};
}

sub generate_unique_key {
	my $ukey;
	my @str = ( 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );

	foreach ( 1 .. 8 ) {
		$ukey .= $str[ int( rand(@str) ) ];
	}
	return $ukey;
}

