package ftp_order_handling;
#
#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################
#
#$Id: ftp_order_handling.pm,v 1.6 2006/05/18 11:27:50 kva Exp $
#
##################################
# Places orders on the ftp site. #
#                                #
# Author : Karen Ambrose         #
##################################

use vars qw(@ISA @EXPORT);
use Exporter;
@ISA = ('Exporter');
@EXPORT = qw(&ftp_order);
require Exporter;

sub ftp_order {
    
    my ($lpid,$info,$label) = @_;
    
    my $prog = $0;
    
    my $outfile; 
    my @output_file = @$info;
    
#The generated files will be written to the UNENCRYPTED ftp directory,
#the CheckPrimerOrder script on timber will then encrypt the file
#and place it on the public directory /nfs/privateftp/ftp-primer/pub/
#for SIGMA to download.
    
    if (!$label){
        $label = 'gen';
    }
    
    my $DESTINATION = {
        "gen" => "/nfs/privateftp/ftp-primer/private/UNENCRYPTED/",
        "tm58_ftp" => "/nfs/privateftp/ftp-primer/private/UNENCRYPTED/",
        "https" => "/nfs/privateftp/ftp-primer/private/HTTPSTAGING/",
    };  
    
##FOR TESTING PURPOSES ONLY
#    my $DESTINATION = {
#        "gen" => "$ENV{'HOME'}/custom_primer/ORDERS/UNENCRYPTED/",
#        "tm58_ftp" => "$ENV{'HOME'}/custom_primer/ORDERS/UNENCRYPTED_58/",
#        "https" => "$ENV{'HOME'}/custom_primer/ORDERS/HTTPSTAGING/",
#    };  
    
    my %DEST = %$DESTINATION;     
    my $file = $DEST{$label};
        
#make the file to be dumped on the ftp site
    $outfile = $file.$lpid.'.xml';
    
    # dn1
    # Output the XML format file made
    print "\tXML output order at $outfile\n";
    
#Opens the FH to the output file
    open(OUTPUT,">$outfile") || die "$prog: Could not open file $outfile for output: $!\n"; 
  
#print the contents of the file to a file handle
    foreach (@output_file){
        print OUTPUT "$_\n";
    }
    
#close the output to the file
    close(OUTPUT);

##set the priv on the files
    my $code = system("chmod 0664 $outfile");
    
#creates a file of zero bytes length
#important do not delete!!!
#a cronjob running on timber only encrypts
#and moves the xml file when both $outfile &
#$outfile.lock are present, ensuring complete
#encrypted xml file on ftp server, and leaving
#behind an unencrypted copy of the file on the
#/private/UNENCRYPTED directory.
    unless ($label=~ /https|tm58\_ftp/){
        open (OUTPUT,">$outfile.lock") || die "$prog: Could not open file $outfile.lock for output: $!\n";
        close(OUTPUT);
    }

}

1;
