#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

use CGI::Carp qw(fatalsToBrowser);

# https://itvadmin.co.za/cgi-bin/scripts/pdf/mail_attach.pl?1182677259885215755/QT-308-20230424.pdf

#------------------------------------------------------------------------------------------

my @ARGV       = split(/\\*\&/, $ENV{'QUERY_STRING'});
my $file       = $ARGV[0] ;
my $attachpath = $mailpath . '/tickets/' . $file ;

#------------------------------------------------------------------------------------------

if (substr($file,-3,3) eq 'pdf') {
	print "Content-Type: application/pdf\n\n";
} elsif (substr($file,-3,3) eq 'doc') {
	print "Content-Type: application/msword\n\n";
} elsif (substr($file,-3,3) eq 'xls') {
	print "Content-Type: application/vnd.ms-excel\n\n";
} elsif (substr($file,-4,4) eq 'docx') {
	print "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\n\n";
} elsif (substr($file,-4,4) eq 'xlsx') {
	print "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n\n";
} elsif (substr($file,-3,3) eq 'gif') {
	print "Content-Type: image/gif\n\n";
} elsif (substr($file,-3,3) eq 'csv') {
	print "Content-Type: text/csv\n\n";
} elsif (substr($file,-3,3) eq 'txt') {
	print "Content-Type: text/plain\n\n";
} elsif (substr($file,-3,3) eq 'jpg' || substr($file,-4,4) eq 'jpeg') {
	print "Content-Type: image/jpeg\n\n";
} else {
	print "Content-type: text/html\n\n";   
}

open(FILE,"$attachpath")  or die("$attachpath : $!");
binmode(FILE);
while (sysread(FILE,$buffer,1024)) {
    print STDOUT $buffer;
}

# if (substr($file,-3,3) eq 'pdf') {
	# print "Content-Type: application/pdf\n\n";
# } elsif (substr($file,-3,3) eq 'xls' || substr($file,-4,4) eq 'xlsx') {
	# print "Content-type: text/html\n\n"; 
	# use CGI::Application::Plugin::Stream (qw/stream_file/);
    # $self->stream_file($attachpath) or $self->error_mode() ;
    # exit;  
# } else {
	# print "Content-type: text/html\n\n";   
# }

# open(FILE,"$attachpath")  or die("$attachpath : $!");
# binmode(FILE);
# while (sysread(FILE,$buffer,1024)) {
    # print STDOUT $buffer;
# }
    
# exit ;
    
exit ;

#------------------------------------------------------------------------------------------

use common ;

1;