#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2014 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::Tracker::Command::DownloadRoyaltyReport::HachetteUK;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use RSApache::Response::DownloadZipDirectory;

sub getResponse {
    my ( $self, $report ) = @_;

    my $periodID = $report->periodID();
    my $tempdir = File::Temp::tempdir( "/tmp/dmtv_355_" . $periodID . "XXXXXXXX", CLEANUP => 1 );

    # Let's create symlinks for the two output files that we want to zip up.
    #
    my $baseDir           = $report->_baseDir();
    my $outputFileNameStd = $report->fileNameStd();

    my $outputFileNameBookpoint    = $report->fileNameBookpoint();
    my $outputFileNameLBS          = $report->fileNameLBS();
    my $outputFileNameHHC          = $report->fileNameHHC();
    my $outputFileNameOther        = $report->fileNameOther();
    my $outputFileNameSupplemental = $report->fileNameSupplemental();

    system("ln -s \"$baseDir/$outputFileNameStd\" \"$tempdir/$outputFileNameStd\"");

    system("ln -s \"$baseDir/$outputFileNameBookpoint\" \"$tempdir/$outputFileNameBookpoint\"")
      if ( -e "$baseDir/$outputFileNameBookpoint" );
    system("ln -s \"$baseDir/$outputFileNameLBS\" \"$tempdir/$outputFileNameLBS\"")     if ( -e "$baseDir/$outputFileNameLBS" );
    system("ln -s \"$baseDir/$outputFileNameHHC\" \"$tempdir/$outputFileNameHHC\"")     if ( -e "$baseDir/$outputFileNameHHC" );
    system("ln -s \"$baseDir/$outputFileNameOther\" \"$tempdir/$outputFileNameOther\"") if ( -e "$baseDir/$outputFileNameOther" );
    system("ln -s \"$baseDir/$outputFileNameSupplemental\" \"$tempdir/$outputFileNameSupplemental\"")
      if ( -e "$baseDir/$outputFileNameSupplemental" );

    my $response = RSApache::Response::DownloadZipDirectory->new( $tempdir, 'application/octet-stream', $report->zipFileName() );

    return $response;
}

1;
