#!/usr/bin/perl

use strict;
use warnings;

use FindBin;
use lib $FindBin::Bin;
use SisFile;
use Cwd;
use File::Path;
use File::Spec;
use File::Basename;
use File::Find;

if (not (scalar(@ARGV) == 2 and $ARGV[0] ne '-h' and $ARGV[0] ne '--help')) {
	die "usage: unsis.pl <sis file or dir> <target dir>";
}


my $resultsdir = $ARGV[1];
my %result;
my @sisfiles;
my $basedir=cwd();

my @files;
if (-d $ARGV[0]) {
	opendir DIR, $ARGV[0] or die "Unable to opendir $ARGV[0]: $!";
	@files=grep { /\.sis$/oi } readdir DIR;
	@files= map ( File::Spec->catfile($ARGV[0],$_), @files);
	close DIR;
} else {
	$files[0]=$ARGV[0];
}
	
foreach my $sisfilename ( @files ) {
	print "\nProcessing: $sisfilename\n";
	my $fullname = $sisfilename;
	my $sisfile = SisFile->New($fullname);
	if (not defined $sisfile) {
		print STDERR "Could not process SIS file $sisfilename\n";
		next;
	}
	
	$sisfilename =~ s#([^\\/]+\.sis)#$1#oi;
	$sisfile->ExtractSis($resultsdir);
}
