#!/usr/bin/perl
use FileHandle;
use File::Basename;

if (($#ARGV + 1) < 3){die <<EOS ;}
*** $0
*** Copyright 2015, Gamma Remote Sensing, v1.1 9-Apr-2015 clw ***
*** Swap byte order of a list of data files and store in a specified directory ***

usage: $0 <data_tab> <out_dir> <bpv>  
    data_tab  (input) single column list of data files to swap byte order (little endian <--> big endian)
    out_dir   directory to contain output byte-swapped binary files
    bpv       bytes per value, either 2 or 4
                SHORT:    2
                SCOMPLEX: 2
                FLOAT:    4
                FCOMPLEX: 4
    
    NOTE: all files must have the same number of bytes per value
EOS

open(DATA_TAB, "<$ARGV[0]") or die "\nERROR $0: list of binary data files: $ARGV[0]\n\n";
print "data_tab list of binary data files: $ARGV[0]\n"; 

$out_dir = $ARGV[1];
$bpv     = $ARGV[2];

unless (-d $out_dir) {
  (mkdir $out_dir) or die "ERROR $0: non-zero exit status, could not create output directory $out_dir\n"
}

($bpv == 2 || $bpv == 4) or die "ERROR: invalid value for bpv parameter, must be 2 or 4: $bpv\n";
print "output directory for byte-swapped data: $out_dir\n";
print "bytes per data value: $bpv\n";

$time = localtime;
print "\nprocessing start: $time\n";
$log = "$out_dir/swap_bytes_all.log"; 
open(LOG,">$log") or die "ERROR $0: cannot open log file: $log\n\n";

print LOG "$0 @ARGV\n\n";	#print command line that was used
print LOG "output resampled slc directory: $out_dir\n";
print LOG "\nprocessing start: $time\n";  
close LOG;
 
LINE: while (<DATA_TAB>) {	#read lines of processing list file into $_
  chomp $_;			#remove new line from record
  next LINE if /^$/; 		#skip blank lines in processing list
  next LINE if /^#/; 		#skip comments in processing list
  @fields = split;		#extract the scene identifier and other parameters on the line if present
   
  $din = $fields[0]; 
  -e $din or die "\nERROR $0: data file does not exist: $din\n";    
  my ($s1, $dir1, $ext1) = fileparse($din, qr/\.[^.]*/); #extension is the last bit after the last period
 
  $ext2 = substr $ext1,1;	#delete period at the start of the extension
  $dout = "$out_dir/$s1.$ext2";
     
  $time = localtime;
  print "\ninput data file:  $din\n";
#  print "directory: $dir1  root: $s1  extension: $ext1\n";
#  print "out_dir: $out_dir  dir1: $dir1\n";
  print "output data file: $dout\n";
  if ($din eq $dout) {
    die "\nERROR: input and output files are the same file!\n";
  }

  open(LOG,">>$log") or die "ERROR $0: cannot open log file: $log\n\n";
  print LOG "\ninput data file: $din\n";
  print LOG "output data file: $dout\n";
  print LOG "start time: $time\n";
  close LOG;
  execute("swap_bytes $din $dout $bpv",$log);
}

$time = localtime;
open(LOG,">>$log") or die "ERROR $0: cannot open log file: $log\n\n";
print LOG "\n*** $0 processing end: $time ***\n";
print "\n*** $0 processing end: $time ***\n";
exit 0;

sub execute{
  my ($command, $log) = @_;  
  if (-e $log){open(LOG,">>$log") or die "\nERROR $0: cannot open existing log file: $log\n";}
  else {open(LOG,">$log") or die "\nERROR $0: cannot open new log file: $log\n";} 
  LOG->autoflush; 
  print "$command\n";
  print LOG ("\n${command}\n");
  close LOG;  
  $exit = system("$command 1>> $log");
  $exit == 0 or die "ERROR $0: non-zero exit status: $command\n"
}

sub extract_param{
  my ($infile,$keyword) = @_;
  open(PAR_IN,$infile) || die "\nERROR extract_param: cannot open parameter file: $infile\n";

  while(<PAR_IN>){
    chomp;
    @tokens = split;
    if($tokens[0] eq $keyword){close PAR_IN; return @tokens;}
  }
  close PAR_IN;
  die "\nERROR $0: keyword $keyword not found in file: $infile\n";
}
